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>
58 #include <SUIT_Desktop.h>
59 #include <SUIT_ViewManager.h>
61 #include <BRep_Builder.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <TopoDS_Wire.hxx>
67 #include <BRepTools.hxx>
70 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
71 : HYDROGUI_Operation( theModule )
73 setName( tr( "IMPORT_LANDCOVER" ) );
76 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
81 void HYDROGUI_ImportLandCoverMapOp::startOperation()
83 HYDROGUI_Operation::startOperation();
85 if ( !getPreviewManager() ) {
86 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
87 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
90 if ( !isApplyAndClose() ) {
94 HYDROGUI_ImportLandCoverMapDlg* aPanel =
95 ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
103 void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
105 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
109 QString anObjectName = aPanel->getObjectName().simplified();
110 anObjectName = aPanel->getFileName();
111 if ( !anObjectName.isEmpty() )
112 anObjectName = QFileInfo( anObjectName ).baseName();
114 if ( anObjectName.isEmpty() )
115 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVERMAP_NAME" ) );
116 aPanel->setObjectName( anObjectName );
118 QString aFileName = aPanel->getFileName();
119 if ( aFileName.isEmpty() )
125 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
130 QApplication::setOverrideCursor(Qt::WaitCursor);
132 QStringList aPolygonsList;
133 TopTools_SequenceOfShape aFaces;
134 HYDROData_ShapeFile anImporter;
136 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
142 Handle(AIS_InteractiveContext) aCtx = NULL;
143 int aShapeTypeOfFile = -1;
144 int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
147 aPanel->setPolygonNames(aPolygonsList);
149 LightApp_Application* anApp = module()->getApp();
150 if ( !getPreviewManager() )
151 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
152 OCCViewer_ViewManager* aViewManager = getPreviewManager();
156 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
158 aCtx = aViewer->getAISContext();
159 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
163 for ( int i = 1; i <= aFaces.Length(); i++ )
165 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
166 if ( aViewManager && !aCtx.IsNull() )
168 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
169 //Green color for now..
170 aShape->setFillingColor(QColor(0,255,0), false, false);
171 aShape->setBorderColor(QColor(0,255,0), false, false);
172 if( !aFace.IsNull() )
173 aShape->setShape( aFace);
175 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
178 if ( !aCtx.IsNull() )
180 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
181 aCtx->UpdateCurrentViewer();
184 QApplication::restoreOverrideCursor();
185 commitDocOperation();
190 aPanel->setPolygonNames(QStringList());
191 aPanel->setObjectName("");
192 QApplication::restoreOverrideCursor();
193 QString aMess = "Cannot import land cover;\n";
195 aMess += "Cannot open SHP file";
196 else if (aStat == -2)
197 aMess += "Cannot open SHX file";
199 aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
200 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
201 commitDocOperation();
210 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
212 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
214 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
216 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
218 connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
219 connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onBack( const int ) ) );
220 connect( aPanel, SIGNAL( Finish( const int ) ), SLOT( onFinish( 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 QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
236 aPanel->removePolygonNames( aSelectedtPolygons );
238 if (!aSelectedtPolygons.empty())
240 Handle(HYDROData_LandCoverMap) aLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
241 TopoDS_Shape aResShape;
242 if (aSelectedtPolygons.size() > 1)
246 BB.MakeCompound(cmp);
248 foreach ( QString aName, aSelectedtPolygons ) {
249 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
250 if ( aShape.IsNull() )
253 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
254 delete aShapeToDelete;
260 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
261 if ( !aShape.IsNull() )
263 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
264 delete aShapeToDelete;
270 QString aLCName = aPanel->getObjectName() + "_polygon";
272 for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
273 aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
274 aLCM->SetName( aLCName );
276 //aLCM->SetFillingColor( aLCM->DefaultFillingColor() );
277 //aLCM->SetBorderColor( aLCM->DefaultBorderColor() );
279 //aLCM->SetShape(aResShape);
284 module()->setIsToUpdate( aLCM );
289 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
291 if ( isApplyAndClose() )
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 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
310 Handle(AIS_InteractiveObject) anObject =
311 myPolygonName2PrsShape.value(aName)->getAISObject();
313 bool isSelected = theSelectedNames.contains( aName );
314 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
315 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
316 aCtx->AddOrRemoveSelected( anObject, Standard_False );
320 HYDROGUI_Shape* aHydroSh = myPolygonName2PrsShape.value( aName );
321 aHydroSh->setFillingColor(QColor(0,255,255), true, true);
324 aCtx->UpdateCurrentViewer();
329 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
332 HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
337 OCCViewer_ViewManager* aViewManager = getPreviewManager();
338 Handle(AIS_InteractiveContext) aCtx = NULL;
339 if ( aViewManager ) {
340 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
341 aCtx = aViewer->getAISContext();
345 if ( !aCtx.IsNull() )
347 QStringList aSelectedNames;
348 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
349 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
351 aSelectedNames << aName;
354 aPanel->setSelectedPolygonNames( aSelectedNames );
359 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
361 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
365 myPolygonName2PrsShape.clear();
369 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
371 LightApp_Application* anApp = module()->getApp();
373 anApp->disconnect( this );
378 HYDROGUI_Operation::abortOperation();
382 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
387 void HYDROGUI_ImportLandCoverMapOp::onBack( const int theIndex )