1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportLandCoverMapOp.h"
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>
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>
45 #include <OCCViewer_ViewManager.h>
46 #include <OCCViewer_ViewModel.h>
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>
54 #include <BRep_Builder.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Wire.hxx>
63 #include <QApplication>
65 #define MAX_LCM_NAME_INDEX 1000
67 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
68 : HYDROGUI_Operation( theModule )
70 setName( tr( "IMPORT_LANDCOVER_MAP" ) );
73 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
80 void HYDROGUI_ImportLandCoverMapOp::startOperation()
82 HYDROGUI_Operation::startOperation();
84 if ( !getPreviewManager() )
85 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
87 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
94 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
96 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
100 QString anObjectName = aPanel->getObjectName().simplified();
101 anObjectName = aPanel->getFileName();
102 if ( !anObjectName.isEmpty() )
103 anObjectName = QFileInfo( anObjectName ).baseName();
105 if ( anObjectName.isEmpty() )
106 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
107 aPanel->setObjectName( anObjectName );
109 myFileName = aPanel->getFileName();
110 if ( myFileName.isEmpty() )
117 QApplication::setOverrideCursor(Qt::WaitCursor);
119 QStringList aPolygonsList;
120 myPolygonFaces.Clear();
123 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
129 Handle(AIS_InteractiveContext) aCtx = NULL;
130 int aShapeTypeOfFile = -1;
132 //Import polygons from SHP file as faces
133 //This faces should be added to the new LCM object
135 int aStat = myImporter.ImportPolygons(doc(), myFileName, aPolygonsList, myPolygonFaces, aShapeTypeOfFile);
138 aPanel->setPolygonNames(aPolygonsList);
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();
147 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
149 aCtx = aViewer->getAISContext();
150 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
154 for ( int i = 1; i <= myPolygonFaces.Length(); i++ )
156 TopoDS_Face aFace = TopoDS::Face(myPolygonFaces.Value( i ));
157 if ( aViewManager && !aCtx.IsNull() )
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);
166 myPolygonName2PrsShape.insert( tr( "DEF_POLYGON_NAME" ) + "_" + QString::number(i), aShape);
170 if ( !aCtx.IsNull() )
172 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
173 aCtx->UpdateCurrentViewer();
176 QApplication::restoreOverrideCursor();
177 aPanel->setFirstPageState(true);
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);
184 // add MSG BOX if stat is bad
187 myFieldList = myImporter.DBF_GetFieldList();
188 aPanel->setAttributeNames(myFieldList);
190 aPanel->setDbfState(DBF_Stat);
195 aPanel->setPolygonNames(QStringList());
196 aPanel->setObjectName("");
197 QApplication::restoreOverrideCursor();
198 QString aMess = tr( "CANNT_IMPORT_LCM" ) + "\n";
200 aMess += tr( "CANNT_OPEN_SHP" );
201 else if (aStat == -2)
202 aMess += tr( "CANNT_OPEN_SHX" );
204 aMess += tr ("SHP_TYPEFORMAT_MESS") + myImporter.GetShapeTypeName(aShapeTypeOfFile);
205 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER_MAP" ), aMess);
206 commitDocOperation();
209 aPanel->setFirstPageState(false);
214 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
216 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
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 ) ) );
225 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
226 QString& theErrorMsg,
227 QStringList& theBrowseObjectsEntries )
230 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
234 myLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
235 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
237 QStringList aAttrV_T;
239 aPanel->getValAttr2StricklerTypeCorr(aAttrV_T, aSTL);
241 QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
242 foreach ( int Ind, aSelIndices )
244 TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
245 if ( aShape.IsNull() )
248 if (aPanel->getAttrCheckBoxState())
250 HYDROData_ShapeFile::DBF_AttrValue aDataVal = myAttrValue[Ind];
251 int aStricklerTypesInd = aAttrV_T.indexOf(QString(aDataVal.myStrVal));
252 aST = aSTL.at(aStricklerTypesInd);
254 // else => ST is empty
255 aMapFace2ST.Add( TopoDS::Face( aShape ), aST );
259 myLCM->StoreLandCovers(aMapFace2ST);
262 if ( !aPanel->getFileName().isEmpty() )
263 anObjName = aPanel->getObjectName();
265 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
267 //check if name of LCM is already exists
268 QSet<QString> aNameList;
271 HYDROData_Iterator anIt( aDoc, KIND_LAND_COVER_MAP );
272 for( ; anIt.More(); anIt.Next() )
273 aNameList << anIt.Current()->GetName();
276 QString aNewName = anObjName;
277 for ( int i = 1; i < MAX_LCM_NAME_INDEX && aNameList.contains(aNewName); i++)
278 aNewName = anObjName + "_" + QString::number(i);
280 if( !myLCM.IsNull() )
282 myLCM->SetName( aNewName );
283 myLCM->SetColor( Qt::gray );
285 module()->setIsToUpdate( myLCM );
288 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
297 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
299 Handle(AIS_InteractiveContext) aCtx = NULL;
301 OCCViewer_ViewManager* aViewManager = getPreviewManager();
302 if ( aViewManager ) {
303 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
304 aCtx = aViewer->getAISContext();
308 if ( !aCtx.IsNull() ) {
309 bool bSelChanged = false;
310 foreach ( QString aName, myPolygonName2PrsShape.keys() )
312 Handle(AIS_InteractiveObject) anObject =
313 myPolygonName2PrsShape.value(aName)->getAISObject();
315 bool isSelected = theSelectedNames.contains( aName );
316 if ( ( isSelected && !aCtx->IsSelected( anObject) ) || ( !isSelected && aCtx->IsSelected( anObject) ) )
318 aCtx->AddOrRemoveSelected( anObject, Standard_False );
323 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
327 if (bSelChanged && aPanel->getViewerState())
328 aCtx->UpdateCurrentViewer();
333 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
335 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
340 aCurIndex = aPanel->getCurrentWizardIndex();
345 OCCViewer_ViewManager* aViewManager = getPreviewManager();
346 Handle(AIS_InteractiveContext) aCtx = NULL;
348 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
349 aCtx = aViewer->getAISContext();
351 if ( !aCtx.IsNull() )
353 QStringList aSelectedNames;
354 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
355 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
357 aSelectedNames << aName;
359 aPanel->setViewerState(false);
360 aPanel->setSelectedPolygonNames( aSelectedNames );
361 aPanel->setViewerState(true);
366 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
368 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape )
371 myPolygonName2PrsShape.clear();
375 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
377 LightApp_Application* anApp = module()->getApp();
379 anApp->disconnect( this );
383 HYDROGUI_Operation::abortOperation();
386 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
388 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
394 //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrValue;
395 int anIndOfSelField = myFieldList.indexOf(aPanel->getSelectedFieldName());
396 if (anIndOfSelField == -1)
398 //aPanel->setSecondPageState(true);
400 myImporter.DBF_GetAttributeList(anIndOfSelField, myAttrValue );
402 mySetOfAttrValues.clear();
403 for (size_t i = 0; i < myAttrValue.size(); i++)
405 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrValue[i];
406 mySetOfAttrValues << QString(aV.myStrVal); //take myStrVal for now..
409 //Collect all strickler_types
410 QSet<QString> aSTSet;
411 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
412 Handle(HYDROData_StricklerTable) DefStricklerTableObj;
415 HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
416 for( ; It.More(); It.Next() )
418 Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
419 if (DefStricklerTableObj.IsNull())
420 DefStricklerTableObj = aStricklerTableObj;
421 if ( !aStricklerTableObj.IsNull())
423 const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
424 foreach (QString aStr, aStricklerList)
430 QList<QString> aSetOfAttrValuesList = mySetOfAttrValues.toList();
431 QList<QString> aSTSetList = aSTSet.toList();
432 QVector<QColor> aSTColors;
433 aSTColors.reserve(aSTSetList.size());
434 foreach (QString str, aSTSetList)
436 QColor col = aDoc->GetAssociatedColor(str, NULL);
437 aSTColors.append (col);
440 //add an empty Strickler type
441 aSTSetList.prepend("");
442 aSTColors.prepend(QColor(Qt::gray));
444 QVector<int> aCurCBIndices(aSetOfAttrValuesList.size());
445 if (DefStricklerTableObj->GetAttrName().trimmed() == aPanel->getSelectedFieldName().trimmed())
446 for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
448 QString aST = DefStricklerTableObj->GetType(aSetOfAttrValuesList[i]);
449 int anIndOfSt = aSTSetList.indexOf(aST);
450 aCurCBIndices[i] = anIndOfSt;
453 //TODO add warning ???
454 for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
455 aCurCBIndices[i] = 0;
457 aPanel->FillCorrespondenceTable(aSetOfAttrValuesList, aSTSetList, aCurCBIndices, aSTColors);
463 void HYDROGUI_ImportLandCoverMapOp::onApply()
465 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
469 if (!aPanel->CheckFirstPageFilling())
472 QApplication::setOverrideCursor( Qt::WaitCursor );
473 int anUpdateFlags = 0;
475 QStringList aBrowseObjectsEntries;
477 bool aResult = false;
480 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
484 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), tr ("CANNT_IMPORT_POLYGONS_FROM_SHP"));
488 QApplication::restoreOverrideCursor();
492 module()->update( anUpdateFlags );
494 browseObjects( aBrowseObjectsEntries );
499 module()->setObjectRemoved( myLCM );