1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_LandCoverMapOp.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_OCCDisplayer.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_LandCoverMapDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28 #include "HYDROGUI_ShapeLandCoverMap.h"
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_StricklerTable.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Object.h>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewModel.h>
38 #include <LightApp_Application.h>
40 #include <TopoDS_Face.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <AIS_Shape.hxx>
45 #include <QApplication>
47 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
48 : HYDROGUI_Operation( theModule ),
49 myOperationId( theOperationId ),
52 switch( myOperationId )
54 case CreateLandCoverMapId:
55 setName( tr( "CREATE_LAND_COVER_MAP" ) );
58 setName( tr( "ADD_LAND_COVER" ) );
60 case RemoveLandCoverId:
61 setName( tr( "REMOVE_LAND_COVER" ) );
63 case SplitLandCoverId:
64 setName( tr( "SPLIT_LAND_COVER" ) );
66 case MergeLandCoverId:
67 setName( tr( "MERGE_LAND_COVER" ) );
69 case ChangeLandCoverTypeId:
70 setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
75 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
80 void HYDROGUI_LandCoverMapOp::startOperation()
82 HYDROGUI_Operation::startOperation();
84 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
88 aPanel->blockSignals( true );
92 // Set name of the created/edited land cover map object
93 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
94 if ( myOperationId != CreateLandCoverMapId )
96 if ( isApplyAndClose() )
97 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
98 if ( !myEditedObject.IsNull() )
100 anObjectName = myEditedObject->GetName();
102 // Construct a list of names of all land cover map objects defined within the data model
103 QStringList aLandCoverMapNames;
104 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
105 for( ; anIterator.More(); anIterator.Next() )
107 Handle(HYDROData_LandCoverMap) aLandCoverObj =
108 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
109 if ( !aLandCoverObj.IsNull() )
110 aLandCoverMapNames.append( aLandCoverObj->GetName() );
113 //aLandCoverMapNames.sort();
114 aPanel->setObjectNames( aLandCoverMapNames );
117 aPanel->setObjectName( anObjectName );
120 QStringList aPolylineFaceNames;
121 onCreatePreview( aPolylineFaceNames );
123 aPanel->blockSignals( false );
125 module()->update( UF_OCCViewer | UF_FitAll );
128 void HYDROGUI_LandCoverMapOp::abortOperation()
132 HYDROGUI_Operation::abortOperation();
134 module()->update( UF_OCCViewer | UF_FitAll );
137 void HYDROGUI_LandCoverMapOp::commitOperation()
141 HYDROGUI_Operation::commitOperation();
143 module()->update( UF_OCCViewer | UF_FitAll );
146 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
148 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
149 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
150 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
151 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
152 this, SLOT( onCreatePreview( const QStringList& ) ) );
156 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
157 QString& theErrorMsg,
158 QStringList& theBrowseObjectsEntries )
160 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
164 // Check name of the created/edited object
165 QString anObjectName = aPanel->getObjectName().simplified();
166 if ( anObjectName.isEmpty() )
168 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
172 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
174 // check that there are no other objects with the same name in the document
175 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
176 if( !anObject.IsNull() )
178 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
183 Handle(HYDROData_PolylineXY) aPolyline;
184 Handle(HYDROData_Object) aFace;
186 TopTools_ListOfShape aFacesSelectedInViewer;
188 // Get polyline/face selected in combo-box
189 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
191 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
192 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
193 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
194 if ( aPolyline.IsNull() && aFace.IsNull() )
196 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
200 // Get face(s) selected in the 3d viewer
201 else if ( myOperationId == RemoveLandCoverId ||
202 myOperationId == MergeLandCoverId ||
203 myOperationId == ChangeLandCoverTypeId )
207 // Fill in aFacesSelectedInViewer list
208 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
209 if ( !aCtx.IsNull() && aCtx->NbSelected() > 0 )
211 for ( aCtx->InitSelected(); aCtx->MoreSelected(); aCtx->NextSelected() )
213 TopoDS_Shape aSelectedShape = aCtx->SelectedShape();
214 if ( aSelectedShape.IsNull() )
217 aFacesSelectedInViewer.Append( aSelectedShape );
223 // Get selected Strickler type
224 QString aSelectedStricklerType;
225 if ( myOperationId == CreateLandCoverMapId ||
226 myOperationId == AddLandCoverId ||
227 myOperationId == MergeLandCoverId )
229 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
230 if ( aSelectedStricklerType.isEmpty() )
232 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
237 // Create / find the new / edited land cover map object
238 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
239 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
241 // Set land cover map name
242 aLandCoverMapObj->SetName( anObjectName );
244 // Add land cover to new / edited land cover map
245 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
247 bool aLandCoverAdded = false;
248 if ( !aPolyline.IsNull() )
249 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
250 else if ( !aFace.IsNull() )
251 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
252 if ( !aLandCoverAdded )
254 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
259 // Remove land cover from edited land cover map
260 if ( myOperationId == RemoveLandCoverId )
262 bool aLandCoverRemoved = false;
263 if ( !aFacesSelectedInViewer.IsEmpty() )
265 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
266 if ( !aLandCoverRemoved )
268 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
274 // Split land cover(s) inside edited land cover map
275 if ( myOperationId == SplitLandCoverId )
277 bool aLandCoverSplitted = false;
278 if ( !aPolyline.IsNull() )
279 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
280 else if ( !aFace.IsNull() )
282 // Get the complete boundary of the object face as the splitting polyline
283 QList<TopoDS_Shape> aBoundShapes;
284 QStringList aBoundNames;
285 aFace->GetBoundaries( aBoundShapes, aBoundNames );
287 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
289 TopoDS_Shape aShape = aBoundShapes[i];
290 if( aShape.IsNull() )
293 bool aSplitResult = aLandCoverMapObj->Split( aShape );
294 aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
297 if ( !aLandCoverSplitted )
299 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
304 // Merge land covers inside edited land cover map
305 if ( myOperationId == MergeLandCoverId )
307 bool aLandCoverMerged = false;
308 if ( !aFacesSelectedInViewer.IsEmpty() )
310 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
311 if ( !aLandCoverMerged )
313 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
319 // Update land cover map object and close preview
320 aLandCoverMapObj->Update();
324 // Publish the newly created land cover map in the Object Browser
325 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
326 if ( myOperationId == CreateLandCoverMapId )
328 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
329 theBrowseObjectsEntries.append( anEntry );
332 // Update presentation of land cover object in the 3d viewer
333 module()->setIsToUpdate( aLandCoverMapObj );
334 module()->getOCCDisplayer()->SetToUpdateColorScale();
336 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
341 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
343 // If the edited land cover map was changed in the combo-box, update myEditedObject
344 if ( myOperationId != CreateLandCoverMapId )
346 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
347 if ( !myEditedObject.IsNull() )
349 // Show preview of the newly selected land cover map
351 QStringList aPolylineFaceNames;
352 onCreatePreview( aPolylineFaceNames );
357 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
359 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
363 QApplication::setOverrideCursor( Qt::WaitCursor );
365 LightApp_Application* anApp = module()->getApp();
366 if ( !getPreviewManager() )
367 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
368 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
369 OCCViewer_ViewManager* aViewManager = getPreviewManager();
370 if ( aViewManager && !myPreviewPrs )
372 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
374 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
375 if ( !aCtx.IsNull() )
377 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
378 myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
383 if ( aViewManager && myPreviewPrs )
385 TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
386 if( !aLandCoverMapShape.IsNull() )
388 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId)
389 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
390 myPreviewPrs->setShape( aLandCoverMapShape );
394 module()->update( UF_OCCViewer | UF_FitAll );
396 QApplication::restoreOverrideCursor();
399 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
401 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
405 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
406 if ( !aCtx.IsNull() )
408 int aNbSelected = aCtx->NbSelected();
410 if ( myOperationId == RemoveLandCoverId )
411 aPanel->setApplyEnabled( aNbSelected > 0 );
412 else if ( myOperationId == MergeLandCoverId )
413 aPanel->setApplyEnabled( aNbSelected > 1 );
417 void HYDROGUI_LandCoverMapOp::closePreview()
425 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
429 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId )
430 aPanel->setApplyEnabled( false );
433 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
435 OCCViewer_ViewManager* aViewManager = getPreviewManager();
436 Handle(AIS_InteractiveContext) aCtx = NULL;
437 if ( aViewManager ) {
438 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
439 aCtx = aViewer->getAISContext();