1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImmersibleZoneOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImmersibleZoneDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_PolylineXY.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
43 #include <TopoDS_Wire.hxx>
45 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
46 const bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48 myIsEdit( theIsEdit ),
52 setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
55 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
60 void HYDROGUI_ImmersibleZoneOp::startOperation()
62 HYDROGUI_Operation::startOperation();
64 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
68 aPanel->blockSignals( true );
72 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
74 QString aSelectedPolyline, aSelectedBathymetry;
75 QStringList aSelectedBathymetries;
79 myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80 if( !myEditedObject.IsNull() )
82 anObjectName = myEditedObject->GetName();
84 Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
85 if ( !aRefPolyline.IsNull() )
86 aSelectedPolyline = aRefPolyline->GetName();
88 Handle(HYDROData_Bathymetry) aRefBathymetry = myEditedObject->GetBathymetry();
89 if ( !aRefBathymetry.IsNull() )
90 aSelectedBathymetry = aRefBathymetry->GetName();
94 // collect information about existing closed polylines
95 QStringList aPolylines;
97 HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
98 for ( ; anIter.More(); anIter.Next() )
100 Handle(HYDROData_PolylineXY) aPolylineObj =
101 Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
102 if ( aPolylineObj.IsNull() )//TODO: || !aPolylineObj->IsClosed() )
105 QString aPolylineName = aPolylineObj->GetName();
106 if ( aPolylineName.isEmpty() )
109 aPolylines.append( aPolylineName );
112 // collect information about existing bathymetries
113 QStringList aBathymetries;
115 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
116 for ( ; anIter.More(); anIter.Next() )
118 Handle(HYDROData_Bathymetry) aBathymetryObj =
119 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
120 if ( aBathymetryObj.IsNull() )
123 QString aBathymetryName = aBathymetryObj->GetName();
124 if ( aBathymetryName.isEmpty() )
127 aBathymetries.append( aBathymetryName );
130 aPanel->setObjectName( anObjectName );
132 aPanel->setPolylineNames( aPolylines );
133 aPanel->setBathymetryNames( aBathymetries );
135 aPanel->blockSignals( false );
137 aPanel->setPolylineName( aSelectedPolyline );
138 aPanel->setBathymetryName( aSelectedBathymetry );
141 void HYDROGUI_ImmersibleZoneOp::abortOperation()
145 HYDROGUI_Operation::abortOperation();
148 void HYDROGUI_ImmersibleZoneOp::commitOperation()
152 HYDROGUI_Operation::commitOperation();
155 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
157 HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
158 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
159 this, SLOT( onCreatePreview( const QString& ) ) );
163 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
164 QString& theErrorMsg )
166 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
170 QString anObjectName = aPanel->getObjectName().simplified();
171 if ( anObjectName.isEmpty() )
173 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
177 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
179 // check that there are no other objects with the same name in the document
180 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
181 if( !anObject.IsNull() )
183 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
188 Handle(HYDROData_PolylineXY) aZonePolyline;
189 Handle(HYDROData_Bathymetry) aZoneBathymetry;
191 QString aPolylineName = aPanel->getPolylineName();
192 if ( !aPolylineName.isEmpty() )
194 aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
195 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
198 QString aBathymetryName = aPanel->getBathymetryName();
199 if ( !aBathymetryName.isEmpty() )
201 aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
202 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
206 if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
208 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
212 Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
213 Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
215 aZoneObj->SetName( anObjectName );
219 aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
220 aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
223 aZoneObj->SetPolyline( aZonePolyline );
224 aZoneObj->SetBathymetry( aZoneBathymetry );
230 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
232 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
237 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
239 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
243 TopoDS_Shape aZoneShape;
245 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
246 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
247 if ( !aPolyline.IsNull() )
249 aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
252 LightApp_Application* anApp = module()->getApp();
253 if ( !myViewManager )
254 myViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
255 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
257 if ( myViewManager && !myPreviewPrs )
259 if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
261 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
262 if ( !aCtx.IsNull() )
263 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
267 if ( !myViewManager || !myPreviewPrs )
270 QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
271 QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
272 if ( !myEditedObject.IsNull() ) {
273 aFillingColor = myEditedObject->GetFillingColor();
274 aBorderColor = myEditedObject->GetBorderColor();
277 myPreviewPrs->setFillingColor( aFillingColor, false, false );
278 myPreviewPrs->setBorderColor( aBorderColor, false, false );
280 if( !aZoneShape.IsNull() )
281 aFace = TopoDS::Face( aZoneShape );
282 myPreviewPrs->setFace( aFace );
285 void HYDROGUI_ImmersibleZoneOp::closePreview()