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