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>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_DataOwner.h>
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
50 #include <QApplication>
52 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54 myIsEdit( theIsEdit ),
55 myActiveViewManager( NULL ),
56 myPreviewViewManager( NULL )
58 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
61 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
66 void HYDROGUI_CalculationOp::startOperation()
68 HYDROGUI_Operation::startOperation();
73 HYDROGUI_CalculationDlg* aPanel =
74 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
80 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
82 QStringList aSelectedObjects;
84 myEditedObject.Nullify();
87 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
88 if ( !myEditedObject.IsNull() )
90 anObjectName = myEditedObject->GetName();
91 updateGeomObjectsList(aPanel);
97 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
98 myEditedObject->SetName(anObjectName);
101 aPanel->setObjectName( anObjectName );
102 aPanel->setEditedObject( myEditedObject );
107 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
109 Handle(HYDROData_Object) anObject;
110 Handle(HYDROData_Entity) anEntity;
112 // Update the list in the dialog
113 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
114 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
115 for ( ; anIter.More(); anIter.Next() )
117 anEntity = anIter.Value();
118 if ( !anEntity.IsNull() )
120 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
121 if ( !anObject.IsNull() )
123 aList.append( anObject->GetName() );
127 thePanel->setGeomObjects( aList );
130 void HYDROGUI_CalculationOp::abortOperation()
135 HYDROGUI_Operation::abortOperation();
138 void HYDROGUI_CalculationOp::commitOperation()
141 // Commit transaction
142 commitDocOperation();
143 HYDROGUI_Operation::commitOperation();
146 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
148 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
150 // Connect signals and slots
151 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
152 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
153 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
154 connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
155 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
156 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
157 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
158 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
159 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
160 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
161 connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
162 SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
163 connect( aPanel, SIGNAL( objectSelected( const QString & ) ),
164 SLOT( onObjectSelected( const QString & ) ) );
169 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
171 // Select the appropriate geometry object shape in the viewer
172 selectionMgr()->clearSelected();
174 // Unhighlight all objects except selected
175 HYDROGUI_Shape* aShape;
176 HYDROGUI_Shape* aSelectedShape = 0;
177 Handle(HYDROData_Entity) anEntity;
178 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
179 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
182 for ( ; anIter.More(); anIter.Next() )
184 anEntity = anIter.Value();
185 if ( !anEntity.IsNull() )
187 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
190 aName = anEntity->GetName();
191 isSelected = ( aName == theObjName );
194 aSelectedShape = aShape;
196 if ( aShape->isHighlighted() != isSelected )
200 aShape->highlight( isSelected );
207 if ( aSelectedShape )
209 aSelectedShape->highlight( true );
210 aSelectedShape->update();
214 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
216 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
217 HYDROGUI_Zone* aZoneItem;
218 selectionMgr()->clearSelected();
221 // Select a region in preview
222 SUIT_DataOwnerPtrList aList( true );
223 DataObjectList aZones = aRegionItem->children();
224 for ( int i = 0; i < aZones.length(); i++ )
226 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
229 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
232 selectionMgr()->setSelected( aList );
236 // select a single zone
237 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
240 SUIT_DataOwnerPtrList aList( true );
241 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
242 selectionMgr()->setSelected( aList );
247 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
249 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
252 QList<HYDROGUI_Zone*> aZonesList;
253 HYDROGUI_Zone* aZone;
254 // Get a list of dropped zones
255 for ( int i = 0; i < theZonesList.length(); i++ )
257 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
260 aZonesList.append( aZone );
263 if ( aZonesList.length() > 0 )
265 aRegion->addZones( aZonesList );
266 HYDROGUI_CalculationDlg* aPanel =
267 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
270 aPanel->setEditedObject(myEditedObject);
277 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
279 QList<HYDROGUI_Zone*> aZonesList;
280 HYDROGUI_Zone* aZone;
281 // Get a list of dropped zones
282 for ( int i = 0; i < theZonesList.length(); i++ )
284 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
287 aZonesList.append( aZone );
290 if ( aZonesList.length() > 0 )
292 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
293 HYDROGUI_CalculationDlg* aPanel =
294 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
297 aPanel->setEditedObject(myEditedObject);
303 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
305 HYDROGUI_CalculationDlg* aPanel =
306 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
309 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
312 aZone->setMergeType( theMergeType, theBathymetryName );
313 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
319 aPanel->refreshZonesBrowser();
323 void HYDROGUI_CalculationOp::onAddObjects()
325 // Add geometry objects selected in the module browser to the calculation case
326 Handle(HYDROData_Object) anObject;
327 Handle(HYDROData_Entity) anEntity;
329 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
330 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
332 anEntity = aSeq.Value( anIndex );
333 if ( !anEntity.IsNull() )
335 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
336 if( !anObject.IsNull() )
338 if (myEditedObject->AddGeometryObject( anObject ))
340 aList.append( anObject->GetName() );
345 HYDROGUI_CalculationDlg* aPanel =
346 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
349 updateGeomObjectsList( aPanel );
354 void HYDROGUI_CalculationOp::onRemoveObjects()
356 // Remove selected objects from the calculation case
357 HYDROGUI_CalculationDlg* aPanel =
358 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
361 QStringList aList = aPanel->getSelectedGeomObjects();
362 Handle(HYDROData_Object) anObject;
363 Handle(HYDROData_Entity) anEntity;
364 for (int i = 0; i < aList.length(); i++)
366 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
367 if ( !anEntity.IsNull() )
369 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
370 if ( !anObject.IsNull() )
372 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
373 myEditedObject->RemoveGeometryObject( anObject );
377 updateGeomObjectsList( aPanel );
381 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
382 QString& theErrorMsg )
384 HYDROGUI_CalculationDlg* aPanel =
385 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
389 theUpdateFlags = UF_Model;
394 void HYDROGUI_CalculationOp::onApply()
396 QApplication::setOverrideCursor( Qt::WaitCursor );
398 int anUpdateFlags = 0;
401 bool aResult = false;
405 aResult = processApply( anUpdateFlags, anErrorMsg );
407 catch ( Standard_Failure )
409 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
410 anErrorMsg = aFailure->GetMessageString();
418 QApplication::restoreOverrideCursor();
422 module()->update( anUpdateFlags );
428 QString aMsg = tr( "INPUT_VALID_DATA" );
429 if( !anErrorMsg.isEmpty() )
430 aMsg.prepend( anErrorMsg + "\n" );
431 SUIT_MessageBox::critical( module()->getApp()->desktop(),
432 tr( "INSUFFICIENT_INPUT_DATA" ),
437 void HYDROGUI_CalculationOp::onSplitZones()
439 QApplication::setOverrideCursor( Qt::WaitCursor );
441 if ( myEditedObject->IsMustBeUpdated() )
444 myEditedObject->SplitGeometryObjects();
446 HYDROGUI_CalculationDlg* aPanel =
447 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
450 aPanel->setEditedObject( myEditedObject );
457 setZonesVisible( true );
460 QApplication::restoreOverrideCursor();
463 void HYDROGUI_CalculationOp::onHideZones()
465 setZonesVisible( false );
468 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
470 myShowZones = theIsVisible;
471 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
472 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
473 HYDROData_SequenceOfObjects aZones;
474 Handle(HYDROData_Region) aRegion;
475 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
477 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
478 if ( !aCtx.IsNull() )
480 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
482 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
483 if ( !aRegion.IsNull() )
485 aZones = aRegion->GetZones();
486 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
487 for ( ; aZonesIter.More(); aZonesIter.Next() )
491 showObject( aZonesIter.Value(), aCtx );
495 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
504 void HYDROGUI_CalculationOp::createPreview()
506 LightApp_Application* anApp = module()->getApp();
507 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
508 Handle(HYDROData_Entity) anEntity;
512 // Gather zones for displaying
513 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
514 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
515 HYDROData_SequenceOfObjects aZones;
516 Handle(HYDROData_Region) aRegion;
517 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
519 anEntity = aRegionsIter.Value();
520 if ( !anEntity.IsNull() )
522 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
523 if ( !aRegion.IsNull() )
525 aZones = aRegion->GetZones();
526 aSeq.Append( aZones );
532 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
534 if ( !myActiveViewManager )
536 if ( aSeq.IsEmpty() )
539 myActiveViewManager = anApp->activeViewManager();
542 if ( !myPreviewViewManager )
544 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
545 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
546 if ( myPreviewViewManager )
548 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
549 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
551 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
552 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
556 if ( !myPreviewViewManager )
559 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
561 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
562 if ( !aCtx.IsNull() )
564 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
565 for ( ; anIter.More(); anIter.Next() )
567 showObject( anIter.Value(), aCtx );
570 //Process the draw events for viewer
571 QApplication::processEvents();
572 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
578 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
580 if ( !theEntity.IsNull() )
582 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
585 aShape = new HYDROGUI_Shape( theCtx, theEntity );
586 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
592 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
597 void HYDROGUI_CalculationOp::closePreview()
599 if( myPreviewViewManager )
601 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
602 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
604 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
605 myPreviewViewManager = NULL;
608 if( myActiveViewManager )
610 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
611 myActiveViewManager = NULL;