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_PolylineXY.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_Object.h>
36 #include <HYDROData_Tool.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_POLYLINEXY );
89 Handle(HYDROData_PolylineXY) aPolylineObj;
90 QString aPolylineName;
91 for ( ; anIter.More(); anIter.Next() )
93 aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
95 if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
97 // Check the polyline shape
98 TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
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
154 HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
155 for ( ; anIter.More(); anIter.Next() )
157 Handle(HYDROData_Entity) anEntity = anIter.Value();
158 if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
161 theNames.append( anEntity->GetName() );
162 theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
166 void HYDROGUI_CalculationOp::abortOperation()
171 HYDROGUI_Operation::abortOperation();
174 void HYDROGUI_CalculationOp::commitOperation()
177 // Commit transaction
178 commitDocOperation();
179 HYDROGUI_Operation::commitOperation();
182 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
184 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
186 // Connect signals and slots
187 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
188 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
189 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
190 connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
191 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
192 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
193 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
194 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
195 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
196 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
197 connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
198 SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
199 connect( aPanel, SIGNAL( objectSelected( const QString & ) ),
200 SLOT( onObjectSelected( const QString & ) ) );
201 connect( aPanel, SIGNAL( boundarySelected( const QString & ) ),
202 SLOT( onBoundarySelected( const QString & ) ) );
207 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
209 // Set the selected boundary polyline to the calculation case
210 Handle(HYDROData_PolylineXY) anObject;
211 Handle(AIS_InteractiveContext) aCtx;
212 if ( myPreviewViewManager )
214 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
216 aCtx = aViewer->getAISContext();
219 // Remove the old boundary from the operation viewer
220 anObject = myEditedObject->GetBoundaryPolyline();
221 if ( !anObject.IsNull() )
223 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
226 if ( theObjName.trimmed().isEmpty() )
228 // No polyline is selected
229 myEditedObject->RemoveBoundaryPolyline();
233 Handle(HYDROData_Entity) anEntity =
234 HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY );
235 if ( !anEntity.IsNull() )
237 anObject = Handle(HYDROData_PolylineXY)::DownCast( anEntity );
238 if ( !anObject.IsNull() )
240 myEditedObject->SetBoundaryPolyline( anObject );
241 if ( !aCtx.IsNull() )
243 showObject( anEntity, aCtx );
250 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
252 // Select the appropriate geometry object shape in the viewer
253 selectionMgr()->clearSelected();
255 // Unhighlight all objects except selected
256 HYDROGUI_Shape* aShape;
257 HYDROGUI_Shape* aSelectedShape = 0;
258 Handle(HYDROData_Entity) anEntity;
259 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
260 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
263 for ( ; anIter.More(); anIter.Next() )
265 anEntity = anIter.Value();
266 if ( !anEntity.IsNull() )
268 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
271 aName = anEntity->GetName();
272 isSelected = ( aName == theObjName );
275 aSelectedShape = aShape;
277 if ( aShape->isHighlighted() != isSelected )
281 aShape->highlight( isSelected );
288 if ( aSelectedShape )
290 aSelectedShape->highlight( true );
291 aSelectedShape->update();
295 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
297 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
298 HYDROGUI_Zone* aZoneItem;
299 selectionMgr()->clearSelected();
302 // Select a region in preview
303 SUIT_DataOwnerPtrList aList( true );
304 DataObjectList aZones = aRegionItem->children();
305 for ( int i = 0; i < aZones.length(); i++ )
307 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
310 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
313 selectionMgr()->setSelected( aList );
317 // select a single zone
318 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
321 SUIT_DataOwnerPtrList aList( true );
322 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
323 selectionMgr()->setSelected( aList );
328 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
330 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
333 QList<HYDROGUI_Zone*> aZonesList;
334 HYDROGUI_Zone* aZone;
335 // Get a list of dropped zones
336 for ( int i = 0; i < theZonesList.length(); i++ )
338 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
341 aZonesList.append( aZone );
344 if ( aZonesList.length() > 0 )
346 aRegion->addZones( aZonesList );
347 HYDROGUI_CalculationDlg* aPanel =
348 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
351 aPanel->setEditedObject(myEditedObject);
358 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
360 QList<HYDROGUI_Zone*> aZonesList;
361 HYDROGUI_Zone* aZone;
362 // Get a list of dropped zones
363 for ( int i = 0; i < theZonesList.length(); i++ )
365 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
368 aZonesList.append( aZone );
371 if ( aZonesList.length() > 0 )
373 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
374 HYDROGUI_CalculationDlg* aPanel =
375 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
378 aPanel->setEditedObject(myEditedObject);
384 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
386 HYDROGUI_CalculationDlg* aPanel =
387 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
390 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
393 aZone->setMergeType( theMergeType, theBathymetryName );
394 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
400 aPanel->refreshZonesBrowser();
404 void HYDROGUI_CalculationOp::onAddObjects()
406 HYDROGUI_CalculationDlg* aPanel =
407 ::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() )
416 QStringList anAddedList;
417 for (int i = 0; i < aSelectedList.length(); i++)
419 Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast(
420 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
421 if ( anObject.IsNull() )
424 if ( myEditedObject->AddGeometryObject( anObject ) )
425 anAddedList.append( anObject->GetName() );
428 if ( !anAddedList.isEmpty() )
430 aPanel->includeGeomObjects( anAddedList );
435 void HYDROGUI_CalculationOp::onRemoveObjects()
437 // Remove selected objects from the calculation case
438 HYDROGUI_CalculationDlg* aPanel =
439 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
443 QStringList aSelectedList = aPanel->getSelectedGeomObjects();
444 if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
447 for (int i = 0; i < aSelectedList.length(); i++)
449 Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast(
450 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
451 if ( anObject.IsNull() )
454 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
455 myEditedObject->RemoveGeometryObject( anObject );
458 aPanel->excludeGeomObjects( aSelectedList );
461 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
463 // Check if the case is already modified or not
464 bool isConfirmed = myEditedObject->IsMustBeUpdated();
467 // If not modified check if the case has already defined regions with zones
468 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
469 if ( aSeq.Length() > 0 )
471 // If there are already defined zones then ask a user to confirm zones recalculation
472 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
473 tr( "REGIONS_CHANGED" ),
474 tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
475 QMessageBox::Yes | QMessageBox::No,
476 QMessageBox::No ) == QMessageBox::Yes );
480 isConfirmed = true; // No regions - no zones - nothing to recalculate
486 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
487 QString& theErrorMsg )
489 HYDROGUI_CalculationDlg* aPanel =
490 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
494 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
499 void HYDROGUI_CalculationOp::onApply()
501 QApplication::setOverrideCursor( Qt::WaitCursor );
503 int anUpdateFlags = 0;
506 bool aResult = false;
510 aResult = processApply( anUpdateFlags, anErrorMsg );
512 catch ( Standard_Failure )
514 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
515 anErrorMsg = aFailure->GetMessageString();
523 QApplication::restoreOverrideCursor();
527 module()->update( anUpdateFlags );
533 QString aMsg = tr( "INPUT_VALID_DATA" );
534 if( !anErrorMsg.isEmpty() )
535 aMsg.prepend( anErrorMsg + "\n" );
536 SUIT_MessageBox::critical( module()->getApp()->desktop(),
537 tr( "INSUFFICIENT_INPUT_DATA" ),
542 void HYDROGUI_CalculationOp::onSplitZones()
544 HYDROGUI_CalculationDlg* aPanel =
545 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
549 QApplication::setOverrideCursor( Qt::WaitCursor );
551 QString aNewCaseName = aPanel->getObjectName();
552 QString anOldCaseName = myEditedObject->GetName();
554 bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
556 // At first we must to update the case name because of
557 // automatic names generation for regions and zones
558 myEditedObject->SetName( aNewCaseName );
560 if ( myEditedObject->IsMustBeUpdated() )
563 myEditedObject->SplitGeometryObjects();
565 aPanel->setEditedObject( myEditedObject );
571 setZonesVisible( true );
574 if ( anIsToUpdateOb )
575 module()->getApp()->updateObjectBrowser( false );
577 QApplication::restoreOverrideCursor();
580 void HYDROGUI_CalculationOp::onHideZones()
582 setZonesVisible( false );
585 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
587 myShowZones = theIsVisible;
588 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
589 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
590 HYDROData_SequenceOfObjects aZones;
591 Handle(HYDROData_Region) aRegion;
592 if ( myPreviewViewManager )
594 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
596 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
597 if ( !aCtx.IsNull() )
599 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
601 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
602 if ( !aRegion.IsNull() )
604 aZones = aRegion->GetZones();
605 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
606 for ( ; aZonesIter.More(); aZonesIter.Next() )
610 showObject( aZonesIter.Value(), aCtx );
614 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
624 void HYDROGUI_CalculationOp::createPreview()
626 LightApp_Application* anApp = module()->getApp();
627 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
628 Handle(HYDROData_Entity) anEntity;
632 // Gather zones for displaying
633 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
634 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
635 HYDROData_SequenceOfObjects aZones;
636 Handle(HYDROData_Region) aRegion;
637 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
639 anEntity = aRegionsIter.Value();
640 if ( !anEntity.IsNull() )
642 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
643 if ( !aRegion.IsNull() )
645 aZones = aRegion->GetZones();
646 aSeq.Append( aZones );
652 // Get a boundary polyline if any
653 aSeq.Append( myEditedObject->GetBoundaryPolyline() );
655 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
657 if ( !myActiveViewManager )
659 if ( aSeq.IsEmpty() )
662 myActiveViewManager = anApp->activeViewManager();
665 if ( !myPreviewViewManager )
667 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
668 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
669 if ( myPreviewViewManager )
671 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
672 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
674 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
675 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
679 if ( !myPreviewViewManager )
682 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
684 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
685 if ( !aCtx.IsNull() )
687 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
688 for ( ; anIter.More(); anIter.Next() )
690 showObject( anIter.Value(), aCtx );
693 //Process the draw events for viewer
694 QApplication::processEvents();
695 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
701 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
703 if ( !theEntity.IsNull() )
705 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
708 aShape = new HYDROGUI_Shape( theCtx, theEntity );
709 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
715 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
720 void HYDROGUI_CalculationOp::closePreview()
722 if( myPreviewViewManager )
724 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
725 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
727 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
728 myPreviewViewManager = NULL;
731 if( myActiveViewManager )
733 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
734 myActiveViewManager = NULL;