Salome HOME
refs #1327: debug of scaling operations
[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   aPanel->SetEditMode(myIsEdit);
65
66   if( myIsEdit )
67   {
68     if ( isApplyAndClose() )
69       myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
70     if( !myEditedObject.IsNull() )
71     {
72       QString aName = myEditedObject->GetName();
73       QStringList aFileNames = myEditedObject->GetFilePaths();
74       bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
75
76       aPanel->setFuseIntoOneOptionChecked( true );
77
78       aPanel->setObjectName( aName );
79       aPanel->addFileNames( aFileNames ); 
80       aPanel->setInvertAltitudes( anIsAltitudesInverted );
81       aPanel->setFuseIntoOneOptionEnabled( false );
82     }
83   }
84 }
85
86 void HYDROGUI_ImportBathymetryOp::abortOperation()
87 {
88   HYDROGUI_Operation::abortOperation();
89 }
90
91 void HYDROGUI_ImportBathymetryOp::commitOperation()
92 {
93   HYDROGUI_Operation::commitOperation();
94 }
95
96 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
97 {
98   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
99   
100   connect ( aPanel, SIGNAL( FileSelected( const QStringList& ) ), SLOT( onFileSelected() ) );
101
102   return aPanel;
103 }
104
105 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
106                                                 QString& theErrorMsg,
107                                                 QStringList& theBrowseObjectsEntries )
108 {
109   HYDROGUI_ImportBathymetryDlg* aPanel = 
110     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
111   if ( !aPanel )
112     return false;
113
114   QString anObjectName = aPanel->getObjectName().simplified();
115   if ( anObjectName.isEmpty() )
116   {
117     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
118     return false;
119   }
120
121   QStringList aFileNames = aPanel->getFileNames();
122
123   QStringList DummyFileList;
124   foreach (QString str, aFileNames)
125     DummyFileList << str.simplified();
126
127   aFileNames = DummyFileList;
128   DummyFileList.clear();
129
130   if ( aFileNames.isEmpty() )
131   {
132     theErrorMsg = tr( "EMPTY_FILENAMES" );
133     return false;
134   }
135
136   QString inexistWarn;
137
138   foreach (QString aFileName, aFileNames )
139   {
140     QFileInfo aFileInfo( aFileName );
141     if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
142     {
143       inexistWarn += "\n" + aFileName;
144       continue;
145     }
146     DummyFileList << aFileName;
147   }
148
149   if (!inexistWarn.isNull())
150     SUIT_MessageBox::warning( module()->getApp()->desktop(), 
151     tr( "BATHYMETRY_IMPORT_WARNING" ), "Can't read the next files:" + inexistWarn );
152
153   aFileNames = DummyFileList;
154
155   bool isFuseIntoOneOption = aPanel->isFuseIntoOneOptionChecked(); 
156   bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
157
158   QString replacemWarn;
159   QString UnreadFilesWarn;
160
161   if ( myIsEdit )
162   {
163     //edit already existing bath
164     if (myEditedObject.IsNull())
165       return false;
166     QStringList anOldFileNames = myEditedObject->GetFilePaths(); 
167     //bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
168     //myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, false );
169     if ( aFileNames.toSet() != anOldFileNames.toSet() )
170     {
171       myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, true );
172       if ( !myEditedObject->ImportFromFiles( aFileNames ) )
173       {
174         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
175         return false;
176       }
177     }
178     else if ( anIsInvertAltitudes != myEditedObject->IsAltitudesInverted() )
179       myEditedObject->SetAltitudesInverted( anIsInvertAltitudes, true );
180
181     myEditedObject->SetName( anObjectName );
182     myEditedObject->Update();
183   }
184   else
185   {
186     //create the new one
187     if (isFuseIntoOneOption)
188     {
189       Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
190       if ( aBathymetryObj.IsNull() )
191         return false;
192       aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
193       if ( !aBathymetryObj->ImportFromFiles( aFileNames ) )
194       {
195         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileNames.join("\n") );
196         return false;
197       }
198
199       QString aNewObjName;
200       if (CheckNameExistingBathy(anObjectName, aNewObjName))
201       {
202         aBathymetryObj->SetName( aNewObjName );
203         replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
204       }
205       else
206         aBathymetryObj->SetName( anObjectName );
207
208       aBathymetryObj->SetName( anObjectName );
209       aBathymetryObj->Update();
210       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
211       theBrowseObjectsEntries.append( anEntry );
212     }
213     else //myedit off + non-fuse => import separate files
214     {
215       bool AtLeastOneWasImported = false;
216       foreach (QString filename, aFileNames)
217       {
218         Handle(HYDROData_Bathymetry) aBathymetryObj = Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
219         if ( aBathymetryObj.IsNull() )
220           continue;
221         aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
222         if ( !aBathymetryObj->ImportFromFiles( QStringList(filename) ) )
223         {
224           UnreadFilesWarn += "\n" + filename;
225           continue;
226         }
227
228         QString anObjectName = QFileInfo( filename ).baseName();
229         QString aNewObjName;
230         if (CheckNameExistingBathy(anObjectName, aNewObjName))
231         {
232           aBathymetryObj->SetName( aNewObjName );
233           replacemWarn += "\n'" + anObjectName + "' => '" + aNewObjName + "'";
234         }
235         else
236           aBathymetryObj->SetName( anObjectName );
237
238         AtLeastOneWasImported = true;
239         aBathymetryObj->Update();
240         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
241         theBrowseObjectsEntries.append( anEntry );
242       }
243       if (!AtLeastOneWasImported)
244       {
245         theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILES" ).arg( aFileNames.join("\n") ); 
246         return false;
247       }
248     }
249   }
250
251   if (!UnreadFilesWarn.isNull())
252     SUIT_MessageBox::warning( module()->getApp()->desktop(), 
253     tr( "BATHYMETRY_IMPORT_WARNING" ), "The next files cannot be imported:" + UnreadFilesWarn );
254
255
256   if (!replacemWarn.isNull())
257     SUIT_MessageBox::warning( module()->getApp()->desktop(), 
258     tr( "BATHYMETRY_IMPORT_WARNING" ), "The next objects names are already exist in the document; so the new objects was renamed:" + replacemWarn );
259
260   // Activate VTK viewer and show the bathymetry
261   SUIT_ViewManager* aVTKMgr = 0;
262   SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
263   // Try to get a VTK viewer as an active or existing one
264   if ( aViewMgr )
265   {
266     if ( aViewMgr->getType() == SVTK_Viewer::Type() )
267     {
268       aVTKMgr = aViewMgr;
269     }
270     else
271     {
272       aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
273     }
274   }
275
276   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced |
277     UF_VTKViewer | UF_VTK_Forced;
278
279   return true;
280 }
281  
282 void HYDROGUI_ImportBathymetryOp::onFileSelected()
283 {
284   HYDROGUI_ImportBathymetryDlg* aPanel = 
285     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
286   if ( !aPanel )
287     return;
288
289   aPanel->UpdateCheckBoxStates();
290 }
291
292 bool HYDROGUI_ImportBathymetryOp::CheckNameExistingBathy(const QString& InpName, QString& OutputName)
293 {
294   Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), InpName );
295   if (anObject.IsNull())
296     return false;
297   else
298   {
299     OutputName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
300     return true;
301   }
302 }
303
304