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( Next( const int ) ), SLOT( onSplitZones() ) );
190 connect( aPanel, SIGNAL( Back( const int ) ), 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 bool anIsToUpdateViewer = false;
211 // Remove the old boundary from the operation viewer
212 Handle(HYDROData_PolylineXY) aPrevPolyline =
213 myEditedObject->GetBoundaryPolyline();
214 if ( !aPrevPolyline.IsNull() )
216 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aPrevPolyline );
217 anIsToUpdateViewer = true;
220 // Set the selected boundary polyline to the calculation case
221 Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
222 HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
223 myEditedObject->SetBoundaryPolyline( aNewPolyline );
225 if ( myPreviewViewManager )
227 OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
230 if ( !aNewPolyline.IsNull() )
232 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
233 if ( !aCtx.IsNull() )
235 showObject( aNewPolyline, aCtx );
236 anIsToUpdateViewer = true;
240 if ( anIsToUpdateViewer )
246 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
248 // Select the appropriate geometry object shape in the viewer
249 selectionMgr()->clearSelected();
251 // Unhighlight all objects except selected
252 HYDROGUI_Shape* aShape;
253 HYDROGUI_Shape* aSelectedShape = 0;
254 Handle(HYDROData_Entity) anEntity;
255 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
256 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
259 for ( ; anIter.More(); anIter.Next() )
261 anEntity = anIter.Value();
262 if ( !anEntity.IsNull() )
264 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
267 aName = anEntity->GetName();
268 isSelected = ( aName == theObjName );
271 aSelectedShape = aShape;
273 if ( aShape->isHighlighted() != isSelected )
277 aShape->highlight( isSelected );
284 if ( aSelectedShape )
286 aSelectedShape->highlight( true );
287 aSelectedShape->update();
291 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
293 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
294 HYDROGUI_Zone* aZoneItem;
295 selectionMgr()->clearSelected();
298 // Select a region in preview
299 SUIT_DataOwnerPtrList aList( true );
300 DataObjectList aZones = aRegionItem->children();
301 for ( int i = 0; i < aZones.length(); i++ )
303 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
306 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
309 selectionMgr()->setSelected( aList );
313 // select a single zone
314 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
317 SUIT_DataOwnerPtrList aList( true );
318 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
319 selectionMgr()->setSelected( aList );
324 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
326 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
329 QList<HYDROGUI_Zone*> aZonesList;
330 HYDROGUI_Zone* aZone;
331 // Get a list of dropped zones
332 for ( int i = 0; i < theZonesList.length(); i++ )
334 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
337 aZonesList.append( aZone );
340 if ( aZonesList.length() > 0 )
342 aRegion->addZones( aZonesList );
343 HYDROGUI_CalculationDlg* aPanel =
344 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
347 aPanel->setEditedObject(myEditedObject);
354 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
356 QList<HYDROGUI_Zone*> aZonesList;
357 HYDROGUI_Zone* aZone;
358 // Get a list of dropped zones
359 for ( int i = 0; i < theZonesList.length(); i++ )
361 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
364 aZonesList.append( aZone );
367 if ( aZonesList.length() > 0 )
369 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
370 HYDROGUI_CalculationDlg* aPanel =
371 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
374 aPanel->setEditedObject(myEditedObject);
380 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
382 HYDROGUI_CalculationDlg* aPanel =
383 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
386 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
389 aZone->setMergeType( theMergeType, theBathymetryName );
390 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
396 aPanel->refreshZonesBrowser();
400 void HYDROGUI_CalculationOp::onAddObjects()
402 HYDROGUI_CalculationDlg* aPanel =
403 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
407 // Add geometry objects selected in the module browser to the calculation case
408 QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
409 if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
412 QStringList anAddedList;
413 for (int i = 0; i < aSelectedList.length(); i++)
415 Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast(
416 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
417 if ( anObject.IsNull() )
420 if ( myEditedObject->AddGeometryObject( anObject ) )
421 anAddedList.append( anObject->GetName() );
424 if ( !anAddedList.isEmpty() )
426 aPanel->includeGeomObjects( anAddedList );
431 void HYDROGUI_CalculationOp::onRemoveObjects()
433 // Remove selected objects from the calculation case
434 HYDROGUI_CalculationDlg* aPanel =
435 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
439 QStringList aSelectedList = aPanel->getSelectedGeomObjects();
440 if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
443 for (int i = 0; i < aSelectedList.length(); i++)
445 Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast(
446 HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
447 if ( anObject.IsNull() )
450 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
451 myEditedObject->RemoveGeometryObject( anObject );
454 aPanel->excludeGeomObjects( aSelectedList );
457 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
459 // Check if the case is already modified or not
460 bool isConfirmed = myEditedObject->IsMustBeUpdated();
463 // If not modified check if the case has already defined regions with zones
464 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
465 if ( aSeq.Length() > 0 )
467 // If there are already defined zones then ask a user to confirm zones recalculation
468 isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
469 tr( "REGIONS_CHANGED" ),
470 tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
471 QMessageBox::Yes | QMessageBox::No,
472 QMessageBox::No ) == QMessageBox::Yes );
476 isConfirmed = true; // No regions - no zones - nothing to recalculate
482 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
483 QString& theErrorMsg )
485 HYDROGUI_CalculationDlg* aPanel =
486 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
490 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
495 void HYDROGUI_CalculationOp::onApply()
497 QApplication::setOverrideCursor( Qt::WaitCursor );
499 int anUpdateFlags = 0;
502 bool aResult = false;
506 aResult = processApply( anUpdateFlags, anErrorMsg );
508 catch ( Standard_Failure )
510 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
511 anErrorMsg = aFailure->GetMessageString();
519 QApplication::restoreOverrideCursor();
523 module()->update( anUpdateFlags );
529 QString aMsg = tr( "INPUT_VALID_DATA" );
530 if( !anErrorMsg.isEmpty() )
531 aMsg.prepend( anErrorMsg + "\n" );
532 SUIT_MessageBox::critical( module()->getApp()->desktop(),
533 tr( "INSUFFICIENT_INPUT_DATA" ),
538 void HYDROGUI_CalculationOp::onSplitZones()
540 HYDROGUI_CalculationDlg* aPanel =
541 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
545 QApplication::setOverrideCursor( Qt::WaitCursor );
547 QString aNewCaseName = aPanel->getObjectName();
548 QString anOldCaseName = myEditedObject->GetName();
550 bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
552 // At first we must to update the case name because of
553 // automatic names generation for regions and zones
554 myEditedObject->SetName( aNewCaseName );
556 if ( myEditedObject->IsMustBeUpdated() )
559 myEditedObject->SplitGeometryObjects();
561 aPanel->setEditedObject( myEditedObject );
567 setZonesVisible( true );
570 if ( anIsToUpdateOb )
571 module()->getApp()->updateObjectBrowser( false );
573 QApplication::restoreOverrideCursor();
576 void HYDROGUI_CalculationOp::onHideZones()
578 setZonesVisible( false );
581 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
583 myShowZones = theIsVisible;
584 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
585 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
586 HYDROData_SequenceOfObjects aZones;
587 Handle(HYDROData_Region) aRegion;
588 if ( myPreviewViewManager )
590 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
592 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
593 if ( !aCtx.IsNull() )
595 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
597 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
598 if ( !aRegion.IsNull() )
600 aZones = aRegion->GetZones();
601 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
602 for ( ; aZonesIter.More(); aZonesIter.Next() )
606 showObject( aZonesIter.Value(), aCtx );
610 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
622 void HYDROGUI_CalculationOp::createPreview()
624 LightApp_Application* anApp = module()->getApp();
625 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
626 Handle(HYDROData_Entity) anEntity;
630 // Gather zones for displaying
631 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
632 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
633 HYDROData_SequenceOfObjects aZones;
634 Handle(HYDROData_Region) aRegion;
635 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
637 anEntity = aRegionsIter.Value();
638 if ( !anEntity.IsNull() )
640 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
641 if ( !aRegion.IsNull() )
643 aZones = aRegion->GetZones();
644 aSeq.Append( aZones );
650 // Get a boundary polyline if any
651 aSeq.Append( myEditedObject->GetBoundaryPolyline() );
653 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
655 if ( !myActiveViewManager )
657 if ( aSeq.IsEmpty() )
660 myActiveViewManager = anApp->activeViewManager();
663 if ( !myPreviewViewManager )
665 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
666 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
667 if ( myPreviewViewManager )
669 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
670 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
672 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
673 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
677 if ( !myPreviewViewManager )
680 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
682 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
683 if ( !aCtx.IsNull() )
685 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
686 for ( ; anIter.More(); anIter.Next() )
688 showObject( anIter.Value(), aCtx );
691 //Process the draw events for viewer
692 QApplication::processEvents();
693 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 );
711 aShape->update( false );
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;