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 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
72 : HYDROGUI_Operation( theModule )
74 setName( tr( "IMPORT_LANDCOVERMAP" ) );
77 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
84 void HYDROGUI_ImportLandCoverMapOp::startOperation()
86 HYDROGUI_Operation::startOperation();
88 if ( !getPreviewManager() ) {
89 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
90 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
93 HYDROGUI_ImportLandCoverMapDlg* aPanel =
94 ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
102 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
104 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
108 QString anObjectName = aPanel->getObjectName().simplified();
109 anObjectName = aPanel->getFileName();
110 if ( !anObjectName.isEmpty() )
111 anObjectName = QFileInfo( anObjectName ).baseName();
113 if ( anObjectName.isEmpty() )
114 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
115 aPanel->setObjectName( anObjectName );
117 myFileName = aPanel->getFileName();
118 if ( myFileName.isEmpty() )
124 QString anExt = myFileName.split('.', QString::SkipEmptyParts).back();
129 QApplication::setOverrideCursor(Qt::WaitCursor);
131 QStringList aPolygonsList;
132 myPolygonFaces.Clear();
135 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
141 Handle(AIS_InteractiveContext) aCtx = NULL;
142 int aShapeTypeOfFile = -1;
144 //Import polygons from SHP file as faces
145 //This faces should be added to the new LCM object
147 int aStat = myImporter.ImportPolygons(myFileName, aPolygonsList, myPolygonFaces, aShapeTypeOfFile);
150 aPanel->setPolygonNames(aPolygonsList);
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();
159 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
161 aCtx = aViewer->getAISContext();
162 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
166 for ( int i = 1; i <= myPolygonFaces.Length(); i++ )
168 TopoDS_Face aFace = TopoDS::Face(myPolygonFaces.Value( i ));
169 if ( aViewManager && !aCtx.IsNull() )
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);
178 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
181 if ( !aCtx.IsNull() )
183 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
184 aCtx->UpdateCurrentViewer();
187 QApplication::restoreOverrideCursor();
188 aPanel->setFirstPageState(true);
193 aPanel->setPolygonNames(QStringList());
194 aPanel->setObjectName("");
195 QApplication::restoreOverrideCursor();
196 QString aMess = "Cannot import land cover;\n";
198 aMess += "Cannot open SHP file";
199 else if (aStat == -2)
200 aMess += "Cannot open SHX file";
202 aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
203 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
204 commitDocOperation();
207 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() );
235 Handle(HYDROData_LandCoverMap) aLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
236 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
238 QStringList aAttrV_T;
240 aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
242 QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
243 foreach ( int Ind, aSelIndices ) //check i-base.
245 TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
246 if ( aShape.IsNull() )
249 if (aPanel->getAttrCheckBoxState())
251 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
252 int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
253 aST = aSTL.at(StricklerTypesInd);
255 // else => ST is empty
256 aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
260 aLCM->StoreLandCovers(aMapFace2ST);
263 if ( !aPanel->getFileName().isEmpty() )
264 ObjName = aPanel->getObjectName();
268 aLCM->SetName( ObjName );
269 aLCM->SetColor( Qt::gray );
271 module()->setIsToUpdate( aLCM );
274 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
283 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
285 Handle(AIS_InteractiveContext) aCtx = NULL;
287 OCCViewer_ViewManager* aViewManager = getPreviewManager();
288 if ( aViewManager ) {
289 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
290 aCtx = aViewer->getAISContext();
294 if ( !aCtx.IsNull() ) {
295 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
296 Handle(AIS_InteractiveObject) anObject =
297 myPolygonName2PrsShape.value(aName)->getAISObject();
299 bool isSelected = theSelectedNames.contains( aName );
300 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
301 ( !isSelected && aCtx->IsSelected( anObject) ) )
302 aCtx->AddOrRemoveSelected( anObject, Standard_False );
304 aCtx->UpdateCurrentViewer();
309 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
311 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
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();
324 if ( !aCtx.IsNull() )
326 QStringList aSelectedNames;
327 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
328 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
330 aSelectedNames << aName;
332 aPanel->setSelectedPolygonNames( aSelectedNames );
337 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
339 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
343 myPolygonName2PrsShape.clear();
347 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
349 LightApp_Application* anApp = module()->getApp();
351 anApp->disconnect( this );
356 HYDROGUI_Operation::abortOperation();
359 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
361 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
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);
372 // add MSG BOX if stat is bad
373 myFieldList = myImporter.DBF_GetFieldList();
374 aPanel->setAttributeNames(myFieldList);
379 //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
380 int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
385 //aPanel->setSecondPageState(true);
387 myImporter.DBF_GetAttributeList(Ind, myAttrV );
389 mySetOfAttrValues.clear();
390 for (size_t i = 0; i < myAttrV.size(); i++)
392 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
393 mySetOfAttrValues << QString(aV.myStrVal); //take myStrVal by now..
396 //Collect all strickler_types
397 QSet<QString> aSTSet;
398 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
401 HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
402 for( ; It.More(); It.Next() )
404 Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
405 if ( !aStricklerTableObj.IsNull())
407 const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
408 foreach (QString aStr, aStricklerList)
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)
419 QColor col = aDoc->GetAssociatedColor(str, NULL);
420 STColors.append (col);
423 aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
429 void HYDROGUI_ImportLandCoverMapOp::onApply()
431 QApplication::setOverrideCursor( Qt::WaitCursor );
432 int anUpdateFlags = 0;
434 QStringList aBrowseObjectsEntries;
436 bool aResult = false;
439 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
443 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
447 QApplication::restoreOverrideCursor();
451 module()->update( anUpdateFlags );
453 browseObjects( aBrowseObjectsEntries );