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_ZoneOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ZoneDlg.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_Polyline.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
42 #include <TopoDS_Wire.hxx>
44 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
45 const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47 myIsEdit( theIsEdit ),
48 myActiveViewManager( 0 ),
49 myPreviewViewManager( 0 ),
52 setName( theIsEdit ? tr( "EDIT_ZONE" ) : tr( "CREATE_ZONE" ) );
55 HYDROGUI_ZoneOp::~HYDROGUI_ZoneOp()
60 void HYDROGUI_ZoneOp::startOperation()
62 HYDROGUI_Operation::startOperation();
64 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
68 aPanel->blockSignals( true );
72 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
74 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
75 QColor aBorderColor( HYDROData_Zone::DefaultBorderColor() );
76 QString aSelectedPolyline;
77 QStringList aSelectedBathymetries;
81 myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if( !myEditedObject.IsNull() )
84 anObjectName = myEditedObject->GetName();
86 aFillingColor = myEditedObject->GetFillingColor();
87 aBorderColor = myEditedObject->GetBorderColor();
89 Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
90 if ( !aRefPolyline.IsNull() )
91 aSelectedPolyline = aRefPolyline->GetName();
93 HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
94 HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
95 for ( ; anIter.More(); anIter.Next() )
97 Handle(HYDROData_Bathymetry) aRefBathymetry =
98 Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
99 if ( aRefBathymetry.IsNull() )
102 QString aRefBathymetryName = aRefBathymetry->GetName();
103 if ( aRefBathymetryName.isEmpty() )
106 aSelectedBathymetries.append( aRefBathymetryName );
111 // collect information about existing closed polylines
112 QStringList aPolylines;
114 HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
115 for ( ; anIter.More(); anIter.Next() )
117 Handle(HYDROData_Polyline) aPolylineObj =
118 Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
119 if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
122 QString aPolylineName = aPolylineObj->GetName();
123 if ( aPolylineName.isEmpty() )
126 aPolylines.append( aPolylineName );
129 // collect information about existing bathymetries
130 QStringList aBathymetries;
132 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
133 for ( ; anIter.More(); anIter.Next() )
135 Handle(HYDROData_Bathymetry) aBathymetryObj =
136 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
137 if ( aBathymetryObj.IsNull() )
140 QString aBathymetryName = aBathymetryObj->GetName();
141 if ( aBathymetryName.isEmpty() )
144 aBathymetries.append( aBathymetryName );
147 aPanel->setObjectName( anObjectName );
149 aPanel->setFillingColor( aFillingColor );
150 aPanel->setBorderColor( aBorderColor );
152 aPanel->setPolylineNames( aPolylines );
154 aPanel->setBathymetries( aBathymetries );
155 aPanel->setSelectedBathymetries( aSelectedBathymetries );
157 aPanel->blockSignals( false );
159 aPanel->setPolylineName( aSelectedPolyline );
163 void HYDROGUI_ZoneOp::abortOperation()
167 HYDROGUI_Operation::abortOperation();
170 void HYDROGUI_ZoneOp::commitOperation()
174 HYDROGUI_Operation::commitOperation();
177 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
179 HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
180 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
181 this, SLOT( onCreatePreview( const QString& ) ) );
185 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
186 QString& theErrorMsg )
188 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
192 QString anObjectName = aPanel->getObjectName().simplified();
193 if ( anObjectName.isEmpty() )
195 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
199 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
201 // check that there are no other objects with the same name in the document
202 Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
203 if( !anObject.IsNull() )
205 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
210 Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
211 Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
212 if ( aZoneObj.IsNull() )
215 aZoneObj->SetName( anObjectName );
217 aZoneObj->SetFillingColor( aPanel->getFillingColor() );
218 aZoneObj->SetBorderColor( aPanel->getBorderColor() );
220 Handle(HYDROData_Polyline) aZonePolyline;
222 QString aPolylineName = aPanel->getPolylineName();
223 if ( !aPolylineName.isEmpty() )
225 aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
226 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
229 aZoneObj->SetPolyline( aZonePolyline );
232 aZoneObj->RemoveBathymetries();
234 QStringList aBathymetries = aPanel->getSelectedBathymetries();
235 for ( int i = 0; i < aBathymetries.length(); ++i )
237 const QString& aBathymetryName = aBathymetries.at( i );
238 if ( aBathymetryName.isEmpty() )
241 Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
242 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
243 if ( aBathymetry.IsNull() )
246 aZoneObj->AddBathymetry( aBathymetry );
252 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
254 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
259 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
261 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
267 Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
268 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
269 if ( !aPolyline.IsNull() )
271 aWire = aPolyline->Wire();
274 LightApp_Application* anApp = module()->getApp();
276 if ( !myActiveViewManager )
278 myActiveViewManager = anApp->activeViewManager();
281 if ( !myPreviewViewManager )
283 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
284 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
285 if ( myPreviewViewManager )
287 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
288 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
290 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewZone );
291 myPreviewViewManager->setTitle( tr( "PREVIEW_ZONE" ) );
293 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
295 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
296 if ( !aCtx.IsNull() )
298 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
304 if ( !myPreviewViewManager || !myPreviewPrs )
307 myPreviewPrs->setFillingColor( aPanel->getFillingColor(), false, false );
308 myPreviewPrs->setBorderColor( aPanel->getBorderColor(), false, false );
309 myPreviewPrs->setWire( aWire );
312 void HYDROGUI_ZoneOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
317 void HYDROGUI_ZoneOp::closePreview()
325 if( myPreviewViewManager )
327 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
328 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
330 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
331 myPreviewViewManager = 0;
334 if( myActiveViewManager )
336 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
337 myActiveViewManager = 0;