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 );
170 aPanel->refreshZonesBrowser();
174 void HYDROGUI_CalculationOp::onAddObjects()
176 // Add geometry objects selected in the module browser to the calculation case
177 Handle(HYDROData_Object) anObject;
178 Handle(HYDROData_Entity) anEntity;
180 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
181 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
183 anEntity = aSeq.Value( anIndex );
184 if ( !anEntity.IsNull() )
186 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
187 if( !anObject.IsNull() )
189 if (myEditedObject->AddGeometryObject( anObject ))
191 aList.append( anObject->GetName() );
196 HYDROGUI_CalculationDlg* aPanel =
197 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
200 updateGeomObjectsList( aPanel );
204 void HYDROGUI_CalculationOp::onRemoveObjects()
206 // Remove selected objects from the calculation case
207 HYDROGUI_CalculationDlg* aPanel =
208 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
211 QStringList aList = aPanel->getSelectedGeomObjects();
212 Handle(HYDROData_Object) anObject;
213 Handle(HYDROData_Entity) anEntity;
214 for (int i = 0; i < aList.length(); i++)
216 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
217 if ( !anEntity.IsNull() )
219 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
220 if ( !anObject.IsNull() )
222 myEditedObject->RemoveGeometryObject( anObject );
226 updateGeomObjectsList( aPanel );
230 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
231 QString& theErrorMsg )
233 HYDROGUI_CalculationDlg* aPanel =
234 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
238 //QString anObjectName = aPanel->getObjectName().simplified();
239 //if ( anObjectName.isEmpty() )
241 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
245 //// check that there are no other objects with the same name in the document
246 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
248 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
249 // if ( !anObject.IsNull() )
251 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
256 //Handle(HYDROData_Document) aDocument = doc();
258 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
259 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
260 //if ( aCalculObj.IsNull() )
263 //aCalculObj->SetName( anObjectName );
265 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
266 //HYDROData_SequenceOfObjects aGeomObjects =
267 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
269 theUpdateFlags = UF_Model;
274 void HYDROGUI_CalculationOp::onApply()
276 QApplication::setOverrideCursor( Qt::WaitCursor );
278 int anUpdateFlags = 0;
281 bool aResult = false;
285 aResult = processApply( anUpdateFlags, anErrorMsg );
287 catch ( Standard_Failure )
289 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
290 anErrorMsg = aFailure->GetMessageString();
298 QApplication::restoreOverrideCursor();
302 module()->update( anUpdateFlags );
308 QString aMsg = tr( "INPUT_VALID_DATA" );
309 if( !anErrorMsg.isEmpty() )
310 aMsg.prepend( anErrorMsg + "\n" );
311 SUIT_MessageBox::critical( module()->getApp()->desktop(),
312 tr( "INSUFFICIENT_INPUT_DATA" ),
317 void HYDROGUI_CalculationOp::onSplitZones()
319 //myRegionsList.clear();
321 HYDROGUI_CalculationDlg* aPanel =
322 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
326 QApplication::setOverrideCursor( Qt::WaitCursor );
328 //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
329 //HYDROData_SequenceOfObjects aGeomObjects =
330 // HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
332 //QStringList aResSplittedZones;
334 //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
335 // HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
337 //QStringList aUsedNames;
339 //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
340 //while( anIter.hasNext() )
343 // aRegion.SplitData = anIter.next();
345 // aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
346 // aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
348 // aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
350 // aUsedNames.append( aRegion.RegionName );
352 // aResSplittedZones.append( aRegion.RegionName );
354 // myRegionsList.append( aRegion );
358 myEditedObject->SplitGeometryObjects();
359 aPanel->setEditedObject( myEditedObject );
362 QApplication::restoreOverrideCursor();
365 void HYDROGUI_CalculationOp::createPreview()
367 LightApp_Application* anApp = module()->getApp();
369 if ( !myActiveViewManager )
371 if ( myRegionsList.isEmpty() )
374 myActiveViewManager = anApp->activeViewManager();
377 if ( !myPreviewViewManager )
379 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
380 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
381 if ( myPreviewViewManager )
383 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
384 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
386 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
387 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
391 if ( !myPreviewViewManager )
394 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
396 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
397 if ( !aCtx.IsNull() )
399 RegionsList::iterator anIter = myRegionsList.begin();
400 for ( ; anIter != myRegionsList.end(); ++anIter )
402 Region& aRegion = *anIter;
405 aRegion.Shape->erase( false );
406 delete aRegion.Shape;
409 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
411 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
412 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
413 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
416 //Process the draw events for viewer
417 QApplication::processEvents();
418 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
424 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
429 void HYDROGUI_CalculationOp::closePreview()
431 RegionsList::iterator anIter= myRegionsList.begin();
432 for ( ; anIter != myRegionsList.end(); ++anIter )
434 Region& aRegion = *anIter;
437 aRegion.Shape->erase( false );
438 delete aRegion.Shape;
439 aRegion.Shape = NULL;
443 if( myPreviewViewManager )
445 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
446 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
448 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
449 myPreviewViewManager = NULL;
452 if( myActiveViewManager )
454 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
455 myActiveViewManager = NULL;