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_ImportLandCoverOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_ImportLandCoverDlg.h"
30 #include "HYDROGUI_Shape.h"
31 #include <HYDROData_LandCover.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>
68 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
69 : HYDROGUI_Operation( theModule )
71 setName( tr( "IMPORT_LANDCOVER" ) );
74 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
79 void HYDROGUI_ImportLandCoverOp::startOperation()
81 HYDROGUI_Operation::startOperation();
83 if ( !getPreviewManager() ) {
84 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
85 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
88 if ( !isApplyAndClose() ) {
92 HYDROGUI_ImportLandCoverDlg* aPanel =
93 ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
102 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
104 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
106 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
108 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
113 bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
114 QString& theErrorMsg,
115 QStringList& theBrowseObjectsEntries )
118 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
123 QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
124 aPanel->removePolygonNames( aSelectedtPolygons );
126 if (!aSelectedtPolygons.empty())
128 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
129 TopoDS_Shape aResShape;
130 if (aSelectedtPolygons.size() > 1)
134 BB.MakeCompound(cmp);
136 foreach ( QString aName, aSelectedtPolygons ) {
137 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
138 if ( aShape.IsNull() )
141 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
142 delete aShapeToDelete;
148 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
149 if ( !aShape.IsNull() )
151 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
152 delete aShapeToDelete;
158 QString aLCName = aPanel->getObjectName() + "_polygon";
160 for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
161 aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
162 aLC->SetName( aLCName );
163 aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
164 aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
166 aLC->setShape(aResShape);
171 module()->setIsToUpdate( aLC );
176 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
178 if ( isApplyAndClose() )
185 void HYDROGUI_ImportLandCoverOp::onFileSelected()
187 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
191 QString anObjectName = aPanel->getObjectName().simplified();
192 anObjectName = aPanel->getFileName();
193 if ( !anObjectName.isEmpty() )
194 anObjectName = QFileInfo( anObjectName ).baseName();
196 if ( anObjectName.isEmpty() )
197 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
198 aPanel->setObjectName( anObjectName );
200 QString aFileName = aPanel->getFileName();
201 if ( aFileName.isEmpty() )
207 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
212 QApplication::setOverrideCursor(Qt::WaitCursor);
214 QStringList aPolygonsList;
215 TopTools_SequenceOfShape aFaces;
216 HYDROData_ShapeFile anImporter;
218 if (!anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces))
220 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
224 aPanel->setPolygonNames(aPolygonsList);
226 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
233 Handle(AIS_InteractiveContext) aCtx = NULL;
235 LightApp_Application* anApp = module()->getApp();
236 if ( !getPreviewManager() )
237 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
238 OCCViewer_ViewManager* aViewManager = getPreviewManager();
242 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
244 aCtx = aViewer->getAISContext();
245 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
250 QApplication::restoreOverrideCursor();
252 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
253 for ( int i = 1; i <= aFaces.Length(); i++ )
255 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
257 aLC->setShape( aFace );
259 if ( aViewManager && !aCtx.IsNull() )
261 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
263 aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
264 aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
265 if( !aFace.IsNull() )
266 aShape->setShape( aLC->GetShape() );
267 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
272 if ( !aCtx.IsNull() ) {
273 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
274 aCtx->UpdateCurrentViewer();
277 commitDocOperation();
285 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
287 Handle(AIS_InteractiveContext) aCtx = NULL;
289 OCCViewer_ViewManager* aViewManager = getPreviewManager();
290 if ( aViewManager ) {
291 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
292 aCtx = aViewer->getAISContext();
296 if ( !aCtx.IsNull() ) {
297 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
298 Handle(AIS_InteractiveObject) anObject =
299 myPolygonName2PrsShape.value(aName)->getAISObject();
301 bool isSelected = theSelectedNames.contains( aName );
302 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
303 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
304 aCtx->AddOrRemoveSelected( anObject, Standard_False );
307 aCtx->UpdateCurrentViewer();
312 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
315 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
320 OCCViewer_ViewManager* aViewManager = getPreviewManager();
321 Handle(AIS_InteractiveContext) aCtx = NULL;
322 if ( aViewManager ) {
323 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
324 aCtx = aViewer->getAISContext();
328 if ( !aCtx.IsNull() )
330 QStringList aSelectedNames;
331 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
332 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
334 aSelectedNames << aName;
337 aPanel->setSelectedPolygonNames( aSelectedNames );
342 void HYDROGUI_ImportLandCoverOp::erasePreview()
344 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
348 myPolygonName2PrsShape.clear();
352 void HYDROGUI_ImportLandCoverOp::abortOperation()
354 LightApp_Application* anApp = module()->getApp();
356 anApp->disconnect( this );
361 HYDROGUI_Operation::abortOperation();