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_PrsZone.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 <GraphicsView_ViewManager.h>
37 #include <GraphicsView_ViewPort.h>
38 #include <GraphicsView_Viewer.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
43 HYDROGUI_ZoneOp::HYDROGUI_ZoneOp( HYDROGUI_Module* theModule,
44 const bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46 myIsEdit( theIsEdit ),
47 myActiveViewManager( 0 ),
48 myPreviewViewManager( 0 ),
51 setName( theIsEdit ? tr( "EDIT_ZONE" ) : tr( "CREATE_ZONE" ) );
54 HYDROGUI_ZoneOp::~HYDROGUI_ZoneOp()
59 void HYDROGUI_ZoneOp::startOperation()
61 HYDROGUI_Operation::startOperation();
63 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
69 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
71 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
72 QColor aBorderColor( HYDROData_Zone::DefaultBorderColor() );
73 QString aSelectedPolyline;
74 QStringList aSelectedBathymetries;
78 myEditedObject = Handle(HYDROData_Zone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
79 if( !myEditedObject.IsNull() )
81 anObjectName = myEditedObject->GetName();
83 aFillingColor = myEditedObject->GetFillingColor();
84 aBorderColor = myEditedObject->GetBorderColor();
86 Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
87 if ( !aRefPolyline.IsNull() )
88 aSelectedPolyline = aRefPolyline->GetName();
90 HYDROData_SequenceOfObjects aRefBathymetries = myEditedObject->GetBathymetries();
91 HYDROData_SequenceOfObjects::Iterator anIter( aRefBathymetries );
92 for ( ; anIter.More(); anIter.Next() )
94 Handle(HYDROData_Bathymetry) aRefBathymetry =
95 Handle(HYDROData_Bathymetry)::DownCast( anIter.Value() );
96 if ( aRefBathymetry.IsNull() )
99 QString aRefBathymetryName = aRefBathymetry->GetName();
100 if ( aRefBathymetryName.isEmpty() )
103 aSelectedBathymetries.append( aRefBathymetryName );
108 // collect information about existing closed polylines
109 QStringList aPolylines;
111 HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
112 for ( ; anIter.More(); anIter.Next() )
114 Handle(HYDROData_Polyline) aPolylineObj =
115 Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
116 if ( aPolylineObj.IsNull() || !aPolylineObj->isClosed() )
119 QString aPolylineName = aPolylineObj->GetName();
120 if ( aPolylineName.isEmpty() )
123 aPolylines.append( aPolylineName );
126 // collect information about existing bathymetries
127 QStringList aBathymetries;
129 anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
130 for ( ; anIter.More(); anIter.Next() )
132 Handle(HYDROData_Bathymetry) aBathymetryObj =
133 Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
134 if ( aBathymetryObj.IsNull() )
137 QString aBathymetryName = aBathymetryObj->GetName();
138 if ( aBathymetryName.isEmpty() )
141 aBathymetries.append( aBathymetryName );
144 aPanel->setObjectName( anObjectName );
146 aPanel->setFillingColor( aFillingColor );
147 aPanel->setBorderColor( aBorderColor );
149 aPanel->setPolylineNames( aPolylines );
150 aPanel->setPolylineName( aSelectedPolyline );
152 aPanel->setBathymetries( aBathymetries );
153 aPanel->setSelectedBathymetries( aSelectedBathymetries );
157 void HYDROGUI_ZoneOp::abortOperation()
161 HYDROGUI_Operation::abortOperation();
164 void HYDROGUI_ZoneOp::commitOperation()
168 HYDROGUI_Operation::commitOperation();
171 HYDROGUI_InputPanel* HYDROGUI_ZoneOp::createInputPanel() const
173 HYDROGUI_ZoneDlg* aPanel = new HYDROGUI_ZoneDlg( module(), getName() );
174 connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
175 this, SLOT( onCreatePreview( const QString& ) ) );
179 bool HYDROGUI_ZoneOp::processApply( int& theUpdateFlags,
180 QString& theErrorMsg )
182 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
186 QString anObjectName = aPanel->getObjectName().simplified();
187 if ( anObjectName.isEmpty() )
189 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
193 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
195 // check that there are no other objects with the same name in the document
196 Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
197 if( !anObject.IsNull() )
199 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
204 Handle(HYDROData_Zone) aZoneObj = myIsEdit ? myEditedObject :
205 Handle(HYDROData_Zone)::DownCast( doc()->CreateObject( KIND_ZONE ) );
206 if ( aZoneObj.IsNull() )
209 aZoneObj->SetName( anObjectName );
211 aZoneObj->SetFillingColor( aPanel->getFillingColor() );
212 aZoneObj->SetBorderColor( aPanel->getBorderColor() );
214 Handle(HYDROData_Polyline) aZonePolyline;
216 QString aPolylineName = aPanel->getPolylineName();
217 if ( !aPolylineName.isEmpty() )
219 aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
220 HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
223 aZoneObj->SetPolyline( aZonePolyline );
226 aZoneObj->RemoveBathymetries();
228 QStringList aBathymetries = aPanel->getSelectedBathymetries();
229 for ( int i = 0; i < aBathymetries.length(); ++i )
231 const QString& aBathymetryName = aBathymetries.at( i );
232 if ( aBathymetryName.isEmpty() )
235 Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
236 HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
237 if ( aBathymetry.IsNull() )
240 aZoneObj->AddBathymetry( aBathymetry );
244 theUpdateFlags = UF_Model;
249 void HYDROGUI_ZoneOp::onCreatePreview( const QString& thePolylineName )
251 HYDROGUI_ZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ZoneDlg*>( inputPanel() );
257 Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
258 HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
259 if ( !aPolyline.IsNull() )
261 aPath = aPolyline->painterPath();
264 LightApp_Application* anApp = module()->getApp();
266 if ( !myActiveViewManager )
268 myActiveViewManager = anApp->activeViewManager();
273 myPreviewPrs = new HYDROGUI_PrsZone( myIsEdit ? myEditedObject : 0 );
276 myPreviewPrs->setPath( aPath );
277 myPreviewPrs->setFillingColor( aPanel->getFillingColor() );
278 myPreviewPrs->setBorderColor( aPanel->getBorderColor() );
279 myPreviewPrs->compute();
281 if ( !myPreviewViewManager )
283 myPreviewViewManager = ::qobject_cast<GraphicsView_ViewManager*>(
284 anApp->createViewManager( GraphicsView_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" ) );
292 if ( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
294 if ( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
296 aViewPort->addItem( myPreviewPrs );
302 if ( myPreviewViewManager )
304 if ( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
306 if ( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
315 void HYDROGUI_ZoneOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
320 void HYDROGUI_ZoneOp::closePreview()
322 // It's very strange, but without calling this method (it's quite safe) a crash is stably reproduced.
323 // Scenario: create any non-Graphics view, activate import op, click apply.
324 // Result: a few SIGSEGVs coming from processEvents(), then crash.
325 if( myPreviewViewManager )
326 if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
327 if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
328 aViewPort->onBoundingRectChanged();
336 if( myPreviewViewManager )
338 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
339 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
341 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
342 myPreviewViewManager = 0;
345 if( myActiveViewManager )
347 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
348 myActiveViewManager = 0;