1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportBathymetryOp.h"
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_ImportBathymetryDlg.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
29 #include <HYDROGUI_OCCDisplayer.h>
31 #include <HYDROData_Bathymetry.h>
33 #include <LightApp_Application.h>
34 #include <LightApp_UpdateFlags.h>
35 #include <SUIT_ViewManager.h>
36 #include <SVTK_ViewModel.h>
38 #include <OCCViewer_ViewManager.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_Desktop.h>
43 #include <HYDROGUI_ShapeBathymetry.h>
46 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule,
47 const bool theIsEdit )
48 : HYDROGUI_Operation( theModule ),
51 setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
54 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
58 void HYDROGUI_ImportBathymetryOp::startOperation()
60 HYDROGUI_Operation::startOperation();
62 HYDROGUI_ImportBathymetryDlg* aPanel =
63 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
68 aPanel->SetEditMode(myIsEdit);
72 if ( isApplyAndClose() )
73 myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
74 if( !myEditedObject.IsNull() )
76 QString aName = myEditedObject->GetName();
77 QStringList aFileNames = myEditedObject->GetFilePaths();
78 bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
80 aPanel->setFuseIntoOneOptionChecked( true );
82 aPanel->setObjectName( aName );
83 aPanel->addFileNames( aFileNames );
84 aPanel->setInvertAltitudes( anIsAltitudesInverted );
85 aPanel->setFuseIntoOneOptionEnabled( false );
90 void HYDROGUI_ImportBathymetryOp::abortOperation()
92 HYDROGUI_Operation::abortOperation();
95 void HYDROGUI_ImportBathymetryOp::commitOperation()
97 HYDROGUI_Operation::commitOperation();
100 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
102 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
104 connect ( aPanel, SIGNAL( FileSelected( const QStringList& ) ), SLOT( onFileSelected() ) );
109 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
110 QString& theErrorMsg,
111 QStringList& theBrowseObjectsEntries )
113 HYDROGUI_ImportBathymetryDlg* aPanel =
114 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
118 QString anObjectName = aPanel->getObjectName().simplified();
119 if ( anObjectName.isEmpty() )
121 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
125 QStringList aFileNames = aPanel->getFileNames();
127 QStringList DummyFileList;
128 foreach (QString str, aFileNames)
129 DummyFileList << str.simplified();
131 aFileNames = DummyFileList;
132 DummyFileList.clear();
134 if ( aFileNames.isEmpty() )
136 theErrorMsg = tr( "EMPTY_FILENAMES" );
142 foreach (QString aFileName, aFileNames )
144 QFileInfo aFileInfo( aFileName );
145 if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
147 inexistWarn += "\n" + aFileName;
150 DummyFileList << aFileName;
153 if (!inexistWarn.isNull())
154 SUIT_MessageBox::warning( module()->getApp()->desktop(),
155 tr( "BATHYMETRY_IMPORT_WARNING" ), "Can't read the next files:" + inexistWarn );
157 aFileNames = DummyFileList;
159 bool isFuseIntoOneOption = aPanel->isFuseIntoOneOptionChecked();
160 bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
162 QString replacemWarn;
163 QString UnreadFilesWarn;
167 //edit already existing bath
168 if (myEditedObject.IsNull())
170 QStringList anOldFileNames = myEditedObject->GetFilePaths();
171 //bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
172 //myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
173 if ( aFileNames.toSet() != anOldFileNames.toSet() )
175 myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, true );
176 if ( !myEditedObject->ImportFromFiles( aFileNames ) )
178 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
182 else if ( anIsInvertAltitudes != myEditedObject->IsAltitudesInverted() )
183 myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, true );
185 myEditedObject->SetName( anObjectName );
186 myEditedObject->Update();
191 if (isFuseIntoOneOption)
193 Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
194 if ( aBathymetryObj.IsNull() )
196 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
197 if ( !aBathymetryObj->ImportFromFiles( aFileNames ) )
199 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
204 if (CheckNameExistingBathy(anObjectName, aNewObjName))
206 aBathymetryObj->SetName( aNewObjName );
207 replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
210 aBathymetryObj->SetName( anObjectName );
212 aBathymetryObj->SetName( anObjectName );
213 aBathymetryObj->Update();
214 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
215 theBrowseObjectsEntries.append( anEntry );
217 else //myedit off + non-fuse => import separate files
219 bool AtLeastOneWasImported = false;
220 foreach (QString filename, aFileNames)
222 Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
223 if ( aBathymetryObj.IsNull() )
225 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
226 if ( !aBathymetryObj->ImportFromFiles( QStringList(filename) ) )
228 UnreadFilesWarn += "\n" + filename;
232 QString anObjectName = QFileInfo( filename ).baseName();
234 if (CheckNameExistingBathy(anObjectName, aNewObjName))
236 aBathymetryObj->SetName( aNewObjName );
237 replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
240 aBathymetryObj->SetName( anObjectName );
242 AtLeastOneWasImported = true;
243 aBathymetryObj->Update();
244 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
245 theBrowseObjectsEntries.append( anEntry );
247 if (!AtLeastOneWasImported)
249 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILES" ).arg( aFileNames.join("\n") );
255 if (!UnreadFilesWarn.isNull())
256 SUIT_MessageBox::warning( module()->getApp()->desktop(),
257 tr( "BATHYMETRY_IMPORT_WARNING" ), "The next files cannot be imported:" + UnreadFilesWarn );
260 if (!replacemWarn.isNull())
261 SUIT_MessageBox::warning( module()->getApp()->desktop(),
262 tr( "BATHYMETRY_IMPORT_WARNING" ), "The next objects names are already exist in the document; so the new objects was renamed:" + replacemWarn );
264 // Activate VTK viewer and show the bathymetry
265 SUIT_ViewManager* aVTKMgr = 0;
266 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
267 // Try to get a VTK viewer as an active or existing one
270 if ( aViewMgr->getType() == SVTK_Viewer::Type() )
276 aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
280 OCCViewer_ViewManager* mgr = dynamic_cast<OCCViewer_ViewManager*>(aViewMgr);
281 OCCViewer_Viewer* occ_viewer = mgr->getOCCViewer();
282 int aViewerId = (size_t)(occ_viewer);
284 HYDROGUI_Shape* aObjSh = module()->getObjectShape( aViewerId, myEditedObject );
285 HYDROGUI_ShapeBathymetry* aBathSh = dynamic_cast<HYDROGUI_ShapeBathymetry*>( aObjSh );
288 aBathSh->update(false, false);
289 aBathSh->RescaleDefault();
290 module()->getOCCDisplayer()->UpdateColorScale( occ_viewer );
293 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced |
294 UF_VTKViewer | UF_VTK_Forced;
299 void HYDROGUI_ImportBathymetryOp::onFileSelected()
301 HYDROGUI_ImportBathymetryDlg* aPanel =
302 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
306 aPanel->UpdateCheckBoxStates();
309 bool HYDROGUI_ImportBathymetryOp::CheckNameExistingBathy(const QString& InpName, QString& OutputName)
311 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), InpName );
312 if (anObject.IsNull())
316 OutputName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );