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>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_Desktop.h>
48 #include <QApplication>
50 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
51 : HYDROGUI_Operation( theModule ),
52 myIsEdit( theIsEdit ),
53 myActiveViewManager( NULL ),
54 myPreviewViewManager( NULL )
56 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
59 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
64 void HYDROGUI_CalculationOp::startOperation()
66 HYDROGUI_Operation::startOperation();
71 HYDROGUI_CalculationDlg* aPanel =
72 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
76 myRegionsList.clear();
79 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
81 QStringList aSelectedObjects;
83 myEditedObject.Nullify();
86 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
87 if ( !myEditedObject.IsNull() )
89 anObjectName = myEditedObject->GetName();
90 updateGeomObjectsList(aPanel);
96 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
97 myEditedObject->SetName(anObjectName);
100 aPanel->setObjectName( anObjectName );
101 aPanel->setEditedObject( myEditedObject );
102 // aPanel->setSelectedGeomObjects( aSelectedObjects );
107 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
109 Handle(HYDROData_Object) anObject;
110 Handle(HYDROData_Entity) anEntity;
112 // Update the list in the dialog
113 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
114 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
115 for ( ; anIter.More(); anIter.Next() )
117 anEntity = anIter.Value();
118 if ( !anEntity.IsNull() )
120 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
121 if ( !anObject.IsNull() )
123 aList.append( anObject->GetName() );
127 thePanel->setSelectedGeomObjects( aList );
130 void HYDROGUI_CalculationOp::abortOperation()
135 HYDROGUI_Operation::abortOperation();
138 void HYDROGUI_CalculationOp::commitOperation()
141 // Commit transaction
142 commitDocOperation();
143 HYDROGUI_Operation::commitOperation();
146 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
148 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
150 // Connect signals and slots
151 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
152 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
153 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
154 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
155 connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
156 connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
157 SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
158 connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
159 SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
164 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
166 HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
169 QList<HYDROGUI_Zone*> aZonesList;
170 HYDROGUI_Zone* aZone;
171 // Get a list of dropped zones
172 for ( int i = 0; i < theZonesList.length(); i++ )
174 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
177 aZonesList.append( aZone );
180 if ( aZonesList.length() > 0 )
182 aRegion->addZones( aZonesList );
183 HYDROGUI_CalculationDlg* aPanel =
184 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
187 aPanel->setEditedObject(myEditedObject);
193 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
195 QList<HYDROGUI_Zone*> aZonesList;
196 HYDROGUI_Zone* aZone;
197 // Get a list of dropped zones
198 for ( int i = 0; i < theZonesList.length(); i++ )
200 aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
203 aZonesList.append( aZone );
206 if ( aZonesList.length() > 0 )
208 module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
209 HYDROGUI_CalculationDlg* aPanel =
210 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
213 aPanel->setEditedObject(myEditedObject);
218 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
220 HYDROGUI_CalculationDlg* aPanel =
221 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
224 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
227 aZone->setMergeType( theMergeType, theBathymetryName );
229 aPanel->refreshZonesBrowser();
233 void HYDROGUI_CalculationOp::onAddObjects()
235 // Add geometry objects selected in the module browser to the calculation case
236 Handle(HYDROData_Object) anObject;
237 Handle(HYDROData_Entity) anEntity;
239 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
240 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
242 anEntity = aSeq.Value( anIndex );
243 if ( !anEntity.IsNull() )
245 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
246 if( !anObject.IsNull() )
248 if (myEditedObject->AddGeometryObject( anObject ))
250 aList.append( anObject->GetName() );
255 HYDROGUI_CalculationDlg* aPanel =
256 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
259 updateGeomObjectsList( aPanel );
263 void HYDROGUI_CalculationOp::onRemoveObjects()
265 // Remove selected objects from the calculation case
266 HYDROGUI_CalculationDlg* aPanel =
267 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
270 QStringList aList = aPanel->getSelectedGeomObjects();
271 Handle(HYDROData_Object) anObject;
272 Handle(HYDROData_Entity) anEntity;
273 for (int i = 0; i < aList.length(); i++)
275 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
276 if ( !anEntity.IsNull() )
278 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
279 if ( !anObject.IsNull() )
281 myEditedObject->RemoveGeometryObject( anObject );
285 updateGeomObjectsList( aPanel );
289 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
290 QString& theErrorMsg )
292 HYDROGUI_CalculationDlg* aPanel =
293 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
297 //QString anObjectName = aPanel->getObjectName().simplified();
298 //if ( anObjectName.isEmpty() )
300 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
304 //// check that there are no other objects with the same name in the document
305 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
307 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
308 // if ( !anObject.IsNull() )
310 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
315 //Handle(HYDROData_Document) aDocument = doc();
317 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
318 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
319 //if ( aCalculObj.IsNull() )
322 //aCalculObj->SetName( anObjectName );
324 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
325 //HYDROData_SequenceOfObjects aGeomObjects =
326 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
328 theUpdateFlags = UF_Model;
333 void HYDROGUI_CalculationOp::onApply()
335 QApplication::setOverrideCursor( Qt::WaitCursor );
337 int anUpdateFlags = 0;
340 bool aResult = false;
344 aResult = processApply( anUpdateFlags, anErrorMsg );
346 catch ( Standard_Failure )
348 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
349 anErrorMsg = aFailure->GetMessageString();
357 QApplication::restoreOverrideCursor();
361 module()->update( anUpdateFlags );
367 QString aMsg = tr( "INPUT_VALID_DATA" );
368 if( !anErrorMsg.isEmpty() )
369 aMsg.prepend( anErrorMsg + "\n" );
370 SUIT_MessageBox::critical( module()->getApp()->desktop(),
371 tr( "INSUFFICIENT_INPUT_DATA" ),
376 void HYDROGUI_CalculationOp::onSplitZones()
378 //myRegionsList.clear();
380 HYDROGUI_CalculationDlg* aPanel =
381 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
385 QApplication::setOverrideCursor( Qt::WaitCursor );
387 //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
388 //HYDROData_SequenceOfObjects aGeomObjects =
389 // HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
391 //QStringList aResSplittedZones;
393 //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
394 // HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
396 //QStringList aUsedNames;
398 //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
399 //while( anIter.hasNext() )
402 // aRegion.SplitData = anIter.next();
404 // aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
405 // aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
407 // aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
409 // aUsedNames.append( aRegion.RegionName );
411 // aResSplittedZones.append( aRegion.RegionName );
413 // myRegionsList.append( aRegion );
417 myEditedObject->SplitGeometryObjects();
418 aPanel->setEditedObject( myEditedObject );
421 QApplication::restoreOverrideCursor();
424 void HYDROGUI_CalculationOp::createPreview()
426 LightApp_Application* anApp = module()->getApp();
428 if ( !myActiveViewManager )
430 if ( myRegionsList.isEmpty() )
433 myActiveViewManager = anApp->activeViewManager();
436 if ( !myPreviewViewManager )
438 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
439 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
440 if ( myPreviewViewManager )
442 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
443 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
445 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
446 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
450 if ( !myPreviewViewManager )
453 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
455 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
456 if ( !aCtx.IsNull() )
458 RegionsList::iterator anIter = myRegionsList.begin();
459 for ( ; anIter != myRegionsList.end(); ++anIter )
461 Region& aRegion = *anIter;
464 aRegion.Shape->erase( false );
465 delete aRegion.Shape;
468 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
470 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
471 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
472 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
475 //Process the draw events for viewer
476 QApplication::processEvents();
477 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
483 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
488 void HYDROGUI_CalculationOp::closePreview()
490 RegionsList::iterator anIter= myRegionsList.begin();
491 for ( ; anIter != myRegionsList.end(); ++anIter )
493 Region& aRegion = *anIter;
496 aRegion.Shape->erase( false );
497 delete aRegion.Shape;
498 aRegion.Shape = NULL;
502 if( myPreviewViewManager )
504 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
505 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
507 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
508 myPreviewViewManager = NULL;
511 if( myActiveViewManager )
513 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
514 myActiveViewManager = NULL;