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 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
35 : HYDROGUI_Operation( theModule ),
36 myOperationId( theOperationId ),
39 switch( myOperationId )
41 case CreateLandCoverMapId:
42 setName( tr( "CREATE_LAND_COVER_MAP" ) );
45 setName( tr( "ADD_LAND_COVER" ) );
47 case RemoveLandCoverId:
48 setName( tr( "REMOVE_LAND_COVER" ) );
50 case SplitLandCoverId:
51 setName( tr( "SPLIT_LAND_COVER" ) );
53 case MergeLandCoverId:
54 setName( tr( "MERGE_LAND_COVER" ) );
59 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
64 void HYDROGUI_LandCoverMapOp::startOperation()
66 HYDROGUI_Operation::startOperation();
68 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
72 aPanel->blockSignals( true );
76 // Set name of the created/edited land cover map object
77 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
78 if ( myOperationId != CreateLandCoverMapId )
80 if ( isApplyAndClose() )
81 myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if ( !myEditedObject.IsNull() )
84 anObjectName = myEditedObject->GetName();
86 // Construct a list of names of all land cover map objects defined within the data model
87 QStringList aLandCoverMapNames;
88 HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
89 for( ; anIterator.More(); anIterator.Next() )
91 Handle(HYDROData_LandCoverMap) aLandCoverObj =
92 Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );
93 if ( !aLandCoverObj.IsNull() )
94 aLandCoverMapNames.append( aLandCoverObj->GetName() );
97 //aLandCoverMapNames.sort();
98 aPanel->setObjectNames( aLandCoverMapNames );
101 aPanel->setObjectName( anObjectName );
103 aPanel->blockSignals( false );
106 void HYDROGUI_LandCoverMapOp::abortOperation()
110 HYDROGUI_Operation::abortOperation();
113 void HYDROGUI_LandCoverMapOp::commitOperation()
117 HYDROGUI_Operation::commitOperation();
120 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
122 HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
124 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
125 this, SLOT( onCreatePreview( const QStringList& ) ) );
126 connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
127 connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
132 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
133 QString& theErrorMsg,
134 QStringList& theBrowseObjectsEntries )
136 HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
140 // Check name of the created/edited object
141 QString anObjectName = aPanel->getObjectName().simplified();
142 if ( anObjectName.isEmpty() )
144 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
148 if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
150 // check that there are no other objects with the same name in the document
151 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
152 if( !anObject.IsNull() )
154 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
159 // Get selected polyline/face
160 Handle(HYDROData_PolylineXY) aPolyline;
161 Handle(HYDROData_Object) aFace;
162 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
164 Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
165 aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
166 aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
167 if ( aPolyline.IsNull() && aFace.IsNull() )
169 theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
174 // Get selected Strickler type
175 QString aSelectedStricklerType;
176 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == MergeLandCoverId )
178 aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
179 if ( aSelectedStricklerType.isEmpty() )
181 theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
186 // Create / find the new / edited land cover map object
187 Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
188 Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
190 // Set land cover map name
191 aLandCoverMapObj->SetName( anObjectName );
193 if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
195 bool aLandCoverAdded = false;
196 if ( !aPolyline.IsNull() )
197 aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
198 else if ( !aFace.IsNull() )
199 aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
200 if ( !aLandCoverAdded )
202 theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
207 /*TCollection_AsciiString anError;
208 if ( HYDROData_LandCover::buildShape( aZonePolylines, anError ).IsNull() )
210 if ( !anError.IsEmpty() ) {
211 theErrorMsg = HYDROGUI_Tool::ToQString( anError );
213 theErrorMsg = tr( "LAND_COVER_OBJECT_CANNOT_BE_CREATED" );
218 /*if ( myOperationId == CreateLandCoverMapId )
220 aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
221 aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
224 //aLandCoverMapObj->SetPolylines( aZonePolylines );
225 //aLandCoverMapObj->SetStricklerType( aSelectedStricklerType );
226 aLandCoverMapObj->Update();
230 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
231 if ( myOperationId == CreateLandCoverMapId )
233 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
234 theBrowseObjectsEntries.append( anEntry );
237 module()->setIsToUpdate( aLandCoverMapObj );
238 module()->getOCCDisplayer()->SetToUpdateColorScale();
240 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
245 void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineNames )
248 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
252 QApplication::setOverrideCursor( Qt::WaitCursor );
254 HYDROData_SequenceOfObjects aZonePolylines;
255 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
256 for( ; anIt!=aLast; anIt++ )
258 QString aPolylineName = *anIt;
259 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
260 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
261 if ( !aPolyline.IsNull() )
262 aZonePolylines.Append( aPolyline );
265 TCollection_AsciiString anError;
266 TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );
268 LightApp_Application* anApp = module()->getApp();
269 if ( !getPreviewManager() )
270 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
271 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
272 OCCViewer_ViewManager* aViewManager = getPreviewManager();
273 if ( aViewManager && !myPreviewPrs )
275 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
277 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
278 if ( !aCtx.IsNull() )
279 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
283 if ( aViewManager && myPreviewPrs )
285 QColor aFillingColor = Qt::magenta;
286 QColor aBorderColor = Qt::transparent;
287 if ( !myEditedObject.IsNull() ) {
288 aFillingColor = myEditedObject->GetFillingColor();
289 aBorderColor = myEditedObject->GetBorderColor();
292 myPreviewPrs->setFillingColor( aFillingColor, false, false );
293 myPreviewPrs->setBorderColor( aBorderColor, false, false );
295 if( !aZoneShape.IsNull() )
296 myPreviewPrs->setShape( aZoneShape );
299 QApplication::restoreOverrideCursor();
303 void HYDROGUI_LandCoverMapOp::closePreview()