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 aPolylineName = aPolylineObj->GetName();
98 if ( !aPolylineName.isEmpty() )
100 aList.append( aPolylineName );
101 anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
105 aPanel->setPolylineNames( aList, anEntryList );
107 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
109 myEditedObject.Nullify();
112 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
113 if ( !myEditedObject.IsNull() )
115 anObjectName = myEditedObject->GetName();
116 aPolylineObj = myEditedObject->GetBoundaryPolyline();
117 if ( aPolylineObj.IsNull() )
119 aPanel->setBoundary( QString() );
123 aPolylineName = aPolylineObj->GetName();
124 aPanel->setBoundary( aPolylineName );
126 aSeq = myEditedObject->GetGeometryObjects();
127 getNamesAndEntries( aSeq, aList, anEntryList );
128 aPanel->includeGeomObjects( aList );
134 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
135 myEditedObject->SetName(anObjectName);
138 aPanel->setObjectName( anObjectName );
139 aPanel->setEditedObject( myEditedObject );
144 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq,
145 QStringList& theNames, QStringList& theEntries ) const
147 Handle(HYDROData_Object) anObject;
148 Handle(HYDROData_Entity) anEntity;
151 HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
152 for ( ; anIter.More(); anIter.Next() )
154 anEntity = anIter.Value();
155 if ( !anEntity.IsNull() )
157 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
158 if ( !anObject.IsNull() )
160 theNames.append( anObject->GetName() );
161 theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anObject ) );
167 void HYDROGUI_CalculationOp::abortOperation()
172 HYDROGUI_Operation::abortOperation();
175 void HYDROGUI_CalculationOp::commitOperation()
178 // Commit transaction
179 commitDocOperation();
180 HYDROGUI_Operation::commitOperation();
183 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
185 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
187 // Connect signals and slots
188 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
189 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
190 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
191 connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
192 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
193 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
194 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
195 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
196 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
197 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
198 connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
199 SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
200 connect( aPanel, SIGNAL( objectSelected( const QString & ) ),
201 SLOT( onObjectSelected( const QString & ) ) );
202 connect( aPanel, SIGNAL( boundarySelected( const QString & ) ),
203 SLOT( onBoundarySelected( const QString & ) ) );
208 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
210 // Set the selected boundary polyline to the calculation case
211 Handle(HYDROData_Polyline) anObject;
212 Handle(AIS_InteractiveContext) aCtx;
213 if ( myPreviewViewManager )
215 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
217 aCtx = aViewer->getAISContext();
220 // Remove the old boundary from the operation viewer
221 anObject = myEditedObject->GetBoundaryPolyline();
222 if ( !anObject.IsNull() )
224 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
227 if ( theObjName.trimmed().isEmpty() )
229 // No polyline is selected
230 myEditedObject->RemoveBoundaryPolyline();
234 Handle(HYDROData_Entity) anEntity =
235 HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINE );
236 if ( !anEntity.IsNull() )
238 anObject = Handle(HYDROData_Polyline)::DownCast( anEntity );
239 if ( !anObject.IsNull() )
241 myEditedObject->SetBoundaryPolyline( anObject );
242 if ( !aCtx.IsNull() )
244 showObject( anEntity, aCtx );
251 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
253 // Select the appropriate geometry object shape in the viewer
254 selectionMgr()->clearSelected();
256 // Unhighlight all objects except selected
257 HYDROGUI_Shape* aShape;
258 HYDROGUI_Shape* aSelectedShape = 0;
259 Handle(HYDROData_Entity) anEntity;
260 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
261 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
264 for ( ; anIter.More(); anIter.Next() )
266 anEntity = anIter.Value();
267 if ( !anEntity.IsNull() )
269 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
272 aName = anEntity->GetName();
273 isSelected = ( aName == theObjName );
276 aSelectedShape = aShape;
278 if ( aShape->isHighlighted() != isSelected )
282 aShape->highlight( isSelected );
289 if ( aSelectedShape )
291 aSelectedShape->highlight( true );
292 aSelectedShape->update();
296 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
298 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
299 HYDROGUI_Zone* aZoneItem;
300 selectionMgr()->clearSelected();
303 // Select a region in preview
304 SUIT_DataOwnerPtrList aList( true );
305 DataObjectList aZones = aRegionItem->children();
306 for ( int i = 0; i < aZones.length(); i++ )
308 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
311 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
314 selectionMgr()->setSelected( aList );
318 // select a single zone
319 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
322 SUIT_DataOwnerPtrList aList( true );
323 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
324 selectionMgr()->setSelected( aList );
329 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
331 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
334 QList<HYDROGUI_Zone*> aZonesList;
335 HYDROGUI_Zone* aZone;
336 // Get a list of dropped zones
337 for ( int i = 0; i < theZonesList.length(); i++ )
339 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
342 aZonesList.append( aZone );
345 if ( aZonesList.length() > 0 )
347 aRegion->addZones( aZonesList );
348 HYDROGUI_CalculationDlg* aPanel =
349 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
352 aPanel->setEditedObject(myEditedObject);
359 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
361 QList<HYDROGUI_Zone*> aZonesList;
362 HYDROGUI_Zone* aZone;
363 // Get a list of dropped zones
364 for ( int i = 0; i < theZonesList.length(); i++ )
366 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
369 aZonesList.append( aZone );
372 if ( aZonesList.length() > 0 )
374 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
375 HYDROGUI_CalculationDlg* aPanel =
376 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
379 aPanel->setEditedObject(myEditedObject);
385 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
387 HYDROGUI_CalculationDlg* aPanel =
388 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
391 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
394 aZone->setMergeType( theMergeType, theBathymetryName );
395 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
401 aPanel->refreshZonesBrowser();
405 void HYDROGUI_CalculationOp::onAddObjects()
407 HYDROGUI_CalculationDlg* aPanel =
408 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
411 // Add geometry objects selected in the module browser to the calculation case
412 QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
413 if ( ( !aSelectedList.isEmpty() ) && ( confirmRegionsChange() ) )
415 Handle(HYDROData_Object) anObject;
416 Handle(HYDROData_Entity) anEntity;
418 for (int i = 0; i < aSelectedList.length(); i++)
420 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
421 if ( !anEntity.IsNull() )
423 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
424 if ( !anObject.IsNull() )
426 if (myEditedObject->AddGeometryObject( anObject ))
428 aList.append( anObject->GetName() );
433 if ( !aList.isEmpty() )
435 aPanel->includeGeomObjects( aList );
442 void HYDROGUI_CalculationOp::onRemoveObjects()
444 // Remove selected objects from the calculation case
445 HYDROGUI_CalculationDlg* aPanel =
446 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
449 QStringList aList = aPanel->getSelectedGeomObjects();
450 if ( ( !aList.isEmpty() ) && ( confirmRegionsChange() ) )
452 Handle(HYDROData_Object) anObject;
453 Handle(HYDROData_Entity) anEntity;
454 for (int i = 0; i < aList.length(); i++)
456 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
457 if ( !anEntity.IsNull() )
459 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
460 if ( !anObject.IsNull() )
462 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
463 myEditedObject->RemoveGeometryObject( anObject );
467 if ( !aList.isEmpty() )
469 aPanel->excludeGeomObjects( aList );
475 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
477 // Check if the case is already modified or not
478 bool isConfirmed = myEditedObject->IsMustBeUpdated();
481 // If not modified check if the case has already defined regions with zones
482 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
483 if ( aSeq.Length() > 0 )
485 // If there are already defined zones then ask a user to confirm zones recalculation
486 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
487 tr( "REGIONS_CHANGED" ),
488 tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
489 QMessageBox::Yes | QMessageBox::No,
490 QMessageBox::No ) == QMessageBox::Yes );
494 isConfirmed = true; // No regions - no zones - nothing to recalculate
500 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
501 QString& theErrorMsg )
503 HYDROGUI_CalculationDlg* aPanel =
504 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
508 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
513 void HYDROGUI_CalculationOp::onApply()
515 QApplication::setOverrideCursor( Qt::WaitCursor );
517 int anUpdateFlags = 0;
520 bool aResult = false;
524 aResult = processApply( anUpdateFlags, anErrorMsg );
526 catch ( Standard_Failure )
528 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
529 anErrorMsg = aFailure->GetMessageString();
537 QApplication::restoreOverrideCursor();
541 module()->update( anUpdateFlags );
547 QString aMsg = tr( "INPUT_VALID_DATA" );
548 if( !anErrorMsg.isEmpty() )
549 aMsg.prepend( anErrorMsg + "\n" );
550 SUIT_MessageBox::critical( module()->getApp()->desktop(),
551 tr( "INSUFFICIENT_INPUT_DATA" ),
556 void HYDROGUI_CalculationOp::onSplitZones()
558 QApplication::setOverrideCursor( Qt::WaitCursor );
560 if ( myEditedObject->IsMustBeUpdated() )
563 myEditedObject->SplitGeometryObjects();
565 HYDROGUI_CalculationDlg* aPanel =
566 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
569 aPanel->setEditedObject( myEditedObject );
576 setZonesVisible( true );
579 QApplication::restoreOverrideCursor();
582 void HYDROGUI_CalculationOp::onHideZones()
584 setZonesVisible( false );
587 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
589 myShowZones = theIsVisible;
590 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
591 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
592 HYDROData_SequenceOfObjects aZones;
593 Handle(HYDROData_Region) aRegion;
594 if ( myPreviewViewManager )
596 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
598 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
599 if ( !aCtx.IsNull() )
601 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
603 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
604 if ( !aRegion.IsNull() )
606 aZones = aRegion->GetZones();
607 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
608 for ( ; aZonesIter.More(); aZonesIter.Next() )
612 showObject( aZonesIter.Value(), aCtx );
616 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
626 void HYDROGUI_CalculationOp::createPreview()
628 LightApp_Application* anApp = module()->getApp();
629 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
630 Handle(HYDROData_Entity) anEntity;
634 // Gather zones for displaying
635 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
636 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
637 HYDROData_SequenceOfObjects aZones;
638 Handle(HYDROData_Region) aRegion;
639 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
641 anEntity = aRegionsIter.Value();
642 if ( !anEntity.IsNull() )
644 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
645 if ( !aRegion.IsNull() )
647 aZones = aRegion->GetZones();
648 aSeq.Append( aZones );
654 // Get a boundary polyline if any
655 aSeq.Append( myEditedObject->GetBoundaryPolyline() );
657 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
659 if ( !myActiveViewManager )
661 if ( aSeq.IsEmpty() )
664 myActiveViewManager = anApp->activeViewManager();
667 if ( !myPreviewViewManager )
669 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
670 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
671 if ( myPreviewViewManager )
673 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
674 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
676 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
677 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
681 if ( !myPreviewViewManager )
684 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
686 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
687 if ( !aCtx.IsNull() )
689 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
690 for ( ; anIter.More(); anIter.Next() )
692 showObject( anIter.Value(), aCtx );
695 //Process the draw events for viewer
696 QApplication::processEvents();
697 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
703 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
705 if ( !theEntity.IsNull() )
707 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
710 aShape = new HYDROGUI_Shape( theCtx, theEntity );
711 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
717 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
722 void HYDROGUI_CalculationOp::closePreview()
724 if( myPreviewViewManager )
726 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
727 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
729 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
730 myPreviewViewManager = NULL;
733 if( myActiveViewManager )
735 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
736 myActiveViewManager = NULL;