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(), "Case" );
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 HYDROGUI_Shape* aShape;
213 Handle(AIS_InteractiveContext) aCtx;
214 if ( myPreviewViewManager )
216 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
218 aCtx = aViewer->getAISContext();
221 // Remove the old boundary from the operation viewer
222 anObject = myEditedObject->GetBoundaryPolyline();
223 if ( !anObject.IsNull() )
225 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
228 if ( theObjName.trimmed().isEmpty() )
230 // No polyline is selected
231 myEditedObject->RemoveBoundaryPolyline();
235 Handle(HYDROData_Entity) anEntity =
236 HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINE );
237 if ( !anEntity.IsNull() )
239 anObject = Handle(HYDROData_Polyline)::DownCast( anEntity );
240 if ( !anObject.IsNull() )
242 myEditedObject->SetBoundaryPolyline( anObject );
243 if ( !aCtx.IsNull() )
245 showObject( anEntity, aCtx );
252 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
254 // Select the appropriate geometry object shape in the viewer
255 selectionMgr()->clearSelected();
257 // Unhighlight all objects except selected
258 HYDROGUI_Shape* aShape;
259 HYDROGUI_Shape* aSelectedShape = 0;
260 Handle(HYDROData_Entity) anEntity;
261 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
262 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
265 for ( ; anIter.More(); anIter.Next() )
267 anEntity = anIter.Value();
268 if ( !anEntity.IsNull() )
270 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
273 aName = anEntity->GetName();
274 isSelected = ( aName == theObjName );
277 aSelectedShape = aShape;
279 if ( aShape->isHighlighted() != isSelected )
283 aShape->highlight( isSelected );
290 if ( aSelectedShape )
292 aSelectedShape->highlight( true );
293 aSelectedShape->update();
297 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
299 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
300 HYDROGUI_Zone* aZoneItem;
301 selectionMgr()->clearSelected();
304 // Select a region in preview
305 SUIT_DataOwnerPtrList aList( true );
306 DataObjectList aZones = aRegionItem->children();
307 for ( int i = 0; i < aZones.length(); i++ )
309 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
312 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
315 selectionMgr()->setSelected( aList );
319 // select a single zone
320 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
323 SUIT_DataOwnerPtrList aList( true );
324 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
325 selectionMgr()->setSelected( aList );
330 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
332 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
335 QList<HYDROGUI_Zone*> aZonesList;
336 HYDROGUI_Zone* aZone;
337 // Get a list of dropped zones
338 for ( int i = 0; i < theZonesList.length(); i++ )
340 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
343 aZonesList.append( aZone );
346 if ( aZonesList.length() > 0 )
348 aRegion->addZones( aZonesList );
349 HYDROGUI_CalculationDlg* aPanel =
350 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
353 aPanel->setEditedObject(myEditedObject);
360 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
362 QList<HYDROGUI_Zone*> aZonesList;
363 HYDROGUI_Zone* aZone;
364 // Get a list of dropped zones
365 for ( int i = 0; i < theZonesList.length(); i++ )
367 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
370 aZonesList.append( aZone );
373 if ( aZonesList.length() > 0 )
375 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
376 HYDROGUI_CalculationDlg* aPanel =
377 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
380 aPanel->setEditedObject(myEditedObject);
386 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
388 HYDROGUI_CalculationDlg* aPanel =
389 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
392 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
395 aZone->setMergeType( theMergeType, theBathymetryName );
396 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
402 aPanel->refreshZonesBrowser();
406 void HYDROGUI_CalculationOp::onAddObjects()
408 HYDROGUI_CalculationDlg* aPanel =
409 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
412 // Add geometry objects selected in the module browser to the calculation case
413 Handle(HYDROData_Object) anObject;
414 Handle(HYDROData_Entity) anEntity;
416 QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
417 for (int i = 0; i < aSelectedList.length(); i++)
419 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
420 if ( !anEntity.IsNull() )
422 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
423 if ( !anObject.IsNull() )
425 if (myEditedObject->AddGeometryObject( anObject ))
427 aList.append( anObject->GetName() );
432 if ( !aList.isEmpty() )
434 aPanel->includeGeomObjects( aList );
440 void HYDROGUI_CalculationOp::onRemoveObjects()
442 // Remove selected objects from the calculation case
443 HYDROGUI_CalculationDlg* aPanel =
444 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
447 QStringList aList = aPanel->getSelectedGeomObjects();
448 Handle(HYDROData_Object) anObject;
449 Handle(HYDROData_Entity) anEntity;
450 for (int i = 0; i < aList.length(); i++)
452 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
453 if ( !anEntity.IsNull() )
455 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
456 if ( !anObject.IsNull() )
458 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
459 myEditedObject->RemoveGeometryObject( anObject );
463 if ( !aList.isEmpty() )
465 aPanel->excludeGeomObjects( aList );
470 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
471 QString& theErrorMsg )
473 HYDROGUI_CalculationDlg* aPanel =
474 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
478 theUpdateFlags = UF_Model;
483 void HYDROGUI_CalculationOp::onApply()
485 QApplication::setOverrideCursor( Qt::WaitCursor );
487 int anUpdateFlags = 0;
490 bool aResult = false;
494 aResult = processApply( anUpdateFlags, anErrorMsg );
496 catch ( Standard_Failure )
498 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
499 anErrorMsg = aFailure->GetMessageString();
507 QApplication::restoreOverrideCursor();
511 module()->update( anUpdateFlags );
517 QString aMsg = tr( "INPUT_VALID_DATA" );
518 if( !anErrorMsg.isEmpty() )
519 aMsg.prepend( anErrorMsg + "\n" );
520 SUIT_MessageBox::critical( module()->getApp()->desktop(),
521 tr( "INSUFFICIENT_INPUT_DATA" ),
526 void HYDROGUI_CalculationOp::onSplitZones()
528 QApplication::setOverrideCursor( Qt::WaitCursor );
530 if ( myEditedObject->IsMustBeUpdated() )
533 myEditedObject->SplitGeometryObjects();
535 HYDROGUI_CalculationDlg* aPanel =
536 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
539 aPanel->setEditedObject( myEditedObject );
546 setZonesVisible( true );
549 QApplication::restoreOverrideCursor();
552 void HYDROGUI_CalculationOp::onHideZones()
554 setZonesVisible( false );
557 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
559 myShowZones = theIsVisible;
560 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
561 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
562 HYDROData_SequenceOfObjects aZones;
563 Handle(HYDROData_Region) aRegion;
564 if ( myPreviewViewManager )
566 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
568 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
569 if ( !aCtx.IsNull() )
571 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
573 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
574 if ( !aRegion.IsNull() )
576 aZones = aRegion->GetZones();
577 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
578 for ( ; aZonesIter.More(); aZonesIter.Next() )
582 showObject( aZonesIter.Value(), aCtx );
586 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
596 void HYDROGUI_CalculationOp::createPreview()
598 LightApp_Application* anApp = module()->getApp();
599 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
600 Handle(HYDROData_Entity) anEntity;
604 // Gather zones for displaying
605 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
606 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
607 HYDROData_SequenceOfObjects aZones;
608 Handle(HYDROData_Region) aRegion;
609 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
611 anEntity = aRegionsIter.Value();
612 if ( !anEntity.IsNull() )
614 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
615 if ( !aRegion.IsNull() )
617 aZones = aRegion->GetZones();
618 aSeq.Append( aZones );
624 // Get a boundary polyline if any
625 aSeq.Append( myEditedObject->GetBoundaryPolyline() );
627 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
629 if ( !myActiveViewManager )
631 if ( aSeq.IsEmpty() )
634 myActiveViewManager = anApp->activeViewManager();
637 if ( !myPreviewViewManager )
639 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
640 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
641 if ( myPreviewViewManager )
643 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
644 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
646 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
647 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
651 if ( !myPreviewViewManager )
654 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
656 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
657 if ( !aCtx.IsNull() )
659 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
660 for ( ; anIter.More(); anIter.Next() )
662 showObject( anIter.Value(), aCtx );
665 //Process the draw events for viewer
666 QApplication::processEvents();
667 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
673 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
675 if ( !theEntity.IsNull() )
677 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
680 aShape = new HYDROGUI_Shape( theCtx, theEntity );
681 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
687 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
692 void HYDROGUI_CalculationOp::closePreview()
694 if( myPreviewViewManager )
696 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
697 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
699 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
700 myPreviewViewManager = NULL;
703 if( myActiveViewManager )
705 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
706 myActiveViewManager = NULL;