Salome HOME
Merge branch 'BR_MULTI_BATHS' into HEAD
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportBathymetryOp.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImportBathymetryOp.h"
20
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"
28
29 #include <HYDROData_Bathymetry.h>
30
31 #include <LightApp_Application.h>
32 #include <LightApp_UpdateFlags.h>
33 #include <SUIT_ViewManager.h>
34 #include <SVTK_ViewModel.h>
35
36 #include <QFileInfo>
37 #include <QSet>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40
41
42 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule, 
43                                                          const bool theIsEdit  )
44 : HYDROGUI_Operation( theModule ),
45   myIsEdit( theIsEdit )
46 {
47   setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
48 }
49
50 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
51 {
52 }
53
54 void HYDROGUI_ImportBathymetryOp::startOperation()
55 {
56   HYDROGUI_Operation::startOperation();
57
58   HYDROGUI_ImportBathymetryDlg* aPanel = 
59     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
60   if ( !aPanel )
61     return;
62
63   aPanel->reset();
64
65   if( myIsEdit )
66   {
67     if ( isApplyAndClose() )
68       myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
69     if( !myEditedObject.IsNull() )
70     {
71       QString aName = myEditedObject->GetName();
72       QStringList aFileNames = myEditedObject->GetFilePaths();
73       bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
74
75       aPanel->setFuseIntoOneOptionChecked( true );
76
77       aPanel->setObjectName( aName );
78       aPanel->setFileNames( aFileNames ); 
79       aPanel->setInvertAltitudes( anIsAltitudesInverted );
80       aPanel->setFuseIntoOneOptionEnabled( false );
81     }
82   }
83 }
84
85 void HYDROGUI_ImportBathymetryOp::abortOperation()
86 {
87   HYDROGUI_Operation::abortOperation();
88 }
89
90 void HYDROGUI_ImportBathymetryOp::commitOperation()
91 {
92   HYDROGUI_Operation::commitOperation();
93 }
94
95 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
96 {
97   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
98   
99   connect ( aPanel, SIGNAL( FileSelected( const QStringList& ) ), SLOT( onFileSelected() ) );
100
101   return aPanel;
102 }
103
104 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
105                                                 QString& theErrorMsg,
106                                                 QStringList& theBrowseObjectsEntries )
107 {
108   HYDROGUI_ImportBathymetryDlg* aPanel = 
109     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
110   if ( !aPanel )
111     return false;
112
113   QString anObjectName = aPanel->getObjectName().simplified();
114   if ( anObjectName.isEmpty() )
115   {
116     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
117     return false;
118   }
119
120   QStringList aFileNames = aPanel->getFileNames();
121
122   QStringList DummyFileList;
123   foreach (QString str, aFileNames)
124     DummyFileList << str.simplified();
125
126   aFileNames = DummyFileList;
127   DummyFileList.clear();
128
129   if ( aFileNames.isEmpty() )
130   {
131     theErrorMsg = tr( "EMPTY_FILENAMES" );
132     return false;
133   }
134
135   QString inexistWarn;
136
137   foreach (QString aFileName, aFileNames )
138   {
139     QFileInfo aFileInfo( aFileName );
140     if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
141     {
142       inexistWarn += "\n" + aFileName;
143       continue;
144     }
145     DummyFileList << aFileName;
146   }
147
148   if (!inexistWarn.isNull())
149     SUIT_MessageBox::warning( module()->getApp()->desktop(), 
150     tr( "BATHEMETRY_IMPORT_WARNING" ), "Can't read the next files:" + inexistWarn );
151
152   aFileNames = DummyFileList;
153
154   bool isFuseIntoOneOption = aPanel->isFuseIntoOneOptionChecked(); 
155   bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
156
157   QString replacemWarn;
158   QString UnreadFilesWarn;
159
160   if ( myIsEdit )
161   {
162     //edit already existing bath
163     if (myEditedObject.IsNull())
164       return false;
165     QStringList anOldFileNames = myEditedObject->GetFilePaths(); 
166     bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
167     myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
168     if ( aFileNames.toSet() != anOldFileNames.toSet() )
169     {
170       myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
171       if ( !myEditedObject->ImportFromFiles( aFileNames ) )
172       {
173         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
174         return false;
175       }
176     }
177     else if ( anIsInvertAltitudes != myEditedObject->IsAltitudesInverted() )
178       myEditedObject->SetAltitudesInverted( anIsInvertAltitudes );
179
180     QString aNewObjName;
181     if (CheckNameExistingBathy(anObjectName, aNewObjName))
182     {
183       myEditedObject->SetName( aNewObjName );
184       replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
185     }
186     else
187       myEditedObject->SetName( anObjectName );
188     myEditedObject->Update();
189   }
190   else
191   {
192     //create the new one
193     if (isFuseIntoOneOption)
194     {
195       Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
196       if ( aBathymetryObj.IsNull() )
197         return false;
198       aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
199       if ( !aBathymetryObj->ImportFromFiles( aFileNames ) )
200       {
201         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
202         return false;
203       }
204
205       QString aNewObjName;
206       if (CheckNameExistingBathy(anObjectName, aNewObjName))
207       {
208         aBathymetryObj->SetName( aNewObjName );
209         replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
210       }
211       else
212         aBathymetryObj->SetName( anObjectName );
213
214       aBathymetryObj->SetName( anObjectName );
215       aBathymetryObj->Update();
216       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
217       theBrowseObjectsEntries.append( anEntry );
218     }
219     else //myedit off + non-fuse => import separate files
220     {
221       bool AtLeastOneWasImported = false;
222       foreach (QString filename, aFileNames)
223       {
224         Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
225         if ( aBathymetryObj.IsNull() )
226           continue;
227         aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
228         if ( !aBathymetryObj->ImportFromFiles( QStringList(filename) ) )
229         {
230           UnreadFilesWarn += "\n" + filename;
231           continue;
232         }
233
234         QString anObjectName = QFileInfo( filename ).baseName();
235         QString aNewObjName;
236         if (CheckNameExistingBathy(anObjectName, aNewObjName))
237         {
238           aBathymetryObj->SetName( aNewObjName );
239           replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
240         }
241         else
242           aBathymetryObj->SetName( anObjectName );
243
244         AtLeastOneWasImported = true;
245         aBathymetryObj->Update();
246         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
247         theBrowseObjectsEntries.append( anEntry );
248       }
249       if (!AtLeastOneWasImported)
250       {
251         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILES" ).arg( aFileNames.join("\n") ); 
252         return false;
253       }
254     }
255   }
256
257   if (!UnreadFilesWarn.isNull())
258     SUIT_MessageBox::warning( module()->getApp()->desktop(), 
259     tr( "BATHEMETRY_IMPORT_WARNING" ), "The next files cannot be imported:" + UnreadFilesWarn );
260
261
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 );
265
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
270   if ( aViewMgr )
271   {
272     if ( aViewMgr->getType() == SVTK_Viewer::Type() )
273     {
274       aVTKMgr = aViewMgr;
275     }
276     else
277     {
278       aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
279     }
280   }
281
282   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced |
283     UF_VTKViewer | UF_VTK_Forced;
284
285   return true;
286 }
287  
288 void HYDROGUI_ImportBathymetryOp::onFileSelected()
289 {
290   HYDROGUI_ImportBathymetryDlg* aPanel = 
291     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
292   if ( !aPanel )
293     return;
294
295   QStringList aFileNames = aPanel->getFileNames();
296   if ( !aPanel->isFuseIntoOneOptionEnabled() )
297     aPanel->setFuseIntoOneOptionEnabled( !myIsEdit && aFileNames.count() > 1 );
298
299   QString anObjectName = aPanel->getObjectName().simplified();
300   if ( anObjectName.isEmpty() )
301   {
302     if (aFileNames.count() == 1)
303     {
304       anObjectName = aFileNames[0];
305       if ( !anObjectName.isEmpty() )
306         anObjectName = QFileInfo( anObjectName ).baseName();
307     }
308
309     if ( anObjectName.isEmpty() ) {
310       anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
311     }
312     aPanel->setObjectName( anObjectName );
313   }
314 }
315
316 bool HYDROGUI_ImportBathymetryOp::CheckNameExistingBathy(const QString& InpName, QString& OutputName)
317 {
318   Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), InpName );
319   if (anObject.IsNull())
320     return false;
321   else
322   {
323     OutputName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
324     return true;
325   }
326 }
327
328