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() ) );
105 if ( !myEditedObject.IsNull() )
107 anObjectName = myEditedObject->GetName();
109 // Construct a list of names of all land cover map objects defined within the data model
110 QStringList aLandCoverMapNames;
111 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
112 for( ; anIterator.More(); anIterator.Next() )
114 Handle(HYDROData_LandCoverMap) aLandCoverObj =
115 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
116 if ( !aLandCoverObj.IsNull() )
117 aLandCoverMapNames.append( aLandCoverObj->GetName() );
120 //aLandCoverMapNames.sort();
121 aPanel->setObjectNames( aLandCoverMapNames );
124 aPanel->setObjectName( anObjectName );
127 if ( myOperationId != CreateLandCoverMapId )
130 aPanel->blockSignals( false );
132 module()->update( UF_OCCViewer | UF_FitAll );
135 void HYDROGUI_LandCoverMapOp::abortOperation()
139 HYDROGUI_Operation::abortOperation();
141 module()->update( UF_OCCViewer | UF_FitAll );
144 void HYDROGUI_LandCoverMapOp::commitOperation()
148 HYDROGUI_Operation::commitOperation();
150 module()->update( UF_OCCViewer | UF_FitAll );
153 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
155 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
156 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
157 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
161 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
162 QString& theErrorMsg,
163 QStringList& theBrowseObjectsEntries )
165 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
169 // Check name of the created/edited object
170 QString anObjectName = aPanel->getObjectName().simplified();
171 if ( anObjectName.isEmpty() )
173 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
177 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
179 // check that there are no other objects with the same name in the document
180 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
181 if( !anObject.IsNull() )
183 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
188 Handle(HYDROData_PolylineXY) aPolyline;
189 Handle(HYDROData_Object) aFace;
191 TopTools_ListOfShape aFacesSelectedInViewer;
193 // Get polyline/face selected in combo-box
194 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
196 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
197 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
198 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
199 if ( aPolyline.IsNull() && aFace.IsNull() )
201 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
205 // Get face(s) selected in the 3d viewer
206 else if ( myOperationId == RemoveLandCoverId ||
207 myOperationId == MergeLandCoverId ||
208 myOperationId == ChangeLandCoverTypeId )
212 // Fill in aFacesSelectedInViewer list
213 Handle(AIS_InteractiveContext) aCtx;
214 getSelectedShapes( aFacesSelectedInViewer, aCtx );
218 // Get selected Strickler type
219 QString aSelectedStricklerType;
220 if ( myOperationId == CreateLandCoverMapId ||
221 myOperationId == AddLandCoverId ||
222 myOperationId == MergeLandCoverId ||
223 myOperationId == ChangeLandCoverTypeId )
225 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
226 if ( aSelectedStricklerType.isEmpty() )
228 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
233 // Create / find the new / edited land cover map object
234 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
235 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
237 // Set land cover map name
238 aLandCoverMapObj->SetName( anObjectName );
240 // Add land cover to new / edited land cover map
241 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
243 bool aLandCoverAdded = false;
244 if ( !aPolyline.IsNull() )
245 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
246 else if ( !aFace.IsNull() )
247 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
248 if ( !aLandCoverAdded )
250 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
255 // Remove land cover from edited land cover map
256 if ( myOperationId == RemoveLandCoverId )
258 bool aLandCoverRemoved = false;
259 if ( !aFacesSelectedInViewer.IsEmpty() )
261 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
262 if ( !aLandCoverRemoved )
264 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
270 // Split land cover(s) inside edited land cover map
271 if ( myOperationId == SplitLandCoverId )
273 bool aLandCoverSplitted = false;
274 if ( !aPolyline.IsNull() )
275 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
276 else if ( !aFace.IsNull() )
278 // Get the complete boundary of the object face as the splitting polyline
279 QList<TopoDS_Shape> aBoundShapes;
280 QStringList aBoundNames;
281 aFace->GetBoundaries( aBoundShapes, aBoundNames );
283 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
285 TopoDS_Shape aShape = aBoundShapes[i];
286 if( aShape.IsNull() )
289 bool aSplitResult = aLandCoverMapObj->Split( aShape );
290 aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
293 if ( !aLandCoverSplitted )
295 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
300 // Merge land covers inside edited land cover map
301 if ( myOperationId == MergeLandCoverId )
303 bool aLandCoverMerged = false;
304 if ( !aFacesSelectedInViewer.IsEmpty() )
306 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
307 if ( !aLandCoverMerged )
309 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
315 // Change Strickler type for land cover(s) inside edited land cover map
316 if ( myOperationId == ChangeLandCoverTypeId )
318 bool aLandCoverChangeType = false;
319 if ( !aFacesSelectedInViewer.IsEmpty() )
321 aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
322 if ( !aLandCoverChangeType )
324 theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
330 // Update land cover map object and close preview
331 aLandCoverMapObj->Update();
335 // Publish the newly created land cover map in the Object Browser
336 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
337 if ( myOperationId == CreateLandCoverMapId )
339 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
340 theBrowseObjectsEntries.append( anEntry );
343 // Update presentation of land cover object in the 3d viewer
344 module()->setIsToUpdate( aLandCoverMapObj );
345 module()->getOCCDisplayer()->SetToUpdateColorScale();
347 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
352 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
354 // If the edited land cover map was changed in the combo-box, update myEditedObject
355 if ( myOperationId != CreateLandCoverMapId )
357 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
358 if ( !myEditedObject.IsNull() )
360 // Show preview of the newly selected land cover map
367 void HYDROGUI_LandCoverMapOp::onCreatePreview()
369 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
373 QApplication::setOverrideCursor( Qt::WaitCursor );
375 LightApp_Application* anApp = module()->getApp();
376 if ( !getPreviewManager() )
377 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
378 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
379 OCCViewer_ViewManager* aViewManager = getPreviewManager();
380 if ( aViewManager && !myPreviewPrs )
382 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
384 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
385 if ( !aCtx.IsNull() )
387 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
388 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
389 disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
390 aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
392 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
393 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
394 connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
395 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
397 LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
400 QList<SUIT_Selector*> aSelectorList;
401 aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
402 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
403 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
405 HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
406 if ( aHydroSelector )
408 disconnect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
409 connect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
414 connect( this, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
415 myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
420 if ( aViewManager && myPreviewPrs )
422 TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
423 if( !aLandCoverMapShape.IsNull() )
425 if ( myOperationId == RemoveLandCoverId ||
426 myOperationId == MergeLandCoverId ||
427 myOperationId == ChangeLandCoverTypeId )
428 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
429 myPreviewPrs->setShape( aLandCoverMapShape );
433 module()->update( UF_OCCViewer | UF_FitAll );
435 QApplication::restoreOverrideCursor();
438 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
440 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
444 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
445 if ( !aCtx.IsNull() )
447 int aNbSelected = aCtx->NbSelected();
449 if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
450 // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
451 aPanel->setApplyEnabled( aNbSelected > 0 );
452 else if ( myOperationId == MergeLandCoverId )
453 // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
454 aPanel->setApplyEnabled( aNbSelected > 1 );
456 if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
458 if ( aNbSelected == 1 && !myEditedObject.IsNull() )
460 TopTools_ListOfShape aFacesSelectedInViewer;
461 getSelectedShapes( aFacesSelectedInViewer, aCtx );
462 if ( aFacesSelectedInViewer.Extent() == 1 )
464 QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
465 if ( !aType.isEmpty() )
466 aPanel->setSelectedStricklerTypeName( aType );
473 void HYDROGUI_LandCoverMapOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
475 myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
478 void HYDROGUI_LandCoverMapOp::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
480 if (theEvent->button() != Qt::LeftButton) return;
481 if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
483 OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
487 OCCViewer_ViewManager* aViewManager = getPreviewManager();
491 OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
495 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
499 myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
501 if (myStartPnt == myEndPnt)
503 if ( !aViewer->isPreselectionEnabled() ) {
504 Handle(V3d_View) aView3d = aView->getViewPort()->getView();
505 if ( !aView3d.IsNull() ) {
506 aCtx->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
510 Handle(StdSelect_ViewerSelector3d) aMainSelector = aCtx->MainSelector();
511 if ( aMainSelector.IsNull() )
513 const Standard_Integer aDetectedNb = aMainSelector->NbPicked();
514 if ( aDetectedNb == 0 )
516 aCtx->ClearSelected( false );
520 for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
522 Handle(SelectMgr_EntityOwner) anOwner = aMainSelector->Picked (aDetIter);
523 aCtx->AddOrRemoveSelected( anOwner, Standard_False );
528 aCtx->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
529 myEndPnt.x(), myEndPnt.y(),
530 aView->getViewPort()->getView(), Standard_False );
533 aCtx->UpdateCurrentViewer();
534 emit selectionChanged();
537 void HYDROGUI_LandCoverMapOp::closePreview()
545 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
549 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
550 aPanel->setApplyEnabled( false );
552 OCCViewer_ViewManager* aViewManager = getPreviewManager();
555 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
557 disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
558 this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
559 disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
560 this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
561 connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
562 aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
563 connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
564 aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
566 LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
569 QList<SUIT_Selector*> aSelectorList;
570 aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
571 QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
572 for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
574 HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
575 if ( aHydroSelector )
577 disconnect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
578 connect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
586 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
588 OCCViewer_ViewManager* aViewManager = getPreviewManager();
589 Handle(AIS_InteractiveContext) aCtx = NULL;
590 if ( aViewManager ) {
591 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
592 aCtx = aViewer->getAISContext();
598 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
599 Handle(AIS_InteractiveContext)& theCtx )
601 if ( theCtx.IsNull() )
602 theCtx = getInteractiveContext();
604 if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
606 for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
608 TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
609 if ( aSelectedShape.IsNull() )
612 theSelectedShapes.Append( aSelectedShape );