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_Tool.h"
29 #include "HYDROGUI_ImportLandCoverMapDlg.h"
30 #include "HYDROGUI_Shape.h"
31 #include <HYDROData_LandCoverMap.h>
32 #include <HYDROGUI_ZLayers.h>
34 #include <HYDROGUI_DataObject.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_ShapeFile.h>
37 #include <HYDROData_Profile.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_FileDlg.h>
41 #include <LightApp_Application.h>
43 #include <QApplication>
46 #include <SUIT_MessageBox.h>
48 #include <OCCViewer_ViewManager.h>
49 #include <OCCViewer_ViewModel.h>
51 #include <SalomeApp_Study.h>
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>
59 #include <SUIT_Desktop.h>
60 #include <SUIT_ViewManager.h>
62 #include <BRep_Builder.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Wire.hxx>
68 #include <HYDROData_LandCoverMap.h>
70 //TODO add definitions into TS file
71 //TODO check if LCM name is already exists..
73 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
74 : HYDROGUI_Operation( theModule )
76 setName( tr( "IMPORT_LANDCOVERMAP" ) );
79 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
86 void HYDROGUI_ImportLandCoverMapOp::startOperation()
88 HYDROGUI_Operation::startOperation();
90 if ( !getPreviewManager() ) {
91 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
92 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
95 HYDROGUI_ImportLandCoverMapDlg* aPanel =
96 ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
104 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
106 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
110 QString anObjectName = aPanel->getObjectName().simplified();
111 anObjectName = aPanel->getFileName();
112 if ( !anObjectName.isEmpty() )
113 anObjectName = QFileInfo( anObjectName ).baseName();
115 if ( anObjectName.isEmpty() )
116 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
117 aPanel->setObjectName( anObjectName );
119 myFileName = aPanel->getFileName();
120 if ( myFileName.isEmpty() )
126 QString anExt = myFileName.split('.', QString::SkipEmptyParts).back();
131 QApplication::setOverrideCursor(Qt::WaitCursor);
133 QStringList aPolygonsList;
134 myPolygonFaces.Clear();
137 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
143 Handle(AIS_InteractiveContext) aCtx = NULL;
144 int aShapeTypeOfFile = -1;
146 //Import polygons from SHP file as faces
147 //This faces should be added to the new LCM object
149 int aStat = myImporter.ImportPolygons(myFileName, aPolygonsList, myPolygonFaces, aShapeTypeOfFile);
152 aPanel->setPolygonNames(aPolygonsList);
154 LightApp_Application* anApp = module()->getApp();
155 if ( !getPreviewManager() )
156 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
157 OCCViewer_ViewManager* aViewManager = getPreviewManager();
161 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
163 aCtx = aViewer->getAISContext();
164 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
168 for ( int i = 1; i <= myPolygonFaces.Length(); i++ )
170 TopoDS_Face aFace = TopoDS::Face(myPolygonFaces.Value( i ));
171 if ( aViewManager && !aCtx.IsNull() )
173 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
174 //Green color for now..
175 aShape->setFillingColor(QColor(0,255,0), false, false);
176 aShape->setBorderColor(QColor(0,255,0), false, false);
177 if( !aFace.IsNull() )
178 aShape->setShape( aFace);
180 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
184 if ( !aCtx.IsNull() )
186 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
187 aCtx->UpdateCurrentViewer();
190 QApplication::restoreOverrideCursor();
191 aPanel->setFirstPageState(true);
193 //Try to load DBF-database...
194 QString theDBFFileName;
195 theDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
196 bool DBF_Stat = myImporter.DBF_OpenDBF(theDBFFileName);
198 // add MSG BOX if stat is bad
201 myFieldList = myImporter.DBF_GetFieldList();
202 aPanel->setAttributeNames(myFieldList);
204 aPanel->setDbfState(DBF_Stat);
209 aPanel->setPolygonNames(QStringList());
210 aPanel->setObjectName("");
211 QApplication::restoreOverrideCursor();
212 QString aMess = "Cannot import land cover;\n";
214 aMess += "Cannot open SHP file";
215 else if (aStat == -2)
216 aMess += "Cannot open SHX file";
218 aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
219 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
220 commitDocOperation();
223 aPanel->setFirstPageState(false);
230 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
232 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
234 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
235 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
236 connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
241 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
242 QString& theErrorMsg,
243 QStringList& theBrowseObjectsEntries )
246 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
250 myLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
251 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
253 QStringList aAttrV_T;
255 aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
257 QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
258 foreach ( int Ind, aSelIndices ) //check i-base.
260 TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
261 if ( aShape.IsNull() )
264 if (aPanel->getAttrCheckBoxState())
266 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
267 int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
268 aST = aSTL.at(StricklerTypesInd);
270 // else => ST is empty
271 aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
275 myLCM->StoreLandCovers(aMapFace2ST);
278 if ( !aPanel->getFileName().isEmpty() )
279 ObjName = aPanel->getObjectName();
281 if( !myLCM.IsNull() )
283 myLCM->SetName( ObjName );
284 myLCM->SetColor( Qt::gray );
286 module()->setIsToUpdate( myLCM );
289 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
298 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
300 Handle(AIS_InteractiveContext) aCtx = NULL;
302 OCCViewer_ViewManager* aViewManager = getPreviewManager();
303 if ( aViewManager ) {
304 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
305 aCtx = aViewer->getAISContext();
309 if ( !aCtx.IsNull() ) {
310 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
311 Handle(AIS_InteractiveObject) anObject =
312 myPolygonName2PrsShape.value(aName)->getAISObject();
314 bool isSelected = theSelectedNames.contains( aName );
315 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
316 ( !isSelected && aCtx->IsSelected( anObject) ) )
317 aCtx->AddOrRemoveSelected( anObject, Standard_False );
319 aCtx->UpdateCurrentViewer();
324 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
326 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
331 OCCViewer_ViewManager* aViewManager = getPreviewManager();
332 Handle(AIS_InteractiveContext) aCtx = NULL;
333 if ( aViewManager ) {
334 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
335 aCtx = aViewer->getAISContext();
339 if ( !aCtx.IsNull() )
341 QStringList aSelectedNames;
342 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
343 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
345 aSelectedNames << aName;
347 aPanel->setSelectedPolygonNames( aSelectedNames );
352 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
354 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
358 myPolygonName2PrsShape.clear();
362 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
364 LightApp_Application* anApp = module()->getApp();
366 anApp->disconnect( this );
371 HYDROGUI_Operation::abortOperation();
374 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
376 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
382 //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
383 int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
386 //aPanel->setSecondPageState(true);
388 myImporter.DBF_GetAttributeList(Ind, myAttrV );
390 mySetOfAttrValues.clear();
391 for (size_t i = 0; i < myAttrV.size(); i++)
393 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
394 mySetOfAttrValues << QString(aV.myStrVal); //take myStrVal by now..
397 //Collect all strickler_types
398 QSet<QString> aSTSet;
399 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
402 HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
403 for( ; It.More(); It.Next() )
405 Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
406 QString TT = aStricklerTableObj->GetAttrName();
407 if ( !aStricklerTableObj.IsNull())
409 const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
410 foreach (QString aStr, aStricklerList)
412 QString AttVal = aStricklerTableObj->GetAttrValue(aStr);
418 QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
419 QList<QString> aSTSetList = aSTSet.toList();
420 QVector<QColor> STColors;
421 STColors.reserve(aSTSetList.size());
422 foreach (QString str, aSTSetList)
424 QColor col = aDoc->GetAssociatedColor(str, NULL);
425 STColors.append (col);
428 aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
434 void HYDROGUI_ImportLandCoverMapOp::onApply()
436 QApplication::setOverrideCursor( Qt::WaitCursor );
437 int anUpdateFlags = 0;
439 QStringList aBrowseObjectsEntries;
441 bool aResult = false;
444 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
448 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
452 QApplication::restoreOverrideCursor();
456 module()->update( anUpdateFlags );
458 browseObjects( aBrowseObjectsEntries );
462 Handle(HYDROData_Entity) LCM = Handle(HYDROData_Entity)::DownCast( myLCM );
464 module()->setObjectRemoved( myLCM );