Salome HOME
Import/Export // API revision. p2
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverOp.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ImportLandCoverOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_ImportLandCoverDlg.h"
30 #include "HYDROGUI_Shape.h"
31 #include <HYDROData_LandCover.h>
32 #include <HYDROGUI_ZLayers.h>
33
34 #include <HYDROGUI_DataObject.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_ShapeFile.h>
37 #include <HYDROData_Profile.h>
38
39 #include <SUIT_Desktop.h>
40 #include <SUIT_FileDlg.h>
41 #include <LightApp_Application.h>
42
43 #include <QApplication>
44 #include <QFile>
45 #include <QFileInfo>
46 #include <SUIT_MessageBox.h>
47
48 #include <OCCViewer_ViewManager.h>
49 #include <OCCViewer_ViewModel.h>
50
51 #include <SalomeApp_Study.h>
52
53 #include <LightApp_Application.h>
54 #include <LightApp_DataOwner.h>
55 #include <LightApp_Displayer.h>
56 #include <LightApp_SelectionMgr.h>
57
58 #include <SUIT_Desktop.h>
59 #include <SUIT_ViewManager.h>
60
61 #include <BRep_Builder.hxx>
62 #include <TopoDS.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <TopoDS_Wire.hxx>
65
66
67
68 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
69 : HYDROGUI_Operation( theModule )
70 {
71   setName( tr( "IMPORT_LANDCOVER" ) );
72 }
73
74 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
75 {
76   erasePreview();
77 }
78
79 void HYDROGUI_ImportLandCoverOp::startOperation()
80 {
81   HYDROGUI_Operation::startOperation();
82
83   if ( !getPreviewManager() ) {
84     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
85                        module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
86   }
87
88   if ( !isApplyAndClose() ) {
89     return;
90   }
91
92   HYDROGUI_ImportLandCoverDlg* aPanel = 
93     ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
94   if ( !aPanel ) {
95     return;
96   }
97
98   aPanel->reset();
99 }
100
101
102 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
103 {
104   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
105
106   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
107
108   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
109
110   return aPanel;
111 }
112
113 bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
114                                                 QString& theErrorMsg,
115                                                 QStringList& theBrowseObjectsEntries )
116 {
117
118   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
119   if ( !aPanel ) {
120     return false;
121   }
122   
123   QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
124   aPanel->removePolygonNames( aSelectedtPolygons );
125     
126   if (!aSelectedtPolygons.empty())
127   {
128     Handle(HYDROData_LandCover) aLC =  Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
129     TopoDS_Shape aResShape;
130     if (aSelectedtPolygons.size() > 1) 
131     {
132       TopoDS_Compound cmp;
133       BRep_Builder BB;
134       BB.MakeCompound(cmp);
135
136       foreach ( QString aName, aSelectedtPolygons ) {
137         TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
138         if ( aShape.IsNull() ) 
139           continue;
140         BB.Add(cmp, aShape);
141         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
142         delete aShapeToDelete;
143       }
144       aResShape = cmp;
145     }
146     else
147     {         
148       TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
149       if ( !aShape.IsNull() ) 
150       {
151         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
152         delete aShapeToDelete;
153        aResShape = aShape;
154       }
155     }
156     if( !aLC.IsNull() ) 
157     {
158       QString aLCName = aPanel->getObjectName() + "_polygon";
159       int i = 0;
160       for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
161         aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
162       aLC->SetName( aLCName );
163       aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
164       aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
165       
166       aLC->setShape(aResShape);
167       aLC->Show();
168             
169       //erasePreview();
170       
171       module()->setIsToUpdate( aLC );
172       
173     }
174   }
175  
176   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
177
178   if ( isApplyAndClose() )
179     erasePreview();
180
181   return true;
182 }
183
184
185 void HYDROGUI_ImportLandCoverOp::onFileSelected()
186 {
187   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
188   if ( !aPanel )
189     return;
190   
191   QString anObjectName = aPanel->getObjectName().simplified();
192   anObjectName = aPanel->getFileName();
193   if ( !anObjectName.isEmpty() ) 
194       anObjectName = QFileInfo( anObjectName ).baseName();
195
196   if ( anObjectName.isEmpty() ) 
197     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
198   aPanel->setObjectName( anObjectName );
199
200   QString aFileName = aPanel->getFileName();
201   if ( aFileName.isEmpty() )
202   {
203     abort();
204     return;
205   }
206
207   QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
208
209   if (anExt == "shp")
210   {     
211     startDocOperation();    
212     QApplication::setOverrideCursor(Qt::WaitCursor);
213     
214     QStringList aPolygonsList;
215     TopTools_SequenceOfShape aFaces;
216     HYDROData_ShapeFile anImporter;
217
218     if (!anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces))
219     {
220       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
221       abort();
222     }
223
224     aPanel->setPolygonNames(aPolygonsList);
225
226     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
227     if ( !aStudy ) {
228       return;
229     }
230
231     erasePreview();
232
233     Handle(AIS_InteractiveContext) aCtx = NULL;
234
235     LightApp_Application* anApp = module()->getApp();
236     if ( !getPreviewManager() )
237       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
238     OCCViewer_ViewManager* aViewManager = getPreviewManager();
239
240     if ( aViewManager )
241     {
242       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
243       {
244         aCtx = aViewer->getAISContext();
245         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
246       }
247     }
248
249
250     QApplication::restoreOverrideCursor();
251
252     Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
253     for ( int i = 1; i <= aFaces.Length(); i++ ) 
254     {
255       TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
256
257       aLC->setShape( aFace );      
258       
259       if ( aViewManager && !aCtx.IsNull() )
260       {
261         HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
262
263         aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
264         aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
265         if( !aFace.IsNull() )
266           aShape->setShape( aLC->GetShape() );
267         myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
268       }
269     }
270     aLC->Remove();
271
272     if ( !aCtx.IsNull() ) {
273       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
274       aCtx->UpdateCurrentViewer();
275     }
276
277     commitDocOperation();
278
279     anImporter.Free();
280
281   }
282   
283 }
284
285 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
286 {
287   Handle(AIS_InteractiveContext) aCtx = NULL;
288
289   OCCViewer_ViewManager* aViewManager = getPreviewManager();
290   if ( aViewManager ) {
291     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
292       aCtx = aViewer->getAISContext();
293     }
294   }
295
296   if ( !aCtx.IsNull() ) {
297     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
298       Handle(AIS_InteractiveObject) anObject = 
299         myPolygonName2PrsShape.value(aName)->getAISObject();
300
301       bool isSelected = theSelectedNames.contains( aName );
302       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
303            ( !isSelected && aCtx->IsSelected( anObject) ) ) {
304         aCtx->AddOrRemoveSelected( anObject, Standard_False );
305       }
306     }
307     aCtx->UpdateCurrentViewer();
308   }
309 }
310
311
312 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
313 {
314   // Get panel
315   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
316   if ( !aPanel ) {
317     return;
318   }
319
320   OCCViewer_ViewManager* aViewManager = getPreviewManager();
321   Handle(AIS_InteractiveContext) aCtx = NULL;
322   if ( aViewManager ) {
323     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
324       aCtx = aViewer->getAISContext();
325     }
326   }
327   
328   if ( !aCtx.IsNull() )
329   {
330     QStringList aSelectedNames;
331     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
332       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
333       if ( isSelected ) {
334         aSelectedNames << aName;
335       }
336     }
337     aPanel->setSelectedPolygonNames( aSelectedNames );
338   }
339 }
340
341
342 void HYDROGUI_ImportLandCoverOp::erasePreview()
343 {
344   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
345     delete aShape;
346   }
347
348   myPolygonName2PrsShape.clear();
349 }
350
351
352 void HYDROGUI_ImportLandCoverOp::abortOperation()
353 {
354   LightApp_Application* anApp = module()->getApp();
355   if ( anApp ) {
356     anApp->disconnect( this );
357   }
358
359   erasePreview();
360
361   HYDROGUI_Operation::abortOperation();
362 }
363
364