]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_CalculationOp.cxx
Salome HOME
Add/remove objects is implemented on the first page of the Calculation Case operation...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_CalculationOp.h"
24
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
31 #include <HYDROData_Polyline.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_ImmersibleZone.h>
34 #include <HYDROData_Object.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_Desktop.h>
45
46 #include <QApplication>
47
48 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
49 : HYDROGUI_Operation( theModule ),
50   myIsEdit( theIsEdit ),
51   myActiveViewManager( NULL ),
52   myPreviewViewManager( NULL )
53 {
54   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
55 }
56
57 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
58 {
59   closePreview();
60 }
61
62 void HYDROGUI_CalculationOp::startOperation()
63 {
64   HYDROGUI_Operation::startOperation();
65   
66   // Begin transaction
67   startDocOperation();
68
69   HYDROGUI_CalculationDlg* aPanel = 
70     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
71   if ( !aPanel )
72     return;
73
74   myRegionsList.clear();
75   aPanel->reset();
76
77   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
78
79   QStringList aSelectedObjects;
80
81   myEditedObject.Nullify();
82   if ( myIsEdit )
83   {
84     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
85     if ( !myEditedObject.IsNull() )
86     {
87       anObjectName = myEditedObject->GetName();
88       updateGeomObjectsList(aPanel);
89     }
90   }
91   else
92   {
93     myEditedObject =
94       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
95     myEditedObject->SetName(anObjectName);
96   }
97
98   aPanel->setObjectName( anObjectName );
99   aPanel->setEditedObject( myEditedObject );
100 //  aPanel->setSelectedGeomObjects( aSelectedObjects );
101
102   createPreview();
103 }
104
105 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
106 {
107   Handle(HYDROData_Object) anObject;
108   Handle(HYDROData_Entity) anEntity;
109   QStringList aList;
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() )
114   {
115     anEntity = anIter.Value();
116     if ( !anEntity.IsNull() )
117     {
118       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
119       if ( !anObject.IsNull() )
120       {
121         aList.append( anObject->GetName() );
122       }
123     }
124   }
125   thePanel->setSelectedGeomObjects( aList );
126 }
127
128 void HYDROGUI_CalculationOp::abortOperation()
129 {
130   closePreview();
131   // Abort transaction
132   abortDocOperation();
133   HYDROGUI_Operation::abortOperation();
134 }
135
136 void HYDROGUI_CalculationOp::commitOperation()
137 {
138   closePreview();
139   // Commit transaction
140   commitDocOperation();
141   HYDROGUI_Operation::commitOperation();
142 }
143
144 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
145 {
146   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
147
148   // Connect signals and slots
149   connect( aPanel, SIGNAL( addObjects() ), this, SLOT( onAddObjects() ) );
150   connect( aPanel, SIGNAL( removeObjects() ), this, SLOT( onRemoveObjects() ) );
151
152   return aPanel;
153 }
154
155 void HYDROGUI_CalculationOp::onAddObjects()
156 {
157   // Add geometry objects selected in the module browser to the calculation case
158   Handle(HYDROData_Object) anObject;
159   Handle(HYDROData_Entity) anEntity;
160   QStringList aList;
161   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
162   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
163   {
164     anEntity = aSeq.Value( anIndex );
165     if ( !anEntity.IsNull() )
166     {
167       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
168       if( !anObject.IsNull() )
169       {
170         if (myEditedObject->AddGeometryObject( anObject ))
171         {
172           aList.append( anObject->GetName() );
173         }
174       }
175     }
176   }
177   HYDROGUI_CalculationDlg* aPanel = 
178     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
179   if ( aPanel )
180   {
181     updateGeomObjectsList( aPanel );
182   }
183 }
184
185 void HYDROGUI_CalculationOp::onRemoveObjects()
186 {
187   // Remove selected objects from the calculation case
188   HYDROGUI_CalculationDlg* aPanel = 
189     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
190   if ( aPanel )
191   {
192     QStringList aList = aPanel->getSelectedGeomObjects();
193     Handle(HYDROData_Object) anObject;
194     Handle(HYDROData_Entity) anEntity;
195     for (int i = 0; i < aList.length(); i++)
196     {
197       anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
198       if ( !anEntity.IsNull() )
199       {
200         anObject = Handle(HYDROData_Object)::DownCast( anEntity );
201         if ( !anObject.IsNull() )
202         {
203           myEditedObject->RemoveGeometryObject( anObject );
204         }
205       }
206     }
207     updateGeomObjectsList( aPanel );
208   }
209 }
210
211 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
212                                            QString& theErrorMsg )
213 {
214   HYDROGUI_CalculationDlg* aPanel = 
215     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
216   if ( !aPanel )
217     return false;
218
219   //QString anObjectName = aPanel->getObjectName().simplified();
220   //if ( anObjectName.isEmpty() )
221   //{
222   //  theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
223   //  return false;
224   //}
225
226   //// check that there are no other objects with the same name in the document
227   //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
228   //{
229   //  Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
230   //  if ( !anObject.IsNull() )
231   //  {
232   //    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
233   //    return false;
234   //  }
235   //}
236
237   //Handle(HYDROData_Document) aDocument = doc();
238
239   //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
240   //  Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
241   //if ( aCalculObj.IsNull() )
242   //  return false;
243
244   //aCalculObj->SetName( anObjectName );
245
246   //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
247   //HYDROData_SequenceOfObjects aGeomObjects = 
248   //  HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
249
250   theUpdateFlags = UF_Model;
251
252   return true;
253 }
254
255 void HYDROGUI_CalculationOp::onApply()
256 {
257   QApplication::setOverrideCursor( Qt::WaitCursor );
258
259   int anUpdateFlags = 0;
260   QString anErrorMsg;
261
262   bool aResult = false;
263   
264   try
265   {
266     aResult = processApply( anUpdateFlags, anErrorMsg );
267   }
268   catch ( Standard_Failure )
269   {
270     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
271     anErrorMsg = aFailure->GetMessageString();
272     aResult = false;
273   }
274   catch ( ... )
275   {
276     aResult = false;
277   }
278   
279   QApplication::restoreOverrideCursor();
280
281   if ( aResult )
282   {
283     module()->update( anUpdateFlags );
284     commit();
285   }
286   else
287   {
288     abort();
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" ),
294                                aMsg ); 
295   }
296 }
297
298 void HYDROGUI_CalculationOp::onSplitZones()
299 {
300   myRegionsList.clear();
301
302   HYDROGUI_CalculationDlg* aPanel = 
303     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
304   if ( !aPanel )
305     return;
306
307   QApplication::setOverrideCursor( Qt::WaitCursor );
308
309   QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
310   HYDROData_SequenceOfObjects aGeomObjects = 
311     HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
312
313   QStringList aResSplittedZones;
314
315   HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
316     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
317
318   QStringList aUsedNames;
319
320   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
321   while( anIter.hasNext() )
322   {
323     Region aRegion;
324     aRegion.SplitData = anIter.next();
325
326     aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
327     aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
328
329     aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
330
331     aUsedNames.append( aRegion.RegionName );
332
333     aResSplittedZones.append( aRegion.RegionName );
334
335     myRegionsList.append( aRegion );
336   }
337   
338   createPreview();
339
340   QApplication::restoreOverrideCursor();
341 }
342
343 void HYDROGUI_CalculationOp::createPreview()
344 {
345   LightApp_Application* anApp = module()->getApp();
346
347   if ( !myActiveViewManager )
348   {
349     if ( myRegionsList.isEmpty() )
350       return;
351
352     myActiveViewManager = anApp->activeViewManager();
353   }
354
355   if ( !myPreviewViewManager )
356   {
357     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
358       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
359     if ( myPreviewViewManager )
360     {
361       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
362                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
363
364       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
365       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
366     }
367   }
368
369   if ( !myPreviewViewManager )
370     return;
371
372   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
373   {
374     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
375     if ( !aCtx.IsNull() )
376     {
377       RegionsList::iterator anIter = myRegionsList.begin();
378       for ( ; anIter != myRegionsList.end(); ++anIter )
379       {
380         Region& aRegion = *anIter;
381         if ( aRegion.Shape )
382         {
383           aRegion.Shape->erase( false );
384           delete aRegion.Shape;
385         }
386
387         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
388
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 );
392       }
393
394       //Process the draw events for viewer
395       QApplication::processEvents();
396       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
397         vw->onTopView();
398     }
399   }
400 }
401
402 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
403 {
404   closePreview();
405 }
406
407 void HYDROGUI_CalculationOp::closePreview()
408 {
409   RegionsList::iterator anIter= myRegionsList.begin();
410   for ( ; anIter != myRegionsList.end(); ++anIter )
411   {
412     Region& aRegion = *anIter;
413     if ( aRegion.Shape )
414     {
415       aRegion.Shape->erase( false );
416       delete aRegion.Shape;
417       aRegion.Shape = NULL;
418     }
419   }
420
421   if( myPreviewViewManager )
422   {
423     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
424                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
425
426     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
427     myPreviewViewManager = NULL;
428   }
429
430   if( myActiveViewManager )
431   {
432     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
433     myActiveViewManager = NULL;
434   }
435 }
436
437