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>
41 #include <TopoDS_Face.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <AIS_Shape.hxx>
46 #include <QApplication>
48 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
49 : HYDROGUI_Operation( theModule ),
50 myOperationId( theOperationId ),
53 switch( myOperationId )
55 case CreateLandCoverMapId:
56 setName( tr( "CREATE_LAND_COVER_MAP" ) );
59 setName( tr( "ADD_LAND_COVER" ) );
61 case RemoveLandCoverId:
62 setName( tr( "REMOVE_LAND_COVER" ) );
64 case SplitLandCoverId:
65 setName( tr( "SPLIT_LAND_COVER" ) );
67 case MergeLandCoverId:
68 setName( tr( "MERGE_LAND_COVER" ) );
70 case ChangeLandCoverTypeId:
71 setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
76 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
81 void HYDROGUI_LandCoverMapOp::startOperation()
83 HYDROGUI_Operation::startOperation();
85 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
89 aPanel->blockSignals( true );
93 // Set name of the created/edited land cover map object
94 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
95 if ( myOperationId != CreateLandCoverMapId )
97 if ( isApplyAndClose() )
98 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
99 if ( !myEditedObject.IsNull() )
101 anObjectName = myEditedObject->GetName();
103 // Construct a list of names of all land cover map objects defined within the data model
104 QStringList aLandCoverMapNames;
105 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
106 for( ; anIterator.More(); anIterator.Next() )
108 Handle(HYDROData_LandCoverMap) aLandCoverObj =
109 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
110 if ( !aLandCoverObj.IsNull() )
111 aLandCoverMapNames.append( aLandCoverObj->GetName() );
114 //aLandCoverMapNames.sort();
115 aPanel->setObjectNames( aLandCoverMapNames );
118 aPanel->setObjectName( anObjectName );
121 QStringList aPolylineFaceNames;
122 onCreatePreview( aPolylineFaceNames );
124 aPanel->blockSignals( false );
126 module()->update( UF_OCCViewer | UF_FitAll );
129 void HYDROGUI_LandCoverMapOp::abortOperation()
133 HYDROGUI_Operation::abortOperation();
135 module()->update( UF_OCCViewer | UF_FitAll );
138 void HYDROGUI_LandCoverMapOp::commitOperation()
142 HYDROGUI_Operation::commitOperation();
144 module()->update( UF_OCCViewer | UF_FitAll );
147 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
149 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
150 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
151 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
152 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
153 this, SLOT( onCreatePreview( const QStringList& ) ) );
157 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
158 QString& theErrorMsg,
159 QStringList& theBrowseObjectsEntries )
161 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
165 // Check name of the created/edited object
166 QString anObjectName = aPanel->getObjectName().simplified();
167 if ( anObjectName.isEmpty() )
169 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
173 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
175 // check that there are no other objects with the same name in the document
176 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
177 if( !anObject.IsNull() )
179 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
184 Handle(HYDROData_PolylineXY) aPolyline;
185 Handle(HYDROData_Object) aFace;
187 TopTools_ListOfShape aFacesSelectedInViewer;
189 // Get polyline/face selected in combo-box
190 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
192 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
193 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
194 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
195 if ( aPolyline.IsNull() && aFace.IsNull() )
197 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
201 // Get face(s) selected in the 3d viewer
202 else if ( myOperationId == RemoveLandCoverId ||
203 myOperationId == MergeLandCoverId ||
204 myOperationId == ChangeLandCoverTypeId )
208 // Fill in aFacesSelectedInViewer list
209 Handle(AIS_InteractiveContext) aCtx;
210 getSelectedShapes( aFacesSelectedInViewer, aCtx );
214 // Get selected Strickler type
215 QString aSelectedStricklerType;
216 if ( myOperationId == CreateLandCoverMapId ||
217 myOperationId == AddLandCoverId ||
218 myOperationId == MergeLandCoverId ||
219 myOperationId == ChangeLandCoverTypeId )
221 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
222 if ( aSelectedStricklerType.isEmpty() )
224 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
229 // Create / find the new / edited land cover map object
230 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
231 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
233 // Set land cover map name
234 aLandCoverMapObj->SetName( anObjectName );
236 // Add land cover to new / edited land cover map
237 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
239 bool aLandCoverAdded = false;
240 if ( !aPolyline.IsNull() )
241 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
242 else if ( !aFace.IsNull() )
243 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
244 if ( !aLandCoverAdded )
246 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
251 // Remove land cover from edited land cover map
252 if ( myOperationId == RemoveLandCoverId )
254 bool aLandCoverRemoved = false;
255 if ( !aFacesSelectedInViewer.IsEmpty() )
257 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
258 if ( !aLandCoverRemoved )
260 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
266 // Split land cover(s) inside edited land cover map
267 if ( myOperationId == SplitLandCoverId )
269 bool aLandCoverSplitted = false;
270 if ( !aPolyline.IsNull() )
271 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
272 else if ( !aFace.IsNull() )
274 // Get the complete boundary of the object face as the splitting polyline
275 QList<TopoDS_Shape> aBoundShapes;
276 QStringList aBoundNames;
277 aFace->GetBoundaries( aBoundShapes, aBoundNames );
279 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
281 TopoDS_Shape aShape = aBoundShapes[i];
282 if( aShape.IsNull() )
285 bool aSplitResult = aLandCoverMapObj->Split( aShape );
286 aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
289 if ( !aLandCoverSplitted )
291 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
296 // Merge land covers inside edited land cover map
297 if ( myOperationId == MergeLandCoverId )
299 bool aLandCoverMerged = false;
300 if ( !aFacesSelectedInViewer.IsEmpty() )
302 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
303 if ( !aLandCoverMerged )
305 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
311 // Change Strickler type for land cover(s) inside edited land cover map
312 if ( myOperationId == ChangeLandCoverTypeId )
314 bool aLandCoverChangeType = false;
315 if ( !aFacesSelectedInViewer.IsEmpty() )
317 aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
318 if ( !aLandCoverChangeType )
320 theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
326 // Update land cover map object and close preview
327 aLandCoverMapObj->Update();
331 // Publish the newly created land cover map in the Object Browser
332 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
333 if ( myOperationId == CreateLandCoverMapId )
335 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
336 theBrowseObjectsEntries.append( anEntry );
339 // Update presentation of land cover object in the 3d viewer
340 module()->setIsToUpdate( aLandCoverMapObj );
341 module()->getOCCDisplayer()->SetToUpdateColorScale();
343 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
348 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
350 // If the edited land cover map was changed in the combo-box, update myEditedObject
351 if ( myOperationId != CreateLandCoverMapId )
353 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
354 if ( !myEditedObject.IsNull() )
356 // Show preview of the newly selected land cover map
358 QStringList aPolylineFaceNames;
359 onCreatePreview( aPolylineFaceNames );
364 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
366 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
370 QApplication::setOverrideCursor( Qt::WaitCursor );
372 LightApp_Application* anApp = module()->getApp();
373 if ( !getPreviewManager() )
374 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
375 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
376 OCCViewer_ViewManager* aViewManager = getPreviewManager();
377 if ( aViewManager && !myPreviewPrs )
379 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
381 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
382 if ( !aCtx.IsNull() )
384 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
385 myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
390 if ( aViewManager && myPreviewPrs )
392 TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
393 if( !aLandCoverMapShape.IsNull() )
395 if ( myOperationId == RemoveLandCoverId ||
396 myOperationId == MergeLandCoverId ||
397 myOperationId == ChangeLandCoverTypeId )
398 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
399 myPreviewPrs->setShape( aLandCoverMapShape );
403 module()->update( UF_OCCViewer | UF_FitAll );
405 QApplication::restoreOverrideCursor();
408 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
410 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
414 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
415 if ( !aCtx.IsNull() )
417 int aNbSelected = aCtx->NbSelected();
419 if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
420 // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
421 aPanel->setApplyEnabled( aNbSelected > 0 );
422 else if ( myOperationId == MergeLandCoverId )
423 // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
424 aPanel->setApplyEnabled( aNbSelected > 1 );
426 if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
428 if ( aNbSelected == 1 && !myEditedObject.IsNull() )
430 TopTools_ListOfShape aFacesSelectedInViewer;
431 getSelectedShapes( aFacesSelectedInViewer, aCtx );
432 if ( aFacesSelectedInViewer.Extent() == 1 )
434 QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
435 if ( !aType.isEmpty() )
436 aPanel->setSelectedStricklerTypeName( aType );
443 void HYDROGUI_LandCoverMapOp::closePreview()
451 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
455 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
456 aPanel->setApplyEnabled( false );
459 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
461 OCCViewer_ViewManager* aViewManager = getPreviewManager();
462 Handle(AIS_InteractiveContext) aCtx = NULL;
463 if ( aViewManager ) {
464 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
465 aCtx = aViewer->getAISContext();
471 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
472 Handle(AIS_InteractiveContext)& theCtx )
474 if ( theCtx.IsNull() )
475 theCtx = getInteractiveContext();
477 if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
479 for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
481 TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
482 if ( aSelectedShape.IsNull() )
485 theSelectedShapes.Append( aSelectedShape );