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