Salome HOME
Merge branch 'BR_v14_rc' into BR_quadtree
[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   myShowLandCovers( false ),
61   myShowZones( false ),
62   myUpdateLandCoverZonesPrs( false )
63 {
64   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
65 }
66
67 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
68 {
69   closePreview();
70 }
71
72 void HYDROGUI_CalculationOp::startOperation()
73 {
74   HYDROGUI_Operation::startOperation();
75   
76   // Begin transaction
77   startDocOperation();
78
79   HYDROGUI_CalculationDlg* aPanel = 
80     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
81   if ( !aPanel )
82     return;
83
84   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
85   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
86   QShortcut* aShortcut;
87   foreach( aShortcut, aShortcuts )
88   {
89     if ( aShortcut->key() == 
90       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
91       SUIT_DataBrowser::RenameShortcut ) ) )
92     {
93       aShortcut->setEnabled( false );
94     }
95   }
96
97   aPanel->reset();
98   QStringList aList;
99   QStringList anEntryList;
100   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
101   getNamesAndEntries( aSeq, aList, anEntryList );
102   aPanel->setAllGeomObjects( aList, anEntryList );
103
104   // Get all polylines
105   aList.clear();
106   anEntryList.clear();
107   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
108   Handle(HYDROData_PolylineXY) aPolylineObj;
109   QString aPolylineName;
110   for ( ; anIter.More(); anIter.Next() )
111   {
112     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
113
114     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
115     { 
116       // Check the polyline shape
117       TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
118       if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
119         aPolylineName = aPolylineObj->GetName();
120         if ( !aPolylineName.isEmpty() )
121         {
122           aList.append( aPolylineName );
123           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
124         }
125       }
126     }
127   }
128   aPanel->setPolylineNames( aList, anEntryList );
129
130   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
131
132   myEditedObject.Nullify();
133   if ( myIsEdit )
134   {
135     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
136     if ( !myEditedObject.IsNull() )
137     {
138       aPanel->setMode( myEditedObject->GetAssignmentMode() );
139       anObjectName = myEditedObject->GetName();
140       aPolylineObj = myEditedObject->GetBoundaryPolyline();
141       if ( aPolylineObj.IsNull() )
142       {
143         aPanel->setBoundary( QString() );
144       }
145       else
146       {
147         aPolylineName = aPolylineObj->GetName();
148         aPanel->setBoundary( aPolylineName );
149       }
150
151       aSeq = myEditedObject->GetGeometryObjects();
152       getNamesAndEntries( aSeq, aList, anEntryList );
153       aPanel->includeGeomObjects( aList );
154
155       // set rules
156       setRules( HYDROData_CalculationCase::DataTag_CustomRules );      
157     }
158   }
159   else
160   {
161     myEditedObject =
162       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
163     myEditedObject->SetName( anObjectName );
164     myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getMode() );
165   }
166
167   aPanel->setObjectName( anObjectName );
168   aPanel->setEditedObject( myEditedObject );
169
170   createPreview( false );
171 }
172
173 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
174                                                 QStringList& theNames, QStringList& theEntries ) const
175 {
176  
177   theNames.clear();
178   theEntries.clear();
179   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
180   for ( ; anIter.More(); anIter.Next() )
181   {
182     Handle(HYDROData_Entity) anEntity = anIter.Value();
183     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
184     //  continue;
185
186     theNames.append( anEntity->GetName() );
187     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
188   }
189 }
190
191 void HYDROGUI_CalculationOp::abortOperation()
192 {
193   closePreview();
194   // Abort transaction
195   abortDocOperation();
196   HYDROGUI_Operation::abortOperation();
197   module()->getApp()->updateObjectBrowser();
198 }
199
200 void HYDROGUI_CalculationOp::commitOperation()
201 {
202   closePreview();
203   // Commit transaction
204   commitDocOperation();
205   HYDROGUI_Operation::commitOperation();
206 }
207
208 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
209 {
210   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
211
212   // Connect signals and slots
213   connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );  
214   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
215   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
216   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
217   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
218
219   connect( aPanel, SIGNAL( changeLandCoverMode( int ) ), SLOT( onChangeLandCoverMode( int ) ) );
220   connect( aPanel, SIGNAL( addLandCovers() ), SLOT( onAddLandCovers() ) );
221   connect( aPanel, SIGNAL( removeLandCovers() ), SLOT( onRemoveLandCovers() ) );
222   
223   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
224   connect( aPanel, SIGNAL( orderLandCoverChanged( bool& ) ), SLOT( onOrderLandCoverChanged( bool& ) ) );
225
226   connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
227   connect( aPanel, SIGNAL( ruleLandCoverChanged( bool& ) ), SLOT( onRuleLandCoverChanged( bool& ) ) );
228
229   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
230   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones( const int ) ) );
231   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
232   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
233   connect( aPanel, SIGNAL( setMergeStricklerType( int, QString& ) ), SLOT( onSetMergeStricklerType( int, QString& ) ) );
234   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>&, bool ) ),
235     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>&, bool ) ) );
236   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
237     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
238   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
239     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
240   connect( aPanel, SIGNAL( objectsSelected() ), 
241            SLOT( onObjectsSelected() ) );
242   connect( aPanel, SIGNAL( landCoversSelected() ), 
243            SLOT( onLandCoversSelected() ) );
244   connect( aPanel, SIGNAL( createLandCoverRegion( const QList<SUIT_DataObject*>& ) ),
245     SLOT( onCreateLandCoverRegion( const QList<SUIT_DataObject*>& ) ) );
246   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
247     SLOT( onBoundarySelected( const QString & ) ) );
248   connect( aPanel, SIGNAL( StricklerTableSelected( const QString & ) ), 
249     SLOT( onStricklerTableSelected( const QString & ) ) );
250
251   return aPanel;
252 }
253
254 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
255 {
256   bool anIsToUpdateViewer = false;
257
258   // Remove the old boundary from the operation viewer
259   Handle(HYDROData_PolylineXY) aPrevPolyline = 
260     myEditedObject->GetBoundaryPolyline();
261   if ( !aPrevPolyline.IsNull() )
262   {
263     setObjectVisibility( aPrevPolyline, false );
264     anIsToUpdateViewer = true;
265   }
266
267   // Set the selected boundary polyline to the calculation case
268   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
269     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
270   myEditedObject->SetBoundaryPolyline( aNewPolyline );
271
272   if ( myPreviewViewManager )
273   {
274     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
275     if ( aViewer )
276     {
277       if ( !aNewPolyline.IsNull() )
278       {
279         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
280         if ( !aCtx.IsNull() )
281         {
282           setObjectVisibility( aNewPolyline, true );
283           anIsToUpdateViewer = true;
284         }
285       }
286
287       if ( anIsToUpdateViewer )
288         module()->update( UF_OCCViewer );
289     }
290   }
291 }
292
293 void HYDROGUI_CalculationOp::onStricklerTableSelected ( const QString & theObjName )
294 {
295   bool anIsToUpdateViewer = false;
296
297   // Remove old presentations of land covers from the operation viewer  
298   Handle(HYDROData_Entity) anEntity;
299   Handle(HYDROData_LandCover) aLandCover;
300   HYDROData_SequenceOfObjects aLandCovers;
301   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
302   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
303   for ( ; anIter.More(); anIter.Next() )
304   {
305     anEntity = anIter.Value();
306     if ( !anEntity.IsNull() )
307     {
308       aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
309       if ( !aLandCover.IsNull() )
310       {
311         aLandCovers.Append( aLandCover );
312         setObjectVisibility( aLandCover, false );
313         anIsToUpdateViewer = true;
314       }
315     }
316   }
317
318   // Set the selected Strickler table to the calculation case
319   Handle(HYDROData_StricklerTable) aNewStricklerTable = Handle(HYDROData_StricklerTable)::DownCast(
320     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_STRICKLER_TABLE ) );
321   myEditedObject->SetStricklerTable( aNewStricklerTable );
322
323   if ( myPreviewViewManager )
324   {
325     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
326     if ( aViewer )
327     {
328       if ( !aNewStricklerTable.IsNull() )
329       {
330         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
331         if ( !aCtx.IsNull() )
332         {
333           HYDROData_SequenceOfObjects::Iterator anIter( aLandCovers );
334           for ( ; anIter.More(); anIter.Next() )
335           {
336             anEntity = anIter.Value();
337             if ( !anEntity.IsNull() )
338             {
339               aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
340               if ( !aLandCover.IsNull() )
341               {
342                 setObjectVisibility( aLandCover, true );
343                 anIsToUpdateViewer = true;
344               }
345             }
346           }          
347         }
348       }
349
350       if ( anIsToUpdateViewer )
351         module()->update( UF_OCCViewer );
352     }
353   }
354 }
355
356 void HYDROGUI_CalculationOp::onObjectsSelected()
357 {
358   HYDROGUI_CalculationDlg* aPanel = 
359     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
360
361   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
362   QMap<QString, bool> aSelectedObjsMap;
363   foreach( QString aName, aSelectedObjs )
364     aSelectedObjsMap[aName] = true;
365
366
367   // Select the appropriate geometry object shape in the viewer
368   selectionMgr()->clearSelected();
369
370   // Unhighlight all objects except selected
371   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
372   Handle(HYDROData_Entity) anEntity;
373   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
374   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
375   bool isSelected;
376   QString aName;
377   for ( ; anIter.More(); anIter.Next() )
378   {
379     anEntity = anIter.Value();
380     if ( !anEntity.IsNull() )
381     {
382       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
383       if ( aShape )
384       {
385         aName = anEntity->GetName();
386         isSelected = aSelectedObjsMap.contains( aName );
387         aShape->highlight( isSelected, false );
388         aShape->update( false, false );
389         aLastShape = aShape;
390       }
391     }
392   }
393   if( aLastShape )
394     aLastShape->update( true, false );
395 }
396
397 void HYDROGUI_CalculationOp::onLandCoversSelected()
398 {
399   HYDROGUI_CalculationDlg* aPanel = 
400     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
401
402   QStringList aSelectedObjs = aPanel->getSelectedLandCovers();
403   QMap<QString, bool> aSelectedObjsMap;
404   foreach( QString aName, aSelectedObjs )
405     aSelectedObjsMap[aName] = true;
406
407
408   // Select the appropriate land cover shape in the viewer
409   selectionMgr()->clearSelected();
410
411   // Unhighlight all land covers except selected
412   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
413   Handle(HYDROData_Entity) anEntity;
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   if( aLastShape )
435     aLastShape->update( true, false );
436 }
437
438 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
439 {
440   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
441   HYDROGUI_Zone* aZoneItem;
442   selectionMgr()->clearSelected();
443   if ( aRegionItem )
444   {
445     // Select a region in preview
446     SUIT_DataOwnerPtrList aList( true );
447     DataObjectList aZones = aRegionItem->children();
448     for ( int i = 0; i < aZones.length(); i++ )
449     {
450       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
451       if ( aZoneItem )
452       {
453         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
454       }
455     }
456     selectionMgr()->setSelected( aList );
457   }
458   else
459   {
460     // select a single zone
461     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
462     if ( aZoneItem )
463     {
464       SUIT_DataOwnerPtrList aList( true );
465       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
466       selectionMgr()->setSelected( aList );
467     }
468   }
469 }
470
471 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem,
472                                           const QList<SUIT_DataObject*>& theZonesList,
473                                           bool theLandCover )
474 {
475   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
476   if ( aRegion )
477   {
478     QList<HYDROGUI_Zone*> aZonesList;
479     HYDROGUI_Zone* aZone;
480     // Get a list of dropped zones
481     for ( int i = 0; i < theZonesList.length(); i++ )
482     {
483       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
484       if ( aZone )
485       {
486         aZonesList.append( aZone );
487       }
488     }
489     if ( aZonesList.length() > 0 )
490     {
491       aRegion->addZones( aZonesList );
492       HYDROGUI_CalculationDlg* aPanel = 
493         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
494       if ( aPanel )
495       {
496         theLandCover ? aPanel->refreshLandCoverZonesBrowser(): aPanel->refreshZonesBrowser();
497       }
498       createPreview( theLandCover );
499     }
500   }
501 }
502
503 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
504 {
505   if ( createRegion( theZonesList, false ) )
506   {
507     HYDROGUI_CalculationDlg* aPanel = 
508       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
509     if ( aPanel )
510     {
511       aPanel->refreshZonesBrowser();
512     }
513     createPreview( false );
514   }
515 }
516
517 void HYDROGUI_CalculationOp::onCreateLandCoverRegion( const QList<SUIT_DataObject*>& theZonesList )
518 {
519   if ( createRegion( theZonesList, true ) )
520   {
521     HYDROGUI_CalculationDlg* aPanel = 
522       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
523     if ( aPanel )
524     {
525       aPanel->refreshLandCoverZonesBrowser();
526     }
527     createPreview( true );
528   }
529 }
530
531 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theMergeObjectName )
532 {
533   HYDROGUI_CalculationDlg* aPanel = 
534     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
535   if ( aPanel )
536   {
537     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
538     if ( aZone )
539     {
540       aZone->setMergeType( theMergeType, theMergeObjectName );
541       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
542       if ( aShape )
543       {
544         aShape->update( true, false );
545       }
546     }
547     aPanel->refreshZonesBrowser();
548   }
549 }
550
551 void HYDROGUI_CalculationOp::onSetMergeStricklerType( int theMergeType, QString& theStricklerTypeName )
552 {
553   HYDROGUI_CalculationDlg* aPanel = 
554     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
555   if ( aPanel )
556   {
557     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
558     if ( aZone )
559     {
560       aZone->setMergeType( theMergeType, theStricklerTypeName );
561       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
562       if ( aShape )
563       {
564         aShape->update( true, false );
565       }
566     }
567     aPanel->refreshLandCoverZonesBrowser();
568   }
569 }
570
571 void HYDROGUI_CalculationOp::onAddObjects()
572 {
573   HYDROGUI_CalculationDlg* aPanel = 
574     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
575   if ( !aPanel )
576     return;
577
578   // Add geometry objects selected in the module browser to the calculation case
579   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
580   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
581     return;
582
583   QStringList anAddedList;
584   for (int i = 0; i < aSelectedList.length(); i++)
585   {
586     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
587       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
588     if ( anObject.IsNull() )
589       continue;
590
591     if ( myEditedObject->AddGeometryObject( anObject ) )
592       anAddedList.append( anObject->GetName() );
593   }
594
595   if ( !anAddedList.isEmpty() )
596   {
597     aPanel->includeGeomObjects( anAddedList );
598     createPreview( false );
599   }
600 }
601
602 void HYDROGUI_CalculationOp::onRemoveObjects()
603 {
604   // Remove selected objects from the calculation case
605   HYDROGUI_CalculationDlg* aPanel = 
606     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
607   if ( !aPanel )
608     return;
609
610   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
611   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
612     return;
613
614   for (int i = 0; i < aSelectedList.length(); i++)
615   {
616     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
617       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
618     if ( anObject.IsNull() )
619       continue;
620
621     setObjectVisibility( anObject, false );
622     myEditedObject->RemoveGeometryObject( anObject );
623   }
624
625   module()->update( UF_OCCViewer );
626   aPanel->excludeGeomObjects( aSelectedList );
627 }
628
629 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
630 {
631   // Check if the case is already modified or not
632   bool isConfirmed = myEditedObject->IsMustBeUpdated();
633   if ( !isConfirmed )
634   {
635     // If not modified check if the case has already defined regions with zones
636     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( false );
637     if ( aSeq.Length() > 0 )
638     {
639       // If there are already defined zones then ask a user to confirm zones recalculation
640       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
641                                tr( "REGIONS_CHANGED" ),
642                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
643                                QMessageBox::Yes | QMessageBox::No,
644                                QMessageBox::No ) == QMessageBox::Yes );
645     }
646     else
647     {
648       isConfirmed = true; // No regions - no zones - nothing to recalculate
649     }
650   }
651   return isConfirmed;
652 }
653
654 bool HYDROGUI_CalculationOp::confirmOrderChange() const
655 {
656   // Check if the case is already modified or not
657   bool isConfirmed = myEditedObject->IsMustBeUpdated();
658   if ( !isConfirmed )
659   {
660     // If not modified check if the case has already defined regions with zones
661     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( false );
662     if ( aSeq.Length() > 0 )
663     {
664       // If there are already defined zones then ask a user to confirm zones recalculation
665       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
666                                tr( "ORDER_CHANGED" ),
667                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
668                                QMessageBox::Yes | QMessageBox::No,
669                                QMessageBox::No ) == QMessageBox::Yes );
670     }
671     else
672     {
673       isConfirmed = true; // No regions - no zones - nothing to recalculate
674     }
675   }
676   return isConfirmed;
677 }
678
679 bool HYDROGUI_CalculationOp::confirmRuleChange() const
680 {
681   // Check if the case is already modified or not
682   bool isConfirmed = myEditedObject->IsMustBeUpdated();
683   if ( !isConfirmed )
684   {
685     // If not modified check if the case has already defined regions with zones
686     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( false );
687     if ( aSeq.Length() > 0 )
688     {
689       // If there are already defined zones then ask a user to confirm zones recalculation
690       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
691                                tr( "RULE_CHANGED" ),
692                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
693                                QMessageBox::Yes | QMessageBox::No,
694                                QMessageBox::No ) == QMessageBox::Yes );
695     }
696     else
697     {
698       isConfirmed = true; // No regions - no zones - nothing to recalculate
699     }
700   }
701   return isConfirmed;
702 }
703
704 bool HYDROGUI_CalculationOp::confirmModeChange() const
705 {
706   // Check if the case is already modified or not
707   bool isConfirmed = myEditedObject->IsMustBeUpdated();
708   if ( !isConfirmed )
709   {
710     // If not modified check if the case has already defined regions with zones
711     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( false );
712     if ( aSeq.Length() > 0 )
713     {
714       // If there are already defined zones then ask a user to confirm zones recalculation
715       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
716                                tr( "MODE_CHANGED" ),
717                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
718                                QMessageBox::Yes | QMessageBox::No,
719                                QMessageBox::No ) == QMessageBox::Yes );
720     }
721     else
722     {
723       isConfirmed = true; // No regions - no zones - nothing to recalculate
724     }
725   }
726   return isConfirmed;
727 }
728
729 bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
730 {
731   HYDROData_WarningType aType = theWarning.Type;
732   if ( aType == WARN_OK ) {
733     return true;
734   }
735
736   QString aTitle;
737   QString aMsg;
738   switch ( aType )
739   {
740     case WARN_EMPTY_REGIONS:
741       aTitle = tr( "EMPTY_REGIONS" );
742       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
743       break;
744     default:
745       aTitle = tr( "WARNING" );
746       aMsg = theWarning.Data;
747   }
748
749
750   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
751                                            aTitle, aMsg,
752                                            QMessageBox::Yes | QMessageBox::No,
753                                            QMessageBox::No );
754
755   return ( anAnswer == QMessageBox::Yes );
756 }
757
758 bool HYDROGUI_CalculationOp::confirmLandCoverRegionsChange() const
759 {
760   // Check if the case is already modified or not
761   bool isConfirmed = myEditedObject->IsMustBeUpdated();
762   if ( !isConfirmed )
763   {
764     // If not modified check if the case has already defined regions with land cover zones
765     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
766     if ( aSeq.Length() > 0 )
767     {
768       // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
769       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
770                                tr( "REGIONS_CHANGED" ),
771                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
772                                QMessageBox::Yes | QMessageBox::No,
773                                QMessageBox::No ) == QMessageBox::Yes );
774     }
775     else
776     {
777       isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
778     }
779   }
780   return isConfirmed;
781 }
782
783 bool HYDROGUI_CalculationOp::confirmLandCoverModeChange() const
784 {
785   // Check if the case is already modified or not
786   bool isConfirmed = myEditedObject->IsMustBeUpdated();
787   if ( !isConfirmed )
788   {
789     // If not modified check if the case has already defined regions with land cover zones
790     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
791     if ( aSeq.Length() > 0 )
792     {
793       // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
794       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
795                                tr( "MODE_CHANGED" ),
796                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_MODE" ),
797                                QMessageBox::Yes | QMessageBox::No,
798                                QMessageBox::No ) == QMessageBox::Yes );
799     }
800     else
801     {
802       isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
803     }
804   }
805   return isConfirmed;
806 }
807
808 bool HYDROGUI_CalculationOp::confirmLandCoverOrderChange() const
809 {
810   // Check if the case is already modified or not
811   bool isConfirmed = myEditedObject->IsMustBeUpdated();
812   if ( !isConfirmed )
813   {
814     // If not modified check if the case has already defined regions with land cover zones
815     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
816     if ( aSeq.Length() > 0 )
817     {
818       // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
819       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
820                                tr( "ORDER_CHANGED" ),
821                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
822                                QMessageBox::Yes | QMessageBox::No,
823                                QMessageBox::No ) == QMessageBox::Yes );
824     }
825     else
826     {
827       isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
828     }
829   }
830   return isConfirmed;
831 }
832
833 bool HYDROGUI_CalculationOp::confirmLandCoverRuleChange() const
834 {
835   // Check if the case is already modified or not
836   bool isConfirmed = myEditedObject->IsMustBeUpdated();
837   if ( !isConfirmed )
838   {
839     // If not modified check if the case has already defined regions with land cover zones
840     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
841     if ( aSeq.Length() > 0 )
842     {
843       // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
844       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
845                                tr( "RULE_CHANGED" ),
846                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
847                                QMessageBox::Yes | QMessageBox::No,
848                                QMessageBox::No ) == QMessageBox::Yes );
849     }
850     else
851     {
852       isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
853     }
854   }
855   return isConfirmed;
856 }
857
858 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
859                                            QString& theErrorMsg,
860                                            QStringList& theBrowseObjectsEntries )
861 {
862   HYDROGUI_CalculationDlg* aPanel = 
863     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
864   if ( !aPanel )
865     return false;
866
867   if( !myIsEdit )
868   {
869     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
870     theBrowseObjectsEntries.append( anEntry );
871   }
872
873   // For manual mode priority rules are redundant
874   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
875     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
876   }
877   if ( aPanel->getLandCoverMode() == HYDROData_CalculationCase::MANUAL ) {
878     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, false );
879   }
880  
881   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
882
883   return true;
884 }
885
886 void HYDROGUI_CalculationOp::onApply()
887 {
888   // Check warnings
889   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
890   if ( aWarning.Type != WARN_OK ) {
891     if ( !confirmContinueWithWarning( aWarning ) ) {
892       // Go back to the first page
893       HYDROGUI_CalculationDlg* aPanel = 
894         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
895       if ( aPanel ) {
896         aPanel->onFirstPage();
897       }
898       return;
899     }
900   }
901
902   QApplication::setOverrideCursor( Qt::WaitCursor );
903
904   int anUpdateFlags = 0;
905   QString anErrorMsg;
906   QStringList aBrowseObjectsEntries;
907
908   bool aResult = false;
909   
910   try
911   {
912     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
913   }
914   catch ( Standard_Failure )
915   {
916     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
917     anErrorMsg = aFailure->GetMessageString();
918     aResult = false;
919   }
920   catch ( ... )
921   {
922     aResult = false;
923   }
924   
925   QApplication::restoreOverrideCursor();
926
927   if ( aResult )
928   {
929     module()->update( anUpdateFlags );
930     commit();
931     browseObjects( aBrowseObjectsEntries );
932   }
933   else
934   {
935     abort();
936     QString aMsg = tr( "INPUT_VALID_DATA" );
937     if( !anErrorMsg.isEmpty() )
938       aMsg.prepend( anErrorMsg + "\n" );
939     SUIT_MessageBox::critical( module()->getApp()->desktop(),
940                                tr( "INSUFFICIENT_INPUT_DATA" ),
941                                aMsg ); 
942   }
943 }
944
945 void HYDROGUI_CalculationOp::onNext( const int theIndex )
946 {
947   if( theIndex==1 )
948   {
949     setAvailableGroups();
950   }
951   else if( theIndex==2 )
952   {
953     // Land covers panel
954      HYDROGUI_CalculationDlg* aPanel = 
955       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
956     if ( !aPanel )
957       return;
958
959     setLandCoversVisible( true );
960
961     QStringList aList;
962     QStringList anEntryList;
963     HYDROData_SequenceOfObjects aSeq;
964
965     Handle(HYDROData_StricklerTable) aStricklerTableObj;
966     QString aStricklerTableName;
967
968     // Get all Strickler table objects to fill in combo-box
969     aList.clear();
970     anEntryList.clear();
971     HYDROData_Iterator anIter( doc(), KIND_STRICKLER_TABLE );      
972     for ( ; anIter.More(); anIter.Next() )
973     {
974       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
975
976       if ( !aStricklerTableObj.IsNull() )
977       { 
978         aStricklerTableName = aStricklerTableObj->GetName();
979         if ( !aStricklerTableName.isEmpty() )
980         {
981           aList.append( aStricklerTableName );
982           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
983         }        
984       }
985     }
986     aPanel->setStricklerTableNames( aList, anEntryList );
987     bool anUpdateState = myEditedObject->IsMustBeUpdated();
988     if ( !aList.isEmpty() )
989       aPanel->setStricklerTable( aList.at( 0 ), false );
990     myEditedObject->SetToUpdate( anUpdateState );
991
992     // Fill in list widget with all available land covers
993     aSeq = HYDROGUI_Tool::GetLandCovers( module() );
994     getNamesAndEntries( aSeq, aList, anEntryList );
995     aPanel->setAllLandCovers( aList, anEntryList );
996
997     // Set list of included land covers
998     aSeq = myEditedObject->GetLandCovers();
999     getNamesAndEntries( aSeq, aList, anEntryList );
1000     aPanel->includeLandCovers( aList, true );
1001     
1002     if ( !myEditedObject.IsNull() )
1003     {
1004       if ( myIsEdit )
1005       {      
1006         // Select the certain Strickler table object in combo-box
1007         aStricklerTableObj = myEditedObject->GetStricklerTable();
1008         if ( aStricklerTableObj.IsNull() )
1009         {
1010           aPanel->setStricklerTable( QString() );
1011         }
1012         else
1013         {
1014           aStricklerTableName = aStricklerTableObj->GetName();
1015           aPanel->setStricklerTable( aStricklerTableName );
1016         }
1017
1018         // Set mode (Auto or Manual) to defined priority of land covers
1019         aPanel->setLandCoverMode( myEditedObject->GetAssignmentLandCoverMode() );
1020
1021         // Set rules defined on land covers
1022         setRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
1023       }
1024       else
1025       {
1026         myEditedObject->SetAssignmentLandCoverMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getLandCoverMode() );
1027       }
1028     }
1029
1030     closePreview( false );
1031     createPreview( true );
1032   }
1033   else if( theIndex==3 )
1034   {
1035     HYDROGUI_CalculationDlg* aPanel = 
1036       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1037     if ( !aPanel )
1038       return;
1039
1040     QApplication::setOverrideCursor( Qt::WaitCursor );
1041
1042     setGeomObjectsVisible( false );
1043     setLandCoversVisible( false );
1044
1045     QString aNewCaseName = aPanel->getObjectName();
1046     QString anOldCaseName = myEditedObject->GetName();
1047     bool isNameChanged = anOldCaseName != aNewCaseName;
1048   
1049     bool anIsToUpdateOb = isNameChanged;
1050
1051     // At first we must to update the case name because of 
1052     // automatic names generation for regions and zones
1053     myEditedObject->SetName( aNewCaseName );
1054     
1055     // Zones: set parameters for automatic mode
1056     int aMode = aPanel->getMode();
1057     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
1058     {
1059       // Set objects in the specified order
1060       if( myEditedObject->IsMustBeUpdated() )
1061       {
1062         myEditedObject->RemoveGeometryObjects();
1063         foreach ( const QString& aName, aPanel->getAllGeomObjects() )
1064         {
1065           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
1066             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
1067           if ( anObject.IsNull() )
1068           {
1069             continue;
1070           }
1071           myEditedObject->AddGeometryObject( anObject );
1072         }
1073
1074         // Clear priority rules
1075         //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
1076           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
1077         //@ASL }
1078         // Set priority rules
1079         foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
1080           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
1081                                   aRule.Object2, aRule.MergeType,
1082                                   HYDROData_CalculationCase::DataTag_CustomRules );
1083         }
1084       }
1085     }
1086     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
1087
1088     // Land cover zones: set parameters for automatic mode
1089     aMode = aPanel->getLandCoverMode();
1090     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
1091     {
1092       // Set objects in the specified order
1093       if( myEditedObject->IsMustBeUpdated() )
1094       {
1095         myEditedObject->RemoveLandCovers();
1096         foreach ( const QString& aName, aPanel->getAllLandCovers() )
1097         {
1098           Handle(HYDROData_LandCover) aLandCover = Handle(HYDROData_LandCover)::DownCast( 
1099             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
1100           if ( aLandCover.IsNull() )
1101           {
1102             continue;
1103           }
1104           myEditedObject->AddLandCover( aLandCover );
1105         }
1106
1107         // Clear priority rules
1108         myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, true );
1109         // Set priority rules
1110         foreach ( const HYDROData_CustomRule& aRule, aPanel->getLandCoverRules() ) {
1111           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
1112                                    aRule.Object2, HYDROData_Zone::Merge_Object,
1113                                    HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
1114         }
1115       }
1116     }
1117     aPanel->setEditLandCoverZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
1118          
1119     if ( myEditedObject->IsMustBeUpdated() )
1120     {
1121       myShowZones = true;
1122       myEditedObject->Update();
1123       
1124       AssignDefaultZonesColors( false );
1125
1126       //aPanel->setEditedObject( myEditedObject );
1127       aPanel->refreshZonesBrowser();
1128     
1129       closePreview( false );
1130       createPreview( false );
1131
1132       anIsToUpdateOb = true;
1133
1134       myUpdateLandCoverZonesPrs = true;
1135     }
1136     else
1137     {
1138       // Show zones
1139       setZonesVisible( true, false );
1140
1141       if ( isNameChanged ) {
1142         module()->getDataModel()->updateObjectTree( myEditedObject );
1143       }
1144     }
1145
1146     QApplication::restoreOverrideCursor();
1147   }
1148   else if( theIndex==4 )
1149   {
1150     // Partition of Land covers panel
1151     HYDROGUI_CalculationDlg* aPanel = 
1152       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1153     if ( !aPanel )
1154       return;
1155
1156     QApplication::setOverrideCursor( Qt::WaitCursor );
1157
1158     bool anIsToUpdateOb = false;
1159     if ( myUpdateLandCoverZonesPrs )
1160     {
1161       myShowZones = true;
1162       
1163       AssignDefaultZonesColors( true );
1164
1165       aPanel->refreshLandCoverZonesBrowser();
1166
1167       anIsToUpdateOb = true;
1168     }
1169
1170     closePreview( false );
1171     createPreview( true );
1172     
1173     if ( myUpdateLandCoverZonesPrs )
1174       myUpdateLandCoverZonesPrs = false;
1175     else
1176     {
1177       // Hide zones
1178       setZonesVisible( false, false );
1179       // Show land cover zones
1180       setZonesVisible( true, true );
1181     }
1182
1183     if ( anIsToUpdateOb ) {
1184       SUIT_DataBrowser* anObjBrowser = ((LightApp_Application*)module()->application())->objectBrowser();
1185       if ( anObjBrowser ) {
1186         anObjBrowser->updateTree( module()->getDataModel()->getDataObject( myEditedObject ), false );
1187       }
1188     }
1189
1190     QApplication::restoreOverrideCursor();
1191   }
1192 }
1193
1194 void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
1195 {
1196   if( theIndex==1 )
1197   {
1198     setGeomObjectsVisible( true );
1199
1200     closePreview( false );
1201     createPreview( false );
1202   }
1203   if( theIndex==2 )
1204   {
1205     setLandCoversVisible( true );
1206
1207     closePreview( false );
1208     createPreview( true );
1209
1210     // Hide zones
1211     setZonesVisible( false, false );
1212     // Hide land covers
1213     setZonesVisible( false, true );
1214   }
1215   else if( theIndex==3 )
1216   {
1217     AssignDefaultZonesColors( false );
1218
1219     closePreview( false );
1220     createPreview( false );
1221
1222     // Hide land cover zones
1223     setZonesVisible( false, true );
1224     // Show zones
1225     setZonesVisible( true, false );
1226   }
1227 }
1228
1229 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible, const bool theLandCover )
1230 {
1231   myShowZones = theIsVisible;
1232   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions( theLandCover );
1233   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1234   HYDROData_SequenceOfObjects aZones;
1235   Handle(HYDROData_Region) aRegion;
1236   if ( myPreviewViewManager ) 
1237   {
1238     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1239     {
1240       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1241       if ( !aCtx.IsNull() )
1242       {
1243         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1244         {
1245           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1246           if ( !aRegion.IsNull() )
1247           {
1248             aZones = aRegion->GetZones();
1249             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1250             for ( ; aZonesIter.More(); aZonesIter.Next() )
1251             {
1252               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1253             }
1254           }
1255         }
1256       }
1257
1258       module()->update( UF_OCCViewer );
1259     }
1260   }
1261 }
1262
1263 void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
1264 {
1265   myShowGeomObjects = theIsVisible;
1266
1267   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1268
1269   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1270   for ( ; anIter.More(); anIter.Next() ) {
1271     setObjectVisibility( anIter.Value(), theIsVisible );
1272   }
1273 }
1274
1275 void HYDROGUI_CalculationOp::setLandCoversVisible( bool theIsVisible )
1276 {
1277   myShowLandCovers = theIsVisible;
1278
1279   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
1280
1281   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1282   for ( ; anIter.More(); anIter.Next() ) {
1283     setObjectVisibility( anIter.Value(), theIsVisible );
1284   }
1285 }
1286
1287 void HYDROGUI_CalculationOp::AssignDefaultZonesColors( const bool theLandCover )
1288 {
1289   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions( theLandCover );
1290   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1291   HYDROData_SequenceOfObjects aZones;
1292   Handle(HYDROData_Region) aRegion;
1293   if ( myPreviewViewManager ) 
1294   {
1295     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1296     {
1297       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1298       if ( !aCtx.IsNull() )
1299       {
1300         int aCounter = 0;        
1301         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1302         {
1303           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1304           if ( !aRegion.IsNull() )
1305           {
1306             aZones = aRegion->GetZones();
1307             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1308             for ( ; aZonesIter.More(); aZonesIter.Next() )
1309             {
1310               // Zone
1311               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1312               if ( !aZone.IsNull() )
1313               {
1314                 QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
1315                 while (aFillingColor == Qt::red)
1316                   aFillingColor = GenerateDefaultZonesColor(++aCounter);
1317                 
1318                 aZone->SetColor(aFillingColor);
1319               }
1320             }
1321           }
1322         }
1323       }
1324     }
1325   }
1326 }
1327
1328 QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
1329                                                           float theSaturation/* = 0.5*/,
1330                                                           float theValue/* = 0.95*/ ) const
1331 {
1332   float aGoldenRatioConjugate = (float)(360./582.);
1333   float aHue = (float)(rand()%100);
1334   aHue += aGoldenRatioConjugate*theIndex;
1335   aHue -= floor(aHue);
1336
1337   float aR = 0., aG = 0., aB = 0.;
1338   int aHueInt = (int)(aHue*6.);
1339   float aF = aHue*6. - aHueInt;
1340   float aP = theValue * (1. - theSaturation);
1341   float aQ = theValue * (1. - aF*theSaturation);
1342   float aT = theValue * (1. - (1. - aF) * theSaturation);
1343   switch (aHueInt)
1344   {
1345   case 0: { aR = theValue; aG = aT; aB = aP; break; }
1346   case 1: { aR = aQ; aG = theValue; aB = aP; break; }
1347   case 2: { aR = aP; aG = theValue; aB = aT; break; }
1348   case 3: { aR = aP; aG = aQ; aB = theValue; break; }
1349   case 4: { aR = aT; aG = aP; aB = theValue; break; }
1350   case 5: { aR = theValue; aG = aP; aB = aQ; break; }
1351   default: break;
1352   }
1353
1354   QColor aColor = QColor( (int)(aR*256.), (int)(aG*256.), (int)(aB*256.) );
1355   return ( aColor.isValid() ? aColor : HYDROData_ImmersibleZone::DefaultFillingColor() );
1356 }
1357
1358 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1359 {
1360   HYDROGUI_CalculationDlg* aPanel = 
1361     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1362   if ( !aPanel )
1363     return;
1364
1365   HYDROData_ListOfRules aRules;
1366   Handle(HYDROData_Entity) anObject1, anObject2;
1367   HYDROData_PriorityType aPriority;
1368   HYDROData_Zone::MergeType aMergeType;
1369   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1370   for ( int anIndex = 0; ; anIndex++ )
1371   {
1372     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1373       HYDROData_CustomRule aRule;
1374       aRule.Object1 = anObject1;
1375       aRule.Object2 = anObject2;
1376       aRule.Priority = aPriority;
1377       aRule.MergeType = aMergeType;
1378
1379       aRules << aRule;
1380     }
1381     else
1382       break;
1383   }
1384
1385   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1386     aPanel->setRules( aRules );
1387   else if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomLandCoverRules )
1388     aPanel->setLandCoverRules( aRules );
1389 }
1390
1391 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList,
1392                                            const bool theLandCover )
1393 {
1394   bool aRetValue = false;
1395
1396   QList<HYDROGUI_Zone*> aZonesList;
1397   HYDROGUI_Zone* aZone;
1398   // Get a list of dropped zones
1399   for ( int i = 0; i < theZonesList.length(); i++ )
1400   {
1401     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1402     if ( aZone )
1403     {
1404       aZonesList.append( aZone );
1405     }
1406   }
1407   if ( aZonesList.length() > 0 )
1408   {
1409     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList, theLandCover );
1410     aRetValue = true;
1411   }
1412    
1413   return aRetValue;
1414 }
1415
1416 void HYDROGUI_CalculationOp::createPreview( const bool theLandCover )
1417 {
1418   LightApp_Application* anApp = module()->getApp();
1419   HYDROData_SequenceOfObjects aSeq;
1420   if ( theLandCover && myShowLandCovers )
1421   {
1422     HYDROData_SequenceOfObjects aSeqLC = myEditedObject->GetLandCovers();
1423     aSeq.Append( aSeqLC );
1424   }
1425   else if ( !theLandCover && myShowGeomObjects )
1426   {
1427     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1428     aSeq.Append( aSeqGO );
1429   }
1430
1431   Handle(HYDROData_Entity) anEntity;
1432
1433   if ( myShowZones )
1434   {
1435     // Gather zones for displaying
1436     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions( theLandCover );
1437     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1438     HYDROData_SequenceOfObjects aZones;
1439     Handle(HYDROData_Region) aRegion;
1440     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1441     {
1442       anEntity = aRegionsIter.Value();
1443       if ( !anEntity.IsNull() )
1444       {
1445         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1446         if ( !aRegion.IsNull() )
1447         {
1448           aZones = aRegion->GetZones();
1449           aSeq.Append( aZones );
1450         }
1451       }
1452     }
1453   }
1454
1455   // Get a boundary polyline if any
1456   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
1457
1458   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1459
1460   if ( !myActiveViewManager )
1461   {
1462     if ( aSeq.IsEmpty() )
1463       return;
1464
1465     myActiveViewManager = anApp->activeViewManager();
1466   }
1467
1468   if ( !myPreviewViewManager )
1469   {
1470     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1471       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1472     if ( myPreviewViewManager )
1473     {
1474       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1475                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1476
1477       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1478       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1479     }
1480   }
1481
1482   if ( !myPreviewViewManager )
1483     return;
1484
1485   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1486   {
1487     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1488     if ( !aCtx.IsNull() )
1489     {
1490       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1491       for ( ; anIter.More(); anIter.Next() )
1492       {
1493         setObjectVisibility( anIter.Value(), true );
1494       }
1495
1496       //Process the draw events for viewer
1497       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1498       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
1499         vw->onTopView();
1500     }
1501
1502     module()->update( UF_OCCViewer | UF_FitAll );
1503   }
1504 }
1505
1506 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1507 {
1508   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1509     return;
1510   }
1511
1512   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1513   if ( aViewer ) {
1514     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1515   }
1516 }
1517
1518 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1519 {
1520   closePreview();
1521 }
1522
1523 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1524 {
1525   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1526   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1527   QShortcut* aShortcut;
1528   foreach( aShortcut, aShortcuts )
1529   {
1530     if ( aShortcut->key() == 
1531       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1532       SUIT_DataBrowser::RenameShortcut ) ) )
1533     {
1534       aShortcut->setEnabled( true );
1535     }
1536   }
1537
1538
1539   if( myPreviewViewManager )
1540   {
1541     // Hide all the displayed objects in the preview view
1542     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1543     if ( aViewer ) {
1544       size_t aViewId = (size_t)aViewer;
1545       HYDROData_Iterator anIterator( doc() );
1546       for( ; anIterator.More(); anIterator.Next() ) {
1547         Handle(HYDROData_Entity) anObject = anIterator.Current();
1548         if( !anObject.IsNull() ) {
1549           module()->setObjectVisible( aViewId, anObject, false );
1550         }
1551       }
1552     }
1553
1554     if ( theRemoveViewManager )
1555     {
1556       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1557                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1558
1559       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1560       myPreviewViewManager = NULL;
1561     }
1562   }
1563
1564   if( myActiveViewManager && theRemoveViewManager )
1565   {
1566     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1567     myActiveViewManager = NULL;
1568   }
1569 }
1570
1571 void HYDROGUI_CalculationOp::setAvailableGroups()
1572 {
1573   HYDROGUI_CalculationDlg* aPanel = 
1574       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1575
1576   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1577   QStringList aList, anEntryList;
1578   getNamesAndEntries( aSeq, aList, anEntryList );
1579
1580   QStringList aGroupsNames;
1581
1582   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1583   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1584   {
1585     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
1586     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1587     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1588     {
1589       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
1590       aGroupsNames.append( aGroup->GetName() );
1591     }
1592   }
1593   if( myEditedObject->IsMustBeUpdated() ) {
1594     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1595       Handle(HYDROData_ShapesGroup) aGeomGroup =
1596         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1597       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1598         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1599       }
1600     }
1601   }
1602
1603   aPanel->setAvailableGroups( aGroupsNames );
1604   aPanel->includeGroups( aList );
1605
1606   bool isUpdated = myEditedObject->IsMustBeUpdated();
1607 }
1608
1609 void HYDROGUI_CalculationOp::onAddGroups()
1610 {
1611   HYDROGUI_CalculationDlg* aPanel = 
1612     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1613   if ( !aPanel )
1614     return;
1615
1616   // Add geometry objects selected in the module browser to the calculation case
1617   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1618   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1619     return;
1620
1621   QStringList anAddedList;
1622   for (int i = 0; i < aSelectedList.length(); i++)
1623   {
1624     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1625       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1626     if ( aGroup.IsNull() )
1627       continue;
1628
1629     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1630       anAddedList.append( aGroup->GetName() );
1631   }
1632
1633   if ( !anAddedList.isEmpty() )
1634   {
1635     aPanel->includeGroups( anAddedList );
1636   }
1637 }
1638
1639 void HYDROGUI_CalculationOp::onRemoveGroups()
1640 {
1641   // Remove selected objects from the calculation case
1642   HYDROGUI_CalculationDlg* aPanel = 
1643     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1644   if ( !aPanel )
1645     return;
1646
1647   QStringList aSelectedList = aPanel->getSelectedGroups();
1648   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1649     return;
1650
1651   for (int i = 0; i < aSelectedList.length(); i++)
1652   {
1653     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1654       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1655     if ( aGroup.IsNull() )
1656       continue;
1657
1658     myEditedObject->RemoveGeometryGroup( aGroup );
1659   }
1660
1661   aPanel->excludeGroups( aSelectedList );
1662 }
1663
1664 void HYDROGUI_CalculationOp::onChangeLandCoverMode( int theMode )
1665 {
1666   HYDROGUI_CalculationDlg* aPanel = 
1667     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1668   if ( !aPanel )
1669     return;
1670
1671   if ( !confirmLandCoverModeChange() ) {
1672     aPanel->setLandCoverMode( myEditedObject->GetAssignmentLandCoverMode() );
1673     return;
1674   }
1675
1676   myEditedObject->SetAssignmentLandCoverMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1677   aPanel->setLandCoverMode( theMode );
1678 }
1679
1680 void HYDROGUI_CalculationOp::onAddLandCovers()
1681 {
1682   HYDROGUI_CalculationDlg* aPanel = 
1683     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1684   if ( !aPanel )
1685     return;
1686
1687   // Add land covers selected in the module browser to the calculation case
1688   QStringList aSelectedList = aPanel->getSelectedAvailableLandCovers();
1689   if ( aSelectedList.isEmpty() || !confirmLandCoverRegionsChange() )
1690     return;
1691
1692   QStringList anAddedList;
1693   for (int i = 0; i < aSelectedList.length(); i++)
1694   {
1695     Handle(HYDROData_LandCover) anObject = Handle(HYDROData_LandCover)::DownCast( 
1696       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1697     if ( anObject.IsNull() )
1698       continue;
1699
1700     if ( myEditedObject->AddLandCover( anObject ) )
1701       anAddedList.append( anObject->GetName() );
1702   }
1703
1704   if ( !anAddedList.isEmpty() )
1705   {
1706     aPanel->includeLandCovers( anAddedList, false );
1707     createPreview( true );
1708   }
1709 }
1710
1711 void HYDROGUI_CalculationOp::onRemoveLandCovers()
1712 {
1713   // Remove selected objects from the calculation case
1714   HYDROGUI_CalculationDlg* aPanel = 
1715     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1716   if ( !aPanel )
1717     return;
1718
1719   QStringList aSelectedList = aPanel->getSelectedLandCovers();
1720   if ( aSelectedList.isEmpty() || !confirmLandCoverRegionsChange() )
1721     return;
1722
1723   for (int i = 0; i < aSelectedList.length(); i++)
1724   {
1725     Handle(HYDROData_LandCover) anObject = Handle(HYDROData_LandCover)::DownCast( 
1726       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1727     if ( anObject.IsNull() )
1728       continue;
1729
1730     setObjectVisibility( anObject, false );
1731     myEditedObject->RemoveLandCover( anObject );
1732   }
1733
1734   module()->update( UF_OCCViewer );
1735   aPanel->excludeLandCovers( aSelectedList );
1736 }
1737
1738 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1739 {
1740   HYDROGUI_CalculationDlg* aPanel = 
1741     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1742   if ( !aPanel )
1743     return;
1744
1745   if ( !confirmModeChange() ) {
1746     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1747     return;
1748   }
1749
1750   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1751   aPanel->setMode( theMode );
1752 }
1753
1754 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1755 {
1756   HYDROGUI_CalculationDlg* aPanel = 
1757     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1758   if ( !aPanel )
1759     return;
1760
1761   isConfirmed = confirmOrderChange();
1762   if( isConfirmed )
1763     myEditedObject->SetToUpdate( true );
1764 }
1765
1766 void HYDROGUI_CalculationOp::onOrderLandCoverChanged( bool& isConfirmed )
1767 {
1768   HYDROGUI_CalculationDlg* aPanel = 
1769     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1770   if ( !aPanel )
1771     return;
1772
1773   isConfirmed = confirmLandCoverOrderChange();
1774   if( isConfirmed )
1775     myEditedObject->SetToUpdate( true );
1776 }
1777
1778 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1779 {
1780   HYDROGUI_CalculationDlg* aPanel = 
1781     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1782   if ( !aPanel )
1783     return;
1784
1785   isConfirmed = confirmRuleChange();
1786   if( isConfirmed )
1787     myEditedObject->SetToUpdate( true );
1788 }
1789
1790 void HYDROGUI_CalculationOp::onRuleLandCoverChanged( bool& isConfirmed )
1791 {
1792   HYDROGUI_CalculationDlg* aPanel = 
1793     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1794   if ( !aPanel )
1795     return;
1796
1797   isConfirmed = confirmLandCoverRuleChange();
1798   if( isConfirmed )
1799     myEditedObject->SetToUpdate( true );
1800 }