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"
32 #include <HYDROData_Polyline.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_ImmersibleZone.h>
35 #include <HYDROData_Object.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_UpdateFlags.h>
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_Desktop.h>
47 #include <QApplication>
49 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51 myIsEdit( theIsEdit ),
52 myActiveViewManager( NULL ),
53 myPreviewViewManager( NULL )
55 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
58 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
63 void HYDROGUI_CalculationOp::startOperation()
65 HYDROGUI_Operation::startOperation();
70 HYDROGUI_CalculationDlg* aPanel =
71 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
75 myRegionsList.clear();
78 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
80 QStringList aSelectedObjects;
82 myEditedObject.Nullify();
85 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
86 if ( !myEditedObject.IsNull() )
88 anObjectName = myEditedObject->GetName();
89 updateGeomObjectsList(aPanel);
95 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
96 myEditedObject->SetName(anObjectName);
99 aPanel->setObjectName( anObjectName );
100 aPanel->setEditedObject( myEditedObject );
101 // aPanel->setSelectedGeomObjects( aSelectedObjects );
106 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
108 Handle(HYDROData_Object) anObject;
109 Handle(HYDROData_Entity) anEntity;
111 // Update the list in the dialog
112 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
113 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
114 for ( ; anIter.More(); anIter.Next() )
116 anEntity = anIter.Value();
117 if ( !anEntity.IsNull() )
119 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
120 if ( !anObject.IsNull() )
122 aList.append( anObject->GetName() );
126 thePanel->setSelectedGeomObjects( aList );
129 void HYDROGUI_CalculationOp::abortOperation()
134 HYDROGUI_Operation::abortOperation();
137 void HYDROGUI_CalculationOp::commitOperation()
140 // Commit transaction
141 commitDocOperation();
142 HYDROGUI_Operation::commitOperation();
145 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
147 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
149 // Connect signals and slots
150 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
151 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
152 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
153 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
154 connect( aPanel, SIGNAL( setMergeType( int, QString ) ), SLOT( onSetMergeType( int, QString ) ) );
159 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString theBathymetryName )
161 HYDROGUI_CalculationDlg* aPanel =
162 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
165 HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
168 aZone->setMergeType( theMergeType, theBathymetryName );
173 void HYDROGUI_CalculationOp::onAddObjects()
175 // Add geometry objects selected in the module browser to the calculation case
176 Handle(HYDROData_Object) anObject;
177 Handle(HYDROData_Entity) anEntity;
179 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
180 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
182 anEntity = aSeq.Value( anIndex );
183 if ( !anEntity.IsNull() )
185 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
186 if( !anObject.IsNull() )
188 if (myEditedObject->AddGeometryObject( anObject ))
190 aList.append( anObject->GetName() );
195 HYDROGUI_CalculationDlg* aPanel =
196 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
199 updateGeomObjectsList( aPanel );
203 void HYDROGUI_CalculationOp::onRemoveObjects()
205 // Remove selected objects from the calculation case
206 HYDROGUI_CalculationDlg* aPanel =
207 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
210 QStringList aList = aPanel->getSelectedGeomObjects();
211 Handle(HYDROData_Object) anObject;
212 Handle(HYDROData_Entity) anEntity;
213 for (int i = 0; i < aList.length(); i++)
215 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
216 if ( !anEntity.IsNull() )
218 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
219 if ( !anObject.IsNull() )
221 myEditedObject->RemoveGeometryObject( anObject );
225 updateGeomObjectsList( aPanel );
229 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
230 QString& theErrorMsg )
232 HYDROGUI_CalculationDlg* aPanel =
233 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
237 //QString anObjectName = aPanel->getObjectName().simplified();
238 //if ( anObjectName.isEmpty() )
240 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
244 //// check that there are no other objects with the same name in the document
245 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
247 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
248 // if ( !anObject.IsNull() )
250 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
255 //Handle(HYDROData_Document) aDocument = doc();
257 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
258 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
259 //if ( aCalculObj.IsNull() )
262 //aCalculObj->SetName( anObjectName );
264 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
265 //HYDROData_SequenceOfObjects aGeomObjects =
266 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
268 theUpdateFlags = UF_Model;
273 void HYDROGUI_CalculationOp::onApply()
275 QApplication::setOverrideCursor( Qt::WaitCursor );
277 int anUpdateFlags = 0;
280 bool aResult = false;
284 aResult = processApply( anUpdateFlags, anErrorMsg );
286 catch ( Standard_Failure )
288 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
289 anErrorMsg = aFailure->GetMessageString();
297 QApplication::restoreOverrideCursor();
301 module()->update( anUpdateFlags );
307 QString aMsg = tr( "INPUT_VALID_DATA" );
308 if( !anErrorMsg.isEmpty() )
309 aMsg.prepend( anErrorMsg + "\n" );
310 SUIT_MessageBox::critical( module()->getApp()->desktop(),
311 tr( "INSUFFICIENT_INPUT_DATA" ),
316 void HYDROGUI_CalculationOp::onSplitZones()
318 //myRegionsList.clear();
320 HYDROGUI_CalculationDlg* aPanel =
321 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
325 QApplication::setOverrideCursor( Qt::WaitCursor );
327 //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
328 //HYDROData_SequenceOfObjects aGeomObjects =
329 // HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
331 //QStringList aResSplittedZones;
333 //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
334 // HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
336 //QStringList aUsedNames;
338 //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
339 //while( anIter.hasNext() )
342 // aRegion.SplitData = anIter.next();
344 // aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
345 // aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
347 // aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
349 // aUsedNames.append( aRegion.RegionName );
351 // aResSplittedZones.append( aRegion.RegionName );
353 // myRegionsList.append( aRegion );
357 myEditedObject->SplitGeometryObjects();
358 aPanel->setEditedObject( myEditedObject );
361 QApplication::restoreOverrideCursor();
364 void HYDROGUI_CalculationOp::createPreview()
366 LightApp_Application* anApp = module()->getApp();
368 if ( !myActiveViewManager )
370 if ( myRegionsList.isEmpty() )
373 myActiveViewManager = anApp->activeViewManager();
376 if ( !myPreviewViewManager )
378 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
379 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
380 if ( myPreviewViewManager )
382 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
383 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
385 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
386 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
390 if ( !myPreviewViewManager )
393 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
395 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
396 if ( !aCtx.IsNull() )
398 RegionsList::iterator anIter = myRegionsList.begin();
399 for ( ; anIter != myRegionsList.end(); ++anIter )
401 Region& aRegion = *anIter;
404 aRegion.Shape->erase( false );
405 delete aRegion.Shape;
408 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
410 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
411 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
412 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
415 //Process the draw events for viewer
416 QApplication::processEvents();
417 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
423 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
428 void HYDROGUI_CalculationOp::closePreview()
430 RegionsList::iterator anIter= myRegionsList.begin();
431 for ( ; anIter != myRegionsList.end(); ++anIter )
433 Region& aRegion = *anIter;
436 aRegion.Shape->erase( false );
437 delete aRegion.Shape;
438 aRegion.Shape = NULL;
442 if( myPreviewViewManager )
444 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
445 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
447 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
448 myPreviewViewManager = NULL;
451 if( myActiveViewManager )
453 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
454 myActiveViewManager = NULL;