Salome HOME
b9089a7feb34ed047277060c737f43abaa07b027
[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 #include "HYDROGUI_Region.h"
32
33 #include <HYDROData_Polyline.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_ImmersibleZone.h>
36 #include <HYDROData_Object.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42 #include <LightApp_Application.h>
43 #include <LightApp_UpdateFlags.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_DataOwner.h>
46
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
49
50 #include <QApplication>
51
52 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54   myIsEdit( theIsEdit ),
55   myActiveViewManager( NULL ),
56   myPreviewViewManager( NULL )
57 {
58   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
59 }
60
61 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
62 {
63   closePreview();
64 }
65
66 void HYDROGUI_CalculationOp::startOperation()
67 {
68   HYDROGUI_Operation::startOperation();
69   
70   // Begin transaction
71   startDocOperation();
72
73   HYDROGUI_CalculationDlg* aPanel = 
74     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
75   if ( !aPanel )
76     return;
77
78   aPanel->reset();
79
80   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
81
82   QStringList aSelectedObjects;
83
84   myEditedObject.Nullify();
85   if ( myIsEdit )
86   {
87     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
88     if ( !myEditedObject.IsNull() )
89     {
90       anObjectName = myEditedObject->GetName();
91       updateGeomObjectsList(aPanel);
92     }
93   }
94   else
95   {
96     myEditedObject =
97       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
98     myEditedObject->SetName(anObjectName);
99   }
100
101   aPanel->setObjectName( anObjectName );
102   aPanel->setEditedObject( myEditedObject );
103
104   createPreview();
105 }
106
107 void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
108 {
109   Handle(HYDROData_Object) anObject;
110   Handle(HYDROData_Entity) anEntity;
111   QStringList aList;
112   // Update the list in the dialog
113   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
114   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
115   for ( ; anIter.More(); anIter.Next() )
116   {
117     anEntity = anIter.Value();
118     if ( !anEntity.IsNull() )
119     {
120       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
121       if ( !anObject.IsNull() )
122       {
123         aList.append( anObject->GetName() );
124       }
125     }
126   }
127   thePanel->setGeomObjects( aList );
128 }
129
130 void HYDROGUI_CalculationOp::abortOperation()
131 {
132   closePreview();
133   // Abort transaction
134   abortDocOperation();
135   HYDROGUI_Operation::abortOperation();
136 }
137
138 void HYDROGUI_CalculationOp::commitOperation()
139 {
140   closePreview();
141   // Commit transaction
142   commitDocOperation();
143   HYDROGUI_Operation::commitOperation();
144 }
145
146 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
147 {
148   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
149
150   // Connect signals and slots
151   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
152   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
153   connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
154   connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
155   connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
156   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
157   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
158     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
159   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
160     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
161   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
162     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
163   connect( aPanel, SIGNAL( objectSelected( const QString & ) ), 
164     SLOT( onObjectSelected( const QString & ) ) );
165
166   return aPanel;
167 }
168
169 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
170 {
171   // Select the appropriate geometry object shape in the viewer
172   selectionMgr()->clearSelected();
173
174   // Unhighlight all objects except selected
175   HYDROGUI_Shape* aShape;
176   HYDROGUI_Shape* aSelectedShape = 0;
177   Handle(HYDROData_Entity) anEntity;
178   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
179   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
180   bool isSelected;
181   QString aName;
182   for ( ; anIter.More(); anIter.Next() )
183   {
184     anEntity = anIter.Value();
185     if ( !anEntity.IsNull() )
186     {
187       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
188       if ( aShape )
189       {
190         aName = anEntity->GetName();
191         isSelected = ( aName == theObjName );
192         if ( isSelected )
193         {
194           aSelectedShape = aShape;
195         }
196         if ( aShape->isHighlighted() != isSelected )
197         {
198           if ( !isSelected )
199           {
200             aShape->highlight( isSelected );
201             aShape->update();
202           }
203         }
204       }
205     }
206   }
207   if ( aSelectedShape )
208   {
209     aSelectedShape->highlight( true );
210     aSelectedShape->update();
211   }
212 }
213
214 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
215 {
216   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
217   HYDROGUI_Zone* aZoneItem;
218   selectionMgr()->clearSelected();
219   if ( aRegionItem )
220   {
221     // Select a region in preview
222     SUIT_DataOwnerPtrList aList( true );
223     DataObjectList aZones = aRegionItem->children();
224     for ( int i = 0; i < aZones.length(); i++ )
225     {
226       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
227       if ( aZoneItem )
228       {
229         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
230       }
231     }
232     selectionMgr()->setSelected( aList );
233   }
234   else
235   {
236     // select a single zone
237     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
238     if ( aZoneItem )
239     {
240       SUIT_DataOwnerPtrList aList( true );
241       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
242       selectionMgr()->setSelected( aList );
243     }
244   }
245 }
246
247 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
248 {
249   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
250   if ( aRegion )
251   {
252     QList<HYDROGUI_Zone*> aZonesList;
253     HYDROGUI_Zone* aZone;
254     // Get a list of dropped zones
255     for ( int i = 0; i < theZonesList.length(); i++ )
256     {
257       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
258       if ( aZone )
259       {
260         aZonesList.append( aZone );
261       }
262     }
263     if ( aZonesList.length() > 0 )
264     {
265       aRegion->addZones( aZonesList );
266       HYDROGUI_CalculationDlg* aPanel = 
267         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
268       if ( aPanel )
269       {
270         aPanel->setEditedObject(myEditedObject);
271       }
272       createPreview();
273     }
274   }
275 }
276
277 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
278 {
279   QList<HYDROGUI_Zone*> aZonesList;
280   HYDROGUI_Zone* aZone;
281   // Get a list of dropped zones
282   for ( int i = 0; i < theZonesList.length(); i++ )
283   {
284     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
285     if ( aZone )
286     {
287       aZonesList.append( aZone );
288     }
289   }
290   if ( aZonesList.length() > 0 )
291   {
292     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
293     HYDROGUI_CalculationDlg* aPanel = 
294       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
295     if ( aPanel )
296     {
297       aPanel->setEditedObject(myEditedObject);
298     }
299     createPreview();
300   }
301 }
302
303 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
304 {
305   HYDROGUI_CalculationDlg* aPanel = 
306     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
307   if ( aPanel )
308   {
309     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
310     if ( aZone )
311     {
312       aZone->setMergeType( theMergeType, theBathymetryName );
313       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
314       if ( aShape )
315       {
316         aShape->update();
317       }
318     }
319     aPanel->refreshZonesBrowser();
320   }
321 }
322
323 void HYDROGUI_CalculationOp::onAddObjects()
324 {
325   // Add geometry objects selected in the module browser to the calculation case
326   Handle(HYDROData_Object) anObject;
327   Handle(HYDROData_Entity) anEntity;
328   QStringList aList;
329   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
330   for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
331   {
332     anEntity = aSeq.Value( anIndex );
333     if ( !anEntity.IsNull() )
334     {
335       anObject = Handle(HYDROData_Object)::DownCast( anEntity );
336       if( !anObject.IsNull() )
337       {
338         if (myEditedObject->AddGeometryObject( anObject ))
339         {
340           aList.append( anObject->GetName() );
341         }
342       }
343     }
344   }
345   HYDROGUI_CalculationDlg* aPanel = 
346     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
347   if ( aPanel )
348   {
349     updateGeomObjectsList( aPanel );
350   }
351   createPreview();
352 }
353
354 void HYDROGUI_CalculationOp::onRemoveObjects()
355 {
356   // Remove selected objects from the calculation case
357   HYDROGUI_CalculationDlg* aPanel = 
358     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
359   if ( aPanel )
360   {
361     QStringList aList = aPanel->getSelectedGeomObjects();
362     Handle(HYDROData_Object) anObject;
363     Handle(HYDROData_Entity) anEntity;
364     for (int i = 0; i < aList.length(); i++)
365     {
366       anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
367       if ( !anEntity.IsNull() )
368       {
369         anObject = Handle(HYDROData_Object)::DownCast( anEntity );
370         if ( !anObject.IsNull() )
371         {
372           module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
373           myEditedObject->RemoveGeometryObject( anObject );
374         }
375       }
376     }
377     updateGeomObjectsList( aPanel );
378   }
379 }
380
381 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
382                                            QString& theErrorMsg )
383 {
384   HYDROGUI_CalculationDlg* aPanel = 
385     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
386   if ( !aPanel )
387     return false;
388
389   theUpdateFlags = UF_Model;
390
391   return true;
392 }
393
394 void HYDROGUI_CalculationOp::onApply()
395 {
396   QApplication::setOverrideCursor( Qt::WaitCursor );
397
398   int anUpdateFlags = 0;
399   QString anErrorMsg;
400
401   bool aResult = false;
402   
403   try
404   {
405     aResult = processApply( anUpdateFlags, anErrorMsg );
406   }
407   catch ( Standard_Failure )
408   {
409     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
410     anErrorMsg = aFailure->GetMessageString();
411     aResult = false;
412   }
413   catch ( ... )
414   {
415     aResult = false;
416   }
417   
418   QApplication::restoreOverrideCursor();
419
420   if ( aResult )
421   {
422     module()->update( anUpdateFlags );
423     commit();
424   }
425   else
426   {
427     abort();
428     QString aMsg = tr( "INPUT_VALID_DATA" );
429     if( !anErrorMsg.isEmpty() )
430       aMsg.prepend( anErrorMsg + "\n" );
431     SUIT_MessageBox::critical( module()->getApp()->desktop(),
432                                tr( "INSUFFICIENT_INPUT_DATA" ),
433                                aMsg ); 
434   }
435 }
436
437 void HYDROGUI_CalculationOp::onSplitZones()
438 {
439   QApplication::setOverrideCursor( Qt::WaitCursor );
440
441   if ( myEditedObject->IsMustBeUpdated() )
442   {
443     myShowZones = true;
444     myEditedObject->SplitGeometryObjects();
445
446     HYDROGUI_CalculationDlg* aPanel = 
447       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
448     if ( aPanel )
449     {
450       aPanel->setEditedObject( myEditedObject );
451     }
452
453     createPreview();
454   }
455   else
456   {
457     setZonesVisible( true );
458   }
459
460   QApplication::restoreOverrideCursor();
461 }
462
463 void HYDROGUI_CalculationOp::onHideZones()
464 {
465   setZonesVisible( false );
466 }
467
468 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
469 {
470   myShowZones = theIsVisible;
471   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
472   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
473   HYDROData_SequenceOfObjects aZones;
474   Handle(HYDROData_Region) aRegion;
475   if ( myPreviewViewManager ) 
476   {
477     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
478     {
479       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
480       if ( !aCtx.IsNull() )
481       {
482         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
483         {
484           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
485           if ( !aRegion.IsNull() )
486           {
487             aZones = aRegion->GetZones();
488             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
489             for ( ; aZonesIter.More(); aZonesIter.Next() )
490             {
491               if ( theIsVisible )
492               {
493                 showObject( aZonesIter.Value(), aCtx );
494               }
495               else
496               {
497                 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
498               }
499             }
500           }
501         }
502       }
503     }
504   }
505 }
506
507 void HYDROGUI_CalculationOp::createPreview()
508 {
509   LightApp_Application* anApp = module()->getApp();
510   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
511   Handle(HYDROData_Entity) anEntity;
512
513   if ( myShowZones )
514   {
515     // Gather zones for displaying
516     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
517     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
518     HYDROData_SequenceOfObjects aZones;
519     Handle(HYDROData_Region) aRegion;
520     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
521     {
522       anEntity = aRegionsIter.Value();
523       if ( !anEntity.IsNull() )
524       {
525         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
526         if ( !aRegion.IsNull() )
527         {
528           aZones = aRegion->GetZones();
529           aSeq.Append( aZones );
530         }
531       }
532     }
533   }
534
535   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
536
537   if ( !myActiveViewManager )
538   {
539     if ( aSeq.IsEmpty() )
540       return;
541
542     myActiveViewManager = anApp->activeViewManager();
543   }
544
545   if ( !myPreviewViewManager )
546   {
547     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
548       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
549     if ( myPreviewViewManager )
550     {
551       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
552                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
553
554       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
555       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
556     }
557   }
558
559   if ( !myPreviewViewManager )
560     return;
561
562   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
563   {
564     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
565     if ( !aCtx.IsNull() )
566     {
567       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
568       for ( ; anIter.More(); anIter.Next() )
569       {
570         showObject( anIter.Value(), aCtx );
571       }
572
573       //Process the draw events for viewer
574       QApplication::processEvents();
575       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
576         vw->onTopView();
577     }
578   }
579 }
580
581 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
582 {
583   if ( !theEntity.IsNull() )
584   {
585     HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
586     if ( !aShape )
587     {
588       aShape = new HYDROGUI_Shape( theCtx, theEntity );
589       module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
590     }
591     aShape->update();
592   }
593 }
594
595 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
596 {
597   closePreview();
598 }
599
600 void HYDROGUI_CalculationOp::closePreview()
601 {
602   if( myPreviewViewManager )
603   {
604     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
605                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
606
607     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
608     myPreviewViewManager = NULL;
609   }
610
611   if( myActiveViewManager )
612   {
613     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
614     myActiveViewManager = NULL;
615   }
616 }
617
618