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 HYDROData_SequenceOfObjects aRefPolylines = myEditedObject->GetPolylines();
84 for ( int i = aRefPolylines.Lower(); i <= aRefPolylines.Upper(); i++ ) {
85 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
86 if ( !aPolyline.IsNull() ) {
87 aSelectedPolylines.append( aPolyline->GetName() );
92 aSelectedStricklerType = myEditedObject->GetStricklerType();
95 aPanel->setObjectName( anObjectName );
96 aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
97 // Construct a list of unique names of all Strickler types defined within the data model
98 QStringList aStricklerTypes;
99 HYDROData_Iterator anIterator( doc(), KIND_STRICKLER_TABLE );
100 for( ; anIterator.More(); anIterator.Next() )
102 Handle(HYDROData_StricklerTable) aStricklerTableObj =
103 Handle(HYDROData_StricklerTable)::DownCast( anIterator.Current() );
104 if ( !aStricklerTableObj.IsNull() )
106 // TODO: implement GetTypes method in data model
107 //QStringList aTypes = aStricklerTableObj->GetTypes();
108 // The code below is a sample of data filling list of Strickler types
110 aTypes.append("Zones de champs, prairies, sans cultures");
111 aTypes.append("Canaux naturels");
112 aTypes.append("Zones à faible urbanization (bourg)");
113 QStringList::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
114 for( ; anIt!=aLast; anIt++ )
116 QString aType = *anIt;
117 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
118 aStricklerTypes.append( aType );
122 aStricklerTypes.sort();
123 aPanel->setAdditionalParams( aStricklerTypes );
125 aPanel->blockSignals( false );
127 aPanel->setSelectedPolylineNames( aSelectedPolylines );
128 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
131 void HYDROGUI_LandCoverOp::abortOperation()
135 HYDROGUI_Operation::abortOperation();
138 void HYDROGUI_LandCoverOp::commitOperation()
142 HYDROGUI_Operation::commitOperation();
145 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
147 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
148 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
149 this, SLOT( onCreatePreview( const QStringList& ) ) );
153 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
154 QString& theErrorMsg,
155 QStringList& theBrowseObjectsEntries )
157 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
161 QString anObjectName = aPanel->getObjectName().simplified();
162 if ( anObjectName.isEmpty() )
164 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
168 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
170 // check that there are no other objects with the same name in the document
171 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
172 if( !anObject.IsNull() )
174 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
179 HYDROData_SequenceOfObjects aZonePolylines;
180 QString aStricklerType;
182 QStringList aSelectedPolylineNames = aPanel->getSelectedPolylineNames();
183 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
184 for( ; anIt!=aLast; anIt++ )
186 QString aPolylineName = *anIt;
187 if ( !aPolylineName.isEmpty() )
189 aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
190 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
194 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
196 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
198 if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
200 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
205 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
206 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
208 aZoneObj->SetName( anObjectName );
212 aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
213 aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
216 aZoneObj->SetPolylines( aZonePolylines );
217 aZoneObj->SetStricklerType( aSelectedStricklerType );
224 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
225 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
226 theBrowseObjectsEntries.append( anEntry );
229 module()->setIsToUpdate( aZoneObj );
231 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
236 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
238 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
242 QApplication::setOverrideCursor( Qt::WaitCursor );
244 QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
245 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
246 for( ; anIt!=aLast; anIt++ )
248 QString aPolylineName = *anIt;
249 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
250 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
251 if ( !aPolyline.IsNull() )
252 aZonePolylines.append( aPolyline );
255 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
256 TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
257 if( aZoneShape.IsNull() )
258 printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
261 LightApp_Application* anApp = module()->getApp();
262 if ( !getPreviewManager() )
263 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
264 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
265 OCCViewer_ViewManager* aViewManager = getPreviewManager();
266 if ( aViewManager && !myPreviewPrs )
268 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
270 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
271 if ( !aCtx.IsNull() )
272 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
276 if ( aViewManager && myPreviewPrs )
278 // TODO: Temporary commented until GetFillingColor, DefaultFillingColor,
279 // GetBorderColor and DefaultBorderColor data model methods will be implemented
281 QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
282 QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
283 if ( !myEditedObject.IsNull() ) {
284 aFillingColor = myEditedObject->GetFillingColor();
285 aBorderColor = myEditedObject->GetBorderColor();
288 myPreviewPrs->setFillingColor( aFillingColor, false, false );
289 myPreviewPrs->setBorderColor( aBorderColor, false, false );
293 if( !aZoneShape.IsNull() )
294 aFace = TopoDS::Face( aZoneShape );
295 myPreviewPrs->setFace( aFace, true, true, "" );
298 QApplication::restoreOverrideCursor();
301 void HYDROGUI_LandCoverOp::closePreview()