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 <HYDROData_Bathymetry.h>
31 #include <LightApp_Application.h>
32 #include <LightApp_UpdateFlags.h>
33 #include <SUIT_ViewManager.h>
34 #include <SVTK_ViewModel.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
42 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule,
43 const bool theIsEdit )
44 : HYDROGUI_Operation( theModule ),
47 setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
50 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
54 void HYDROGUI_ImportBathymetryOp::startOperation()
56 HYDROGUI_Operation::startOperation();
58 HYDROGUI_ImportBathymetryDlg* aPanel =
59 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
67 if ( isApplyAndClose() )
68 myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
69 if( !myEditedObject.IsNull() )
71 QString aName = myEditedObject->GetName();
72 QStringList aFileNames = myEditedObject->GetFilePaths();
73 bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
75 aPanel->setFuseIntoOneOptionChecked( true );
77 aPanel->setObjectName( aName );
78 aPanel->setFileNames( aFileNames );
79 aPanel->setInvertAltitudes( anIsAltitudesInverted );
80 aPanel->setFuseIntoOneOptionEnabled( false );
85 void HYDROGUI_ImportBathymetryOp::abortOperation()
87 HYDROGUI_Operation::abortOperation();
90 void HYDROGUI_ImportBathymetryOp::commitOperation()
92 HYDROGUI_Operation::commitOperation();
95 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
97 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
99 connect ( aPanel, SIGNAL( FileSelected( const QStringList& ) ), SLOT( onFileSelected() ) );
104 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
105 QString& theErrorMsg,
106 QStringList& theBrowseObjectsEntries )
108 HYDROGUI_ImportBathymetryDlg* aPanel =
109 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
113 QString anObjectName = aPanel->getObjectName().simplified();
114 if ( anObjectName.isEmpty() )
116 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
120 QStringList aFileNames = aPanel->getFileNames();
122 QStringList DummyFileList;
123 foreach (QString str, aFileNames)
124 DummyFileList << str.simplified();
126 aFileNames = DummyFileList;
127 DummyFileList.clear();
129 if ( aFileNames.isEmpty() )
131 theErrorMsg = tr( "EMPTY_FILENAMES" );
137 foreach (QString aFileName, aFileNames )
139 QFileInfo aFileInfo( aFileName );
140 if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
142 inexistWarn += "\n" + aFileName;
145 DummyFileList << aFileName;
148 if (!inexistWarn.isNull())
149 SUIT_MessageBox::warning( module()->getApp()->desktop(),
150 tr( "BATHEMETRY_IMPORT_WARNING" ), "Can't read the next files:" + inexistWarn );
152 aFileNames = DummyFileList;
154 bool isFuseIntoOneOption = aPanel->isFuseIntoOneOptionChecked();
155 bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
157 QString replacemWarn;
158 QString UnreadFilesWarn;
162 //edit already existing bath
163 if (myEditedObject.IsNull())
165 QStringList anOldFileNames = myEditedObject->GetFilePaths();
166 bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
167 myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
168 if ( aFileNames.toSet() != anOldFileNames.toSet() )
170 myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
171 if ( !myEditedObject->ImportFromFiles( aFileNames ) )
173 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
177 else if ( anIsInvertAltitudes != myEditedObject->IsAltitudesInverted() )
178 myEditedObject->SetAltitudesInverted( anIsInvertAltitudes );
181 if (CheckNameExistingBathy(anObjectName, aNewObjName))
183 myEditedObject->SetName( aNewObjName );
184 replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
187 myEditedObject->SetName( anObjectName );
188 myEditedObject->Update();
193 if (isFuseIntoOneOption)
195 Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
196 if ( aBathymetryObj.IsNull() )
198 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
199 if ( !aBathymetryObj->ImportFromFiles( aFileNames ) )
201 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
206 if (CheckNameExistingBathy(anObjectName, aNewObjName))
208 aBathymetryObj->SetName( aNewObjName );
209 replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
212 aBathymetryObj->SetName( anObjectName );
214 aBathymetryObj->SetName( anObjectName );
215 aBathymetryObj->Update();
216 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
217 theBrowseObjectsEntries.append( anEntry );
219 else //myedit off + non-fuse => import separate files
221 bool AtLeastOneWasImported = false;
222 foreach (QString filename, aFileNames)
224 Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
225 if ( aBathymetryObj.IsNull() )
227 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
228 if ( !aBathymetryObj->ImportFromFiles( QStringList(filename) ) )
230 UnreadFilesWarn += "\n" + filename;
234 QString anObjectName = QFileInfo( filename ).baseName();
236 if (CheckNameExistingBathy(anObjectName, aNewObjName))
238 aBathymetryObj->SetName( aNewObjName );
239 replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
242 aBathymetryObj->SetName( anObjectName );
244 AtLeastOneWasImported = true;
245 aBathymetryObj->Update();
246 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
247 theBrowseObjectsEntries.append( anEntry );
249 if (!AtLeastOneWasImported)
251 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILES" ).arg( aFileNames.join("\n") );
257 if (!UnreadFilesWarn.isNull())
258 SUIT_MessageBox::warning( module()->getApp()->desktop(),
259 tr( "BATHEMETRY_IMPORT_WARNING" ), "The next files cannot be imported:" + UnreadFilesWarn );
262 if (!replacemWarn.isNull())
263 SUIT_MessageBox::warning( module()->getApp()->desktop(),
264 tr( "BATHEMETRY_IMPORT_WARNING" ), "The next objects names are already exist in the document; so the new objects was renamed:" + replacemWarn );
266 // Activate VTK viewer and show the bathymetry
267 SUIT_ViewManager* aVTKMgr = 0;
268 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
269 // Try to get a VTK viewer as an active or existing one
272 if ( aViewMgr->getType() == SVTK_Viewer::Type() )
278 aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
282 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced |
283 UF_VTKViewer | UF_VTK_Forced;
288 void HYDROGUI_ImportBathymetryOp::onFileSelected()
290 HYDROGUI_ImportBathymetryDlg* aPanel =
291 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
295 QStringList aFileNames = aPanel->getFileNames();
296 if ( !aPanel->isFuseIntoOneOptionEnabled() )
297 aPanel->setFuseIntoOneOptionEnabled( !myIsEdit && aFileNames.count() > 1 );
299 QString anObjectName = aPanel->getObjectName().simplified();
300 if ( anObjectName.isEmpty() )
302 if (aFileNames.count() == 1)
304 anObjectName = aFileNames[0];
305 if ( !anObjectName.isEmpty() )
306 anObjectName = QFileInfo( anObjectName ).baseName();
309 if ( anObjectName.isEmpty() ) {
310 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
312 aPanel->setObjectName( anObjectName );
316 bool HYDROGUI_ImportBathymetryOp::CheckNameExistingBathy(const QString& InpName, QString& OutputName)
318 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), InpName );
319 if (anObject.IsNull())
323 OutputName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );