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 // Get Strickler table data from the data model
107 TColStd_SequenceOfExtendedString aTypes = aStricklerTableObj->GetTypes();
108 for ( int i = 1; i <= aTypes.Length(); i++ )
110 QString aType = HYDROGUI_Tool::ToQString( aTypes.Value( i ) );
111 if ( !aType.isEmpty() && !aStricklerTypes.contains( aType ))
112 aStricklerTypes.append( aType );
116 aStricklerTypes.sort();
117 aPanel->setAdditionalParams( aStricklerTypes );
119 aPanel->blockSignals( false );
121 aPanel->setSelectedPolylineNames( aSelectedPolylines );
122 aPanel->setSelectedAdditionalParamName( aSelectedStricklerType );
125 void HYDROGUI_LandCoverOp::abortOperation()
129 HYDROGUI_Operation::abortOperation();
132 void HYDROGUI_LandCoverOp::commitOperation()
136 HYDROGUI_Operation::commitOperation();
139 HYDROGUI_InputPanel* HYDROGUI_LandCoverOp::createInputPanel() const
141 HYDROGUI_LandCoverDlg* aPanel = new HYDROGUI_LandCoverDlg( module(), getName() );
142 connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
143 this, SLOT( onCreatePreview( const QStringList& ) ) );
147 bool HYDROGUI_LandCoverOp::processApply( int& theUpdateFlags,
148 QString& theErrorMsg,
149 QStringList& theBrowseObjectsEntries )
151 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
155 QString anObjectName = aPanel->getObjectName().simplified();
156 if ( anObjectName.isEmpty() )
158 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
162 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
164 // check that there are no other objects with the same name in the document
165 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
166 if( !anObject.IsNull() )
168 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
173 HYDROData_SequenceOfObjects aZonePolylines;
174 QString aStricklerType;
176 QStringList aSelectedPolylineNames = aPanel->getSelectedPolylineNames();
177 QStringList::const_iterator anIt = aSelectedPolylineNames.begin(), aLast = aSelectedPolylineNames.end();
178 for( ; anIt!=aLast; anIt++ )
180 QString aPolylineName = *anIt;
181 if ( !aPolylineName.isEmpty() )
183 aZonePolylines.Append( Handle(HYDROData_PolylineXY)::DownCast(
184 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) ) );
188 QString aSelectedStricklerType = aPanel->getSelectedAdditionalParamName();
190 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
192 if ( HYDROData_LandCover::generateTopShape( aZonePolylines ).IsNull() )
194 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
199 Handle(HYDROData_LandCover) aZoneObj = myIsEdit ? myEditedObject :
200 Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
202 aZoneObj->SetName( anObjectName );
206 aZoneObj->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
207 aZoneObj->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
210 aZoneObj->SetPolylines( aZonePolylines );
211 aZoneObj->SetStricklerType( aSelectedStricklerType );
218 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
219 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
220 theBrowseObjectsEntries.append( anEntry );
223 module()->setIsToUpdate( aZoneObj );
225 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
230 void HYDROGUI_LandCoverOp::onCreatePreview( const QStringList& thePolylineNames )
232 HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
236 QApplication::setOverrideCursor( Qt::WaitCursor );
238 QList<Handle(HYDROData_PolylineXY)> aZonePolylines;
239 QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
240 for( ; anIt!=aLast; anIt++ )
242 QString aPolylineName = *anIt;
243 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
244 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
245 if ( !aPolyline.IsNull() )
246 aZonePolylines.append( aPolyline );
249 // TODO: Generate TopoDS_Shape based on the set of polylines, implement generateTopShape data model method
250 TopoDS_Shape aZoneShape;/* = HYDROData_LandCover::generateTopShape( aZonePolylines );
251 if( aZoneShape.IsNull() )
252 printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
255 LightApp_Application* anApp = module()->getApp();
256 if ( !getPreviewManager() )
257 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
258 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
259 OCCViewer_ViewManager* aViewManager = getPreviewManager();
260 if ( aViewManager && !myPreviewPrs )
262 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
264 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
265 if ( !aCtx.IsNull() )
266 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
270 if ( aViewManager && myPreviewPrs )
272 QColor aFillingColor = HYDROData_LandCover::DefaultFillingColor();
273 QColor aBorderColor = HYDROData_LandCover::DefaultBorderColor();
274 if ( !myEditedObject.IsNull() ) {
275 aFillingColor = myEditedObject->GetFillingColor();
276 aBorderColor = myEditedObject->GetBorderColor();
279 myPreviewPrs->setFillingColor( aFillingColor, false, false );
280 myPreviewPrs->setBorderColor( aBorderColor, false, false );
283 if( !aZoneShape.IsNull() )
284 aFace = TopoDS::Face( aZoneShape );
285 myPreviewPrs->setFace( aFace, true, true, "" );
288 QApplication::restoreOverrideCursor();
291 void HYDROGUI_LandCoverOp::closePreview()