Salome HOME
correction of the compilation
[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( aLC->DefaultFillingColor() );
164       aLC->SetBorderColor( aLC->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     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
219     if ( !aStudy )
220       return;
221
222     erasePreview();
223
224     Handle(AIS_InteractiveContext) aCtx = NULL;
225     int aShapeTypeOfFile = -1;
226     int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
227     if (aStat == 1)
228     {
229       aPanel->setPolygonNames(aPolygonsList);
230
231       LightApp_Application* anApp = module()->getApp();
232       if ( !getPreviewManager() )
233         setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
234       OCCViewer_ViewManager* aViewManager = getPreviewManager();
235
236       if ( aViewManager )
237       {
238         if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
239         {
240           aCtx = aViewer->getAISContext();
241           connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
242         }
243       }
244
245       Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
246       for ( int i = 1; i <= aFaces.Length(); i++ ) 
247       {
248         TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
249
250         aLC->SetShape( aFace );      
251         
252         if ( aViewManager && !aCtx.IsNull() )
253         {
254           HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
255
256           aShape->setFillingColor( aLC->DefaultFillingColor(), false, false );
257           aShape->setBorderColor( aLC->DefaultBorderColor(), false, false );
258           if( !aFace.IsNull() )
259             aShape->setShape( aLC->GetShape() );
260           myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
261         }
262       }
263       aLC->Remove();
264
265       if ( !aCtx.IsNull() ) 
266       {
267         UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
268         aCtx->UpdateCurrentViewer();
269       }
270       
271       QApplication::restoreOverrideCursor();
272       commitDocOperation();
273     }
274     else
275     {
276       erasePreview();
277       aPanel->setPolygonNames(QStringList());
278       aPanel->setObjectName("");
279       QApplication::restoreOverrideCursor();
280       QString aMess = "Cannot import land cover;\n";
281       if (aStat == -1)
282         aMess += "Cannot open SHP file";
283       else if (aStat == -2)
284         aMess += "Cannot open SHX file";
285       else 
286         aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
287       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
288       commitDocOperation();
289       //abort();
290     }
291     anImporter.Free();
292
293   }
294   
295 }
296
297 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
298 {
299   Handle(AIS_InteractiveContext) aCtx = NULL;
300
301   OCCViewer_ViewManager* aViewManager = getPreviewManager();
302   if ( aViewManager ) {
303     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
304       aCtx = aViewer->getAISContext();
305     }
306   }
307
308   if ( !aCtx.IsNull() ) {
309     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
310       Handle(AIS_InteractiveObject) anObject = 
311         myPolygonName2PrsShape.value(aName)->getAISObject();
312
313       bool isSelected = theSelectedNames.contains( aName );
314       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
315            ( !isSelected && aCtx->IsSelected( anObject) ) ) {
316         aCtx->AddOrRemoveSelected( anObject, Standard_False );
317       }
318     }
319     aCtx->UpdateCurrentViewer();
320   }
321 }
322
323
324 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
325 {
326   // Get panel
327   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
328   if ( !aPanel ) {
329     return;
330   }
331
332   OCCViewer_ViewManager* aViewManager = getPreviewManager();
333   Handle(AIS_InteractiveContext) aCtx = NULL;
334   if ( aViewManager ) {
335     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
336       aCtx = aViewer->getAISContext();
337     }
338   }
339   
340   if ( !aCtx.IsNull() )
341   {
342     QStringList aSelectedNames;
343     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
344       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
345       if ( isSelected ) {
346         aSelectedNames << aName;
347       }
348     }
349     aPanel->setSelectedPolygonNames( aSelectedNames );
350   }
351 }
352
353
354 void HYDROGUI_ImportLandCoverOp::erasePreview()
355 {
356   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
357     delete aShape;
358   }
359
360   myPolygonName2PrsShape.clear();
361 }
362
363
364 void HYDROGUI_ImportLandCoverOp::abortOperation()
365 {
366   LightApp_Application* anApp = module()->getApp();
367   if ( anApp ) {
368     anApp->disconnect( this );
369   }
370
371   erasePreview();
372
373   HYDROGUI_Operation::abortOperation();
374 }
375
376