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