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" ) );
63 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
68 void HYDROGUI_LandCoverMapOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
76 aPanel->blockSignals( true );
80 // Set name of the created/edited land cover map object
81 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
82 if ( myOperationId != CreateLandCoverMapId )
84 if ( isApplyAndClose() )
85 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
86 if ( !myEditedObject.IsNull() )
88 anObjectName = myEditedObject->GetName();
90 // Construct a list of names of all land cover map objects defined within the data model
91 QStringList aLandCoverMapNames;
92 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
93 for( ; anIterator.More(); anIterator.Next() )
95 Handle(HYDROData_LandCoverMap) aLandCoverObj =
96 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
97 if ( !aLandCoverObj.IsNull() )
98 aLandCoverMapNames.append( aLandCoverObj->GetName() );
101 //aLandCoverMapNames.sort();
102 aPanel->setObjectNames( aLandCoverMapNames );
105 aPanel->setObjectName( anObjectName );
107 aPanel->blockSignals( false );
110 void HYDROGUI_LandCoverMapOp::abortOperation()
114 HYDROGUI_Operation::abortOperation();
117 void HYDROGUI_LandCoverMapOp::commitOperation()
121 HYDROGUI_Operation::commitOperation();
124 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
126 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
127 connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
128 this, SLOT( onLandCoverMapChanged( const QString& ) ) );
129 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
130 this, SLOT( onCreatePreview( const QStringList& ) ) );
132 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
133 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
138 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
139 QString& theErrorMsg,
140 QStringList& theBrowseObjectsEntries )
142 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
146 // Check name of the created/edited object
147 QString anObjectName = aPanel->getObjectName().simplified();
148 if ( anObjectName.isEmpty() )
150 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
154 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
156 // check that there are no other objects with the same name in the document
157 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
158 if( !anObject.IsNull() )
160 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
165 Handle(HYDROData_PolylineXY) aPolyline;
166 Handle(HYDROData_Object) aFace;
168 TopTools_ListOfShape aFacesSelectedInViewer;
170 // Get polyline/face selected in combo-box
171 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
173 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
174 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
175 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
176 if ( aPolyline.IsNull() && aFace.IsNull() )
178 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
182 // Get face(s) selected in the 3d viewer
183 else if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId )
186 //Fill in aFacesSelectedInViewer list
189 // Get selected Strickler type
190 QString aSelectedStricklerType;
191 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == MergeLandCoverId )
193 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
194 if ( aSelectedStricklerType.isEmpty() )
196 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
201 // Create / find the new / edited land cover map object
202 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
203 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
205 // Set land cover map name
206 aLandCoverMapObj->SetName( anObjectName );
208 // Add land cover to new / edited land cover map
209 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
211 bool aLandCoverAdded = false;
212 if ( !aPolyline.IsNull() )
213 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
214 else if ( !aFace.IsNull() )
215 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
216 if ( !aLandCoverAdded )
218 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
223 // Remove land cover from edited land cover map
224 if ( myOperationId == RemoveLandCoverId )
226 bool aLandCoverRemoved = false;
227 if ( !aFacesSelectedInViewer.IsEmpty() )
229 aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
230 if ( !aLandCoverRemoved )
232 theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
238 // Split land cover(s) inside edited land cover map
239 if ( myOperationId == SplitLandCoverId )
241 bool aLandCoverSplitted = false;
242 if ( !aPolyline.IsNull() )
243 aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
244 else if ( !aFace.IsNull() )
247 //Get the complete boundary of the object face as the splitting polyline
248 Handle(HYDROData_PolylineXY) aBoundaryPolyline;
249 aLandCoverSplitted = aLandCoverMapObj->Split( aBoundaryPolyline );
251 if ( !aLandCoverSplitted )
253 theErrorMsg = tr( "LAND_COVER_NOT_SPLITTED" );
258 // Merge land covers inside edited land cover map
259 if ( myOperationId == MergeLandCoverId )
261 bool aLandCoverMerged = false;
262 if ( !aFacesSelectedInViewer.IsEmpty() )
264 aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
265 if ( !aLandCoverMerged )
267 theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
273 /*if ( myOperationId == CreateLandCoverMapId )
275 aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
276 aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
279 // Update land cover map object and close preview
280 aLandCoverMapObj->Update();
284 // Publish the newly created land cover map in the Object Browser
285 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
286 if ( myOperationId == CreateLandCoverMapId )
288 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
289 theBrowseObjectsEntries.append( anEntry );
292 // Update presentation of land cover object in the 3d viewer
293 module()->setIsToUpdate( aLandCoverMapObj );
294 module()->getOCCDisplayer()->SetToUpdateColorScale();
296 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
301 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
303 // If the edited land cover map was changed in the combo-box, update myEditedObject
304 if ( myOperationId != CreateLandCoverMapId )
306 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
307 if ( !myEditedObject.IsNull() )
309 // Show preview of the newly selected land cover map
310 QStringList aPolylineFaceNames;
311 onCreatePreview( aPolylineFaceNames );
316 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
319 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
323 QApplication::setOverrideCursor( Qt::WaitCursor );
325 HYDROData_SequenceOfObjects aZonePolylines;
326 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
327 for( ; anIt!=aLast; anIt++ )
329 QString aPolylineName = *anIt;
330 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
331 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
332 if ( !aPolyline.IsNull() )
333 aZonePolylines.Append( aPolyline );
336 TCollection_AsciiString anError;
337 TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );
339 LightApp_Application* anApp = module()->getApp();
340 if ( !getPreviewManager() )
341 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
342 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
343 OCCViewer_ViewManager* aViewManager = getPreviewManager();
344 if ( aViewManager && !myPreviewPrs )
346 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
348 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
349 if ( !aCtx.IsNull() )
350 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
354 if ( aViewManager && myPreviewPrs )
356 QColor aFillingColor = Qt::magenta;
357 QColor aBorderColor = Qt::transparent;
358 if ( !myEditedObject.IsNull() ) {
359 aFillingColor = myEditedObject->GetFillingColor();
360 aBorderColor = myEditedObject->GetBorderColor();
363 myPreviewPrs->setFillingColor( aFillingColor, false, false );
364 myPreviewPrs->setBorderColor( aBorderColor, false, false );
366 if( !aZoneShape.IsNull() )
367 myPreviewPrs->setShape( aZoneShape );
370 QApplication::restoreOverrideCursor();
374 void HYDROGUI_LandCoverMapOp::closePreview()