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