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 QStringList anEntryList;
81 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
82 getNamesAndEntries( aSeq, aList, anEntryList );
83 aPanel->setAllGeomObjects( aList, anEntryList );
88 HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
89 Handle(HYDROData_Polyline) aPolylineObj;
90 QString aPolylineName;
91 for ( ; anIter.More(); anIter.Next() )
93 aPolylineObj = Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
95 if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
97 // Check the polyline shape
98 TopoDS_Shape aPolylineShape = aPolylineObj->GetTopShape();
99 if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
100 aPolylineName = aPolylineObj->GetName();
101 if ( !aPolylineName.isEmpty() )
103 aList.append( aPolylineName );
104 anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
109 aPanel->setPolylineNames( aList, anEntryList );
111 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
113 myEditedObject.Nullify();
116 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
117 if ( !myEditedObject.IsNull() )
119 anObjectName = myEditedObject->GetName();
120 aPolylineObj = myEditedObject->GetBoundaryPolyline();
121 if ( aPolylineObj.IsNull() )
123 aPanel->setBoundary( QString() );
127 aPolylineName = aPolylineObj->GetName();
128 aPanel->setBoundary( aPolylineName );
130 aSeq = myEditedObject->GetGeometryObjects();
131 getNamesAndEntries( aSeq, aList, anEntryList );
132 aPanel->includeGeomObjects( aList );
138 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
139 myEditedObject->SetName(anObjectName);
142 aPanel->setObjectName( anObjectName );
143 aPanel->setEditedObject( myEditedObject );
148 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq,
149 QStringList& theNames, QStringList& theEntries ) const
151 Handle(HYDROData_Object) anObject;
152 Handle(HYDROData_Entity) anEntity;
155 HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
156 for ( ; anIter.More(); anIter.Next() )
158 anEntity = anIter.Value();
159 if ( !anEntity.IsNull() )
161 // Temporary solution will be revised later
162 //anObject = Handle(HYDROData_Object)::DownCast( anEntity );
163 anObject = Handle(HYDROData_ImmersibleZone)::DownCast( anEntity );
164 if ( !anObject.IsNull() )
166 theNames.append( anObject->GetName() );
167 theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anObject ) );
173 void HYDROGUI_CalculationOp::abortOperation()
178 HYDROGUI_Operation::abortOperation();
181 void HYDROGUI_CalculationOp::commitOperation()
184 // Commit transaction
185 commitDocOperation();
186 HYDROGUI_Operation::commitOperation();
189 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
191 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
193 // Connect signals and slots
194 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
195 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
196 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
197 connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
198 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
199 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
200 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
201 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
202 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
203 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
204 connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
205 SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
206 connect( aPanel, SIGNAL( objectSelected( const QString & ) ),
207 SLOT( onObjectSelected( const QString & ) ) );
208 connect( aPanel, SIGNAL( boundarySelected( const QString & ) ),
209 SLOT( onBoundarySelected( const QString & ) ) );
214 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
216 // Set the selected boundary polyline to the calculation case
217 Handle(HYDROData_Polyline) anObject;
218 Handle(AIS_InteractiveContext) aCtx;
219 if ( myPreviewViewManager )
221 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
223 aCtx = aViewer->getAISContext();
226 // Remove the old boundary from the operation viewer
227 anObject = myEditedObject->GetBoundaryPolyline();
228 if ( !anObject.IsNull() )
230 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
233 if ( theObjName.trimmed().isEmpty() )
235 // No polyline is selected
236 myEditedObject->RemoveBoundaryPolyline();
240 Handle(HYDROData_Entity) anEntity =
241 HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINE );
242 if ( !anEntity.IsNull() )
244 anObject = Handle(HYDROData_Polyline)::DownCast( anEntity );
245 if ( !anObject.IsNull() )
247 myEditedObject->SetBoundaryPolyline( anObject );
248 if ( !aCtx.IsNull() )
250 showObject( anEntity, aCtx );
257 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
259 // Select the appropriate geometry object shape in the viewer
260 selectionMgr()->clearSelected();
262 // Unhighlight all objects except selected
263 HYDROGUI_Shape* aShape;
264 HYDROGUI_Shape* aSelectedShape = 0;
265 Handle(HYDROData_Entity) anEntity;
266 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
267 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
270 for ( ; anIter.More(); anIter.Next() )
272 anEntity = anIter.Value();
273 if ( !anEntity.IsNull() )
275 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
278 aName = anEntity->GetName();
279 isSelected = ( aName == theObjName );
282 aSelectedShape = aShape;
284 if ( aShape->isHighlighted() != isSelected )
288 aShape->highlight( isSelected );
295 if ( aSelectedShape )
297 aSelectedShape->highlight( true );
298 aSelectedShape->update();
302 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
304 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
305 HYDROGUI_Zone* aZoneItem;
306 selectionMgr()->clearSelected();
309 // Select a region in preview
310 SUIT_DataOwnerPtrList aList( true );
311 DataObjectList aZones = aRegionItem->children();
312 for ( int i = 0; i < aZones.length(); i++ )
314 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
317 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
320 selectionMgr()->setSelected( aList );
324 // select a single zone
325 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
328 SUIT_DataOwnerPtrList aList( true );
329 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
330 selectionMgr()->setSelected( aList );
335 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
337 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
340 QList<HYDROGUI_Zone*> aZonesList;
341 HYDROGUI_Zone* aZone;
342 // Get a list of dropped zones
343 for ( int i = 0; i < theZonesList.length(); i++ )
345 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
348 aZonesList.append( aZone );
351 if ( aZonesList.length() > 0 )
353 aRegion->addZones( aZonesList );
354 HYDROGUI_CalculationDlg* aPanel =
355 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
358 aPanel->setEditedObject(myEditedObject);
365 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
367 QList<HYDROGUI_Zone*> aZonesList;
368 HYDROGUI_Zone* aZone;
369 // Get a list of dropped zones
370 for ( int i = 0; i < theZonesList.length(); i++ )
372 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
375 aZonesList.append( aZone );
378 if ( aZonesList.length() > 0 )
380 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
381 HYDROGUI_CalculationDlg* aPanel =
382 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
385 aPanel->setEditedObject(myEditedObject);
391 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
393 HYDROGUI_CalculationDlg* aPanel =
394 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
397 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
400 aZone->setMergeType( theMergeType, theBathymetryName );
401 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
407 aPanel->refreshZonesBrowser();
411 void HYDROGUI_CalculationOp::onAddObjects()
413 HYDROGUI_CalculationDlg* aPanel =
414 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
417 // Add geometry objects selected in the module browser to the calculation case
418 QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
419 if ( ( !aSelectedList.isEmpty() ) && ( confirmRegionsChange() ) )
421 Handle(HYDROData_Object) anObject;
422 Handle(HYDROData_Entity) anEntity;
424 for (int i = 0; i < aSelectedList.length(); i++)
426 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
427 if ( !anEntity.IsNull() )
429 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
430 if ( !anObject.IsNull() )
432 if (myEditedObject->AddGeometryObject( anObject ))
434 aList.append( anObject->GetName() );
439 if ( !aList.isEmpty() )
441 aPanel->includeGeomObjects( aList );
448 void HYDROGUI_CalculationOp::onRemoveObjects()
450 // Remove selected objects from the calculation case
451 HYDROGUI_CalculationDlg* aPanel =
452 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
455 QStringList aList = aPanel->getSelectedGeomObjects();
456 if ( ( !aList.isEmpty() ) && ( confirmRegionsChange() ) )
458 Handle(HYDROData_Object) anObject;
459 Handle(HYDROData_Entity) anEntity;
460 for (int i = 0; i < aList.length(); i++)
462 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
463 if ( !anEntity.IsNull() )
465 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
466 if ( !anObject.IsNull() )
468 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
469 myEditedObject->RemoveGeometryObject( anObject );
473 if ( !aList.isEmpty() )
475 aPanel->excludeGeomObjects( aList );
481 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
483 // Check if the case is already modified or not
484 bool isConfirmed = myEditedObject->IsMustBeUpdated();
487 // If not modified check if the case has already defined regions with zones
488 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
489 if ( aSeq.Length() > 0 )
491 // If there are already defined zones then ask a user to confirm zones recalculation
492 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
493 tr( "REGIONS_CHANGED" ),
494 tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
495 QMessageBox::Yes | QMessageBox::No,
496 QMessageBox::No ) == QMessageBox::Yes );
500 isConfirmed = true; // No regions - no zones - nothing to recalculate
506 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
507 QString& theErrorMsg )
509 HYDROGUI_CalculationDlg* aPanel =
510 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
514 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
519 void HYDROGUI_CalculationOp::onApply()
521 QApplication::setOverrideCursor( Qt::WaitCursor );
523 int anUpdateFlags = 0;
526 bool aResult = false;
530 aResult = processApply( anUpdateFlags, anErrorMsg );
532 catch ( Standard_Failure )
534 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
535 anErrorMsg = aFailure->GetMessageString();
543 QApplication::restoreOverrideCursor();
547 module()->update( anUpdateFlags );
553 QString aMsg = tr( "INPUT_VALID_DATA" );
554 if( !anErrorMsg.isEmpty() )
555 aMsg.prepend( anErrorMsg + "\n" );
556 SUIT_MessageBox::critical( module()->getApp()->desktop(),
557 tr( "INSUFFICIENT_INPUT_DATA" ),
562 void HYDROGUI_CalculationOp::onSplitZones()
564 HYDROGUI_CalculationDlg* aPanel =
565 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
569 QApplication::setOverrideCursor( Qt::WaitCursor );
571 QString aNewCaseName = aPanel->getObjectName();
572 QString anOldCaseName = myEditedObject->GetName();
574 bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
576 // At first we must to update the case name because of
577 // automatic names generation for regions and zones
578 myEditedObject->SetName( aNewCaseName );
580 if ( myEditedObject->IsMustBeUpdated() )
583 myEditedObject->SplitGeometryObjects();
585 aPanel->setEditedObject( myEditedObject );
591 setZonesVisible( true );
594 if ( anIsToUpdateOb )
595 module()->getApp()->updateObjectBrowser( false );
597 QApplication::restoreOverrideCursor();
600 void HYDROGUI_CalculationOp::onHideZones()
602 setZonesVisible( false );
605 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
607 myShowZones = theIsVisible;
608 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
609 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
610 HYDROData_SequenceOfObjects aZones;
611 Handle(HYDROData_Region) aRegion;
612 if ( myPreviewViewManager )
614 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
616 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
617 if ( !aCtx.IsNull() )
619 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
621 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
622 if ( !aRegion.IsNull() )
624 aZones = aRegion->GetZones();
625 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
626 for ( ; aZonesIter.More(); aZonesIter.Next() )
630 showObject( aZonesIter.Value(), aCtx );
634 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
644 void HYDROGUI_CalculationOp::createPreview()
646 LightApp_Application* anApp = module()->getApp();
647 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
648 Handle(HYDROData_Entity) anEntity;
652 // Gather zones for displaying
653 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
654 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
655 HYDROData_SequenceOfObjects aZones;
656 Handle(HYDROData_Region) aRegion;
657 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
659 anEntity = aRegionsIter.Value();
660 if ( !anEntity.IsNull() )
662 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
663 if ( !aRegion.IsNull() )
665 aZones = aRegion->GetZones();
666 aSeq.Append( aZones );
672 // Get a boundary polyline if any
673 aSeq.Append( myEditedObject->GetBoundaryPolyline() );
675 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
677 if ( !myActiveViewManager )
679 if ( aSeq.IsEmpty() )
682 myActiveViewManager = anApp->activeViewManager();
685 if ( !myPreviewViewManager )
687 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
688 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
689 if ( myPreviewViewManager )
691 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
692 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
694 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
695 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
699 if ( !myPreviewViewManager )
702 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
704 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
705 if ( !aCtx.IsNull() )
707 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
708 for ( ; anIter.More(); anIter.Next() )
710 showObject( anIter.Value(), aCtx );
713 //Process the draw events for viewer
714 QApplication::processEvents();
715 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
721 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
723 if ( !theEntity.IsNull() )
725 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
728 aShape = new HYDROGUI_Shape( theCtx, theEntity );
729 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
735 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
740 void HYDROGUI_CalculationOp::closePreview()
742 if( myPreviewViewManager )
744 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
745 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
747 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
748 myPreviewViewManager = NULL;
751 if( myActiveViewManager )
753 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
754 myActiveViewManager = NULL;