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_CalculationOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_CalculationDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30 #include "HYDROGUI_Zone.h"
31 #include "HYDROGUI_Region.h"
33 #include <HYDROData_Polyline.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_ImmersibleZone.h>
36 #include <HYDROData_Object.h>
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
42 #include <LightApp_Application.h>
43 #include <LightApp_UpdateFlags.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_Desktop.h>
48 #include <QApplication>
50 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
51 : HYDROGUI_Operation( theModule ),
52 myIsEdit( theIsEdit ),
53 myActiveViewManager( NULL ),
54 myPreviewViewManager( NULL )
56 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
59 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
64 void HYDROGUI_CalculationOp::startOperation()
66 HYDROGUI_Operation::startOperation();
71 HYDROGUI_CalculationDlg* aPanel =
72 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
76 myRegionsList.clear();
79 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
81 QStringList aSelectedObjects;
83 myEditedObject.Nullify();
86 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
87 if ( !myEditedObject.IsNull() )
89 anObjectName = myEditedObject->GetName();
90 updateGeomObjectsList(aPanel);
96 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
97 myEditedObject->SetName(anObjectName);
100 aPanel->setObjectName( anObjectName );
101 aPanel->setEditedObject( myEditedObject );
106 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
108 Handle(HYDROData_Object) anObject;
109 Handle(HYDROData_Entity) anEntity;
111 // Update the list in the dialog
112 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
113 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
114 for ( ; anIter.More(); anIter.Next() )
116 anEntity = anIter.Value();
117 if ( !anEntity.IsNull() )
119 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
120 if ( !anObject.IsNull() )
122 aList.append( anObject->GetName() );
126 thePanel->setSelectedGeomObjects( aList );
129 void HYDROGUI_CalculationOp::abortOperation()
134 HYDROGUI_Operation::abortOperation();
137 void HYDROGUI_CalculationOp::commitOperation()
140 // Commit transaction
141 commitDocOperation();
142 HYDROGUI_Operation::commitOperation();
145 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
147 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
149 // Connect signals and slots
150 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
151 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
152 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
153 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
154 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
155 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
156 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
157 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
158 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
163 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
165 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
168 QList<HYDROGUI_Zone*> aZonesList;
169 HYDROGUI_Zone* aZone;
170 // Get a list of dropped zones
171 for ( int i = 0; i < theZonesList.length(); i++ )
173 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
176 aZonesList.append( aZone );
179 if ( aZonesList.length() > 0 )
181 aRegion->addZones( aZonesList );
182 HYDROGUI_CalculationDlg* aPanel =
183 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
186 aPanel->setEditedObject(myEditedObject);
192 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
194 QList<HYDROGUI_Zone*> aZonesList;
195 HYDROGUI_Zone* aZone;
196 // Get a list of dropped zones
197 for ( int i = 0; i < theZonesList.length(); i++ )
199 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
202 aZonesList.append( aZone );
205 if ( aZonesList.length() > 0 )
207 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
208 HYDROGUI_CalculationDlg* aPanel =
209 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
212 aPanel->setEditedObject(myEditedObject);
217 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
219 HYDROGUI_CalculationDlg* aPanel =
220 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
223 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
226 aZone->setMergeType( theMergeType, theBathymetryName );
228 aPanel->refreshZonesBrowser();
232 void HYDROGUI_CalculationOp::onAddObjects()
234 // Add geometry objects selected in the module browser to the calculation case
235 Handle(HYDROData_Object) anObject;
236 Handle(HYDROData_Entity) anEntity;
238 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
239 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
241 anEntity = aSeq.Value( anIndex );
242 if ( !anEntity.IsNull() )
244 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
245 if( !anObject.IsNull() )
247 if (myEditedObject->AddGeometryObject( anObject ))
249 aList.append( anObject->GetName() );
254 HYDROGUI_CalculationDlg* aPanel =
255 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
258 updateGeomObjectsList( aPanel );
263 void HYDROGUI_CalculationOp::onRemoveObjects()
265 // Remove selected objects from the calculation case
266 HYDROGUI_CalculationDlg* aPanel =
267 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
270 QStringList aList = aPanel->getSelectedGeomObjects();
271 Handle(HYDROData_Object) anObject;
272 Handle(HYDROData_Entity) anEntity;
273 for (int i = 0; i < aList.length(); i++)
275 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
276 if ( !anEntity.IsNull() )
278 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
279 if ( !anObject.IsNull() )
281 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
282 myEditedObject->RemoveGeometryObject( anObject );
286 updateGeomObjectsList( aPanel );
290 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
291 QString& theErrorMsg )
293 HYDROGUI_CalculationDlg* aPanel =
294 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
298 theUpdateFlags = UF_Model;
303 void HYDROGUI_CalculationOp::onApply()
305 QApplication::setOverrideCursor( Qt::WaitCursor );
307 int anUpdateFlags = 0;
310 bool aResult = false;
314 aResult = processApply( anUpdateFlags, anErrorMsg );
316 catch ( Standard_Failure )
318 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
319 anErrorMsg = aFailure->GetMessageString();
327 QApplication::restoreOverrideCursor();
331 module()->update( anUpdateFlags );
337 QString aMsg = tr( "INPUT_VALID_DATA" );
338 if( !anErrorMsg.isEmpty() )
339 aMsg.prepend( anErrorMsg + "\n" );
340 SUIT_MessageBox::critical( module()->getApp()->desktop(),
341 tr( "INSUFFICIENT_INPUT_DATA" ),
346 void HYDROGUI_CalculationOp::onSplitZones()
348 HYDROGUI_CalculationDlg* aPanel =
349 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
353 QApplication::setOverrideCursor( Qt::WaitCursor );
355 myEditedObject->SplitGeometryObjects();
356 aPanel->setEditedObject( myEditedObject );
359 QApplication::restoreOverrideCursor();
362 void HYDROGUI_CalculationOp::createPreview()
364 LightApp_Application* anApp = module()->getApp();
365 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
366 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
368 if ( !myActiveViewManager )
370 if ( aSeq.IsEmpty() )
373 myActiveViewManager = anApp->activeViewManager();
376 if ( !myPreviewViewManager )
378 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
379 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
380 if ( myPreviewViewManager )
382 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
383 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
385 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
386 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
390 if ( !myPreviewViewManager )
393 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
395 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
396 if ( !aCtx.IsNull() )
398 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
399 Handle(HYDROData_Object) anObject;
400 Handle(HYDROData_Entity) anEntity;
401 HYDROGUI_Shape* aShape;
402 for ( ; anIter.More(); anIter.Next() )
404 anEntity = anIter.Value();
405 if ( !anEntity.IsNull() )
407 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
408 if ( !anObject.IsNull() )
410 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
413 aShape = new HYDROGUI_Shape( aCtx, anObject );
416 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject, aShape );
421 //Process the draw events for viewer
422 QApplication::processEvents();
423 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
429 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
434 void HYDROGUI_CalculationOp::closePreview()
436 if( myPreviewViewManager )
438 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
439 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
441 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
442 myPreviewViewManager = NULL;
445 if( myActiveViewManager )
447 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
448 myActiveViewManager = NULL;