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