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"
29 #include "HYDROGUI_OCCSelector.h"
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_StricklerTable.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Object.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <SUIT_ViewWindow.h>
46 #include <TopoDS_Face.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <AIS_Shape.hxx>
51 #include <QApplication>
52 #include <QMouseEvent>
54 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
55 : HYDROGUI_Operation( theModule ),
56 myOperationId( theOperationId ),
59 switch( myOperationId )
61 case CreateLandCoverMapId:
62 setName( tr( "CREATE_LAND_COVER_MAP" ) );
65 setName( tr( "ADD_LAND_COVER" ) );
67 case RemoveLandCoverId:
68 setName( tr( "REMOVE_LAND_COVER" ) );
70 case SplitLandCoverId:
71 setName( tr( "SPLIT_LAND_COVER" ) );
73 case MergeLandCoverId:
74 setName( tr( "MERGE_LAND_COVER" ) );
76 case ChangeLandCoverTypeId:
77 setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
82 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
87 void HYDROGUI_LandCoverMapOp::startOperation()
89 HYDROGUI_Operation::startOperation();
91 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
95 aPanel->blockSignals( true );
99 // Set name of the created/edited land cover map object
100 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
101 if ( myOperationId != CreateLandCoverMapId )
103 if ( isApplyAndClose() )
104 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
106 // Construct a list of names of all land cover map objects defined within the data model
107 QStringList aLandCoverMapNames;
108 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
109 for( ; anIterator.More(); anIterator.Next() )
111 Handle(HYDROData_LandCoverMap) aLandCoverObj =
112 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
113 if ( !aLandCoverObj.IsNull() )
114 aLandCoverMapNames.append( aLandCoverObj->GetName() );
117 //aLandCoverMapNames.sort();
118 aPanel->setObjectNames( aLandCoverMapNames );
120 if ( myEditedObject.IsNull() )
122 if ( !aLandCoverMapNames.empty() )
124 anObjectName = aLandCoverMapNames.first();
125 if ( !anObjectName.isEmpty())
127 Handle(HYDROData_LandCoverMap) anObject =
128 Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), anObjectName ) );
129 if( !anObject.IsNull() )
130 myEditedObject = anObject;
135 anObjectName = myEditedObject->GetName();
137 aPanel->setObjectName( anObjectName );
140 if ( myOperationId != CreateLandCoverMapId )
143 aPanel->blockSignals( false );
145 module()->update( UF_OCCViewer | UF_FitAll );
148 void HYDROGUI_LandCoverMapOp::abortOperation()
152 HYDROGUI_Operation::abortOperation();
154 module()->update( UF_OCCViewer | UF_FitAll );
157 void HYDROGUI_LandCoverMapOp::commitOperation()
161 HYDROGUI_Operation::commitOperation();
163 module()->update( UF_OCCViewer | UF_FitAll );
166 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
168 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
169 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
170 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
174 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
175 QString& theErrorMsg,
176 QStringList& theBrowseObjectsEntries )
178 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
182 // Check name of the created/edited object
183 QString anObjectName = aPanel->getObjectName().simplified();
184 if ( anObjectName.isEmpty() )
186 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
190 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
192 // check that there are no other objects with the same name in the document
193 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
194 if( !anObject.IsNull() )
196 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
201 Handle(HYDROData_PolylineXY) aPolyline;
202 Handle(HYDROData_Object) aFace;
204 TopTools_ListOfShape aFacesSelectedInViewer;
206 // Get polyline/face selected in combo-box
207 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
209 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
210 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
211 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
212 if ( aPolyline.IsNull() && aFace.IsNull() )
214 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
218 // Get face(s) selected in the 3d viewer
219 else if ( myOperationId == RemoveLandCoverId ||
220 myOperationId == MergeLandCoverId ||
221 myOperationId == ChangeLandCoverTypeId )
225 // Fill in aFacesSelectedInViewer list
226 Handle(AIS_InteractiveContext) aCtx;
227 getSelectedShapes( aFacesSelectedInViewer, aCtx );
231 // Get selected Strickler type
232 QString aSelectedStricklerType;
233 if ( myOperationId == CreateLandCoverMapId ||
234 myOperationId == AddLandCoverId ||
235 myOperationId == MergeLandCoverId ||
236 myOperationId == ChangeLandCoverTypeId )
238 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
239 if ( aSelectedStricklerType.isEmpty() )
241 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
246 // Create / find the new / edited land cover map object
247 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
248 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
249 if ( aLandCoverMapObj.IsNull() )
251 theErrorMsg = tr( "LAND_COVER_MAP_UNDEFINED" );
255 // Set land cover map name
256 aLandCoverMapObj->SetName( anObjectName );
258 // Add land cover to new / edited land cover map
259 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
261 bool aLandCoverAdded = false;
262 if ( !aPolyline.IsNull() )
263 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
264 else if ( !aFace.IsNull() )
265 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
266 if ( !aLandCoverAdded )
268 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
273 // Remove land cover from edited land cover map
274 if ( myOperationId == RemoveLandCoverId )
276 bool aLandCoverRemoved = false;
277 if ( !aFacesSelectedInViewer.IsEmpty() )
279 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
280 if ( !aLandCoverRemoved )
282 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
288 // Split land cover(s) inside edited land cover map
289 if ( myOperationId == SplitLandCoverId )
291 bool aLandCoverSplitted = false;
292 if ( !aPolyline.IsNull() )
293 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
294 else if ( !aFace.IsNull() )
296 // Get the complete boundary of the object face as the splitting polyline
297 QList<TopoDS_Shape> aBoundShapes;
298 QStringList aBoundNames;
299 aFace->GetBoundaries( aBoundShapes, aBoundNames );
301 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
303 TopoDS_Shape aShape = aBoundShapes[i];
304 if( aShape.IsNull() )
307 bool aSplitResult = aLandCoverMapObj->Split( aShape );
308 aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
311 if ( !aLandCoverSplitted )
313 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
318 // Merge land covers inside edited land cover map
319 if ( myOperationId == MergeLandCoverId )
321 bool aLandCoverMerged = false;
322 if ( !aFacesSelectedInViewer.IsEmpty() )
324 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
325 if ( !aLandCoverMerged )
327 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
333 // Change Strickler type for land cover(s) inside edited land cover map
334 if ( myOperationId == ChangeLandCoverTypeId )
336 bool aLandCoverChangeType = false;
337 if ( !aFacesSelectedInViewer.IsEmpty() )
339 aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
340 if ( !aLandCoverChangeType )
342 theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
348 // Update land cover map object and close preview
349 aLandCoverMapObj->Update();
353 // Publish the newly created land cover map in the Object Browser
354 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
355 if ( myOperationId == CreateLandCoverMapId )
357 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
358 theBrowseObjectsEntries.append( anEntry );
361 // Update presentation of land cover object in the 3d viewer
362 module()->setIsToUpdate( aLandCoverMapObj );
363 module()->getOCCDisplayer()->SetToUpdateColorScale();
365 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
367 if ( myOperationId == CreateLandCoverMapId )
368 module()->enableLCMActions();
373 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
375 // If the edited land cover map was changed in the combo-box, update myEditedObject
376 if ( myOperationId != CreateLandCoverMapId )
378 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
379 if ( !myEditedObject.IsNull() )
381 // Show preview of the newly selected land cover map
388 void HYDROGUI_LandCoverMapOp::onCreatePreview()
390 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
394 QApplication::setOverrideCursor( Qt::WaitCursor );
396 LightApp_Application* anApp = module()->getApp();
397 if ( !getPreviewManager() )
398 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
399 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
400 OCCViewer_ViewManager* aViewManager = getPreviewManager();
401 if ( aViewManager && !myPreviewPrs )
403 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
405 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
406 if ( !aCtx.IsNull() )
408 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
409 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
410 disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
411 aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
413 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
414 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
415 connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
416 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
418 LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
421 QList<SUIT_Selector*> aSelectorList;
422 aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
423 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
424 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
426 HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
427 if ( aHydroSelector )
429 disconnect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
430 connect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
435 connect( this, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
436 myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
441 if ( aViewManager && myPreviewPrs && !myEditedObject.IsNull() )
443 TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
444 if( !aLandCoverMapShape.IsNull() )
446 if ( myOperationId == RemoveLandCoverId ||
447 myOperationId == MergeLandCoverId ||
448 myOperationId == ChangeLandCoverTypeId )
449 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
450 myPreviewPrs->setShape( aLandCoverMapShape );
454 module()->update( UF_OCCViewer | UF_FitAll );
456 QApplication::restoreOverrideCursor();
459 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
461 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
465 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
466 if ( !aCtx.IsNull() )
468 int aNbSelected = aCtx->NbSelected();
470 if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
471 // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
472 aPanel->setApplyEnabled( aNbSelected > 0 );
473 else if ( myOperationId == MergeLandCoverId )
474 // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
475 aPanel->setApplyEnabled( aNbSelected > 1 );
477 if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
479 if ( aNbSelected == 1 && !myEditedObject.IsNull() )
481 TopTools_ListOfShape aFacesSelectedInViewer;
482 getSelectedShapes( aFacesSelectedInViewer, aCtx );
483 if ( aFacesSelectedInViewer.Extent() == 1 )
485 QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
486 if ( !aType.isEmpty() )
487 aPanel->setSelectedStricklerTypeName( aType );
494 void HYDROGUI_LandCoverMapOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
496 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
499 void HYDROGUI_LandCoverMapOp::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
501 if (theEvent->button() != Qt::LeftButton) return;
502 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
504 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
508 OCCViewer_ViewManager* aViewManager = getPreviewManager();
512 OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
516 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
520 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
522 if (myStartPnt == myEndPnt)
524 if ( !aViewer->isPreselectionEnabled() ) {
525 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
526 if ( !aView3d.IsNull() ) {
527 aCtx->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
531 Handle(StdSelect_ViewerSelector3d) aMainSelector = aCtx->MainSelector();
532 if ( aMainSelector.IsNull() )
534 const Standard_Integer aDetectedNb = aMainSelector->NbPicked();
535 if ( aDetectedNb == 0 )
537 aCtx->ClearSelected( false );
541 for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
543 Handle(SelectMgr_EntityOwner) anOwner = aMainSelector->Picked (aDetIter);
544 aCtx->AddOrRemoveSelected( anOwner, Standard_False );
549 aCtx->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
550 myEndPnt.x(), myEndPnt.y(),
551 aView->getViewPort()->getView(), Standard_False );
554 aCtx->UpdateCurrentViewer();
555 emit selectionChanged();
558 void HYDROGUI_LandCoverMapOp::closePreview()
566 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
570 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
571 aPanel->setApplyEnabled( false );
573 OCCViewer_ViewManager* aViewManager = getPreviewManager();
576 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
578 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
579 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
580 disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
581 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
582 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
583 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
584 connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
585 aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
587 LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
590 QList<SUIT_Selector*> aSelectorList;
591 aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
592 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
593 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
595 HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
596 if ( aHydroSelector )
598 disconnect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
599 connect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
607 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
609 OCCViewer_ViewManager* aViewManager = getPreviewManager();
610 Handle(AIS_InteractiveContext) aCtx = NULL;
611 if ( aViewManager ) {
612 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
613 aCtx = aViewer->getAISContext();
619 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
620 Handle(AIS_InteractiveContext)& theCtx )
622 if ( theCtx.IsNull() )
623 theCtx = getInteractiveContext();
625 if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
627 for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
629 TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
630 if ( aSelectedShape.IsNull() )
633 theSelectedShapes.Append( aSelectedShape );