Salome HOME
Merge branch 'BR_HYDRO_IMPS_2016' of ssh://gitolite3@git.salome-platform.org/modules...
[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   startDocOperation();    
117   QApplication::setOverrideCursor(Qt::WaitCursor);
118   
119   QStringList aPolygonsList;
120   myPolygonFaces.Clear();
121   myImporter.Free();
122
123   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
124   if ( !aStudy )
125     return;
126
127   erasePreview();
128
129   Handle(AIS_InteractiveContext) aCtx = NULL;
130   int aShapeTypeOfFile = -1;
131
132   //Import polygons from SHP file as faces
133   //This faces should be added to the new LCM object
134
135   int aStat = myImporter.ImportPolygons(doc(), myFileName, aPolygonsList, myPolygonFaces, aShapeTypeOfFile);
136   if (aStat == 1)
137   {
138     aPanel->setPolygonNames(aPolygonsList);
139
140     LightApp_Application* anApp = module()->getApp();
141     if ( !getPreviewManager() )
142       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
143     OCCViewer_ViewManager* aViewManager = getPreviewManager();
144
145     if ( aViewManager )
146     {
147       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
148       {
149         aCtx = aViewer->getAISContext();
150         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
151       }
152     }
153
154     for ( int i = 1; i <= myPolygonFaces.Length(); i++ ) 
155     {
156       TopoDS_Face aFace = TopoDS::Face(myPolygonFaces.Value( i ));
157       if ( aViewManager && !aCtx.IsNull() )
158       {
159         HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
160         //Preview color is grey
161         aShape->setFillingColor(QColor(50,50,50), false, false);
162         aShape->setBorderColor(QColor(50,50,50), false, false);
163         if( !aFace.IsNull() )
164           aShape->setShape( aFace);
165
166         myPolygonName2PrsShape.insert( tr( "DEF_POLYGON_NAME" ) + "_" + QString::number(i), aShape);
167       }
168     }
169
170     if ( !aCtx.IsNull() ) 
171     {
172       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
173       aCtx->UpdateCurrentViewer();
174     }
175     //
176     QApplication::restoreOverrideCursor();
177     aPanel->setFirstPageState(true); 
178     //
179     //Try to load DBF-database...
180     QString aDBFFileName;
181     aDBFFileName = myFileName.simplified().replace( myFileName.simplified().size() - 4, 4, ".dbf");
182     bool DBF_Stat = myImporter.DBF_OpenDBF(aDBFFileName);
183     // TODO:
184     // add MSG BOX if stat is bad
185     if (DBF_Stat)
186     {
187       myFieldList = myImporter.DBF_GetFieldList();
188       aPanel->setAttributeNames(myFieldList);
189     }
190     aPanel->setDbfState(DBF_Stat);
191     aPanel->setDbfRecordsNb(myImporter.DBF_GetNbRecords());
192   }
193   else
194   {
195     erasePreview();
196     aPanel->setPolygonNames(QStringList());
197     aPanel->setObjectName("");
198     QApplication::restoreOverrideCursor();
199     QString aMess = tr( "CANNT_IMPORT_LCM" ) + "\n"; 
200     if (aStat == -1)
201       aMess += tr( "CANNT_OPEN_SHP" );
202     else if (aStat == -2)
203       aMess += tr( "CANNT_OPEN_SHX" );
204     else 
205       aMess += tr ("SHP_TYPEFORMAT_MESS") + myImporter.GetShapeTypeName(aShapeTypeOfFile);
206     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER_MAP" ), aMess);
207     commitDocOperation();
208     myImporter.Free();
209     //abort();
210     aPanel->setFirstPageState(false);
211   }
212
213 }
214
215 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
216 {
217   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
218
219   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
220   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
221   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
222     
223   return aPanel;
224 }
225
226 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
227                                                   QString& theErrorMsg,
228                                                   QStringList& theBrowseObjectsEntries )
229 {
230
231   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
232   if ( !aPanel ) 
233     return false;
234
235   myLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
236   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
237
238   QStringList aAttrV_T;
239   QStringList aSTL;
240   aPanel->getValAttr2StricklerTypeCorr(aAttrV_T, aSTL);
241
242   QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
243   foreach ( int Ind, aSelIndices )
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 aDataVal = myAttrValue[Ind];
252       int aStricklerTypesInd = aAttrV_T.indexOf(QString(aDataVal.myStrVal));
253       aST = aSTL.at(aStricklerTypesInd);
254     }
255     // else => ST is empty
256     aMapFace2ST.Add( TopoDS::Face( aShape ), aST );
257   }
258
259   //
260   myLCM->StoreLandCovers(aMapFace2ST);
261
262   QString anObjName;
263   if ( !aPanel->getFileName().isEmpty() )
264     anObjName = aPanel->getObjectName();
265   
266   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
267
268   //check if name of LCM is already exists
269   QSet<QString> aNameList;
270   if (aDoc)
271   {
272     HYDROData_Iterator anIt( aDoc, KIND_LAND_COVER_MAP );
273     for( ; anIt.More(); anIt.Next() )
274       aNameList << anIt.Current()->GetName();
275   }
276
277   QString aNewName = anObjName;
278   for ( int i = 1; i < MAX_LCM_NAME_INDEX && aNameList.contains(aNewName); i++)
279     aNewName = anObjName + "_" + QString::number(i);
280
281   if( !myLCM.IsNull() ) 
282   {
283     myLCM->SetName( aNewName );
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   myAttrValue.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     bool bSelChanged = false;
311     foreach ( QString aName, myPolygonName2PrsShape.keys() ) 
312     {
313       Handle(AIS_InteractiveObject) anObject = 
314         myPolygonName2PrsShape.value(aName)->getAISObject();
315
316       bool isSelected = theSelectedNames.contains( aName );
317       if ( ( isSelected && !aCtx->IsSelected( anObject) ) || ( !isSelected && aCtx->IsSelected( anObject) ) )
318       {
319         aCtx->AddOrRemoveSelected( anObject, Standard_False );
320         bSelChanged = true;
321       }
322     }
323
324     HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
325     if ( !aPanel ) 
326       return;
327
328     if (bSelChanged && aPanel->getViewerState())
329       aCtx->UpdateCurrentViewer();
330   }
331 }
332
333
334 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
335 {
336   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
337   if ( !aPanel )
338     return;
339
340   int aCurIndex = -1;
341   aCurIndex = aPanel->getCurrentWizardIndex();
342
343   if (aCurIndex != 0)
344     return;
345
346   OCCViewer_ViewManager* aViewManager = getPreviewManager();
347   Handle(AIS_InteractiveContext) aCtx = NULL;
348   if ( aViewManager )
349     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
350       aCtx = aViewer->getAISContext();
351   
352   if ( !aCtx.IsNull() )
353   {
354     QStringList aSelectedNames;
355     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
356       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
357       if ( isSelected )
358         aSelectedNames << aName;
359     }
360     aPanel->setViewerState(false);
361     aPanel->setSelectedPolygonNames( aSelectedNames );
362     aPanel->setViewerState(true);
363   }
364 }
365
366
367 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
368 {
369   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape )
370     delete aShape;
371
372   myPolygonName2PrsShape.clear();
373 }
374
375
376 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
377 {
378   LightApp_Application* anApp = module()->getApp();
379   if ( anApp )
380     anApp->disconnect( this );
381
382   erasePreview();
383   myImporter.Free();
384   HYDROGUI_Operation::abortOperation();
385 }
386
387 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
388 {  
389   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
390   if ( !aPanel )
391     return;
392
393   if (theIndex == 2)
394   {
395     //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrValue;
396     int anIndOfSelField = myFieldList.indexOf(aPanel->getSelectedFieldName());
397     if (anIndOfSelField == -1)
398       return;
399     //aPanel->setSecondPageState(true);
400     myAttrValue.clear();
401     myImporter.DBF_GetAttributeList(anIndOfSelField, myAttrValue ); 
402
403     mySetOfAttrValues.clear();
404     for (size_t i = 0; i < myAttrValue.size(); i++)
405     {
406       HYDROData_ShapeFile::DBF_AttrValue aV = myAttrValue[i];
407       mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal for now..
408     }
409
410     //Collect all strickler_types
411     QSet<QString> aSTSet;
412     Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
413     Handle(HYDROData_StricklerTable) DefStricklerTableObj;
414     if ( aDoc )
415     {
416       HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
417       for( ; It.More(); It.Next() )
418       {
419         Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
420         if (DefStricklerTableObj.IsNull())
421           DefStricklerTableObj = aStricklerTableObj;
422         if ( !aStricklerTableObj.IsNull())
423         {
424           const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
425           foreach (QString aStr, aStricklerList)
426             aSTSet << aStr;
427         }
428       }
429     }
430
431     QList<QString> aSetOfAttrValuesList = mySetOfAttrValues.toList();
432     QList<QString> aSTSetList = aSTSet.toList();
433     QVector<QColor> aSTColors;
434     aSTColors.reserve(aSTSetList.size());
435     foreach (QString str, aSTSetList)
436     {
437       QColor col = aDoc->GetAssociatedColor(str, NULL);
438       aSTColors.append (col);
439     }
440
441     //add an empty Strickler type
442     aSTSetList.prepend(""); 
443     aSTColors.prepend(QColor(Qt::gray));
444     
445     QVector<int> aCurCBIndices(aSetOfAttrValuesList.size());
446     if (DefStricklerTableObj->GetAttrName().trimmed() == aPanel->getSelectedFieldName().trimmed())
447       for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
448       {
449         QString aST = DefStricklerTableObj->GetType(aSetOfAttrValuesList[i]);
450         int anIndOfSt = aSTSetList.indexOf(aST);
451         aCurCBIndices[i] = anIndOfSt;
452       }
453     else
454       //TODO add warning ???
455       for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
456         aCurCBIndices[i] = 0;
457
458     aPanel->FillCorrespondenceTable(aSetOfAttrValuesList, aSTSetList, aCurCBIndices, aSTColors);    
459   }
460
461 }
462
463
464 void HYDROGUI_ImportLandCoverMapOp::onApply()
465 {
466   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
467   if ( !aPanel )
468     return;
469
470   if (!aPanel->CheckFirstPageFilling())
471     return;
472   
473   QApplication::setOverrideCursor( Qt::WaitCursor );
474   int anUpdateFlags = 0;
475   QString anErrorMsg;
476   QStringList aBrowseObjectsEntries;
477
478   bool aResult = false;
479   try
480   {
481     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
482   }
483   catch (...)
484   {
485     SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), tr ("CANNT_IMPORT_POLYGONS_FROM_SHP"));
486     aResult = false;
487   }
488   
489   QApplication::restoreOverrideCursor();
490
491   if ( aResult )
492   {
493     module()->update( anUpdateFlags );
494     commit();
495     browseObjects( aBrowseObjectsEntries );
496   }
497   else
498   {
499     myLCM->Remove();
500     module()->setObjectRemoved( myLCM );
501     abort();
502   }
503 }
504