Salome HOME
LCM // Import/Export of SHP p.1
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverMapOp.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_ImportLandCoverMapOp.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_ImportLandCoverMapDlg.h"
30 #include "HYDROGUI_Shape.h"
31 #include <HYDROData_LandCoverMap.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 #include <BRepTools.hxx>
68
69
70 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
71 : HYDROGUI_Operation( theModule )
72 {
73   setName( tr( "IMPORT_LANDCOVER" ) );
74 }
75
76 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
77 {
78   erasePreview();
79 }
80
81 void HYDROGUI_ImportLandCoverMapOp::startOperation()
82 {
83   HYDROGUI_Operation::startOperation();
84
85   if ( !getPreviewManager() ) {
86     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
87                        module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
88   }
89
90   if ( !isApplyAndClose() ) {
91     return;
92   }
93
94   HYDROGUI_ImportLandCoverMapDlg* aPanel = 
95     ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
96   if ( !aPanel ) {
97     return;
98   }
99
100   aPanel->reset();
101 }
102
103 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
104 {
105   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
106   if ( !aPanel )
107     return;
108   
109   QString anObjectName = aPanel->getObjectName().simplified();
110   anObjectName = aPanel->getFileName();
111   if ( !anObjectName.isEmpty() ) 
112       anObjectName = QFileInfo( anObjectName ).baseName();
113
114   if ( anObjectName.isEmpty() ) 
115     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
116   aPanel->setObjectName( anObjectName );
117
118   QString aFileName = aPanel->getFileName();
119   if ( aFileName.isEmpty() )
120   {
121     abort();
122     return;
123   }
124
125   QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
126
127   if (anExt == "shp")
128   {     
129     startDocOperation();    
130     QApplication::setOverrideCursor(Qt::WaitCursor);
131     
132     QStringList aPolygonsList;
133     TopTools_SequenceOfShape aFaces;
134     HYDROData_ShapeFile anImporter;
135
136     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
137     if ( !aStudy )
138       return;
139
140     erasePreview();
141
142     Handle(AIS_InteractiveContext) aCtx = NULL;
143     int aShapeTypeOfFile = -1;
144     int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
145     if (aStat == 1)
146     {
147       aPanel->setPolygonNames(aPolygonsList);
148
149       LightApp_Application* anApp = module()->getApp();
150       if ( !getPreviewManager() )
151         setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
152       OCCViewer_ViewManager* aViewManager = getPreviewManager();
153
154       if ( aViewManager )
155       {
156         if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
157         {
158           aCtx = aViewer->getAISContext();
159           connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
160         }
161       }
162
163       for ( int i = 1; i <= aFaces.Length(); i++ ) 
164       {
165         TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
166         if ( aViewManager && !aCtx.IsNull() )
167         {
168           HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
169           //Green color for now..
170           aShape->setFillingColor(QColor(0,255,0), false, false);
171           aShape->setBorderColor(QColor(0,255,0), false, false);
172           if( !aFace.IsNull() )
173             aShape->setShape( aFace);
174
175           myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
176         }
177       }
178       if ( !aCtx.IsNull() ) 
179       {
180         UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
181         aCtx->UpdateCurrentViewer();
182       }
183       
184       QApplication::restoreOverrideCursor();
185       commitDocOperation();
186     }
187     else
188     {
189       erasePreview();
190       aPanel->setPolygonNames(QStringList());
191       aPanel->setObjectName("");
192       QApplication::restoreOverrideCursor();
193       QString aMess = "Cannot import land cover;\n";
194       if (aStat == -1)
195         aMess += "Cannot open SHP file";
196       else if (aStat == -2)
197         aMess += "Cannot open SHX file";
198       else 
199         aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
200       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
201       commitDocOperation();
202       //abort();
203     }
204     anImporter.Free();
205
206   }
207   
208 }
209
210 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
211 {
212   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
213
214   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
215
216   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
217
218   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
219   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onBack( const int ) ) );
220   connect( aPanel, SIGNAL( Finish( const int ) ), SLOT( onFinish( const int ) ) );
221     
222   return aPanel;
223 }
224
225 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
226                                                 QString& theErrorMsg,
227                                                 QStringList& theBrowseObjectsEntries )
228 {
229
230   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
231   if ( !aPanel ) {
232     return false;
233   }
234   
235   QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
236   aPanel->removePolygonNames( aSelectedtPolygons );
237     
238   if (!aSelectedtPolygons.empty())
239   {
240     Handle(HYDROData_LandCoverMap) aLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
241     TopoDS_Shape aResShape;
242     if (aSelectedtPolygons.size() > 1) 
243     {
244       TopoDS_Compound cmp;
245       BRep_Builder BB;
246       BB.MakeCompound(cmp);
247
248       foreach ( QString aName, aSelectedtPolygons ) {
249         TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
250         if ( aShape.IsNull() ) 
251           continue;
252         BB.Add(cmp, aShape);
253         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
254         delete aShapeToDelete;
255       }
256       aResShape = cmp;
257     }
258     else
259     {         
260       TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
261       if ( !aShape.IsNull() ) 
262       {
263         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
264         delete aShapeToDelete;
265         aResShape = aShape;
266       }
267     }
268     if( !aLCM.IsNull() ) 
269     {
270       QString aLCName = aPanel->getObjectName() + "_polygon";
271       int i = 0;
272       for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
273         aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
274       aLCM->SetName( aLCName );
275       //TODO add color
276       //aLCM->SetFillingColor( aLCM->DefaultFillingColor() );
277       //aLCM->SetBorderColor( aLCM->DefaultBorderColor() );
278       
279       //aLCM->SetShape(aResShape);
280       aLCM->Show();
281             
282       //erasePreview();
283       
284       module()->setIsToUpdate( aLCM );
285       
286     }
287   }
288  
289   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
290
291   if ( isApplyAndClose() )
292     erasePreview();
293
294   return true;
295 }
296
297 void HYDROGUI_ImportLandCoverMapOp::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       /*if (isSelected)
319       {
320         HYDROGUI_Shape* aHydroSh = myPolygonName2PrsShape.value( aName );
321         aHydroSh->setFillingColor(QColor(0,255,255), true, true);
322       }*/
323     }
324     aCtx->UpdateCurrentViewer();
325   }
326 }
327
328
329 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
330 {
331   // Get panel
332   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
333   if ( !aPanel ) {
334     return;
335   }
336
337   OCCViewer_ViewManager* aViewManager = getPreviewManager();
338   Handle(AIS_InteractiveContext) aCtx = NULL;
339   if ( aViewManager ) {
340     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
341       aCtx = aViewer->getAISContext();
342     }
343   }
344   
345   if ( !aCtx.IsNull() )
346   {
347     QStringList aSelectedNames;
348     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
349       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
350       if ( isSelected ) {
351         aSelectedNames << aName;
352       }
353     }
354     aPanel->setSelectedPolygonNames( aSelectedNames );
355   }
356 }
357
358
359 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
360 {
361   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
362     delete aShape;
363   }
364
365   myPolygonName2PrsShape.clear();
366 }
367
368
369 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
370 {
371   LightApp_Application* anApp = module()->getApp();
372   if ( anApp ) {
373     anApp->disconnect( this );
374   }
375
376   erasePreview();
377
378   HYDROGUI_Operation::abortOperation();
379 }
380
381
382 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
383 {
384
385 }
386
387 void HYDROGUI_ImportLandCoverMapOp::onBack( const int theIndex )
388 {
389
390 }