Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[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_Tool2.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   setGeomObjectsVisible( true );
170
171   createPreview( false );
172 }
173
174 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
175                                                 QStringList& theNames, QStringList& theEntries ) const
176 {
177  
178   theNames.clear();
179   theEntries.clear();
180   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
181   for ( ; anIter.More(); anIter.Next() )
182   {
183     Handle(HYDROData_Entity) anEntity = anIter.Value();
184     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
185     //  continue;
186
187     theNames.append( anEntity->GetName() );
188     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
189   }
190 }
191
192 void HYDROGUI_CalculationOp::abortOperation()
193 {
194   closePreview();
195   // Abort transaction
196   abortDocOperation();
197   HYDROGUI_Operation::abortOperation();
198   module()->getApp()->updateObjectBrowser();
199 }
200
201 void HYDROGUI_CalculationOp::commitOperation()
202 {
203   closePreview();
204   // Commit transaction
205   commitDocOperation();
206   HYDROGUI_Operation::commitOperation();
207 }
208
209 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
210 {
211   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
212
213   // Connect signals and slots
214   connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );  
215   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
216   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
217   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
218   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
219
220   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
221
222   connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
223
224   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
225   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones( const int ) ) );
226   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
227   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
228   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
229     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
230   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
231     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
232   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
233     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
234   connect( aPanel, SIGNAL( objectsSelected() ), 
235            SLOT( onObjectsSelected() ) );
236   connect( aPanel, SIGNAL( landCoverMapSelected( const QString & ) ), 
237            SLOT( onLandCoverMapSelected( const QString & ) ) );
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   // Remove old presentation of land cover map from the operation viewer  
293   Handle(HYDROData_LandCoverMap) aLandCoverMap = myEditedObject->GetLandCoverMap();
294   if ( !aLandCoverMap.IsNull() )
295   {
296     setObjectVisibility( aLandCoverMap, false );
297     anIsToUpdateViewer = true;
298   }
299
300   // Set the selected Strickler table to the calculation case
301   Handle(HYDROData_StricklerTable) aNewStricklerTable = Handle(HYDROData_StricklerTable)::DownCast(
302     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_STRICKLER_TABLE ) );
303   myEditedObject->SetStricklerTable( aNewStricklerTable );
304
305   if ( myPreviewViewManager )
306   {
307     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
308     if ( aViewer )
309     {
310       if ( !aNewStricklerTable.IsNull() )
311       {
312         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
313         if ( !aCtx.IsNull() )
314         {
315           if ( !aLandCoverMap.IsNull() )
316           {
317             setObjectVisibility( aLandCoverMap, true );
318             anIsToUpdateViewer = true;
319           }          
320         }
321       }
322
323       if ( anIsToUpdateViewer )
324         module()->update( UF_OCCViewer );
325     }
326   }
327 }
328
329 void HYDROGUI_CalculationOp::onObjectsSelected()
330 {
331   HYDROGUI_CalculationDlg* aPanel = 
332     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
333
334   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
335   QMap<QString, bool> aSelectedObjsMap;
336   foreach( QString aName, aSelectedObjs )
337     aSelectedObjsMap[aName] = true;
338
339
340   // Select the appropriate geometry object shape in the viewer
341   selectionMgr()->clearSelected();
342
343   // Unhighlight all objects except selected
344   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
345   Handle(HYDROData_Entity) anEntity;
346   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
347   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
348   bool isSelected;
349   QString aName;
350   for ( ; anIter.More(); anIter.Next() )
351   {
352     anEntity = anIter.Value();
353     if ( !anEntity.IsNull() )
354     {
355       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
356       if ( aShape )
357       {
358         aName = anEntity->GetName();
359         isSelected = aSelectedObjsMap.contains( aName );
360         aShape->highlight( isSelected, false );
361         aShape->update( false, false );
362         aLastShape = aShape;
363       }
364     }
365   }
366   if( aLastShape )
367     aLastShape->update( true, false );
368 }
369
370 void HYDROGUI_CalculationOp::onLandCoverMapSelected( const QString & theObjName )
371 {
372   bool anIsToUpdateViewer = false;
373   
374   // Remove old presentation of land cover map from the operation viewer  
375   Handle(HYDROData_LandCoverMap) aPrevLandCoverMap = myEditedObject->GetLandCoverMap();
376   if ( !aPrevLandCoverMap.IsNull() )
377   {
378     setObjectVisibility( aPrevLandCoverMap, false );
379     anIsToUpdateViewer = true;
380   }
381
382   // Select the appropriate land cover map shape in the viewer
383   selectionMgr()->clearSelected();
384
385   // Set the selected land cover map to the calculation case
386   Handle(HYDROData_LandCoverMap) aNewLandCoverMap = Handle(HYDROData_LandCoverMap)::DownCast(
387     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_LAND_COVER_MAP ) );
388   myEditedObject->SetLandCoverMap( aNewLandCoverMap );
389
390   createPreview( true );
391 }
392
393 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
394 {
395   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
396   HYDROGUI_Zone* aZoneItem;
397   selectionMgr()->clearSelected();
398   if ( aRegionItem )
399   {
400     // Select a region in preview
401     SUIT_DataOwnerPtrList aList( true );
402     DataObjectList aZones = aRegionItem->children();
403     for ( int i = 0; i < aZones.length(); i++ )
404     {
405       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
406       if ( aZoneItem )
407       {
408         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
409       }
410     }
411     selectionMgr()->setSelected( aList );
412   }
413   else
414   {
415     // select a single zone
416     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
417     if ( aZoneItem )
418     {
419       SUIT_DataOwnerPtrList aList( true );
420       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
421       selectionMgr()->setSelected( aList );
422     }
423   }
424 }
425
426 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem,
427                                           const QList<SUIT_DataObject*>& theZonesList )
428 {
429   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
430   if ( aRegion )
431   {
432     QList<HYDROGUI_Zone*> aZonesList;
433     HYDROGUI_Zone* aZone;
434     // Get a list of dropped zones
435     for ( int i = 0; i < theZonesList.length(); i++ )
436     {
437       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
438       if ( aZone )
439       {
440         aZonesList.append( aZone );
441       }
442     }
443     if ( aZonesList.length() > 0 )
444     {
445       aRegion->addZones( aZonesList );
446       HYDROGUI_CalculationDlg* aPanel = 
447         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
448       if ( aPanel )
449         aPanel->refreshZonesBrowser();
450       createPreview( false );
451     }
452   }
453 }
454
455 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
456 {
457   if ( createRegion( theZonesList ) )
458   {
459     HYDROGUI_CalculationDlg* aPanel = 
460       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
461     if ( aPanel )
462     {
463       aPanel->refreshZonesBrowser();
464     }
465     createPreview( false );
466   }
467 }
468
469 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theMergeObjectName )
470 {
471   HYDROGUI_CalculationDlg* aPanel = 
472     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
473   if ( aPanel )
474   {
475     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
476     if ( aZone )
477     {
478       aZone->setMergeType( theMergeType, theMergeObjectName );
479       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
480       if ( aShape )
481       {
482         aShape->update( true, false );
483       }
484     }
485     aPanel->refreshZonesBrowser();
486   }
487 }
488
489 void HYDROGUI_CalculationOp::onAddObjects()
490 {
491   HYDROGUI_CalculationDlg* aPanel = 
492     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
493   if ( !aPanel )
494     return;
495
496   // Add geometry objects selected in the module browser to the calculation case
497   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
498   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
499     return;
500
501   QStringList anAddedList;
502   for (int i = 0; i < aSelectedList.length(); i++)
503   {
504     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
505       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
506     if ( anObject.IsNull() )
507       continue;
508
509     if ( myEditedObject->AddGeometryObject( anObject ) )
510       anAddedList.append( anObject->GetName() );
511   }
512
513   if ( !anAddedList.isEmpty() )
514   {
515     aPanel->includeGeomObjects( anAddedList );
516     createPreview( false );
517   }
518 }
519
520 void HYDROGUI_CalculationOp::onRemoveObjects()
521 {
522   // Remove selected objects from the calculation case
523   HYDROGUI_CalculationDlg* aPanel = 
524     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
525   if ( !aPanel )
526     return;
527
528   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
529   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
530     return;
531
532   for (int i = 0; i < aSelectedList.length(); i++)
533   {
534     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
535       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
536     if ( anObject.IsNull() )
537       continue;
538
539     setObjectVisibility( anObject, false );
540     myEditedObject->RemoveGeometryObject( anObject );
541   }
542
543   module()->update( UF_OCCViewer );
544   aPanel->excludeGeomObjects( aSelectedList );
545 }
546
547 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
548 {
549   // Check if the case is already modified or not
550   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
551   if ( !isConfirmed )
552   {
553     // If not modified check if the case has already defined regions with zones
554     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
555     if ( aSeq.Length() > 0 )
556     {
557       // If there are already defined zones then ask a user to confirm zones recalculation
558       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
559                                tr( "REGIONS_CHANGED" ),
560                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
561                                QMessageBox::Yes | QMessageBox::No,
562                                QMessageBox::No ) == QMessageBox::Yes );
563     }
564     else
565     {
566       isConfirmed = true; // No regions - no zones - nothing to recalculate
567     }
568   }
569   return isConfirmed;
570 }
571
572 bool HYDROGUI_CalculationOp::confirmOrderChange() const
573 {
574   // Check if the case is already modified or not
575   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
576   if ( !isConfirmed )
577   {
578     // If not modified check if the case has already defined regions with zones
579     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
580     if ( aSeq.Length() > 0 )
581     {
582       // If there are already defined zones then ask a user to confirm zones recalculation
583       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
584                                tr( "ORDER_CHANGED" ),
585                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
586                                QMessageBox::Yes | QMessageBox::No,
587                                QMessageBox::No ) == QMessageBox::Yes );
588     }
589     else
590     {
591       isConfirmed = true; // No regions - no zones - nothing to recalculate
592     }
593   }
594   return isConfirmed;
595 }
596
597 bool HYDROGUI_CalculationOp::confirmRuleChange() 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( "RULE_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::confirmModeChange() 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( "MODE_CHANGED" ),
635                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
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::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
648 {
649   HYDROData_WarningType aType = theWarning.Type;
650   if ( aType == WARN_OK ) {
651     return true;
652   }
653
654   QString aTitle;
655   QString aMsg;
656   switch ( aType )
657   {
658     case WARN_EMPTY_REGIONS:
659       aTitle = tr( "EMPTY_REGIONS" );
660       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
661       break;
662     default:
663       aTitle = tr( "WARNING" );
664       aMsg = theWarning.Data;
665   }
666
667
668   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
669                                            aTitle, aMsg,
670                                            QMessageBox::Yes | QMessageBox::No,
671                                            QMessageBox::No );
672
673   return ( anAnswer == QMessageBox::Yes );
674 }
675
676 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
677                                            QString& theErrorMsg,
678                                            QStringList& theBrowseObjectsEntries )
679 {
680   HYDROGUI_CalculationDlg* aPanel = 
681     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
682   if ( !aPanel )
683     return false;
684
685   if( !myIsEdit )
686   {
687     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
688     theBrowseObjectsEntries.append( anEntry );
689   }
690
691   // For manual mode priority rules are redundant
692   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
693     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
694   }
695  
696   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
697
698   return true;
699 }
700
701 void HYDROGUI_CalculationOp::onApply()
702 {
703   // Check warnings
704   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
705   if ( aWarning.Type != WARN_OK ) {
706     if ( !confirmContinueWithWarning( aWarning ) ) {
707       // Go back to the first page
708       HYDROGUI_CalculationDlg* aPanel = 
709         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
710       if ( aPanel ) {
711         aPanel->onFirstPage();
712       }
713       return;
714     }
715   }
716
717   QApplication::setOverrideCursor( Qt::WaitCursor );
718
719   int anUpdateFlags = 0;
720   QString anErrorMsg;
721   QStringList aBrowseObjectsEntries;
722
723   bool aResult = false;
724   
725   try
726   {
727     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
728   }
729   catch ( Standard_Failure )
730   {
731     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
732     anErrorMsg = aFailure->GetMessageString();
733     aResult = false;
734   }
735   catch ( ... )
736   {
737     aResult = false;
738   }
739   
740   QApplication::restoreOverrideCursor();
741
742   if ( aResult )
743   {
744     module()->update( anUpdateFlags );
745     commit();
746     browseObjects( aBrowseObjectsEntries );
747   }
748   else
749   {
750     abort();
751     QString aMsg = tr( "INPUT_VALID_DATA" );
752     if( !anErrorMsg.isEmpty() )
753       aMsg.prepend( anErrorMsg + "\n" );
754     SUIT_MessageBox::critical( module()->getApp()->desktop(),
755                                tr( "INSUFFICIENT_INPUT_DATA" ),
756                                aMsg ); 
757   }
758 }
759
760 void HYDROGUI_CalculationOp::onNext( const int theIndex )
761 {
762   if( theIndex==1 )
763   {
764     setAvailableGroups();
765   }
766   else if( theIndex==2 )
767   {
768     // Land cover map panel
769      HYDROGUI_CalculationDlg* aPanel = 
770       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
771     if ( !aPanel )
772       return;
773
774     setLandCoverMapVisible( true );
775
776     QStringList aList;
777     QStringList anEntryList;
778
779     // Get all land cover map objects to fill in combo-box
780     Handle(HYDROData_LandCoverMap) aLandCoverMapObj;
781     QString aLandCoverMapName;
782
783     aList.clear();
784     anEntryList.clear();
785     HYDROData_Iterator anIter( doc(), KIND_LAND_COVER_MAP );
786     for ( ; anIter.More(); anIter.Next() )
787     {
788       aLandCoverMapObj = Handle(HYDROData_LandCoverMap)::DownCast( anIter.Current() );
789
790       if ( !aLandCoverMapObj.IsNull() )
791       { 
792         aLandCoverMapName = aLandCoverMapObj->GetName();
793         if ( !aLandCoverMapName.isEmpty() )
794         {
795           aList.append( aLandCoverMapName );
796           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj ) );
797         }
798       }
799     }
800     aPanel->setLandCoverMapsNames( aList, anEntryList );
801     aLandCoverMapObj = myEditedObject->GetLandCoverMap();
802     if ( !aList.isEmpty() )
803     {
804       if ( aLandCoverMapObj.IsNull() )
805         aPanel->setLandCoverMap( aList.at( 0 ), false );
806       else if ( myIsEdit )
807         aPanel->setLandCoverMap( aList.at( 0 ), true );
808       else
809         aPanel->setLandCoverMap( aList.at( aList.indexOf( aLandCoverMapObj->GetName() ) ), true );
810     }
811
812     // Get all Strickler table objects to fill in combo-box
813     Handle(HYDROData_StricklerTable) aStricklerTableObj;
814     QString aStricklerTableName;
815
816     aList.clear();
817     anEntryList.clear();
818     anIter = HYDROData_Iterator( doc(), KIND_STRICKLER_TABLE );
819     for ( ; anIter.More(); anIter.Next() )
820     {
821       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
822
823       if ( !aStricklerTableObj.IsNull() )
824       { 
825         aStricklerTableName = aStricklerTableObj->GetName();
826         if ( !aStricklerTableName.isEmpty() )
827         {
828           aList.append( aStricklerTableName );
829           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
830         }        
831       }
832     }
833     aPanel->setStricklerTableNames( aList, anEntryList );
834     //@ASL: bool anUpdateState = myEditedObject->IsMustBeUpdated();
835     if ( !aList.isEmpty() )
836       aPanel->setStricklerTable( aList.at( 0 ), false );
837     //@ASL: myEditedObject->SetToUpdate( anUpdateState );
838
839     if ( !myEditedObject.IsNull() )
840     {
841       if ( myIsEdit )
842       {      
843         // Select the certain Strickler table object in combo-box
844         aStricklerTableObj = myEditedObject->GetStricklerTable();
845         if ( aStricklerTableObj.IsNull() )
846         {
847           aPanel->setStricklerTable( QString() );
848         }
849         else
850         {
851           aStricklerTableName = aStricklerTableObj->GetName();
852           aPanel->setStricklerTable( aStricklerTableName );
853         }
854
855         // Select the certain land cover map object in combo-box
856         if ( aLandCoverMapObj.IsNull() )
857         {
858           aPanel->setLandCoverMap( QString() );
859         }
860         else
861         {
862           aLandCoverMapName = aLandCoverMapObj->GetName();
863           aPanel->setLandCoverMap( aLandCoverMapName );
864         }
865       }
866     }
867
868     closePreview( false );
869     createPreview( true );
870   }
871   else if( theIndex==3 )
872   {
873     HYDROGUI_CalculationDlg* aPanel = 
874       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
875     if ( !aPanel )
876       return;
877
878     QApplication::setOverrideCursor( Qt::WaitCursor );
879
880     setGeomObjectsVisible( false );
881     setLandCoverMapVisible( false );
882
883     QString aNewCaseName = aPanel->getObjectName();
884     QString anOldCaseName = myEditedObject->GetName();
885     bool isNameChanged = anOldCaseName != aNewCaseName;
886   
887     bool anIsToUpdateOb = isNameChanged;
888
889     // At first we must to update the case name because of 
890     // automatic names generation for regions and zones
891     myEditedObject->SetName( aNewCaseName );
892     
893     // Zones: set parameters for automatic mode
894     int aMode = aPanel->getMode();
895     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
896     {
897       // Set objects in the specified order
898       if( myEditedObject->IsMustBeUpdated(  HYDROData_Entity::Geom_2d ) )
899       {
900         myEditedObject->RemoveGeometryObjects();
901         foreach ( const QString& aName, aPanel->getAllGeomObjects() )
902         {
903           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
904             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
905           if ( anObject.IsNull() )
906           {
907             continue;
908           }
909           myEditedObject->AddGeometryObject( anObject );
910         }
911
912         // Clear priority rules
913         //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
914           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
915         //@ASL }
916         // Set priority rules
917         foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
918           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
919                                   aRule.Object2, aRule.MergeType,
920                                   HYDROData_CalculationCase::DataTag_CustomRules );
921         }
922       }
923     }
924     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
925
926     if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
927     {
928       myShowZones = true;
929       myEditedObject->Update();
930       
931       AssignDefaultZonesColors();
932
933       //aPanel->setEditedObject( myEditedObject );
934       aPanel->refreshZonesBrowser();
935     
936       closePreview( false );
937       createPreview( false );
938
939       anIsToUpdateOb = true;
940     }
941     else
942     {
943       // Show zones
944       setZonesVisible( true );
945
946       if ( isNameChanged ) {
947         module()->getDataModel()->updateObjectTree( myEditedObject );
948       }
949     }
950
951     QApplication::restoreOverrideCursor();
952   }  
953 }
954
955 void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
956 {
957   if( theIndex==1 )
958   {
959     setGeomObjectsVisible( true );
960
961     closePreview( false );
962     createPreview( false );
963   }
964   if( theIndex==2 )
965   {
966     setLandCoverMapVisible( true );
967
968     closePreview( false );
969     createPreview( true );
970
971     // Hide zones
972     setZonesVisible( false );
973   }
974   else if( theIndex==3 )
975   {
976     AssignDefaultZonesColors();
977
978     closePreview( false );
979     createPreview( false );
980
981     // Show zones
982     setZonesVisible( true );
983   }
984 }
985
986 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
987 {
988   myShowZones = theIsVisible;
989   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
990   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
991   HYDROData_SequenceOfObjects aZones;
992   Handle(HYDROData_Region) aRegion;
993   if ( myPreviewViewManager ) 
994   {
995     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
996     {
997       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
998       if ( !aCtx.IsNull() )
999       {
1000         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1001         {
1002           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1003           if ( !aRegion.IsNull() )
1004           {
1005             aZones = aRegion->GetZones();
1006             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1007             for ( ; aZonesIter.More(); aZonesIter.Next() )
1008             {
1009               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1010             }
1011           }
1012         }
1013       }
1014
1015       module()->update( UF_OCCViewer );
1016     }
1017   }
1018 }
1019
1020 void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
1021 {
1022   myShowGeomObjects = theIsVisible;
1023
1024   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1025
1026   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1027   for ( ; anIter.More(); anIter.Next() ) {
1028     setObjectVisibility( anIter.Value(), theIsVisible );
1029   }
1030 }
1031
1032 void HYDROGUI_CalculationOp::setLandCoverMapVisible( bool theIsVisible )
1033 {
1034   myShowLandCoverMap = theIsVisible;
1035   setObjectVisibility( myEditedObject->GetLandCoverMap(), theIsVisible );  
1036 }
1037
1038 void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
1039 {
1040   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1041   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1042   HYDROData_SequenceOfObjects aZones;
1043   Handle(HYDROData_Region) aRegion;
1044   if ( myPreviewViewManager ) 
1045   {
1046     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1047     {
1048       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1049       if ( !aCtx.IsNull() )
1050       {
1051         int aCounter = 0;        
1052         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1053         {
1054           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1055           if ( !aRegion.IsNull() )
1056           {
1057             aZones = aRegion->GetZones();
1058             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1059             for ( ; aZonesIter.More(); aZonesIter.Next() )
1060             {
1061               // Zone
1062               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1063               if ( !aZone.IsNull() )
1064               {
1065                 QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
1066                 while (aFillingColor == Qt::red)
1067                   aFillingColor = GenerateDefaultZonesColor(++aCounter);
1068                 
1069                 aZone->SetColor(aFillingColor);
1070               }
1071             }
1072           }
1073         }
1074       }
1075     }
1076   }
1077 }
1078
1079 QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
1080                                                           float theSaturation/* = 0.5*/,
1081                                                           float theValue/* = 0.95*/ ) const
1082 {
1083   float aHue = ( rand()%1000 ) * 0.001f;
1084
1085   QColor aColor;
1086   aColor.setHsl( (int)(aHue*255.), 128, 128 );
1087   int r = aColor.red();
1088   int g = aColor.green();
1089   int b = aColor.blue();
1090   return ( aColor.isValid() ? aColor : Qt::darkBlue );
1091 }
1092
1093 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1094 {
1095   HYDROGUI_CalculationDlg* aPanel = 
1096     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1097   if ( !aPanel )
1098     return;
1099
1100   HYDROData_ListOfRules aRules;
1101   Handle(HYDROData_Entity) anObject1, anObject2;
1102   HYDROData_PriorityType aPriority;
1103   HYDROData_Zone::MergeType aMergeType;
1104   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1105   for ( int anIndex = 0; ; anIndex++ )
1106   {
1107     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1108       HYDROData_CustomRule aRule;
1109       aRule.Object1 = anObject1;
1110       aRule.Object2 = anObject2;
1111       aRule.Priority = aPriority;
1112       aRule.MergeType = aMergeType;
1113
1114       aRules << aRule;
1115     }
1116     else
1117       break;
1118   }
1119
1120   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1121     aPanel->setRules( aRules );  
1122 }
1123
1124 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList )
1125 {
1126   bool aRetValue = false;
1127
1128   QList<HYDROGUI_Zone*> aZonesList;
1129   HYDROGUI_Zone* aZone;
1130   // Get a list of dropped zones
1131   for ( int i = 0; i < theZonesList.length(); i++ )
1132   {
1133     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1134     if ( aZone )
1135     {
1136       aZonesList.append( aZone );
1137     }
1138   }
1139   if ( aZonesList.length() > 0 )
1140   {
1141     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
1142     aRetValue = true;
1143   }
1144    
1145   return aRetValue;
1146 }
1147
1148 void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
1149 {
1150   LightApp_Application* anApp = module()->getApp();
1151   HYDROData_SequenceOfObjects aSeq;
1152   if ( theLandCoverMap && myShowLandCoverMap )
1153   {
1154     aSeq.Append( myEditedObject->GetLandCoverMap() );
1155   }
1156   else if ( !theLandCoverMap && myShowGeomObjects )
1157   {
1158     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1159     aSeq.Append( aSeqGO );
1160   }
1161
1162   Handle(HYDROData_Entity) anEntity;
1163
1164   if ( myShowZones )
1165   {
1166     // Gather zones for displaying
1167     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1168     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1169     HYDROData_SequenceOfObjects aZones;
1170     Handle(HYDROData_Region) aRegion;
1171     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1172     {
1173       anEntity = aRegionsIter.Value();
1174       if ( !anEntity.IsNull() )
1175       {
1176         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1177         if ( !aRegion.IsNull() )
1178         {
1179           aZones = aRegion->GetZones();
1180           aSeq.Append( aZones );
1181         }
1182       }
1183     }
1184   }
1185
1186   // Get a boundary polyline if any
1187   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
1188
1189   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1190
1191   if ( !myActiveViewManager )
1192   {
1193     if ( aSeq.IsEmpty() )
1194       return;
1195
1196     myActiveViewManager = anApp->activeViewManager();
1197   }
1198
1199   if ( !myPreviewViewManager )
1200   {
1201     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1202       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1203     if ( myPreviewViewManager )
1204     {
1205       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1206                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1207
1208       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1209       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1210     }
1211   }
1212
1213   if ( !myPreviewViewManager )
1214     return;
1215
1216   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1217   {
1218     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1219     if ( !aCtx.IsNull() )
1220     {
1221       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1222       for ( ; anIter.More(); anIter.Next() )
1223       {
1224         setObjectVisibility( anIter.Value(), true );
1225       }
1226
1227       //Process the draw events for viewer
1228       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1229       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
1230         vw->onTopView();
1231     }
1232
1233     module()->update( UF_OCCViewer | UF_FitAll );
1234   }
1235 }
1236
1237 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1238 {
1239   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1240     return;
1241   }
1242
1243   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1244   if ( aViewer ) {
1245     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1246   }
1247 }
1248
1249 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1250 {
1251   closePreview();
1252 }
1253
1254 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1255 {
1256   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1257   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1258   QShortcut* aShortcut;
1259   foreach( aShortcut, aShortcuts )
1260   {
1261     if ( aShortcut->key() == 
1262       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1263       SUIT_DataBrowser::RenameShortcut ) ) )
1264     {
1265       aShortcut->setEnabled( true );
1266     }
1267   }
1268
1269
1270   if( myPreviewViewManager )
1271   {
1272     // Hide all the displayed objects in the preview view
1273     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1274     if ( aViewer ) {
1275       size_t aViewId = (size_t)aViewer;
1276       HYDROData_Iterator anIterator( doc() );
1277       for( ; anIterator.More(); anIterator.Next() ) {
1278         Handle(HYDROData_Entity) anObject = anIterator.Current();
1279         if( !anObject.IsNull() ) {
1280           module()->setObjectVisible( aViewId, anObject, false );
1281         }
1282       }
1283     }
1284
1285     if ( theRemoveViewManager )
1286     {
1287       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1288                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1289
1290       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1291       myPreviewViewManager = NULL;
1292     }
1293   }
1294
1295   if( myActiveViewManager && theRemoveViewManager )
1296   {
1297     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1298     myActiveViewManager = NULL;
1299   }
1300 }
1301
1302 void HYDROGUI_CalculationOp::setAvailableGroups()
1303 {
1304   HYDROGUI_CalculationDlg* aPanel = 
1305       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1306
1307   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1308   QStringList aList, anEntryList;
1309   getNamesAndEntries( aSeq, aList, anEntryList );
1310
1311   QStringList aGroupsNames;
1312
1313   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1314   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1315   {
1316     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
1317     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1318     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1319     {
1320       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
1321       aGroupsNames.append( aGroup->GetName() );
1322     }
1323   }
1324   if( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) ) {
1325     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1326       Handle(HYDROData_ShapesGroup) aGeomGroup =
1327         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1328       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1329         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1330       }
1331     }
1332   }
1333
1334   aPanel->setAvailableGroups( aGroupsNames );
1335   aPanel->includeGroups( aList );
1336
1337   //@ASL: bool isUpdated = myEditedObject->IsMustBeUpdated();
1338 }
1339
1340 void HYDROGUI_CalculationOp::onAddGroups()
1341 {
1342   HYDROGUI_CalculationDlg* aPanel = 
1343     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1344   if ( !aPanel )
1345     return;
1346
1347   // Add geometry objects selected in the module browser to the calculation case
1348   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1349   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1350     return;
1351
1352   QStringList anAddedList;
1353   for (int i = 0; i < aSelectedList.length(); i++)
1354   {
1355     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1356       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1357     if ( aGroup.IsNull() )
1358       continue;
1359
1360     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1361       anAddedList.append( aGroup->GetName() );
1362   }
1363
1364   if ( !anAddedList.isEmpty() )
1365   {
1366     aPanel->includeGroups( anAddedList );
1367   }
1368 }
1369
1370 void HYDROGUI_CalculationOp::onRemoveGroups()
1371 {
1372   // Remove selected objects from the calculation case
1373   HYDROGUI_CalculationDlg* aPanel = 
1374     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1375   if ( !aPanel )
1376     return;
1377
1378   QStringList aSelectedList = aPanel->getSelectedGroups();
1379   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1380     return;
1381
1382   for (int i = 0; i < aSelectedList.length(); i++)
1383   {
1384     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1385       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1386     if ( aGroup.IsNull() )
1387       continue;
1388
1389     myEditedObject->RemoveGeometryGroup( aGroup );
1390   }
1391
1392   aPanel->excludeGroups( aSelectedList );
1393 }
1394
1395 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1396 {
1397   HYDROGUI_CalculationDlg* aPanel = 
1398     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1399   if ( !aPanel )
1400     return;
1401
1402   if ( !confirmModeChange() ) {
1403     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1404     return;
1405   }
1406
1407   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1408   aPanel->setMode( theMode );
1409 }
1410
1411 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1412 {
1413   HYDROGUI_CalculationDlg* aPanel = 
1414     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1415   if ( !aPanel )
1416     return;
1417
1418   isConfirmed = confirmOrderChange();
1419   if( isConfirmed )
1420     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1421 }
1422
1423 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1424 {
1425   HYDROGUI_CalculationDlg* aPanel = 
1426     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1427   if ( !aPanel )
1428     return;
1429
1430   isConfirmed = confirmRuleChange();
1431   if( isConfirmed )
1432     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1433 }
1434
1435 void HYDROGUI_CalculationOp::onRegenerateColors()
1436 {
1437   // For geometry zones
1438   AssignDefaultZonesColors();
1439   setZonesVisible( false );
1440   setZonesVisible( true );
1441 }