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_LandCoverOp.h"
21 #include "HYDROGUI_LandCoverDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_DataObject.h"
28 #include <HYDROData_PolylineXY.h>
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_StricklerTable.h>
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewModel.h>
36 #include <LightApp_Application.h>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Wire.hxx>
42 #include <QApplication>
44 HYDROGUI_LandCoverOp::HYDROGUI_LandCoverOp( HYDROGUI_Module* theModule,
45 const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47 myIsEdit( theIsEdit ),
50 setName( theIsEdit ? tr( "EDIT_LAND_COVER" ) : tr( "CREATE_LAND_COVER" ) );
53 HYDROGUI_LandCoverOp::~HYDROGUI_LandCoverOp()
58 void HYDROGUI_LandCoverOp::startOperation()
60 HYDROGUI_Operation::startOperation();
62 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
66 aPanel->blockSignals( true );
70 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_NAME" ) );
72 QStringList aSelectedPolylines;
73 QString aSelectedStricklerType;
77 if ( isApplyAndClose() )
78 myEditedObject = Handle(HYDROData_LandCover)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79 if ( !myEditedObject.IsNull() )
81 anObjectName = myEditedObject->GetName();
83 // TODO: Temporary commented until getPolylinesCount, GetPolyline and GetStricklerType data model methods will be implemented
85 int nNoPolylines = myEditedObject->getPolylinesCount();
86 for (int i=0; i<nNoPolylines; i++)
88 Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline(i);
89 if ( !aRefPolyline.IsNull() )
90 aSelectedPolylines.append( aRefPolyline->GetName() );
93 aSelectedStricklerType = myEditedObject->GetStricklerType();
96 // The code below is a sample of data filling Land cover object
97 aSelectedPolylines.append( "Lake_1" );
98 aSelectedPolylines.append( "Polyline_1" );
99 aSelectedStricklerType = "Canaux naturels";
103 aPanel->setObjectName( anObjectName );
104 aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
105 // Construct a list of unique names of all Strickler types defined within the data model
106 QStringList aStricklerTypes;
107 HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
108 for( ; anIterator.More(); anIterator.Next() )
110 Handle(HYDROData_StricklerTable) aStricklerTableObj =
111 Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );
112 if ( !aStricklerTableObj.IsNull() )
114 // TODO: implement GetTypes method in data model
115 //QStringList aTypes = aStricklerTableObj->GetTypes();
116 // The code below is a sample of data filling list of Strickler types
118 aTypes.append("Zones de champs, prairies, sans cultures");
119 aTypes.append("Canaux naturels");
120 aTypes.append("Zones à faible urbanization (bourg)");
121 QStringList::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
122 for( ; anIt!=aLast; anIt++ )
124 QString aType = *anIt;
125 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
126 aStricklerTypes.append( aType );
130 aStricklerTypes.sort();
131 aPanel->setAdditionalParams( aStricklerTypes );
133 aPanel->blockSignals( false );
135 aPanel->setSelectedPolylineNames( aSelectedPolylines );
136 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
139 void HYDROGUI_LandCoverOp::abortOperation()
143 HYDROGUI_Operation::abortOperation();
146 void HYDROGUI_LandCoverOp::commitOperation()
150 HYDROGUI_Operation::commitOperation();
153 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
155 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
156 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
157 this, SLOT( onCreatePreview( const QStringList& ) ) );
161 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
162 QString& theErrorMsg,
163 QStringList& theBrowseObjectsEntries )
165 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
169 QString anObjectName = aPanel->getObjectName().simplified();
170 if ( anObjectName.isEmpty() )
172 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
176 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
178 // check that there are no other objects with the same name in the document
179 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
180 if( !anObject.IsNull() )
182 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
187 QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
188 QString aStricklerType;
190 QStringList aSelectedPolylineNames = aPanel->getSelectedPolylineNames();
191 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
192 for( ; anIt!=aLast; anIt++ )
194 QString aPolylineName = *anIt;
195 if ( !aPolylineName.isEmpty() )
197 aZonePolylines.append( Handle(HYDROData_PolylineXY)::DownCast(
198 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
202 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
204 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
206 if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
208 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
213 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
214 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
216 aZoneObj->SetName( anObjectName );
218 // TODO: Temporary commented until SetFillingColor, DefaultFillingColor,
219 // SetBorderColor and DefaultBorderColor data model methods will be implemented
223 aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
224 aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
228 // TODO: Temporary commented until SetPolylines and SetStricklerType data model methods will be implemented
229 //aZoneObj->SetPolylines( aZonePolylines );
230 //aZoneObj->SetStricklerType( aSelectedStricklerType );
237 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
238 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
239 theBrowseObjectsEntries.append( anEntry );
242 module()->setIsToUpdate( aZoneObj );
244 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
249 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
251 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
255 QApplication::setOverrideCursor( Qt::WaitCursor );
257 QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
258 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
259 for( ; anIt!=aLast; anIt++ )
261 QString aPolylineName = *anIt;
262 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
263 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
264 if ( !aPolyline.IsNull() )
265 aZonePolylines.append( aPolyline );
268 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
269 TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
270 if( aZoneShape.IsNull() )
271 printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
274 LightApp_Application* anApp = module()->getApp();
275 if ( !getPreviewManager() )
276 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
277 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
278 OCCViewer_ViewManager* aViewManager = getPreviewManager();
279 if ( aViewManager && !myPreviewPrs )
281 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
283 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
284 if ( !aCtx.IsNull() )
285 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
289 if ( aViewManager && myPreviewPrs )
291 // TODO: Temporary commented until GetFillingColor, DefaultFillingColor,
292 // GetBorderColor and DefaultBorderColor data model methods will be implemented
294 QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
295 QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
296 if ( !myEditedObject.IsNull() ) {
297 aFillingColor = myEditedObject->GetFillingColor();
298 aBorderColor = myEditedObject->GetBorderColor();
301 myPreviewPrs->setFillingColor( aFillingColor, false, false );
302 myPreviewPrs->setBorderColor( aBorderColor, false, false );
306 if( !aZoneShape.IsNull() )
307 aFace = TopoDS::Face( aZoneShape );
308 myPreviewPrs->setFace( aFace, true, true, "" );
311 QApplication::restoreOverrideCursor();
314 void HYDROGUI_LandCoverOp::closePreview()