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