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() ) );
152 connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
157 void HYDROGUI_CalculationOp::onAddObjects()
159 // Add geometry objects selected in the module browser to the calculation case
160 Handle(HYDROData_Object) anObject;
161 Handle(HYDROData_Entity) anEntity;
163 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
164 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
166 anEntity = aSeq.Value( anIndex );
167 if ( !anEntity.IsNull() )
169 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
170 if( !anObject.IsNull() )
172 if (myEditedObject->AddGeometryObject( anObject ))
174 aList.append( anObject->GetName() );
179 HYDROGUI_CalculationDlg* aPanel =
180 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
183 updateGeomObjectsList( aPanel );
187 void HYDROGUI_CalculationOp::onRemoveObjects()
189 // Remove selected objects from the calculation case
190 HYDROGUI_CalculationDlg* aPanel =
191 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
194 QStringList aList = aPanel->getSelectedGeomObjects();
195 Handle(HYDROData_Object) anObject;
196 Handle(HYDROData_Entity) anEntity;
197 for (int i = 0; i < aList.length(); i++)
199 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
200 if ( !anEntity.IsNull() )
202 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
203 if ( !anObject.IsNull() )
205 myEditedObject->RemoveGeometryObject( anObject );
209 updateGeomObjectsList( aPanel );
213 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
214 QString& theErrorMsg )
216 HYDROGUI_CalculationDlg* aPanel =
217 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
221 //QString anObjectName = aPanel->getObjectName().simplified();
222 //if ( anObjectName.isEmpty() )
224 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
228 //// check that there are no other objects with the same name in the document
229 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
231 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
232 // if ( !anObject.IsNull() )
234 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
239 //Handle(HYDROData_Document) aDocument = doc();
241 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
242 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
243 //if ( aCalculObj.IsNull() )
246 //aCalculObj->SetName( anObjectName );
248 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
249 //HYDROData_SequenceOfObjects aGeomObjects =
250 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
252 theUpdateFlags = UF_Model;
257 void HYDROGUI_CalculationOp::onApply()
259 QApplication::setOverrideCursor( Qt::WaitCursor );
261 int anUpdateFlags = 0;
264 bool aResult = false;
268 aResult = processApply( anUpdateFlags, anErrorMsg );
270 catch ( Standard_Failure )
272 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
273 anErrorMsg = aFailure->GetMessageString();
281 QApplication::restoreOverrideCursor();
285 module()->update( anUpdateFlags );
291 QString aMsg = tr( "INPUT_VALID_DATA" );
292 if( !anErrorMsg.isEmpty() )
293 aMsg.prepend( anErrorMsg + "\n" );
294 SUIT_MessageBox::critical( module()->getApp()->desktop(),
295 tr( "INSUFFICIENT_INPUT_DATA" ),
300 void HYDROGUI_CalculationOp::onSplitZones()
302 //myRegionsList.clear();
304 HYDROGUI_CalculationDlg* aPanel =
305 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
309 QApplication::setOverrideCursor( Qt::WaitCursor );
311 //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
312 //HYDROData_SequenceOfObjects aGeomObjects =
313 // HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
315 //QStringList aResSplittedZones;
317 //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
318 // HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
320 //QStringList aUsedNames;
322 //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
323 //while( anIter.hasNext() )
326 // aRegion.SplitData = anIter.next();
328 // aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
329 // aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
331 // aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
333 // aUsedNames.append( aRegion.RegionName );
335 // aResSplittedZones.append( aRegion.RegionName );
337 // myRegionsList.append( aRegion );
341 myEditedObject->SplitGeometryObjects();
342 aPanel->setEditedObject( myEditedObject );
345 QApplication::restoreOverrideCursor();
348 void HYDROGUI_CalculationOp::createPreview()
350 LightApp_Application* anApp = module()->getApp();
352 if ( !myActiveViewManager )
354 if ( myRegionsList.isEmpty() )
357 myActiveViewManager = anApp->activeViewManager();
360 if ( !myPreviewViewManager )
362 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
363 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
364 if ( myPreviewViewManager )
366 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
367 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
369 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
370 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
374 if ( !myPreviewViewManager )
377 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
379 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
380 if ( !aCtx.IsNull() )
382 RegionsList::iterator anIter = myRegionsList.begin();
383 for ( ; anIter != myRegionsList.end(); ++anIter )
385 Region& aRegion = *anIter;
388 aRegion.Shape->erase( false );
389 delete aRegion.Shape;
392 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
394 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
395 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
396 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
399 //Process the draw events for viewer
400 QApplication::processEvents();
401 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
407 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
412 void HYDROGUI_CalculationOp::closePreview()
414 RegionsList::iterator anIter= myRegionsList.begin();
415 for ( ; anIter != myRegionsList.end(); ++anIter )
417 Region& aRegion = *anIter;
420 aRegion.Shape->erase( false );
421 delete aRegion.Shape;
422 aRegion.Shape = NULL;
426 if( myPreviewViewManager )
428 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
429 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
431 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
432 myPreviewViewManager = NULL;
435 if( myActiveViewManager )
437 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
438 myActiveViewManager = NULL;