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() ), this, SLOT( onAddObjects() ) );
150 connect( aPanel, SIGNAL( removeObjects() ), this, SLOT( onRemoveObjects() ) );
155 void HYDROGUI_CalculationOp::onAddObjects()
157 // Add geometry objects selected in the module browser to the calculation case
158 Handle(HYDROData_Object) anObject;
159 Handle(HYDROData_Entity) anEntity;
161 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
162 for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
164 anEntity = aSeq.Value( anIndex );
165 if ( !anEntity.IsNull() )
167 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
168 if( !anObject.IsNull() )
170 if (myEditedObject->AddGeometryObject( anObject ))
172 aList.append( anObject->GetName() );
177 HYDROGUI_CalculationDlg* aPanel =
178 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
181 updateGeomObjectsList( aPanel );
185 void HYDROGUI_CalculationOp::onRemoveObjects()
187 // Remove selected objects from the calculation case
188 HYDROGUI_CalculationDlg* aPanel =
189 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
192 QStringList aList = aPanel->getSelectedGeomObjects();
193 Handle(HYDROData_Object) anObject;
194 Handle(HYDROData_Entity) anEntity;
195 for (int i = 0; i < aList.length(); i++)
197 anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
198 if ( !anEntity.IsNull() )
200 anObject = Handle(HYDROData_Object)::DownCast( anEntity );
201 if ( !anObject.IsNull() )
203 myEditedObject->RemoveGeometryObject( anObject );
207 updateGeomObjectsList( aPanel );
211 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
212 QString& theErrorMsg )
214 HYDROGUI_CalculationDlg* aPanel =
215 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
219 //QString anObjectName = aPanel->getObjectName().simplified();
220 //if ( anObjectName.isEmpty() )
222 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
226 //// check that there are no other objects with the same name in the document
227 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
229 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
230 // if ( !anObject.IsNull() )
232 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
237 //Handle(HYDROData_Document) aDocument = doc();
239 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
240 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
241 //if ( aCalculObj.IsNull() )
244 //aCalculObj->SetName( anObjectName );
246 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
247 //HYDROData_SequenceOfObjects aGeomObjects =
248 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
250 theUpdateFlags = UF_Model;
255 void HYDROGUI_CalculationOp::onApply()
257 QApplication::setOverrideCursor( Qt::WaitCursor );
259 int anUpdateFlags = 0;
262 bool aResult = false;
266 aResult = processApply( anUpdateFlags, anErrorMsg );
268 catch ( Standard_Failure )
270 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
271 anErrorMsg = aFailure->GetMessageString();
279 QApplication::restoreOverrideCursor();
283 module()->update( anUpdateFlags );
289 QString aMsg = tr( "INPUT_VALID_DATA" );
290 if( !anErrorMsg.isEmpty() )
291 aMsg.prepend( anErrorMsg + "\n" );
292 SUIT_MessageBox::critical( module()->getApp()->desktop(),
293 tr( "INSUFFICIENT_INPUT_DATA" ),
298 void HYDROGUI_CalculationOp::onSplitZones()
300 myRegionsList.clear();
302 HYDROGUI_CalculationDlg* aPanel =
303 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
307 QApplication::setOverrideCursor( Qt::WaitCursor );
309 QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
310 HYDROData_SequenceOfObjects aGeomObjects =
311 HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
313 QStringList aResSplittedZones;
315 HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
316 HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
318 QStringList aUsedNames;
320 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
321 while( anIter.hasNext() )
324 aRegion.SplitData = anIter.next();
326 aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
327 aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
329 aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
331 aUsedNames.append( aRegion.RegionName );
333 aResSplittedZones.append( aRegion.RegionName );
335 myRegionsList.append( aRegion );
340 QApplication::restoreOverrideCursor();
343 void HYDROGUI_CalculationOp::createPreview()
345 LightApp_Application* anApp = module()->getApp();
347 if ( !myActiveViewManager )
349 if ( myRegionsList.isEmpty() )
352 myActiveViewManager = anApp->activeViewManager();
355 if ( !myPreviewViewManager )
357 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
358 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
359 if ( myPreviewViewManager )
361 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
362 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
364 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
365 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
369 if ( !myPreviewViewManager )
372 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
374 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
375 if ( !aCtx.IsNull() )
377 RegionsList::iterator anIter = myRegionsList.begin();
378 for ( ; anIter != myRegionsList.end(); ++anIter )
380 Region& aRegion = *anIter;
383 aRegion.Shape->erase( false );
384 delete aRegion.Shape;
387 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
389 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
390 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
391 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
394 //Process the draw events for viewer
395 QApplication::processEvents();
396 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
402 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
407 void HYDROGUI_CalculationOp::closePreview()
409 RegionsList::iterator anIter= myRegionsList.begin();
410 for ( ; anIter != myRegionsList.end(); ++anIter )
412 Region& aRegion = *anIter;
415 aRegion.Shape->erase( false );
416 delete aRegion.Shape;
417 aRegion.Shape = NULL;
421 if( myPreviewViewManager )
423 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
424 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
426 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
427 myPreviewViewManager = NULL;
430 if( myActiveViewManager )
432 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
433 myActiveViewManager = NULL;