]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImportLandCoverMapOp.cxx
Salome HOME
LCM // Import/Export of SHP p.6
[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 #include <HYDROData_StricklerTable.h>
58
59 #include <SUIT_Desktop.h>
60 #include <SUIT_ViewManager.h>
61
62 #include <BRep_Builder.hxx>
63 #include <TopoDS.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Wire.hxx>
66 #include <QSet>
67
68 #include <HYDROData_LandCoverMap.h>
69
70 //TODO add definitions into TS file
71 //TODO check if LCM name is already exists..
72 //rename it
73 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
74 : HYDROGUI_Operation( theModule )
75 {
76   setName( tr( "IMPORT_LANDCOVERMAP" ) );
77 }
78
79 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
80 {
81   erasePreview();
82   myImporter.Free();
83   myAttrV.clear();
84 }
85
86 void HYDROGUI_ImportLandCoverMapOp::startOperation()
87 {
88   HYDROGUI_Operation::startOperation();
89
90   if ( !getPreviewManager() ) {
91     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
92                        module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
93   }
94
95   HYDROGUI_ImportLandCoverMapDlg* aPanel = 
96     ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
97   if ( !aPanel ) {
98     return;
99   }
100
101   aPanel->reset();
102 }
103
104 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
105 {
106   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
107   if ( !aPanel )
108     return;
109   
110   QString anObjectName = aPanel->getObjectName().simplified();
111   anObjectName = aPanel->getFileName();
112   if ( !anObjectName.isEmpty() ) 
113       anObjectName = QFileInfo( anObjectName ).baseName();
114
115   if ( anObjectName.isEmpty() ) 
116     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
117   aPanel->setObjectName( anObjectName );
118
119   myFileName = aPanel->getFileName();
120   if ( myFileName.isEmpty() )
121   {
122     abort();
123     return;
124   }
125
126   QString anExt = myFileName.split('.', QString::SkipEmptyParts).back();
127
128   if (anExt == "shp")
129   {     
130     startDocOperation();    
131     QApplication::setOverrideCursor(Qt::WaitCursor);
132     
133     QStringList aPolygonsList;
134     myPolygonFaces.Clear();
135     myImporter.Free();
136
137     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
138     if ( !aStudy )
139       return;
140
141     erasePreview();
142
143     Handle(AIS_InteractiveContext) aCtx = NULL;
144     int aShapeTypeOfFile = -1;
145
146     //Import polygons from SHP file as faces
147     //This faces should be added to the new LCM object
148
149     int aStat = myImporter.ImportPolygons(myFileName, aPolygonsList, myPolygonFaces, aShapeTypeOfFile);
150     if (aStat == 1)
151     {
152       aPanel->setPolygonNames(aPolygonsList);
153
154       LightApp_Application* anApp = module()->getApp();
155       if ( !getPreviewManager() )
156         setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
157       OCCViewer_ViewManager* aViewManager = getPreviewManager();
158
159       if ( aViewManager )
160       {
161         if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
162         {
163           aCtx = aViewer->getAISContext();
164           connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
165         }
166       }
167
168       for ( int i = 1; i <= myPolygonFaces.Length(); i++ ) 
169       {
170         TopoDS_Face aFace = TopoDS::Face(myPolygonFaces.Value( i ));
171         if ( aViewManager && !aCtx.IsNull() )
172         {
173           HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
174           //Green color for now..
175           aShape->setFillingColor(QColor(0,255,0), false, false);
176           aShape->setBorderColor(QColor(0,255,0), false, false);
177           if( !aFace.IsNull() )
178             aShape->setShape( aFace);
179
180           myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
181         }
182       }
183
184       if ( !aCtx.IsNull() ) 
185       {
186         UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
187         aCtx->UpdateCurrentViewer();
188       }
189       //
190       QApplication::restoreOverrideCursor();
191       aPanel->setFirstPageState(true); 
192       //
193       //Try to load DBF-database...
194       QString theDBFFileName;
195       theDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
196       bool DBF_Stat = myImporter.DBF_OpenDBF(theDBFFileName);
197       // TODO:
198       // add MSG BOX if stat is bad
199       if (DBF_Stat)
200       {
201         myFieldList = myImporter.DBF_GetFieldList();
202         aPanel->setAttributeNames(myFieldList);
203       }
204       aPanel->setDbfState(DBF_Stat);
205     }
206     else
207     {
208       erasePreview();
209       aPanel->setPolygonNames(QStringList());
210       aPanel->setObjectName("");
211       QApplication::restoreOverrideCursor();
212       QString aMess = "Cannot import land cover;\n";
213       if (aStat == -1)
214         aMess += "Cannot open SHP file";
215       else if (aStat == -2)
216         aMess += "Cannot open SHX file";
217       else 
218         aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
219       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
220       commitDocOperation();
221       myImporter.Free();
222       //abort();
223       aPanel->setFirstPageState(false);
224     }
225     
226   }
227   
228 }
229
230 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
231 {
232   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
233
234   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
235   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
236   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
237     
238   return aPanel;
239 }
240
241 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
242                                                   QString& theErrorMsg,
243                                                   QStringList& theBrowseObjectsEntries )
244 {
245
246   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
247   if ( !aPanel ) 
248     return false;
249
250   myLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
251   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
252
253   QStringList aAttrV_T;
254   QStringList aSTL;
255   aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
256
257   QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
258   foreach ( int Ind, aSelIndices ) //check i-base.
259   {
260     TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
261     if ( aShape.IsNull() ) 
262       continue;
263     QString aST = "";
264     if (aPanel->getAttrCheckBoxState())
265     {
266       HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
267       int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
268       aST = aSTL.at(StricklerTypesInd);
269     }
270     // else => ST is empty
271     aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
272   }
273
274   //
275   myLCM->StoreLandCovers(aMapFace2ST);
276
277   QString ObjName;
278   if ( !aPanel->getFileName().isEmpty() )
279     ObjName = aPanel->getObjectName();
280
281   if( !myLCM.IsNull() ) 
282   {
283     myLCM->SetName( ObjName );
284     myLCM->SetColor( Qt::gray );
285     myLCM->Show();
286     module()->setIsToUpdate( myLCM );
287   }
288   
289   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
290
291   erasePreview();
292   myImporter.Free();
293   myAttrV.clear();
294
295   return true;
296 }
297
298 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
299 {
300   Handle(AIS_InteractiveContext) aCtx = NULL;
301
302   OCCViewer_ViewManager* aViewManager = getPreviewManager();
303   if ( aViewManager ) {
304     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
305       aCtx = aViewer->getAISContext();
306     }
307   }
308
309   if ( !aCtx.IsNull() ) {
310     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
311       Handle(AIS_InteractiveObject) anObject = 
312         myPolygonName2PrsShape.value(aName)->getAISObject();
313
314       bool isSelected = theSelectedNames.contains( aName );
315       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
316            ( !isSelected && aCtx->IsSelected( anObject) ) )
317         aCtx->AddOrRemoveSelected( anObject, Standard_False );
318     }
319     aCtx->UpdateCurrentViewer();
320   }
321 }
322
323
324 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
325 {
326   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
327   if ( !aPanel ) {
328     return;
329   }
330
331   OCCViewer_ViewManager* aViewManager = getPreviewManager();
332   Handle(AIS_InteractiveContext) aCtx = NULL;
333   if ( aViewManager ) {
334     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
335       aCtx = aViewer->getAISContext();
336     }
337   }
338   
339   if ( !aCtx.IsNull() )
340   {
341     QStringList aSelectedNames;
342     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
343       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
344       if ( isSelected )
345         aSelectedNames << aName;
346     }
347     aPanel->setSelectedPolygonNames( aSelectedNames );
348   }
349 }
350
351
352 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
353 {
354   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
355     delete aShape;
356   }
357
358   myPolygonName2PrsShape.clear();
359 }
360
361
362 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
363 {
364   LightApp_Application* anApp = module()->getApp();
365   if ( anApp ) {
366     anApp->disconnect( this );
367   }
368
369   erasePreview();
370   myImporter.Free();
371   HYDROGUI_Operation::abortOperation();
372 }
373
374 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
375 {  
376   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
377   if ( !aPanel )
378     return;
379
380   if (theIndex == 2)
381   {
382     //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
383     int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
384     if (Ind == -1)
385       return;
386     //aPanel->setSecondPageState(true);
387     myAttrV.clear();
388     myImporter.DBF_GetAttributeList(Ind, myAttrV ); 
389
390     mySetOfAttrValues.clear();
391     for (size_t i = 0; i < myAttrV.size(); i++)
392     {
393       HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
394       mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal by now..
395     }
396
397     //Collect all strickler_types
398     QSet<QString> aSTSet;
399     Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
400     if ( aDoc )
401     {
402       HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
403       for( ; It.More(); It.Next() )
404       {
405         Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
406         QString TT  = aStricklerTableObj->GetAttrName();
407         if ( !aStricklerTableObj.IsNull())
408         {
409           const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
410           foreach (QString aStr, aStricklerList)
411           {
412             QString AttVal = aStricklerTableObj->GetAttrValue(aStr);
413             aSTSet << aStr;
414           }
415         }
416       }
417     }
418     QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
419     QList<QString> aSTSetList = aSTSet.toList();
420     QVector<QColor> STColors;
421     STColors.reserve(aSTSetList.size());
422     foreach (QString str, aSTSetList)
423     {
424       QColor col = aDoc->GetAssociatedColor(str, NULL);
425       STColors.append (col);
426     }
427
428     aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
429   }
430
431 }
432
433
434 void HYDROGUI_ImportLandCoverMapOp::onApply()
435 {
436   QApplication::setOverrideCursor( Qt::WaitCursor );
437   int anUpdateFlags = 0;
438   QString anErrorMsg;
439   QStringList aBrowseObjectsEntries;
440
441   bool aResult = false;
442   try
443   {
444     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
445   }
446   catch (...)
447   {
448     SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
449     aResult = false;
450   }
451   
452   QApplication::restoreOverrideCursor();
453
454   if ( aResult )
455   {
456     module()->update( anUpdateFlags );
457     commit();
458     browseObjects( aBrowseObjectsEntries );
459   }
460   else
461   {
462     Handle(HYDROData_Entity) LCM = Handle(HYDROData_Entity)::DownCast( myLCM );
463     LCM->Remove();
464     module()->setObjectRemoved( myLCM );
465     abort();
466   }
467 }
468