Salome HOME
d410018fb9d69426e6c65c38e2c31dd94ee5b922
[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       if ( !aCtx.IsNull() ) 
182       {
183         UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
184         aCtx->UpdateCurrentViewer();
185       }
186       //
187       QApplication::restoreOverrideCursor();
188       aPanel->setFirstPageState(true); 
189     }
190     else
191     {
192       erasePreview();
193       aPanel->setPolygonNames(QStringList());
194       aPanel->setObjectName("");
195       QApplication::restoreOverrideCursor();
196       QString aMess = "Cannot import land cover;\n";
197       if (aStat == -1)
198         aMess += "Cannot open SHP file";
199       else if (aStat == -2)
200         aMess += "Cannot open SHX file";
201       else 
202         aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
203       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
204       commitDocOperation();
205       myImporter.Free();
206       //abort();
207       aPanel->setFirstPageState(false);
208     }
209     
210   }
211   
212 }
213
214 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
215 {
216   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
217
218   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
219   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
220   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( 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   Handle(HYDROData_LandCoverMap) aLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
236   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
237
238   QStringList aAttrV_T;
239   QStringList aSTL;
240   aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
241
242   QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
243   foreach ( int Ind, aSelIndices ) //check i-base.
244   {
245     TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
246     if ( aShape.IsNull() ) 
247       continue;
248     QString aST = "";
249     if (aPanel->getAttrCheckBoxState())
250     {
251       HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
252       int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
253       aST = aSTL.at(StricklerTypesInd);
254     }
255     // else => ST is empty
256     aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
257   }
258
259   //
260   aLCM->StoreLandCovers(aMapFace2ST);
261
262   QString ObjName;
263   if ( !aPanel->getFileName().isEmpty() )
264     ObjName = aPanel->getObjectName();
265
266   if( !aLCM.IsNull() ) 
267   {
268     aLCM->SetName( ObjName );
269     aLCM->SetColor( Qt::gray );
270     aLCM->Show();
271     module()->setIsToUpdate( aLCM );
272   }
273   
274   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
275
276   erasePreview();
277   myImporter.Free();
278   myAttrV.clear();
279
280   return true;
281 }
282
283 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
284 {
285   Handle(AIS_InteractiveContext) aCtx = NULL;
286
287   OCCViewer_ViewManager* aViewManager = getPreviewManager();
288   if ( aViewManager ) {
289     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
290       aCtx = aViewer->getAISContext();
291     }
292   }
293
294   if ( !aCtx.IsNull() ) {
295     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
296       Handle(AIS_InteractiveObject) anObject = 
297         myPolygonName2PrsShape.value(aName)->getAISObject();
298
299       bool isSelected = theSelectedNames.contains( aName );
300       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
301            ( !isSelected && aCtx->IsSelected( anObject) ) )
302         aCtx->AddOrRemoveSelected( anObject, Standard_False );
303     }
304     aCtx->UpdateCurrentViewer();
305   }
306 }
307
308
309 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
310 {
311   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
312   if ( !aPanel ) {
313     return;
314   }
315
316   OCCViewer_ViewManager* aViewManager = getPreviewManager();
317   Handle(AIS_InteractiveContext) aCtx = NULL;
318   if ( aViewManager ) {
319     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
320       aCtx = aViewer->getAISContext();
321     }
322   }
323   
324   if ( !aCtx.IsNull() )
325   {
326     QStringList aSelectedNames;
327     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
328       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
329       if ( isSelected )
330         aSelectedNames << aName;
331     }
332     aPanel->setSelectedPolygonNames( aSelectedNames );
333   }
334 }
335
336
337 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
338 {
339   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
340     delete aShape;
341   }
342
343   myPolygonName2PrsShape.clear();
344 }
345
346
347 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
348 {
349   LightApp_Application* anApp = module()->getApp();
350   if ( anApp ) {
351     anApp->disconnect( this );
352   }
353
354   erasePreview();
355   myImporter.Free();
356   HYDROGUI_Operation::abortOperation();
357 }
358
359 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
360 {  
361   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
362   if ( !aPanel )
363     return;
364
365   if (theIndex == 1)
366   {
367       //Try to load DBF-database...
368       QString theDBFFileName;
369       theDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
370       bool DBF_Stat = myImporter.DBF_OpenDBF(theDBFFileName);
371       // TODO:
372       // add MSG BOX if stat is bad
373       myFieldList = myImporter.DBF_GetFieldList();
374       aPanel->setAttributeNames(myFieldList);
375   }
376
377   if (theIndex == 2)
378   {
379     //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
380     int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
381     if (Ind == -1)
382     {
383       return;
384     }
385     //aPanel->setSecondPageState(true);
386     myAttrV.clear();
387     myImporter.DBF_GetAttributeList(Ind, myAttrV ); 
388
389     mySetOfAttrValues.clear();
390     for (size_t i = 0; i < myAttrV.size(); i++)
391     {
392       HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
393       mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal by now..
394     }
395
396     //Collect all strickler_types
397     QSet<QString> aSTSet;
398     Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
399     if ( aDoc )
400     {
401       HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
402       for( ; It.More(); It.Next() )
403       {
404         Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );       
405         if ( !aStricklerTableObj.IsNull())
406         {
407           const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
408           foreach (QString aStr, aStricklerList)
409             aSTSet << aStr;
410         }
411       }
412     }
413     QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
414     QList<QString> aSTSetList = aSTSet.toList();
415     QVector<QColor> STColors;
416     STColors.reserve(aSTSetList.size());
417     foreach (QString str, aSTSetList)
418     {
419       QColor col = aDoc->GetAssociatedColor(str, NULL);
420       STColors.append (col);
421     }
422
423     aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
424   }
425
426 }
427
428
429 void HYDROGUI_ImportLandCoverMapOp::onApply()
430 {
431   QApplication::setOverrideCursor( Qt::WaitCursor );
432   int anUpdateFlags = 0;
433   QString anErrorMsg;
434   QStringList aBrowseObjectsEntries;
435
436   bool aResult = false;
437   try
438   {
439     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
440   }
441   catch (...)
442   {
443     SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
444     aResult = false;
445   }
446   
447   QApplication::restoreOverrideCursor();
448
449   if ( aResult )
450   {
451     module()->update( anUpdateFlags );
452     commit();
453     browseObjects( aBrowseObjectsEntries );
454   }
455   else
456   {
457     abort();
458   }
459 }
460