Salome HOME
f36ac726fec8dc87146b20666bef95ce448cb879
[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() ), SLOT( onAddObjects() ) );
150   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
151   connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
152
153   return aPanel;
154 }
155
156 void HYDROGUI_CalculationOp::onAddObjects()
157 {
158   // Add geometry objects selected in the module browser to the calculation case
159   Handle(HYDROData_Object) anObject;
160   Handle(HYDROData_Entity) anEntity;
161   QStringList aList;
162   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
163   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
164   {
165     anEntity = aSeq.Value( anIndex );
166     if ( !anEntity.IsNull() )
167     {
168       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
169       if( !anObject.IsNull() )
170       {
171         if (myEditedObject->AddGeometryObject( anObject ))
172         {
173           aList.append( anObject->GetName() );
174         }
175       }
176     }
177   }
178   HYDROGUI_CalculationDlg* aPanel = 
179     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
180   if ( aPanel )
181   {
182     updateGeomObjectsList( aPanel );
183   }
184 }
185
186 void HYDROGUI_CalculationOp::onRemoveObjects()
187 {
188   // Remove selected objects from the calculation case
189   HYDROGUI_CalculationDlg* aPanel = 
190     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
191   if ( aPanel )
192   {
193     QStringList aList = aPanel->getSelectedGeomObjects();
194     Handle(HYDROData_Object) anObject;
195     Handle(HYDROData_Entity) anEntity;
196     for (int i = 0; i < aList.length(); i++)
197     {
198       anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
199       if ( !anEntity.IsNull() )
200       {
201         anObject = Handle(HYDROData_Object)::DownCast( anEntity );
202         if ( !anObject.IsNull() )
203         {
204           myEditedObject->RemoveGeometryObject( anObject );
205         }
206       }
207     }
208     updateGeomObjectsList( aPanel );
209   }
210 }
211
212 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
213                                            QString& theErrorMsg )
214 {
215   HYDROGUI_CalculationDlg* aPanel = 
216     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
217   if ( !aPanel )
218     return false;
219
220   //QString anObjectName = aPanel->getObjectName().simplified();
221   //if ( anObjectName.isEmpty() )
222   //{
223   //  theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
224   //  return false;
225   //}
226
227   //// check that there are no other objects with the same name in the document
228   //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
229   //{
230   //  Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
231   //  if ( !anObject.IsNull() )
232   //  {
233   //    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
234   //    return false;
235   //  }
236   //}
237
238   //Handle(HYDROData_Document) aDocument = doc();
239
240   //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
241   //  Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
242   //if ( aCalculObj.IsNull() )
243   //  return false;
244
245   //aCalculObj->SetName( anObjectName );
246
247   //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
248   //HYDROData_SequenceOfObjects aGeomObjects = 
249   //  HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
250
251   theUpdateFlags = UF_Model;
252
253   return true;
254 }
255
256 void HYDROGUI_CalculationOp::onApply()
257 {
258   QApplication::setOverrideCursor( Qt::WaitCursor );
259
260   int anUpdateFlags = 0;
261   QString anErrorMsg;
262
263   bool aResult = false;
264   
265   try
266   {
267     aResult = processApply( anUpdateFlags, anErrorMsg );
268   }
269   catch ( Standard_Failure )
270   {
271     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
272     anErrorMsg = aFailure->GetMessageString();
273     aResult = false;
274   }
275   catch ( ... )
276   {
277     aResult = false;
278   }
279   
280   QApplication::restoreOverrideCursor();
281
282   if ( aResult )
283   {
284     module()->update( anUpdateFlags );
285     commit();
286   }
287   else
288   {
289     abort();
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" ),
295                                aMsg ); 
296   }
297 }
298
299 void HYDROGUI_CalculationOp::onSplitZones()
300 {
301   //myRegionsList.clear();
302
303   HYDROGUI_CalculationDlg* aPanel = 
304     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
305   if ( !aPanel )
306     return;
307
308   QApplication::setOverrideCursor( Qt::WaitCursor );
309
310   //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
311   //HYDROData_SequenceOfObjects aGeomObjects = 
312   //  HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
313
314   //QStringList aResSplittedZones;
315
316   //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
317   //  HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
318
319   //QStringList aUsedNames;
320
321   //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
322   //while( anIter.hasNext() )
323   //{
324   //  Region aRegion;
325   //  aRegion.SplitData = anIter.next();
326
327   //  aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
328   //  aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
329
330   //  aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
331
332   //  aUsedNames.append( aRegion.RegionName );
333
334   //  aResSplittedZones.append( aRegion.RegionName );
335
336   //  myRegionsList.append( aRegion );
337   //}
338   //
339
340   myEditedObject->SplitGeometryObjects();
341   aPanel->setEditedObject( myEditedObject );
342   createPreview();
343
344   QApplication::restoreOverrideCursor();
345 }
346
347 void HYDROGUI_CalculationOp::createPreview()
348 {
349   LightApp_Application* anApp = module()->getApp();
350
351   if ( !myActiveViewManager )
352   {
353     if ( myRegionsList.isEmpty() )
354       return;
355
356     myActiveViewManager = anApp->activeViewManager();
357   }
358
359   if ( !myPreviewViewManager )
360   {
361     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
362       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
363     if ( myPreviewViewManager )
364     {
365       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
366                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
367
368       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
369       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
370     }
371   }
372
373   if ( !myPreviewViewManager )
374     return;
375
376   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
377   {
378     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
379     if ( !aCtx.IsNull() )
380     {
381       RegionsList::iterator anIter = myRegionsList.begin();
382       for ( ; anIter != myRegionsList.end(); ++anIter )
383       {
384         Region& aRegion = *anIter;
385         if ( aRegion.Shape )
386         {
387           aRegion.Shape->erase( false );
388           delete aRegion.Shape;
389         }
390
391         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
392
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 );
396       }
397
398       //Process the draw events for viewer
399       QApplication::processEvents();
400       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
401         vw->onTopView();
402     }
403   }
404 }
405
406 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
407 {
408   closePreview();
409 }
410
411 void HYDROGUI_CalculationOp::closePreview()
412 {
413   RegionsList::iterator anIter= myRegionsList.begin();
414   for ( ; anIter != myRegionsList.end(); ++anIter )
415   {
416     Region& aRegion = *anIter;
417     if ( aRegion.Shape )
418     {
419       aRegion.Shape->erase( false );
420       delete aRegion.Shape;
421       aRegion.Shape = NULL;
422     }
423   }
424
425   if( myPreviewViewManager )
426   {
427     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
428                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
429
430     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
431     myPreviewViewManager = NULL;
432   }
433
434   if( myActiveViewManager )
435   {
436     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
437     myActiveViewManager = NULL;
438   }
439 }
440
441