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 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
43 const bool theIsEdit )
44 : HYDROGUI_Operation( theModule ),
45 myIsEdit( theIsEdit ),
46 myActiveViewManager( 0 ),
47 myPreviewViewManager( 0 ),
50 setName( theIsEdit ? tr( "EDIT_ZONE" ) : tr( "CREATE_ZONE" ) );
53 HYDROGUI_ZoneOp::~HYDROGUI_ZoneOp()
58 void HYDROGUI_ZoneOp::startOperation()
60 HYDROGUI_Operation::startOperation();
62 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
66 aPanel->blockSignals( true );
70 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
72 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
73 QColor aBorderColor( HYDROData_Zone::DefaultBorderColor() );
74 QString aSelectedPolyline;
75 QStringList aSelectedBathymetries;
79 myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80 if( !myEditedObject.IsNull() )
82 anObjectName = myEditedObject->GetName();
84 aFillingColor = myEditedObject->GetFillingColor();
85 aBorderColor = myEditedObject->GetBorderColor();
87 Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
88 if ( !aRefPolyline.IsNull() )
89 aSelectedPolyline = aRefPolyline->GetName();
91 HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
92 HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
93 for ( ; anIter.More(); anIter.Next() )
95 Handle(HYDROData_Bathymetry) aRefBathymetry =
96 Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
97 if ( aRefBathymetry.IsNull() )
100 QString aRefBathymetryName = aRefBathymetry->GetName();
101 if ( aRefBathymetryName.isEmpty() )
104 aSelectedBathymetries.append( aRefBathymetryName );
109 // collect information about existing closed polylines
110 QStringList aPolylines;
112 HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
113 for ( ; anIter.More(); anIter.Next() )
115 Handle(HYDROData_Polyline) aPolylineObj =
116 Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
117 if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
120 QString aPolylineName = aPolylineObj->GetName();
121 if ( aPolylineName.isEmpty() )
124 aPolylines.append( aPolylineName );
127 // collect information about existing bathymetries
128 QStringList aBathymetries;
130 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
131 for ( ; anIter.More(); anIter.Next() )
133 Handle(HYDROData_Bathymetry) aBathymetryObj =
134 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
135 if ( aBathymetryObj.IsNull() )
138 QString aBathymetryName = aBathymetryObj->GetName();
139 if ( aBathymetryName.isEmpty() )
142 aBathymetries.append( aBathymetryName );
145 aPanel->setObjectName( anObjectName );
147 aPanel->setFillingColor( aFillingColor );
148 aPanel->setBorderColor( aBorderColor );
150 aPanel->setPolylineNames( aPolylines );
152 aPanel->setBathymetries( aBathymetries );
153 aPanel->setSelectedBathymetries( aSelectedBathymetries );
155 aPanel->blockSignals( false );
157 aPanel->setPolylineName( aSelectedPolyline );
161 void HYDROGUI_ZoneOp::abortOperation()
165 HYDROGUI_Operation::abortOperation();
168 void HYDROGUI_ZoneOp::commitOperation()
172 HYDROGUI_Operation::commitOperation();
175 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
177 HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
178 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
179 this, SLOT( onCreatePreview( const QString& ) ) );
183 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
184 QString& theErrorMsg )
186 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
190 QString anObjectName = aPanel->getObjectName().simplified();
191 if ( anObjectName.isEmpty() )
193 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
197 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
199 // check that there are no other objects with the same name in the document
200 Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
201 if( !anObject.IsNull() )
203 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
208 Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
209 Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
210 if ( aZoneObj.IsNull() )
213 aZoneObj->SetName( anObjectName );
215 aZoneObj->SetFillingColor( aPanel->getFillingColor() );
216 aZoneObj->SetBorderColor( aPanel->getBorderColor() );
218 Handle(HYDROData_Polyline) aZonePolyline;
220 QString aPolylineName = aPanel->getPolylineName();
221 if ( !aPolylineName.isEmpty() )
223 aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
224 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
227 aZoneObj->SetPolyline( aZonePolyline );
230 aZoneObj->RemoveBathymetries();
232 QStringList aBathymetries = aPanel->getSelectedBathymetries();
233 for ( int i = 0; i < aBathymetries.length(); ++i )
235 const QString& aBathymetryName = aBathymetries.at( i );
236 if ( aBathymetryName.isEmpty() )
239 Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
240 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
241 if ( aBathymetry.IsNull() )
244 aZoneObj->AddBathymetry( aBathymetry );
248 theUpdateFlags = UF_Model;
253 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
255 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
261 Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
262 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
263 if ( !aPolyline.IsNull() )
265 aPath = aPolyline->painterPath();
268 LightApp_Application* anApp = module()->getApp();
270 if ( !myActiveViewManager )
272 myActiveViewManager = anApp->activeViewManager();
275 if ( !myPreviewViewManager )
277 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
278 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
279 if ( myPreviewViewManager )
281 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
282 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
284 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewZone );
285 myPreviewViewManager->setTitle( tr( "PREVIEW_ZONE" ) );
287 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
289 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
290 if ( !aCtx.IsNull() )
292 myPreviewPrs = new HYDROGUI_Shape( aCtx );
298 if ( !myPreviewViewManager || !myPreviewPrs )
301 myPreviewPrs->setFillingColor( aPanel->getFillingColor(), false );
302 myPreviewPrs->setBorderColor( aPanel->getBorderColor(), false );
303 myPreviewPrs->setPath( aPath, true );
306 void HYDROGUI_ZoneOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
311 void HYDROGUI_ZoneOp::closePreview()
319 if( myPreviewViewManager )
321 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
322 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
324 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
325 myPreviewViewManager = 0;
328 if( myActiveViewManager )
330 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
331 myActiveViewManager = 0;