Salome HOME
1987887cfab9eb3b9a9a85fc21a90914ef18260a
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_CalculationOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_CalculationDlg.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_Zone.h"
27 #include "HYDROGUI_Region.h"
28
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_ShapesGroup.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_ImmersibleZone.h>
33 #include <HYDROData_Object.h>
34 #include <HYDROData_Tool.h>
35 #include <HYDROData_StricklerTable.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 #include <LightApp_SelectionMgr.h>
44 #include <LightApp_DataOwner.h>
45
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_Desktop.h>
48 #include <SUIT_DataBrowser.h>
49
50 #include <QApplication>
51 #include <QKeySequence>
52 #include <QShortcut>
53
54 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
55 : HYDROGUI_Operation( theModule ),
56   myIsEdit( theIsEdit ),
57   myActiveViewManager( NULL ),
58   myPreviewViewManager( NULL ),
59   myShowGeomObjects( true ),
60   myShowLandCoverMap( false ),
61   myShowZones( false )
62 {
63   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
64 }
65
66 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
67 {
68   closePreview();
69 }
70
71 void HYDROGUI_CalculationOp::startOperation()
72 {
73   HYDROGUI_Operation::startOperation();
74   
75   // Begin transaction
76   startDocOperation();
77
78   HYDROGUI_CalculationDlg* aPanel = 
79     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
80   if ( !aPanel )
81     return;
82
83   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
84   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
85   QShortcut* aShortcut;
86   foreach( aShortcut, aShortcuts )
87   {
88     if ( aShortcut->key() == 
89       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
90       SUIT_DataBrowser::RenameShortcut ) ) )
91     {
92       aShortcut->setEnabled( false );
93     }
94   }
95
96   aPanel->reset();
97   QStringList aList;
98   QStringList anEntryList;
99   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
100   getNamesAndEntries( aSeq, aList, anEntryList );
101   aPanel->setAllGeomObjects( aList, anEntryList );
102
103   // Get all polylines
104   aList.clear();
105   anEntryList.clear();
106   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
107   Handle(HYDROData_PolylineXY) aPolylineObj;
108   QString aPolylineName;
109   for ( ; anIter.More(); anIter.Next() )
110   {
111     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
112
113     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
114     { 
115       // Check the polyline shape
116       TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
117       if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
118         aPolylineName = aPolylineObj->GetName();
119         if ( !aPolylineName.isEmpty() )
120         {
121           aList.append( aPolylineName );
122           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
123         }
124       }
125     }
126   }
127   aPanel->setPolylineNames( aList, anEntryList );
128
129   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
130
131   myEditedObject.Nullify();
132   if ( myIsEdit )
133   {
134     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
135     if ( !myEditedObject.IsNull() )
136     {
137       aPanel->setMode( myEditedObject->GetAssignmentMode() );
138       anObjectName = myEditedObject->GetName();
139       aPolylineObj = myEditedObject->GetBoundaryPolyline();
140       if ( aPolylineObj.IsNull() )
141       {
142         aPanel->setBoundary( QString() );
143       }
144       else
145       {
146         aPolylineName = aPolylineObj->GetName();
147         aPanel->setBoundary( aPolylineName );
148       }
149
150       aSeq = myEditedObject->GetGeometryObjects();
151       getNamesAndEntries( aSeq, aList, anEntryList );
152       aPanel->includeGeomObjects( aList );
153
154       // set rules
155       setRules( HYDROData_CalculationCase::DataTag_CustomRules );      
156     }
157   }
158   else
159   {
160     myEditedObject =
161       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
162     myEditedObject->SetName( anObjectName );
163     myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getMode() );
164   }
165
166   aPanel->setObjectName( anObjectName );
167   aPanel->setEditedObject( myEditedObject );
168
169   createPreview( false );
170 }
171
172 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
173                                                 QStringList& theNames, QStringList& theEntries ) const
174 {
175  
176   theNames.clear();
177   theEntries.clear();
178   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
179   for ( ; anIter.More(); anIter.Next() )
180   {
181     Handle(HYDROData_Entity) anEntity = anIter.Value();
182     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
183     //  continue;
184
185     theNames.append( anEntity->GetName() );
186     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
187   }
188 }
189
190 void HYDROGUI_CalculationOp::abortOperation()
191 {
192   closePreview();
193   // Abort transaction
194   abortDocOperation();
195   HYDROGUI_Operation::abortOperation();
196   module()->getApp()->updateObjectBrowser();
197 }
198
199 void HYDROGUI_CalculationOp::commitOperation()
200 {
201   closePreview();
202   // Commit transaction
203   commitDocOperation();
204   HYDROGUI_Operation::commitOperation();
205 }
206
207 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
208 {
209   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
210
211   // Connect signals and slots
212   connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );  
213   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
214   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
215   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
216   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
217
218   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
219
220   connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
221
222   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
223   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones( const int ) ) );
224   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
225   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
226   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>&, bool ) ),
227     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>&, bool ) ) );
228   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
229     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
230   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
231     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
232   connect( aPanel, SIGNAL( objectsSelected() ), 
233            SLOT( onObjectsSelected() ) );
234   connect( aPanel, SIGNAL( landCoversSelected() ), 
235            SLOT( onLandCoversSelected() ) );
236   connect( aPanel, SIGNAL( createLandCoverRegion( const QList<SUIT_DataObject*>& ) ),
237     SLOT( onCreateLandCoverRegion( const QList<SUIT_DataObject*>& ) ) );
238   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
239     SLOT( onBoundarySelected( const QString & ) ) );
240   connect( aPanel, SIGNAL( StricklerTableSelected( const QString & ) ), 
241     SLOT( onStricklerTableSelected( const QString & ) ) );
242
243   connect( aPanel, SIGNAL( regenerateColors() ), this, 
244     SLOT( onRegenerateColors() ) );
245
246   return aPanel;
247 }
248
249 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
250 {
251   bool anIsToUpdateViewer = false;
252
253   // Remove the old boundary from the operation viewer
254   Handle(HYDROData_PolylineXY) aPrevPolyline = 
255     myEditedObject->GetBoundaryPolyline();
256   if ( !aPrevPolyline.IsNull() )
257   {
258     setObjectVisibility( aPrevPolyline, false );
259     anIsToUpdateViewer = true;
260   }
261
262   // Set the selected boundary polyline to the calculation case
263   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
264     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
265   myEditedObject->SetBoundaryPolyline( aNewPolyline );
266
267   if ( myPreviewViewManager )
268   {
269     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
270     if ( aViewer )
271     {
272       if ( !aNewPolyline.IsNull() )
273       {
274         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
275         if ( !aCtx.IsNull() )
276         {
277           setObjectVisibility( aNewPolyline, true );
278           anIsToUpdateViewer = true;
279         }
280       }
281
282       if ( anIsToUpdateViewer )
283         module()->update( UF_OCCViewer );
284     }
285   }
286 }
287
288 void HYDROGUI_CalculationOp::onStricklerTableSelected ( const QString & theObjName )
289 {
290   bool anIsToUpdateViewer = false;
291
292   /* TODO
293   // Remove old presentations of land covers from the operation viewer  
294   Handle(HYDROData_Entity) anEntity;
295   Handle(HYDROData_LandCover) aLandCover;
296   HYDROData_SequenceOfObjects aLandCovers;
297   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
298   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
299   for ( ; anIter.More(); anIter.Next() )
300   {
301     anEntity = anIter.Value();
302     if ( !anEntity.IsNull() )
303     {
304       aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
305       if ( !aLandCover.IsNull() )
306       {
307         aLandCovers.Append( aLandCover );
308         setObjectVisibility( aLandCover, false );
309         anIsToUpdateViewer = true;
310       }
311     }
312   }
313
314   // Set the selected Strickler table to the calculation case
315   Handle(HYDROData_StricklerTable) aNewStricklerTable = Handle(HYDROData_StricklerTable)::DownCast(
316     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_STRICKLER_TABLE ) );
317   myEditedObject->SetStricklerTable( aNewStricklerTable );
318
319   if ( myPreviewViewManager )
320   {
321     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
322     if ( aViewer )
323     {
324       if ( !aNewStricklerTable.IsNull() )
325       {
326         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
327         if ( !aCtx.IsNull() )
328         {
329           HYDROData_SequenceOfObjects::Iterator anIter( aLandCovers );
330           for ( ; anIter.More(); anIter.Next() )
331           {
332             anEntity = anIter.Value();
333             if ( !anEntity.IsNull() )
334             {
335               aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
336               if ( !aLandCover.IsNull() )
337               {
338                 setObjectVisibility( aLandCover, true );
339                 anIsToUpdateViewer = true;
340               }
341             }
342           }          
343         }
344       }
345
346       if ( anIsToUpdateViewer )
347         module()->update( UF_OCCViewer );
348     }
349   }
350   */
351 }
352
353 void HYDROGUI_CalculationOp::onObjectsSelected()
354 {
355   HYDROGUI_CalculationDlg* aPanel = 
356     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
357
358   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
359   QMap<QString, bool> aSelectedObjsMap;
360   foreach( QString aName, aSelectedObjs )
361     aSelectedObjsMap[aName] = true;
362
363
364   // Select the appropriate geometry object shape in the viewer
365   selectionMgr()->clearSelected();
366
367   // Unhighlight all objects except selected
368   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
369   Handle(HYDROData_Entity) anEntity;
370   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
371   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
372   bool isSelected;
373   QString aName;
374   for ( ; anIter.More(); anIter.Next() )
375   {
376     anEntity = anIter.Value();
377     if ( !anEntity.IsNull() )
378     {
379       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
380       if ( aShape )
381       {
382         aName = anEntity->GetName();
383         isSelected = aSelectedObjsMap.contains( aName );
384         aShape->highlight( isSelected, false );
385         aShape->update( false, false );
386         aLastShape = aShape;
387       }
388     }
389   }
390   if( aLastShape )
391     aLastShape->update( true, false );
392 }
393
394 void HYDROGUI_CalculationOp::onLandCoverMapSelected()
395 {
396   HYDROGUI_CalculationDlg* aPanel = 
397     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
398
399   /* TODO: highlight the selected land cover map in the 3d viewer (synchronize slection in the dialog with a preview in the viewer)
400   QStringList aSelectedObjs = aPanel->getSelectedLandCovers();
401   QMap<QString, bool> aSelectedObjsMap;
402   foreach( QString aName, aSelectedObjs )
403     aSelectedObjsMap[aName] = true;
404   */
405
406
407   // Select the appropriate land cover shape in the viewer
408   selectionMgr()->clearSelected();
409
410   // Unhighlight all land covers except selected
411   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
412   Handle(HYDROData_Entity) anEntity;
413   /* TODO: highlight the selected land cover map in the 3d viewer (synchronize slection in the dialog with a preview in the viewer)
414   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
415   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
416   bool isSelected;
417   QString aName;
418   for ( ; anIter.More(); anIter.Next() )
419   {
420     anEntity = anIter.Value();
421     if ( !anEntity.IsNull() )
422     {
423       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
424       if ( aShape )
425       {
426         aName = anEntity->GetName();
427         isSelected = aSelectedObjsMap.contains( aName );
428         aShape->highlight( isSelected, false );
429         aShape->update( false, false );
430         aLastShape = aShape;
431       }
432     }
433   }
434   */
435
436   // TODO
437   //createPreview( true );
438
439   if( aLastShape )
440     aLastShape->update( true, false );
441 }
442
443 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
444 {
445   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
446   HYDROGUI_Zone* aZoneItem;
447   selectionMgr()->clearSelected();
448   if ( aRegionItem )
449   {
450     // Select a region in preview
451     SUIT_DataOwnerPtrList aList( true );
452     DataObjectList aZones = aRegionItem->children();
453     for ( int i = 0; i < aZones.length(); i++ )
454     {
455       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
456       if ( aZoneItem )
457       {
458         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
459       }
460     }
461     selectionMgr()->setSelected( aList );
462   }
463   else
464   {
465     // select a single zone
466     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
467     if ( aZoneItem )
468     {
469       SUIT_DataOwnerPtrList aList( true );
470       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
471       selectionMgr()->setSelected( aList );
472     }
473   }
474 }
475
476 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem,
477                                           const QList<SUIT_DataObject*>& theZonesList )
478 {
479   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
480   if ( aRegion )
481   {
482     QList<HYDROGUI_Zone*> aZonesList;
483     HYDROGUI_Zone* aZone;
484     // Get a list of dropped zones
485     for ( int i = 0; i < theZonesList.length(); i++ )
486     {
487       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
488       if ( aZone )
489       {
490         aZonesList.append( aZone );
491       }
492     }
493     if ( aZonesList.length() > 0 )
494     {
495       aRegion->addZones( aZonesList );
496       HYDROGUI_CalculationDlg* aPanel = 
497         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
498       if ( aPanel )
499         aPanel->refreshZonesBrowser();
500       createPreview( false );
501     }
502   }
503 }
504
505 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
506 {
507   if ( createRegion( theZonesList ) )
508   {
509     HYDROGUI_CalculationDlg* aPanel = 
510       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
511     if ( aPanel )
512     {
513       aPanel->refreshZonesBrowser();
514     }
515     createPreview( false );
516   }
517 }
518
519 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theMergeObjectName )
520 {
521   HYDROGUI_CalculationDlg* aPanel = 
522     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
523   if ( aPanel )
524   {
525     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
526     if ( aZone )
527     {
528       aZone->setMergeType( theMergeType, theMergeObjectName );
529       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
530       if ( aShape )
531       {
532         aShape->update( true, false );
533       }
534     }
535     aPanel->refreshZonesBrowser();
536   }
537 }
538
539 void HYDROGUI_CalculationOp::onAddObjects()
540 {
541   HYDROGUI_CalculationDlg* aPanel = 
542     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
543   if ( !aPanel )
544     return;
545
546   // Add geometry objects selected in the module browser to the calculation case
547   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
548   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
549     return;
550
551   QStringList anAddedList;
552   for (int i = 0; i < aSelectedList.length(); i++)
553   {
554     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
555       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
556     if ( anObject.IsNull() )
557       continue;
558
559     if ( myEditedObject->AddGeometryObject( anObject ) )
560       anAddedList.append( anObject->GetName() );
561   }
562
563   if ( !anAddedList.isEmpty() )
564   {
565     aPanel->includeGeomObjects( anAddedList );
566     createPreview( false );
567   }
568 }
569
570 void HYDROGUI_CalculationOp::onRemoveObjects()
571 {
572   // Remove selected objects from the calculation case
573   HYDROGUI_CalculationDlg* aPanel = 
574     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
575   if ( !aPanel )
576     return;
577
578   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
579   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
580     return;
581
582   for (int i = 0; i < aSelectedList.length(); i++)
583   {
584     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
585       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
586     if ( anObject.IsNull() )
587       continue;
588
589     setObjectVisibility( anObject, false );
590     myEditedObject->RemoveGeometryObject( anObject );
591   }
592
593   module()->update( UF_OCCViewer );
594   aPanel->excludeGeomObjects( aSelectedList );
595 }
596
597 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
598 {
599   // Check if the case is already modified or not
600   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
601   if ( !isConfirmed )
602   {
603     // If not modified check if the case has already defined regions with zones
604     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
605     if ( aSeq.Length() > 0 )
606     {
607       // If there are already defined zones then ask a user to confirm zones recalculation
608       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
609                                tr( "REGIONS_CHANGED" ),
610                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
611                                QMessageBox::Yes | QMessageBox::No,
612                                QMessageBox::No ) == QMessageBox::Yes );
613     }
614     else
615     {
616       isConfirmed = true; // No regions - no zones - nothing to recalculate
617     }
618   }
619   return isConfirmed;
620 }
621
622 bool HYDROGUI_CalculationOp::confirmOrderChange() const
623 {
624   // Check if the case is already modified or not
625   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
626   if ( !isConfirmed )
627   {
628     // If not modified check if the case has already defined regions with zones
629     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
630     if ( aSeq.Length() > 0 )
631     {
632       // If there are already defined zones then ask a user to confirm zones recalculation
633       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
634                                tr( "ORDER_CHANGED" ),
635                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
636                                QMessageBox::Yes | QMessageBox::No,
637                                QMessageBox::No ) == QMessageBox::Yes );
638     }
639     else
640     {
641       isConfirmed = true; // No regions - no zones - nothing to recalculate
642     }
643   }
644   return isConfirmed;
645 }
646
647 bool HYDROGUI_CalculationOp::confirmRuleChange() const
648 {
649   // Check if the case is already modified or not
650   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
651   if ( !isConfirmed )
652   {
653     // If not modified check if the case has already defined regions with zones
654     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
655     if ( aSeq.Length() > 0 )
656     {
657       // If there are already defined zones then ask a user to confirm zones recalculation
658       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
659                                tr( "RULE_CHANGED" ),
660                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
661                                QMessageBox::Yes | QMessageBox::No,
662                                QMessageBox::No ) == QMessageBox::Yes );
663     }
664     else
665     {
666       isConfirmed = true; // No regions - no zones - nothing to recalculate
667     }
668   }
669   return isConfirmed;
670 }
671
672 bool HYDROGUI_CalculationOp::confirmModeChange() const
673 {
674   // Check if the case is already modified or not
675   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
676   if ( !isConfirmed )
677   {
678     // If not modified check if the case has already defined regions with zones
679     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
680     if ( aSeq.Length() > 0 )
681     {
682       // If there are already defined zones then ask a user to confirm zones recalculation
683       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
684                                tr( "MODE_CHANGED" ),
685                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
686                                QMessageBox::Yes | QMessageBox::No,
687                                QMessageBox::No ) == QMessageBox::Yes );
688     }
689     else
690     {
691       isConfirmed = true; // No regions - no zones - nothing to recalculate
692     }
693   }
694   return isConfirmed;
695 }
696
697 bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
698 {
699   HYDROData_WarningType aType = theWarning.Type;
700   if ( aType == WARN_OK ) {
701     return true;
702   }
703
704   QString aTitle;
705   QString aMsg;
706   switch ( aType )
707   {
708     case WARN_EMPTY_REGIONS:
709       aTitle = tr( "EMPTY_REGIONS" );
710       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
711       break;
712     default:
713       aTitle = tr( "WARNING" );
714       aMsg = theWarning.Data;
715   }
716
717
718   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
719                                            aTitle, aMsg,
720                                            QMessageBox::Yes | QMessageBox::No,
721                                            QMessageBox::No );
722
723   return ( anAnswer == QMessageBox::Yes );
724 }
725
726 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
727                                            QString& theErrorMsg,
728                                            QStringList& theBrowseObjectsEntries )
729 {
730   HYDROGUI_CalculationDlg* aPanel = 
731     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
732   if ( !aPanel )
733     return false;
734
735   if( !myIsEdit )
736   {
737     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
738     theBrowseObjectsEntries.append( anEntry );
739   }
740
741   // For manual mode priority rules are redundant
742   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
743     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
744   }
745  
746   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
747
748   return true;
749 }
750
751 void HYDROGUI_CalculationOp::onApply()
752 {
753   // Check warnings
754   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
755   if ( aWarning.Type != WARN_OK ) {
756     if ( !confirmContinueWithWarning( aWarning ) ) {
757       // Go back to the first page
758       HYDROGUI_CalculationDlg* aPanel = 
759         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
760       if ( aPanel ) {
761         aPanel->onFirstPage();
762       }
763       return;
764     }
765   }
766
767   QApplication::setOverrideCursor( Qt::WaitCursor );
768
769   int anUpdateFlags = 0;
770   QString anErrorMsg;
771   QStringList aBrowseObjectsEntries;
772
773   bool aResult = false;
774   
775   try
776   {
777     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
778   }
779   catch ( Standard_Failure )
780   {
781     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
782     anErrorMsg = aFailure->GetMessageString();
783     aResult = false;
784   }
785   catch ( ... )
786   {
787     aResult = false;
788   }
789   
790   QApplication::restoreOverrideCursor();
791
792   if ( aResult )
793   {
794     module()->update( anUpdateFlags );
795     commit();
796     browseObjects( aBrowseObjectsEntries );
797   }
798   else
799   {
800     abort();
801     QString aMsg = tr( "INPUT_VALID_DATA" );
802     if( !anErrorMsg.isEmpty() )
803       aMsg.prepend( anErrorMsg + "\n" );
804     SUIT_MessageBox::critical( module()->getApp()->desktop(),
805                                tr( "INSUFFICIENT_INPUT_DATA" ),
806                                aMsg ); 
807   }
808 }
809
810 void HYDROGUI_CalculationOp::onNext( const int theIndex )
811 {
812   if( theIndex==1 )
813   {
814     setAvailableGroups();
815   }
816   else if( theIndex==2 )
817   {
818     // Land cover map panel
819      HYDROGUI_CalculationDlg* aPanel = 
820       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
821     if ( !aPanel )
822       return;
823
824     setLandCoverMapVisible( true );
825
826     QStringList aList;
827     QStringList anEntryList;
828     HYDROData_SequenceOfObjects aSeq;
829
830     /* TODO: get all land cover map objects to fill in combo-box
831     Handle(HYDROData_StricklerTable) aStricklerTableObj;
832     QString aStricklerTableName;
833
834     // Get all Strickler table objects to fill in combo-box
835     aList.clear();
836     anEntryList.clear();
837     HYDROData_Iterator anIter( doc(), KIND_STRICKLER_TABLE );      
838     for ( ; anIter.More(); anIter.Next() )
839     {
840       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
841
842       if ( !aStricklerTableObj.IsNull() )
843       { 
844         aStricklerTableName = aStricklerTableObj->GetName();
845         if ( !aStricklerTableName.isEmpty() )
846         {
847           aList.append( aStricklerTableName );
848           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
849         }        
850       }
851     }
852     aPanel->setStricklerTableNames( aList, anEntryList );
853     //@ASL: bool anUpdateState = myEditedObject->IsMustBeUpdated();
854     if ( !aList.isEmpty() )
855       aPanel->setStricklerTable( aList.at( 0 ), false );
856     //@ASL: myEditedObject->SetToUpdate( anUpdateState );
857
858     if ( !myEditedObject.IsNull() )
859     {
860       if ( myIsEdit )
861       {      
862         // Select the certain Strickler table object in combo-box
863         aStricklerTableObj = myEditedObject->GetStricklerTable();
864         if ( aStricklerTableObj.IsNull() )
865         {
866           aPanel->setStricklerTable( QString() );
867         }
868         else
869         {
870           aStricklerTableName = aStricklerTableObj->GetName();
871           aPanel->setStricklerTable( aStricklerTableName );
872         }
873       }
874     }
875     */
876
877     closePreview( false );
878     createPreview( true );
879   }
880   else if( theIndex==3 )
881   {
882     HYDROGUI_CalculationDlg* aPanel = 
883       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
884     if ( !aPanel )
885       return;
886
887     QApplication::setOverrideCursor( Qt::WaitCursor );
888
889     setGeomObjectsVisible( false );
890     setLandCoverMapVisible( false );
891
892     QString aNewCaseName = aPanel->getObjectName();
893     QString anOldCaseName = myEditedObject->GetName();
894     bool isNameChanged = anOldCaseName != aNewCaseName;
895   
896     bool anIsToUpdateOb = isNameChanged;
897
898     // At first we must to update the case name because of 
899     // automatic names generation for regions and zones
900     myEditedObject->SetName( aNewCaseName );
901     
902     // Zones: set parameters for automatic mode
903     int aMode = aPanel->getMode();
904     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
905     {
906       // Set objects in the specified order
907       if( myEditedObject->IsMustBeUpdated(  HYDROData_Entity::Geom_2d ) )
908       {
909         myEditedObject->RemoveGeometryObjects();
910         foreach ( const QString& aName, aPanel->getAllGeomObjects() )
911         {
912           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
913             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
914           if ( anObject.IsNull() )
915           {
916             continue;
917           }
918           myEditedObject->AddGeometryObject( anObject );
919         }
920
921         // Clear priority rules
922         //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
923           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
924         //@ASL }
925         // Set priority rules
926         foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
927           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
928                                   aRule.Object2, aRule.MergeType,
929                                   HYDROData_CalculationCase::DataTag_CustomRules );
930         }
931       }
932     }
933     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
934
935     if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
936     {
937       myShowZones = true;
938       myEditedObject->Update();
939       
940       AssignDefaultZonesColors();
941
942       //aPanel->setEditedObject( myEditedObject );
943       aPanel->refreshZonesBrowser();
944     
945       closePreview( false );
946       createPreview( false );
947
948       anIsToUpdateOb = true;
949     }
950     else
951     {
952       // Show zones
953       setZonesVisible( true );
954
955       if ( isNameChanged ) {
956         module()->getDataModel()->updateObjectTree( myEditedObject );
957       }
958     }
959
960     QApplication::restoreOverrideCursor();
961   }  
962 }
963
964 void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
965 {
966   if( theIndex==1 )
967   {
968     setGeomObjectsVisible( true );
969
970     closePreview( false );
971     createPreview( false );
972   }
973   if( theIndex==2 )
974   {
975     setLandCoverMapVisible( true );
976
977     closePreview( false );
978     createPreview( true );
979
980     // Hide zones
981     setZonesVisible( false );
982   }
983   else if( theIndex==3 )
984   {
985     AssignDefaultZonesColors();
986
987     closePreview( false );
988     createPreview( false );
989
990     // Show zones
991     setZonesVisible( true );
992   }
993 }
994
995 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
996 {
997   myShowZones = theIsVisible;
998   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
999   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1000   HYDROData_SequenceOfObjects aZones;
1001   Handle(HYDROData_Region) aRegion;
1002   if ( myPreviewViewManager ) 
1003   {
1004     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1005     {
1006       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1007       if ( !aCtx.IsNull() )
1008       {
1009         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1010         {
1011           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1012           if ( !aRegion.IsNull() )
1013           {
1014             aZones = aRegion->GetZones();
1015             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1016             for ( ; aZonesIter.More(); aZonesIter.Next() )
1017             {
1018               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1019             }
1020           }
1021         }
1022       }
1023
1024       module()->update( UF_OCCViewer );
1025     }
1026   }
1027 }
1028
1029 void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
1030 {
1031   myShowGeomObjects = theIsVisible;
1032
1033   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1034
1035   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1036   for ( ; anIter.More(); anIter.Next() ) {
1037     setObjectVisibility( anIter.Value(), theIsVisible );
1038   }
1039 }
1040
1041 void HYDROGUI_CalculationOp::setLandCoverMapVisible( bool theIsVisible )
1042 {
1043   myShowLandCoverMap = theIsVisible;
1044   setObjectVisibility( myEditedObject->GetLandCoverMap(), theIsVisible );  
1045 }
1046
1047 void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
1048 {
1049   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1050   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1051   HYDROData_SequenceOfObjects aZones;
1052   Handle(HYDROData_Region) aRegion;
1053   if ( myPreviewViewManager ) 
1054   {
1055     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1056     {
1057       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1058       if ( !aCtx.IsNull() )
1059       {
1060         int aCounter = 0;        
1061         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1062         {
1063           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1064           if ( !aRegion.IsNull() )
1065           {
1066             aZones = aRegion->GetZones();
1067             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1068             for ( ; aZonesIter.More(); aZonesIter.Next() )
1069             {
1070               // Zone
1071               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1072               if ( !aZone.IsNull() )
1073               {
1074                 QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
1075                 while (aFillingColor == Qt::red)
1076                   aFillingColor = GenerateDefaultZonesColor(++aCounter);
1077                 
1078                 aZone->SetColor(aFillingColor);
1079               }
1080             }
1081           }
1082         }
1083       }
1084     }
1085   }
1086 }
1087
1088 QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
1089                                                           float theSaturation/* = 0.5*/,
1090                                                           float theValue/* = 0.95*/ ) const
1091 {
1092   float aHue = ( rand()%1000 ) * 0.001f;
1093
1094   QColor aColor;
1095   aColor.setHsl( (int)(aHue*255.), 128, 128 );
1096   int r = aColor.red();
1097   int g = aColor.green();
1098   int b = aColor.blue();
1099   return ( aColor.isValid() ? aColor : Qt::darkBlue );
1100 }
1101
1102 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1103 {
1104   HYDROGUI_CalculationDlg* aPanel = 
1105     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1106   if ( !aPanel )
1107     return;
1108
1109   HYDROData_ListOfRules aRules;
1110   Handle(HYDROData_Entity) anObject1, anObject2;
1111   HYDROData_PriorityType aPriority;
1112   HYDROData_Zone::MergeType aMergeType;
1113   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1114   for ( int anIndex = 0; ; anIndex++ )
1115   {
1116     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1117       HYDROData_CustomRule aRule;
1118       aRule.Object1 = anObject1;
1119       aRule.Object2 = anObject2;
1120       aRule.Priority = aPriority;
1121       aRule.MergeType = aMergeType;
1122
1123       aRules << aRule;
1124     }
1125     else
1126       break;
1127   }
1128
1129   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1130     aPanel->setRules( aRules );  
1131 }
1132
1133 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList )
1134 {
1135   bool aRetValue = false;
1136
1137   QList<HYDROGUI_Zone*> aZonesList;
1138   HYDROGUI_Zone* aZone;
1139   // Get a list of dropped zones
1140   for ( int i = 0; i < theZonesList.length(); i++ )
1141   {
1142     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1143     if ( aZone )
1144     {
1145       aZonesList.append( aZone );
1146     }
1147   }
1148   if ( aZonesList.length() > 0 )
1149   {
1150     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
1151     aRetValue = true;
1152   }
1153    
1154   return aRetValue;
1155 }
1156
1157 void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
1158 {
1159   LightApp_Application* anApp = module()->getApp();
1160   HYDROData_SequenceOfObjects aSeq;
1161   if ( theLandCoverMap && myShowLandCoverMap )
1162   {
1163     aSeq.Append( myEditedObject->GetLandCoverMap() );
1164   }
1165   else if ( !theLandCoverMap && myShowGeomObjects )
1166   {
1167     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1168     aSeq.Append( aSeqGO );
1169   }
1170
1171   Handle(HYDROData_Entity) anEntity;
1172
1173   if ( myShowZones )
1174   {
1175     // Gather zones for displaying
1176     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1177     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1178     HYDROData_SequenceOfObjects aZones;
1179     Handle(HYDROData_Region) aRegion;
1180     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1181     {
1182       anEntity = aRegionsIter.Value();
1183       if ( !anEntity.IsNull() )
1184       {
1185         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1186         if ( !aRegion.IsNull() )
1187         {
1188           aZones = aRegion->GetZones();
1189           aSeq.Append( aZones );
1190         }
1191       }
1192     }
1193   }
1194
1195   // Get a boundary polyline if any
1196   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
1197
1198   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1199
1200   if ( !myActiveViewManager )
1201   {
1202     if ( aSeq.IsEmpty() )
1203       return;
1204
1205     myActiveViewManager = anApp->activeViewManager();
1206   }
1207
1208   if ( !myPreviewViewManager )
1209   {
1210     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1211       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1212     if ( myPreviewViewManager )
1213     {
1214       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1215                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1216
1217       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1218       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1219     }
1220   }
1221
1222   if ( !myPreviewViewManager )
1223     return;
1224
1225   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1226   {
1227     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1228     if ( !aCtx.IsNull() )
1229     {
1230       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1231       for ( ; anIter.More(); anIter.Next() )
1232       {
1233         setObjectVisibility( anIter.Value(), true );
1234       }
1235
1236       //Process the draw events for viewer
1237       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1238       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
1239         vw->onTopView();
1240     }
1241
1242     module()->update( UF_OCCViewer | UF_FitAll );
1243   }
1244 }
1245
1246 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1247 {
1248   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1249     return;
1250   }
1251
1252   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1253   if ( aViewer ) {
1254     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1255   }
1256 }
1257
1258 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1259 {
1260   closePreview();
1261 }
1262
1263 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1264 {
1265   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1266   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1267   QShortcut* aShortcut;
1268   foreach( aShortcut, aShortcuts )
1269   {
1270     if ( aShortcut->key() == 
1271       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1272       SUIT_DataBrowser::RenameShortcut ) ) )
1273     {
1274       aShortcut->setEnabled( true );
1275     }
1276   }
1277
1278
1279   if( myPreviewViewManager )
1280   {
1281     // Hide all the displayed objects in the preview view
1282     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1283     if ( aViewer ) {
1284       size_t aViewId = (size_t)aViewer;
1285       HYDROData_Iterator anIterator( doc() );
1286       for( ; anIterator.More(); anIterator.Next() ) {
1287         Handle(HYDROData_Entity) anObject = anIterator.Current();
1288         if( !anObject.IsNull() ) {
1289           module()->setObjectVisible( aViewId, anObject, false );
1290         }
1291       }
1292     }
1293
1294     if ( theRemoveViewManager )
1295     {
1296       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1297                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1298
1299       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1300       myPreviewViewManager = NULL;
1301     }
1302   }
1303
1304   if( myActiveViewManager && theRemoveViewManager )
1305   {
1306     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1307     myActiveViewManager = NULL;
1308   }
1309 }
1310
1311 void HYDROGUI_CalculationOp::setAvailableGroups()
1312 {
1313   HYDROGUI_CalculationDlg* aPanel = 
1314       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1315
1316   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1317   QStringList aList, anEntryList;
1318   getNamesAndEntries( aSeq, aList, anEntryList );
1319
1320   QStringList aGroupsNames;
1321
1322   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1323   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1324   {
1325     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
1326     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1327     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1328     {
1329       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
1330       aGroupsNames.append( aGroup->GetName() );
1331     }
1332   }
1333   if( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) ) {
1334     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1335       Handle(HYDROData_ShapesGroup) aGeomGroup =
1336         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1337       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1338         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1339       }
1340     }
1341   }
1342
1343   aPanel->setAvailableGroups( aGroupsNames );
1344   aPanel->includeGroups( aList );
1345
1346   //@ASL: bool isUpdated = myEditedObject->IsMustBeUpdated();
1347 }
1348
1349 void HYDROGUI_CalculationOp::onAddGroups()
1350 {
1351   HYDROGUI_CalculationDlg* aPanel = 
1352     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1353   if ( !aPanel )
1354     return;
1355
1356   // Add geometry objects selected in the module browser to the calculation case
1357   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1358   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1359     return;
1360
1361   QStringList anAddedList;
1362   for (int i = 0; i < aSelectedList.length(); i++)
1363   {
1364     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1365       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1366     if ( aGroup.IsNull() )
1367       continue;
1368
1369     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1370       anAddedList.append( aGroup->GetName() );
1371   }
1372
1373   if ( !anAddedList.isEmpty() )
1374   {
1375     aPanel->includeGroups( anAddedList );
1376   }
1377 }
1378
1379 void HYDROGUI_CalculationOp::onRemoveGroups()
1380 {
1381   // Remove selected objects from the calculation case
1382   HYDROGUI_CalculationDlg* aPanel = 
1383     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1384   if ( !aPanel )
1385     return;
1386
1387   QStringList aSelectedList = aPanel->getSelectedGroups();
1388   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1389     return;
1390
1391   for (int i = 0; i < aSelectedList.length(); i++)
1392   {
1393     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1394       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1395     if ( aGroup.IsNull() )
1396       continue;
1397
1398     myEditedObject->RemoveGeometryGroup( aGroup );
1399   }
1400
1401   aPanel->excludeGroups( aSelectedList );
1402 }
1403
1404 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1405 {
1406   HYDROGUI_CalculationDlg* aPanel = 
1407     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1408   if ( !aPanel )
1409     return;
1410
1411   if ( !confirmModeChange() ) {
1412     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1413     return;
1414   }
1415
1416   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1417   aPanel->setMode( theMode );
1418 }
1419
1420 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1421 {
1422   HYDROGUI_CalculationDlg* aPanel = 
1423     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1424   if ( !aPanel )
1425     return;
1426
1427   isConfirmed = confirmOrderChange();
1428   if( isConfirmed )
1429     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1430 }
1431
1432 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1433 {
1434   HYDROGUI_CalculationDlg* aPanel = 
1435     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1436   if ( !aPanel )
1437     return;
1438
1439   isConfirmed = confirmRuleChange();
1440   if( isConfirmed )
1441     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1442 }
1443
1444 void HYDROGUI_CalculationOp::onRegenerateColors()
1445 {
1446   // For geometry zones
1447   AssignDefaultZonesColors();
1448   setZonesVisible( false );
1449   setZonesVisible( true );
1450 }