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"
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_StricklerTable.h>
31 #include <HYDROData_PolylineXY.h>
32 #include <HYDROData_Object.h>
34 #include <TopoDS_Face.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
38 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
39 : HYDROGUI_Operation( theModule ),
40 myOperationId( theOperationId ),
43 switch( myOperationId )
45 case CreateLandCoverMapId:
46 setName( tr( "CREATE_LAND_COVER_MAP" ) );
49 setName( tr( "ADD_LAND_COVER" ) );
51 case RemoveLandCoverId:
52 setName( tr( "REMOVE_LAND_COVER" ) );
54 case SplitLandCoverId:
55 setName( tr( "SPLIT_LAND_COVER" ) );
57 case MergeLandCoverId:
58 setName( tr( "MERGE_LAND_COVER" ) );
60 case ChangeLandCoverTypeId:
61 setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
66 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
71 void HYDROGUI_LandCoverMapOp::startOperation()
73 HYDROGUI_Operation::startOperation();
75 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
79 aPanel->blockSignals( true );
83 // Set name of the created/edited land cover map object
84 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
85 if ( myOperationId != CreateLandCoverMapId )
87 if ( isApplyAndClose() )
88 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
89 if ( !myEditedObject.IsNull() )
91 anObjectName = myEditedObject->GetName();
93 // Construct a list of names of all land cover map objects defined within the data model
94 QStringList aLandCoverMapNames;
95 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
96 for( ; anIterator.More(); anIterator.Next() )
98 Handle(HYDROData_LandCoverMap) aLandCoverObj =
99 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
100 if ( !aLandCoverObj.IsNull() )
101 aLandCoverMapNames.append( aLandCoverObj->GetName() );
104 //aLandCoverMapNames.sort();
105 aPanel->setObjectNames( aLandCoverMapNames );
108 aPanel->setObjectName( anObjectName );
110 aPanel->blockSignals( false );
113 void HYDROGUI_LandCoverMapOp::abortOperation()
117 HYDROGUI_Operation::abortOperation();
120 void HYDROGUI_LandCoverMapOp::commitOperation()
124 HYDROGUI_Operation::commitOperation();
127 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
129 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
130 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
131 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
132 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
133 this, SLOT( onCreatePreview( const QStringList& ) ) );
135 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
136 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
141 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
142 QString& theErrorMsg,
143 QStringList& theBrowseObjectsEntries )
145 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
149 // Check name of the created/edited object
150 QString anObjectName = aPanel->getObjectName().simplified();
151 if ( anObjectName.isEmpty() )
153 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
157 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
159 // check that there are no other objects with the same name in the document
160 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
161 if( !anObject.IsNull() )
163 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
168 Handle(HYDROData_PolylineXY) aPolyline;
169 Handle(HYDROData_Object) aFace;
171 TopTools_ListOfShape aFacesSelectedInViewer;
173 // Get polyline/face selected in combo-box
174 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
176 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
177 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
178 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
179 if ( aPolyline.IsNull() && aFace.IsNull() )
181 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
185 // Get face(s) selected in the 3d viewer
186 else if ( myOperationId == RemoveLandCoverId ||
187 myOperationId == MergeLandCoverId ||
188 myOperationId == ChangeLandCoverTypeId )
191 //Fill in aFacesSelectedInViewer list
194 // Get selected Strickler type
195 QString aSelectedStricklerType;
196 if ( myOperationId == CreateLandCoverMapId ||
197 myOperationId == AddLandCoverId ||
198 myOperationId == MergeLandCoverId )
200 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
201 if ( aSelectedStricklerType.isEmpty() )
203 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
208 // Create / find the new / edited land cover map object
209 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
210 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
212 // Set land cover map name
213 aLandCoverMapObj->SetName( anObjectName );
215 // Add land cover to new / edited land cover map
216 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
218 bool aLandCoverAdded = false;
219 if ( !aPolyline.IsNull() )
220 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
221 else if ( !aFace.IsNull() )
222 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
223 if ( !aLandCoverAdded )
225 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
230 // Remove land cover from edited land cover map
231 if ( myOperationId == RemoveLandCoverId )
233 bool aLandCoverRemoved = false;
234 if ( !aFacesSelectedInViewer.IsEmpty() )
236 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
237 if ( !aLandCoverRemoved )
239 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
245 // Split land cover(s) inside edited land cover map
246 if ( myOperationId == SplitLandCoverId )
248 bool aLandCoverSplitted = false;
249 if ( !aPolyline.IsNull() )
250 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
251 else if ( !aFace.IsNull() )
254 //Get the complete boundary of the object face as the splitting polyline
255 Handle(HYDROData_PolylineXY) aBoundaryPolyline;
256 aLandCoverSplitted = aLandCoverMapObj->Split( aBoundaryPolyline );
258 if ( !aLandCoverSplitted )
260 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
265 // Merge land covers inside edited land cover map
266 if ( myOperationId == MergeLandCoverId )
268 bool aLandCoverMerged = false;
269 if ( !aFacesSelectedInViewer.IsEmpty() )
271 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
272 if ( !aLandCoverMerged )
274 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
280 /*if ( myOperationId == CreateLandCoverMapId )
282 aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
283 aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
286 // Update land cover map object and close preview
287 aLandCoverMapObj->Update();
291 // Publish the newly created land cover map in the Object Browser
292 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
293 if ( myOperationId == CreateLandCoverMapId )
295 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
296 theBrowseObjectsEntries.append( anEntry );
299 // Update presentation of land cover object in the 3d viewer
300 module()->setIsToUpdate( aLandCoverMapObj );
301 module()->getOCCDisplayer()->SetToUpdateColorScale();
303 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
308 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
310 // If the edited land cover map was changed in the combo-box, update myEditedObject
311 if ( myOperationId != CreateLandCoverMapId )
313 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
314 if ( !myEditedObject.IsNull() )
316 // Show preview of the newly selected land cover map
317 QStringList aPolylineFaceNames;
318 onCreatePreview( aPolylineFaceNames );
323 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
326 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
330 QApplication::setOverrideCursor( Qt::WaitCursor );
332 HYDROData_SequenceOfObjects aZonePolylines;
333 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
334 for( ; anIt!=aLast; anIt++ )
336 QString aPolylineName = *anIt;
337 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
338 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
339 if ( !aPolyline.IsNull() )
340 aZonePolylines.Append( aPolyline );
343 TCollection_AsciiString anError;
344 TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );
346 LightApp_Application* anApp = module()->getApp();
347 if ( !getPreviewManager() )
348 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
349 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
350 OCCViewer_ViewManager* aViewManager = getPreviewManager();
351 if ( aViewManager && !myPreviewPrs )
353 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
355 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
356 if ( !aCtx.IsNull() )
357 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
361 if ( aViewManager && myPreviewPrs )
363 QColor aFillingColor = Qt::magenta;
364 QColor aBorderColor = Qt::transparent;
365 if ( !myEditedObject.IsNull() ) {
366 aFillingColor = myEditedObject->GetFillingColor();
367 aBorderColor = myEditedObject->GetBorderColor();
370 myPreviewPrs->setFillingColor( aFillingColor, false, false );
371 myPreviewPrs->setBorderColor( aBorderColor, false, false );
373 if( !aZoneShape.IsNull() )
374 myPreviewPrs->setShape( aZoneShape );
377 QApplication::restoreOverrideCursor();
381 void HYDROGUI_LandCoverMapOp::closePreview()