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>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
46 #include <TopoDS_Wire.hxx>
48 #include <QApplication>
50 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
51 const bool theIsEdit )
52 : HYDROGUI_Operation( theModule ),
53 myIsEdit( theIsEdit ),
56 setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
59 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
64 void HYDROGUI_ImmersibleZoneOp::startOperation()
66 HYDROGUI_Operation::startOperation();
68 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
72 aPanel->blockSignals( true );
76 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
78 QString aSelectedPolyline, aSelectedBathymetry;
79 QStringList aSelectedBathymetries;
83 myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
84 if( !myEditedObject.IsNull() )
86 anObjectName = myEditedObject->GetName();
88 Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
89 if ( !aRefPolyline.IsNull() )
90 aSelectedPolyline = aRefPolyline->GetName();
92 Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
93 if ( !aRefAltitude.IsNull() )
94 aSelectedBathymetry = aRefAltitude->GetName();
98 // collect information about existing closed polylines
99 QStringList aPolylines;
101 HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
102 for ( ; anIter.More(); anIter.Next() )
104 Handle(HYDROData_PolylineXY) aPolylineObj =
105 Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
106 if ( aPolylineObj.IsNull() )//TODO: || !aPolylineObj->IsClosed() )
109 QString aPolylineName = aPolylineObj->GetName();
110 if ( aPolylineName.isEmpty() )
113 aPolylines.append( aPolylineName );
116 // collect information about existing bathymetries
117 QStringList aBathymetries;
119 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
120 for ( ; anIter.More(); anIter.Next() )
122 Handle(HYDROData_Bathymetry) aBathymetryObj =
123 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
124 if ( aBathymetryObj.IsNull() )
127 QString aBathymetryName = aBathymetryObj->GetName();
128 if ( aBathymetryName.isEmpty() )
131 aBathymetries.append( aBathymetryName );
134 aPanel->setObjectName( anObjectName );
136 aPanel->setPolylineNames( aPolylines );
137 aPanel->setBathymetryNames( aBathymetries );
139 aPanel->blockSignals( false );
141 aPanel->setPolylineName( aSelectedPolyline );
142 aPanel->setBathymetryName( aSelectedBathymetry );
145 void HYDROGUI_ImmersibleZoneOp::abortOperation()
149 HYDROGUI_Operation::abortOperation();
152 void HYDROGUI_ImmersibleZoneOp::commitOperation()
156 HYDROGUI_Operation::commitOperation();
159 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
161 HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
162 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
163 this, SLOT( onCreatePreview( const QString& ) ) );
167 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
168 QString& theErrorMsg )
170 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
174 QString anObjectName = aPanel->getObjectName().simplified();
175 if ( anObjectName.isEmpty() )
177 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
181 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
183 // check that there are no other objects with the same name in the document
184 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
185 if( !anObject.IsNull() )
187 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
192 Handle(HYDROData_PolylineXY) aZonePolyline;
193 Handle(HYDROData_Bathymetry) aZoneBathymetry;
195 QString aPolylineName = aPanel->getPolylineName();
196 if ( !aPolylineName.isEmpty() )
198 aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
199 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
202 QString aBathymetryName = aPanel->getBathymetryName();
203 if ( !aBathymetryName.isEmpty() )
205 aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
206 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
210 if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
212 theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
216 Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
217 Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
219 aZoneObj->SetName( anObjectName );
223 aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
224 aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
227 aZoneObj->SetPolyline( aZonePolyline );
228 aZoneObj->SetAltitudeObject( aZoneBathymetry );
234 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
236 module()->setIsToUpdate( aZoneObj );
238 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
243 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
245 HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
249 QApplication::setOverrideCursor( Qt::WaitCursor );
250 TopoDS_Shape aZoneShape;
252 Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
253 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
254 if ( !aPolyline.IsNull() )
256 aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
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 QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
279 QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
280 if ( !myEditedObject.IsNull() ) {
281 aFillingColor = myEditedObject->GetFillingColor();
282 aBorderColor = myEditedObject->GetBorderColor();
285 myPreviewPrs->setFillingColor( aFillingColor, false, false );
286 myPreviewPrs->setBorderColor( aBorderColor, false, false );
288 if( !aZoneShape.IsNull() )
289 aFace = TopoDS::Face( aZoneShape );
290 myPreviewPrs->setFace( aFace );
293 QApplication::restoreOverrideCursor();
296 void HYDROGUI_ImmersibleZoneOp::closePreview()