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 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
224 Handle(AIS_InteractiveContext) aCtx = NULL;
225 int aShapeTypeOfFile = -1;
226 int aStat = anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces, aShapeTypeOfFile);
229 aPanel->setPolygonNames(aPolygonsList);
231 LightApp_Application* anApp = module()->getApp();
232 if ( !getPreviewManager() )
233 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
234 OCCViewer_ViewManager* aViewManager = getPreviewManager();
238 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
240 aCtx = aViewer->getAISContext();
241 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
245 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
246 for ( int i = 1; i <= aFaces.Length(); i++ )
248 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
250 aLC->setShape( aFace );
252 if ( aViewManager && !aCtx.IsNull() )
254 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
256 aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
257 aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
258 if( !aFace.IsNull() )
259 aShape->setShape( aLC->GetShape() );
260 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
265 if ( !aCtx.IsNull() )
267 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
268 aCtx->UpdateCurrentViewer();
271 QApplication::restoreOverrideCursor();
272 commitDocOperation();
277 aPanel->setPolygonNames(QStringList());
278 aPanel->setObjectName("");
279 QApplication::restoreOverrideCursor();
280 QString aMess = "Cannot import land cover;\n";
282 aMess += "Cannot open SHP file";
283 else if (aStat == -2)
284 aMess += "Cannot open SHX file";
286 aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
287 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
288 commitDocOperation();
297 void HYDROGUI_ImportLandCoverOp::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 );
319 aCtx->UpdateCurrentViewer();
324 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
327 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
332 OCCViewer_ViewManager* aViewManager = getPreviewManager();
333 Handle(AIS_InteractiveContext) aCtx = NULL;
334 if ( aViewManager ) {
335 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
336 aCtx = aViewer->getAISContext();
340 if ( !aCtx.IsNull() )
342 QStringList aSelectedNames;
343 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
344 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
346 aSelectedNames << aName;
349 aPanel->setSelectedPolygonNames( aSelectedNames );
354 void HYDROGUI_ImportLandCoverOp::erasePreview()
356 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
360 myPolygonName2PrsShape.clear();
364 void HYDROGUI_ImportLandCoverOp::abortOperation()
366 LightApp_Application* anApp = module()->getApp();
368 anApp->disconnect( this );
373 HYDROGUI_Operation::abortOperation();