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>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewWindow.h>
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
45 #include <QApplication>
47 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ),
49 myIsEdit( theIsEdit ),
50 myActiveViewManager( NULL ),
51 myPreviewViewManager( NULL )
53 setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
56 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
61 void HYDROGUI_CalculationOp::startOperation()
63 HYDROGUI_Operation::startOperation();
68 HYDROGUI_CalculationDlg* aPanel =
69 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
73 myRegionsList.clear();
76 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
78 QStringList aSelectedObjects;
80 myEditedObject.Nullify();
83 myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
84 if ( !myEditedObject.IsNull() )
86 anObjectName = myEditedObject->GetName();
88 HYDROData_SequenceOfObjects aRefObjects = myEditedObject->GetGeometryObjects();
89 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
90 for ( ; anIter.More(); anIter.Next() )
92 Handle(HYDROData_Object) aRefbject =
93 Handle(HYDROData_Object)::DownCast( anIter.Value() );
94 if ( aRefbject.IsNull() )
97 QString aRefObjectName = aRefbject->GetName();
98 if ( aRefObjectName.isEmpty() )
101 aSelectedObjects.append( aRefObjectName );
108 Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
109 myEditedObject->SetName(anObjectName);
112 aPanel->setObjectName( anObjectName );
113 aPanel->setEditedObject( myEditedObject );
114 aPanel->setSelectedGeomObjects( aSelectedObjects );
119 void HYDROGUI_CalculationOp::abortOperation()
124 HYDROGUI_Operation::abortOperation();
127 void HYDROGUI_CalculationOp::commitOperation()
130 // Commit transaction
131 commitDocOperation();
132 HYDROGUI_Operation::commitOperation();
135 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
137 HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
139 // Connect signals and slots
144 bool HYDROGUI_CalculationOp::processApply( int& theUpdateFlags,
145 QString& theErrorMsg )
147 HYDROGUI_CalculationDlg* aPanel =
148 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
152 //QString anObjectName = aPanel->getObjectName().simplified();
153 //if ( anObjectName.isEmpty() )
155 // theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
159 //// check that there are no other objects with the same name in the document
160 //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
162 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
163 // if ( !anObject.IsNull() )
165 // theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
170 //Handle(HYDROData_Document) aDocument = doc();
172 //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
173 // Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
174 //if ( aCalculObj.IsNull() )
177 //aCalculObj->SetName( anObjectName );
179 //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
180 //HYDROData_SequenceOfObjects aGeomObjects =
181 // HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
183 theUpdateFlags = UF_Model;
188 void HYDROGUI_CalculationOp::onApply()
190 QApplication::setOverrideCursor( Qt::WaitCursor );
192 int anUpdateFlags = 0;
195 bool aResult = false;
199 aResult = processApply( anUpdateFlags, anErrorMsg );
201 catch ( Standard_Failure )
203 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
204 anErrorMsg = aFailure->GetMessageString();
212 QApplication::restoreOverrideCursor();
216 module()->update( anUpdateFlags );
222 QString aMsg = tr( "INPUT_VALID_DATA" );
223 if( !anErrorMsg.isEmpty() )
224 aMsg.prepend( anErrorMsg + "\n" );
225 SUIT_MessageBox::critical( module()->getApp()->desktop(),
226 tr( "INSUFFICIENT_INPUT_DATA" ),
231 void HYDROGUI_CalculationOp::onSplitZones()
233 myRegionsList.clear();
235 HYDROGUI_CalculationDlg* aPanel =
236 ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
240 QApplication::setOverrideCursor( Qt::WaitCursor );
242 QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
243 HYDROData_SequenceOfObjects aGeomObjects =
244 HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
246 QStringList aResSplittedZones;
248 HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
249 HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
251 QStringList aUsedNames;
253 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
254 while( anIter.hasNext() )
257 aRegion.SplitData = anIter.next();
259 aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
260 aRegion.BorderColor = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
262 aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
264 aUsedNames.append( aRegion.RegionName );
266 aResSplittedZones.append( aRegion.RegionName );
268 myRegionsList.append( aRegion );
273 QApplication::restoreOverrideCursor();
276 void HYDROGUI_CalculationOp::createPreview()
278 LightApp_Application* anApp = module()->getApp();
280 if ( !myActiveViewManager )
282 if ( myRegionsList.isEmpty() )
285 myActiveViewManager = anApp->activeViewManager();
288 if ( !myPreviewViewManager )
290 myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
291 anApp->createViewManager( OCCViewer_Viewer::Type() ) );
292 if ( myPreviewViewManager )
294 connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
295 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
297 module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
298 myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
302 if ( !myPreviewViewManager )
305 if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
307 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
308 if ( !aCtx.IsNull() )
310 RegionsList::iterator anIter = myRegionsList.begin();
311 for ( ; anIter != myRegionsList.end(); ++anIter )
313 Region& aRegion = *anIter;
316 aRegion.Shape->erase( false );
317 delete aRegion.Shape;
320 aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
322 aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
323 aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
324 aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
327 //Process the draw events for viewer
328 QApplication::processEvents();
329 if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
335 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
340 void HYDROGUI_CalculationOp::closePreview()
342 RegionsList::iterator anIter= myRegionsList.begin();
343 for ( ; anIter != myRegionsList.end(); ++anIter )
345 Region& aRegion = *anIter;
348 aRegion.Shape->erase( false );
349 delete aRegion.Shape;
350 aRegion.Shape = NULL;
354 if( myPreviewViewManager )
356 disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
357 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
359 module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
360 myPreviewViewManager = NULL;
363 if( myActiveViewManager )
365 HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
366 myActiveViewManager = NULL;