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 if ( myOperationId != CreateLandCoverMapId )
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& ) ) );
155 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
156 QString& theErrorMsg,
157 QStringList& theBrowseObjectsEntries )
159 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
163 // Check name of the created/edited object
164 QString anObjectName = aPanel->getObjectName().simplified();
165 if ( anObjectName.isEmpty() )
167 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
171 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
173 // check that there are no other objects with the same name in the document
174 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
175 if( !anObject.IsNull() )
177 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
182 Handle(HYDROData_PolylineXY) aPolyline;
183 Handle(HYDROData_Object) aFace;
185 TopTools_ListOfShape aFacesSelectedInViewer;
187 // Get polyline/face selected in combo-box
188 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
190 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
191 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
192 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
193 if ( aPolyline.IsNull() && aFace.IsNull() )
195 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
199 // Get face(s) selected in the 3d viewer
200 else if ( myOperationId == RemoveLandCoverId ||
201 myOperationId == MergeLandCoverId ||
202 myOperationId == ChangeLandCoverTypeId )
206 // Fill in aFacesSelectedInViewer list
207 Handle(AIS_InteractiveContext) aCtx;
208 getSelectedShapes( aFacesSelectedInViewer, aCtx );
212 // Get selected Strickler type
213 QString aSelectedStricklerType;
214 if ( myOperationId == CreateLandCoverMapId ||
215 myOperationId == AddLandCoverId ||
216 myOperationId == MergeLandCoverId ||
217 myOperationId == ChangeLandCoverTypeId )
219 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
220 if ( aSelectedStricklerType.isEmpty() )
222 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
227 // Create / find the new / edited land cover map object
228 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
229 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
231 // Set land cover map name
232 aLandCoverMapObj->SetName( anObjectName );
234 // Add land cover to new / edited land cover map
235 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
237 bool aLandCoverAdded = false;
238 if ( !aPolyline.IsNull() )
239 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
240 else if ( !aFace.IsNull() )
241 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
242 if ( !aLandCoverAdded )
244 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
249 // Remove land cover from edited land cover map
250 if ( myOperationId == RemoveLandCoverId )
252 bool aLandCoverRemoved = false;
253 if ( !aFacesSelectedInViewer.IsEmpty() )
255 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
256 if ( !aLandCoverRemoved )
258 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
264 // Split land cover(s) inside edited land cover map
265 if ( myOperationId == SplitLandCoverId )
267 bool aLandCoverSplitted = false;
268 if ( !aPolyline.IsNull() )
269 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
270 else if ( !aFace.IsNull() )
272 // Get the complete boundary of the object face as the splitting polyline
273 QList<TopoDS_Shape> aBoundShapes;
274 QStringList aBoundNames;
275 aFace->GetBoundaries( aBoundShapes, aBoundNames );
277 for( int i=0, n=aBoundShapes.size(); i<n; i++ )
279 TopoDS_Shape aShape = aBoundShapes[i];
280 if( aShape.IsNull() )
283 bool aSplitResult = aLandCoverMapObj->Split( aShape );
284 aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
287 if ( !aLandCoverSplitted )
289 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
294 // Merge land covers inside edited land cover map
295 if ( myOperationId == MergeLandCoverId )
297 bool aLandCoverMerged = false;
298 if ( !aFacesSelectedInViewer.IsEmpty() )
300 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
301 if ( !aLandCoverMerged )
303 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
309 // Change Strickler type for land cover(s) inside edited land cover map
310 if ( myOperationId == ChangeLandCoverTypeId )
312 bool aLandCoverChangeType = false;
313 if ( !aFacesSelectedInViewer.IsEmpty() )
315 aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
316 if ( !aLandCoverChangeType )
318 theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
324 // Update land cover map object and close preview
325 aLandCoverMapObj->Update();
329 // Publish the newly created land cover map in the Object Browser
330 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
331 if ( myOperationId == CreateLandCoverMapId )
333 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
334 theBrowseObjectsEntries.append( anEntry );
337 // Update presentation of land cover object in the 3d viewer
338 module()->setIsToUpdate( aLandCoverMapObj );
339 module()->getOCCDisplayer()->SetToUpdateColorScale();
341 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
346 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
348 // If the edited land cover map was changed in the combo-box, update myEditedObject
349 if ( myOperationId != CreateLandCoverMapId )
351 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
352 if ( !myEditedObject.IsNull() )
354 // Show preview of the newly selected land cover map
361 void HYDROGUI_LandCoverMapOp::onCreatePreview()
363 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
367 QApplication::setOverrideCursor( Qt::WaitCursor );
369 LightApp_Application* anApp = module()->getApp();
370 if ( !getPreviewManager() )
371 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
372 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
373 OCCViewer_ViewManager* aViewManager = getPreviewManager();
374 if ( aViewManager && !myPreviewPrs )
376 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
378 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
379 if ( !aCtx.IsNull() )
381 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
382 myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
387 if ( aViewManager && myPreviewPrs )
389 TopoDS_Shape aLandCoverMapShape = myEditedObject->GetShape();
390 if( !aLandCoverMapShape.IsNull() )
392 if ( myOperationId == RemoveLandCoverId ||
393 myOperationId == MergeLandCoverId ||
394 myOperationId == ChangeLandCoverTypeId )
395 myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
396 myPreviewPrs->setShape( aLandCoverMapShape );
400 module()->update( UF_OCCViewer | UF_FitAll );
402 QApplication::restoreOverrideCursor();
405 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
407 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
411 Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
412 if ( !aCtx.IsNull() )
414 int aNbSelected = aCtx->NbSelected();
416 if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
417 // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
418 aPanel->setApplyEnabled( aNbSelected > 0 );
419 else if ( myOperationId == MergeLandCoverId )
420 // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
421 aPanel->setApplyEnabled( aNbSelected > 1 );
423 if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
425 if ( aNbSelected == 1 && !myEditedObject.IsNull() )
427 TopTools_ListOfShape aFacesSelectedInViewer;
428 getSelectedShapes( aFacesSelectedInViewer, aCtx );
429 if ( aFacesSelectedInViewer.Extent() == 1 )
431 QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
432 if ( !aType.isEmpty() )
433 aPanel->setSelectedStricklerTypeName( aType );
440 void HYDROGUI_LandCoverMapOp::closePreview()
448 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
452 if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
453 aPanel->setApplyEnabled( false );
456 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
458 OCCViewer_ViewManager* aViewManager = getPreviewManager();
459 Handle(AIS_InteractiveContext) aCtx = NULL;
460 if ( aViewManager ) {
461 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
462 aCtx = aViewer->getAISContext();
468 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
469 Handle(AIS_InteractiveContext)& theCtx )
471 if ( theCtx.IsNull() )
472 theCtx = getInteractiveContext();
474 if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
476 for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
478 TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
479 if ( aSelectedShape.IsNull() )
482 theSelectedShapes.Append( aSelectedShape );