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 );
85 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
87 QStringList aSelectedObjects;
89 myEditedObject.Nullify();
92 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
93 if ( !myEditedObject.IsNull() )
95 anObjectName = myEditedObject->GetName();
96 aSeq = myEditedObject->GetGeometryObjects();
97 getNamesAndEntries( aSeq, aList, anEntryList );
98 aPanel->includeGeomObjects( aList );
104 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
105 myEditedObject->SetName(anObjectName);
108 aPanel->setObjectName( anObjectName );
109 aPanel->setEditedObject( myEditedObject );
114 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq,
115 QStringList& theNames, QStringList& theEntries ) const
117 Handle(HYDROData_Object) anObject;
118 Handle(HYDROData_Entity) anEntity;
121 HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
122 for ( ; anIter.More(); anIter.Next() )
124 anEntity = anIter.Value();
125 if ( !anEntity.IsNull() )
127 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
128 if ( !anObject.IsNull() )
130 theNames.append( anObject->GetName() );
131 theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anObject ) );
137 void HYDROGUI_CalculationOp::abortOperation()
142 HYDROGUI_Operation::abortOperation();
145 void HYDROGUI_CalculationOp::commitOperation()
148 // Commit transaction
149 commitDocOperation();
150 HYDROGUI_Operation::commitOperation();
153 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
155 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
157 // Connect signals and slots
158 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
159 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
160 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
161 connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
162 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
163 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
164 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
165 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
166 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
167 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
168 connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
169 SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
170 connect( aPanel, SIGNAL( objectSelected( const QString & ) ),
171 SLOT( onObjectSelected( const QString & ) ) );
176 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
178 // Select the appropriate geometry object shape in the viewer
179 selectionMgr()->clearSelected();
181 // Unhighlight all objects except selected
182 HYDROGUI_Shape* aShape;
183 HYDROGUI_Shape* aSelectedShape = 0;
184 Handle(HYDROData_Entity) anEntity;
185 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
186 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
189 for ( ; anIter.More(); anIter.Next() )
191 anEntity = anIter.Value();
192 if ( !anEntity.IsNull() )
194 aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
197 aName = anEntity->GetName();
198 isSelected = ( aName == theObjName );
201 aSelectedShape = aShape;
203 if ( aShape->isHighlighted() != isSelected )
207 aShape->highlight( isSelected );
214 if ( aSelectedShape )
216 aSelectedShape->highlight( true );
217 aSelectedShape->update();
221 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
223 HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
224 HYDROGUI_Zone* aZoneItem;
225 selectionMgr()->clearSelected();
228 // Select a region in preview
229 SUIT_DataOwnerPtrList aList( true );
230 DataObjectList aZones = aRegionItem->children();
231 for ( int i = 0; i < aZones.length(); i++ )
233 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
236 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
239 selectionMgr()->setSelected( aList );
243 // select a single zone
244 aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
247 SUIT_DataOwnerPtrList aList( true );
248 aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
249 selectionMgr()->setSelected( aList );
254 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
256 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
259 QList<HYDROGUI_Zone*> aZonesList;
260 HYDROGUI_Zone* aZone;
261 // Get a list of dropped zones
262 for ( int i = 0; i < theZonesList.length(); i++ )
264 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
267 aZonesList.append( aZone );
270 if ( aZonesList.length() > 0 )
272 aRegion->addZones( aZonesList );
273 HYDROGUI_CalculationDlg* aPanel =
274 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
277 aPanel->setEditedObject(myEditedObject);
284 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
286 QList<HYDROGUI_Zone*> aZonesList;
287 HYDROGUI_Zone* aZone;
288 // Get a list of dropped zones
289 for ( int i = 0; i < theZonesList.length(); i++ )
291 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
294 aZonesList.append( aZone );
297 if ( aZonesList.length() > 0 )
299 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
300 HYDROGUI_CalculationDlg* aPanel =
301 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
304 aPanel->setEditedObject(myEditedObject);
310 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
312 HYDROGUI_CalculationDlg* aPanel =
313 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
316 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
319 aZone->setMergeType( theMergeType, theBathymetryName );
320 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
326 aPanel->refreshZonesBrowser();
330 void HYDROGUI_CalculationOp::onAddObjects()
332 HYDROGUI_CalculationDlg* aPanel =
333 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
336 // Add geometry objects selected in the module browser to the calculation case
337 Handle(HYDROData_Object) anObject;
338 Handle(HYDROData_Entity) anEntity;
340 QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
341 for (int i = 0; i < aSelectedList.length(); i++)
343 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
344 if ( !anEntity.IsNull() )
346 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
347 if ( !anObject.IsNull() )
349 if (myEditedObject->AddGeometryObject( anObject ))
351 aList.append( anObject->GetName() );
356 if ( !aList.isEmpty() )
358 aPanel->includeGeomObjects( aList );
364 void HYDROGUI_CalculationOp::onRemoveObjects()
366 // Remove selected objects from the calculation case
367 HYDROGUI_CalculationDlg* aPanel =
368 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
371 QStringList aList = aPanel->getSelectedGeomObjects();
372 Handle(HYDROData_Object) anObject;
373 Handle(HYDROData_Entity) anEntity;
374 for (int i = 0; i < aList.length(); i++)
376 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
377 if ( !anEntity.IsNull() )
379 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
380 if ( !anObject.IsNull() )
382 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
383 myEditedObject->RemoveGeometryObject( anObject );
387 if ( !aList.isEmpty() )
389 aPanel->excludeGeomObjects( aList );
394 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
395 QString& theErrorMsg )
397 HYDROGUI_CalculationDlg* aPanel =
398 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
402 theUpdateFlags = UF_Model;
407 void HYDROGUI_CalculationOp::onApply()
409 QApplication::setOverrideCursor( Qt::WaitCursor );
411 int anUpdateFlags = 0;
414 bool aResult = false;
418 aResult = processApply( anUpdateFlags, anErrorMsg );
420 catch ( Standard_Failure )
422 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
423 anErrorMsg = aFailure->GetMessageString();
431 QApplication::restoreOverrideCursor();
435 module()->update( anUpdateFlags );
441 QString aMsg = tr( "INPUT_VALID_DATA" );
442 if( !anErrorMsg.isEmpty() )
443 aMsg.prepend( anErrorMsg + "\n" );
444 SUIT_MessageBox::critical( module()->getApp()->desktop(),
445 tr( "INSUFFICIENT_INPUT_DATA" ),
450 void HYDROGUI_CalculationOp::onSplitZones()
452 QApplication::setOverrideCursor( Qt::WaitCursor );
454 if ( myEditedObject->IsMustBeUpdated() )
457 myEditedObject->SplitGeometryObjects();
459 HYDROGUI_CalculationDlg* aPanel =
460 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
463 aPanel->setEditedObject( myEditedObject );
470 setZonesVisible( true );
473 QApplication::restoreOverrideCursor();
476 void HYDROGUI_CalculationOp::onHideZones()
478 setZonesVisible( false );
481 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
483 myShowZones = theIsVisible;
484 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
485 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
486 HYDROData_SequenceOfObjects aZones;
487 Handle(HYDROData_Region) aRegion;
488 if ( myPreviewViewManager )
490 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
492 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
493 if ( !aCtx.IsNull() )
495 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
497 aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
498 if ( !aRegion.IsNull() )
500 aZones = aRegion->GetZones();
501 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
502 for ( ; aZonesIter.More(); aZonesIter.Next() )
506 showObject( aZonesIter.Value(), aCtx );
510 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
520 void HYDROGUI_CalculationOp::createPreview()
522 LightApp_Application* anApp = module()->getApp();
523 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
524 Handle(HYDROData_Entity) anEntity;
528 // Gather zones for displaying
529 HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
530 HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
531 HYDROData_SequenceOfObjects aZones;
532 Handle(HYDROData_Region) aRegion;
533 for ( ; aRegionsIter.More(); aRegionsIter.Next() )
535 anEntity = aRegionsIter.Value();
536 if ( !anEntity.IsNull() )
538 aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
539 if ( !aRegion.IsNull() )
541 aZones = aRegion->GetZones();
542 aSeq.Append( aZones );
548 module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
550 if ( !myActiveViewManager )
552 if ( aSeq.IsEmpty() )
555 myActiveViewManager = anApp->activeViewManager();
558 if ( !myPreviewViewManager )
560 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
561 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
562 if ( myPreviewViewManager )
564 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
565 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
567 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
568 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
572 if ( !myPreviewViewManager )
575 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
577 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
578 if ( !aCtx.IsNull() )
580 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
581 for ( ; anIter.More(); anIter.Next() )
583 showObject( anIter.Value(), aCtx );
586 //Process the draw events for viewer
587 QApplication::processEvents();
588 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
594 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
596 if ( !theEntity.IsNull() )
598 HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
601 aShape = new HYDROGUI_Shape( theCtx, theEntity );
602 module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
608 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
613 void HYDROGUI_CalculationOp::closePreview()
615 if( myPreviewViewManager )
617 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
618 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
620 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
621 myPreviewViewManager = NULL;
624 if( myActiveViewManager )
626 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
627 myActiveViewManager = NULL;