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"
31 #include <HYDROData_Polyline.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_ImmersibleZone.h>
34 #include <HYDROData_Object.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_Desktop.h>
46 #include <QApplication>
48 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
49 : HYDROGUI_Operation( theModule ),
50 myIsEdit( theIsEdit ),
51 myActiveViewManager( NULL ),
52 myPreviewViewManager( NULL )
54 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
57 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
62 void HYDROGUI_CalculationOp::startOperation()
64 HYDROGUI_Operation::startOperation();
69 HYDROGUI_CalculationDlg* aPanel =
70 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
74 myRegionsList.clear();
77 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
79 QStringList aSelectedObjects;
81 myEditedObject.Nullify();
84 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
85 if ( !myEditedObject.IsNull() )
87 anObjectName = myEditedObject->GetName();
88 updateGeomObjectsList(aPanel);
94 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
95 myEditedObject->SetName(anObjectName);
98 aPanel->setObjectName( anObjectName );
99 aPanel->setEditedObject( myEditedObject );
100 // aPanel->setSelectedGeomObjects( aSelectedObjects );
105 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
107 Handle(HYDROData_Object) anObject;
108 Handle(HYDROData_Entity) anEntity;
110 // Update the list in the dialog
111 HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
112 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
113 for ( ; anIter.More(); anIter.Next() )
115 anEntity = anIter.Value();
116 if ( !anEntity.IsNull() )
118 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
119 if ( !anObject.IsNull() )
121 aList.append( anObject->GetName() );
125 thePanel->setSelectedGeomObjects( aList );
128 void HYDROGUI_CalculationOp::abortOperation()
133 HYDROGUI_Operation::abortOperation();
136 void HYDROGUI_CalculationOp::commitOperation()
139 // Commit transaction
140 commitDocOperation();
141 HYDROGUI_Operation::commitOperation();
144 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
146 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
148 // Connect signals and slots
149 connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
150 connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
151 connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
156 void HYDROGUI_CalculationOp::onAddObjects()
158 // Add geometry objects selected in the module browser to the calculation case
159 Handle(HYDROData_Object) anObject;
160 Handle(HYDROData_Entity) anEntity;
162 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
163 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
165 anEntity = aSeq.Value( anIndex );
166 if ( !anEntity.IsNull() )
168 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
169 if( !anObject.IsNull() )
171 if (myEditedObject->AddGeometryObject( anObject ))
173 aList.append( anObject->GetName() );
178 HYDROGUI_CalculationDlg* aPanel =
179 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
182 updateGeomObjectsList( aPanel );
186 void HYDROGUI_CalculationOp::onRemoveObjects()
188 // Remove selected objects from the calculation case
189 HYDROGUI_CalculationDlg* aPanel =
190 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
193 QStringList aList = aPanel->getSelectedGeomObjects();
194 Handle(HYDROData_Object) anObject;
195 Handle(HYDROData_Entity) anEntity;
196 for (int i = 0; i < aList.length(); i++)
198 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
199 if ( !anEntity.IsNull() )
201 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
202 if ( !anObject.IsNull() )
204 myEditedObject->RemoveGeometryObject( anObject );
208 updateGeomObjectsList( aPanel );
212 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
213 QString& theErrorMsg )
215 HYDROGUI_CalculationDlg* aPanel =
216 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
220 //QString anObjectName = aPanel->getObjectName().simplified();
221 //if ( anObjectName.isEmpty() )
223 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
227 //// check that there are no other objects with the same name in the document
228 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
230 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
231 // if ( !anObject.IsNull() )
233 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
238 //Handle(HYDROData_Document) aDocument = doc();
240 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
241 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
242 //if ( aCalculObj.IsNull() )
245 //aCalculObj->SetName( anObjectName );
247 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
248 //HYDROData_SequenceOfObjects aGeomObjects =
249 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
251 theUpdateFlags = UF_Model;
256 void HYDROGUI_CalculationOp::onApply()
258 QApplication::setOverrideCursor( Qt::WaitCursor );
260 int anUpdateFlags = 0;
263 bool aResult = false;
267 aResult = processApply( anUpdateFlags, anErrorMsg );
269 catch ( Standard_Failure )
271 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
272 anErrorMsg = aFailure->GetMessageString();
280 QApplication::restoreOverrideCursor();
284 module()->update( anUpdateFlags );
290 QString aMsg = tr( "INPUT_VALID_DATA" );
291 if( !anErrorMsg.isEmpty() )
292 aMsg.prepend( anErrorMsg + "\n" );
293 SUIT_MessageBox::critical( module()->getApp()->desktop(),
294 tr( "INSUFFICIENT_INPUT_DATA" ),
299 void HYDROGUI_CalculationOp::onSplitZones()
301 //myRegionsList.clear();
303 HYDROGUI_CalculationDlg* aPanel =
304 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
308 QApplication::setOverrideCursor( Qt::WaitCursor );
310 //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
311 //HYDROData_SequenceOfObjects aGeomObjects =
312 // HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
314 //QStringList aResSplittedZones;
316 //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
317 // HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
319 //QStringList aUsedNames;
321 //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
322 //while( anIter.hasNext() )
325 // aRegion.SplitData = anIter.next();
327 // aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
328 // aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
330 // aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
332 // aUsedNames.append( aRegion.RegionName );
334 // aResSplittedZones.append( aRegion.RegionName );
336 // myRegionsList.append( aRegion );
340 myEditedObject->SplitGeometryObjects();
341 aPanel->setEditedObject( myEditedObject );
344 QApplication::restoreOverrideCursor();
347 void HYDROGUI_CalculationOp::createPreview()
349 LightApp_Application* anApp = module()->getApp();
351 if ( !myActiveViewManager )
353 if ( myRegionsList.isEmpty() )
356 myActiveViewManager = anApp->activeViewManager();
359 if ( !myPreviewViewManager )
361 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
362 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
363 if ( myPreviewViewManager )
365 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
366 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
368 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
369 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
373 if ( !myPreviewViewManager )
376 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
378 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
379 if ( !aCtx.IsNull() )
381 RegionsList::iterator anIter = myRegionsList.begin();
382 for ( ; anIter != myRegionsList.end(); ++anIter )
384 Region& aRegion = *anIter;
387 aRegion.Shape->erase( false );
388 delete aRegion.Shape;
391 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
393 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
394 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
395 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
398 //Process the draw events for viewer
399 QApplication::processEvents();
400 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
406 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
411 void HYDROGUI_CalculationOp::closePreview()
413 RegionsList::iterator anIter= myRegionsList.begin();
414 for ( ; anIter != myRegionsList.end(); ++anIter )
416 Region& aRegion = *anIter;
419 aRegion.Shape->erase( false );
420 delete aRegion.Shape;
421 aRegion.Shape = NULL;
425 if( myPreviewViewManager )
427 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
428 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
430 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
431 myPreviewViewManager = NULL;
434 if( myActiveViewManager )
436 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
437 myActiveViewManager = NULL;