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);
182 if ( !aCtx.IsNull() )
184 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
185 aCtx->UpdateCurrentViewer();
188 QApplication::restoreOverrideCursor();
189 aPanel->setFirstPageState(true);
191 //Try to load DBF-database...
192 QString theDBFFileName;
193 theDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
194 bool DBF_Stat = myImporter.DBF_OpenDBF(theDBFFileName);
196 // add MSG BOX if stat is bad
199 myFieldList = myImporter.DBF_GetFieldList();
200 aPanel->setAttributeNames(myFieldList);
202 aPanel->setDbfState(DBF_Stat);
207 aPanel->setPolygonNames(QStringList());
208 aPanel->setObjectName("");
209 QApplication::restoreOverrideCursor();
210 QString aMess = "Cannot import content of this file as land cover map;\n";
212 aMess += "Cannot open SHP file";
213 else if (aStat == -2)
214 aMess += "Cannot open SHX file";
216 aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
217 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
218 commitDocOperation();
221 aPanel->setFirstPageState(false);
228 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
230 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
232 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
233 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
234 connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
239 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
240 QString& theErrorMsg,
241 QStringList& theBrowseObjectsEntries )
244 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
248 myLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
249 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
251 QStringList aAttrV_T;
253 aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
255 QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
256 foreach ( int Ind, aSelIndices ) //check i-base.
258 TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
259 if ( aShape.IsNull() )
262 if (aPanel->getAttrCheckBoxState())
264 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
265 int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
266 aST = aSTL.at(StricklerTypesInd);
268 // else => ST is empty
269 aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
273 myLCM->StoreLandCovers(aMapFace2ST);
276 if ( !aPanel->getFileName().isEmpty() )
277 ObjName = aPanel->getObjectName();
279 if( !myLCM.IsNull() )
281 myLCM->SetName( ObjName );
282 myLCM->SetColor( Qt::gray );
284 module()->setIsToUpdate( myLCM );
287 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
296 void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSelectedNames )
298 Handle(AIS_InteractiveContext) aCtx = NULL;
300 OCCViewer_ViewManager* aViewManager = getPreviewManager();
301 if ( aViewManager ) {
302 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
303 aCtx = aViewer->getAISContext();
307 if ( !aCtx.IsNull() ) {
308 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
309 Handle(AIS_InteractiveObject) anObject =
310 myPolygonName2PrsShape.value(aName)->getAISObject();
312 bool isSelected = theSelectedNames.contains( aName );
313 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
314 ( !isSelected && aCtx->IsSelected( anObject) ) )
315 aCtx->AddOrRemoveSelected( anObject, Standard_False );
317 aCtx->UpdateCurrentViewer();
322 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
324 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
329 OCCViewer_ViewManager* aViewManager = getPreviewManager();
330 Handle(AIS_InteractiveContext) aCtx = NULL;
331 if ( aViewManager ) {
332 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
333 aCtx = aViewer->getAISContext();
337 if ( !aCtx.IsNull() )
339 QStringList aSelectedNames;
340 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
341 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
343 aSelectedNames << aName;
345 aPanel->setSelectedPolygonNames( aSelectedNames );
350 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
352 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
356 myPolygonName2PrsShape.clear();
360 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
362 LightApp_Application* anApp = module()->getApp();
364 anApp->disconnect( this );
369 HYDROGUI_Operation::abortOperation();
372 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
374 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
380 //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
381 int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
384 //aPanel->setSecondPageState(true);
386 myImporter.DBF_GetAttributeList(Ind, myAttrV );
388 mySetOfAttrValues.clear();
389 for (size_t i = 0; i < myAttrV.size(); i++)
391 HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
392 mySetOfAttrValues << QString(aV.myStrVal); //take myStrVal by now..
395 //Collect all strickler_types
396 QSet<QString> aSTSet;
397 Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
400 HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
401 for( ; It.More(); It.Next() )
403 Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
404 if ( !aStricklerTableObj.IsNull())
406 const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
407 foreach (QString aStr, aStricklerList)
412 QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
413 QList<QString> aSTSetList = aSTSet.toList();
414 QVector<QColor> STColors;
415 STColors.reserve(aSTSetList.size());
416 foreach (QString str, aSTSetList)
418 QColor col = aDoc->GetAssociatedColor(str, NULL);
419 STColors.append (col);
422 aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
428 void HYDROGUI_ImportLandCoverMapOp::onApply()
430 QApplication::setOverrideCursor( Qt::WaitCursor );
431 int anUpdateFlags = 0;
433 QStringList aBrowseObjectsEntries;
435 bool aResult = false;
438 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
442 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
446 QApplication::restoreOverrideCursor();
450 module()->update( anUpdateFlags );
452 browseObjects( aBrowseObjectsEntries );
456 Handle(HYDROData_Entity) LCM = Handle(HYDROData_Entity)::DownCast( myLCM );
458 module()->setObjectRemoved( myLCM );