Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_CalculationOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_CalculationDlg.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_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();
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() ) );
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( boundarySelected( const QString & ) ), 
237     SLOT( onBoundarySelected( const QString & ) ) );
238   connect( aPanel, SIGNAL( StricklerTableSelected( const QString & ) ), 
239     SLOT( onStricklerTableSelected( const QString & ) ) );
240
241   return aPanel;
242 }
243
244 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
245 {
246   bool anIsToUpdateViewer = false;
247
248   // Remove the old boundary from the operation viewer
249   Handle(HYDROData_PolylineXY) aPrevPolyline = 
250     myEditedObject->GetBoundaryPolyline();
251   if ( !aPrevPolyline.IsNull() )
252   {
253     setObjectVisibility( aPrevPolyline, false );
254     anIsToUpdateViewer = true;
255   }
256
257   // Set the selected boundary polyline to the calculation case
258   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
259     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
260   myEditedObject->SetBoundaryPolyline( aNewPolyline );
261
262   if ( myPreviewViewManager )
263   {
264     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
265     if ( aViewer )
266     {
267       if ( !aNewPolyline.IsNull() )
268       {
269         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
270         if ( !aCtx.IsNull() )
271         {
272           setObjectVisibility( aNewPolyline, true );
273           anIsToUpdateViewer = true;
274         }
275       }
276
277       if ( anIsToUpdateViewer )
278         module()->update( UF_OCCViewer );
279     }
280   }
281 }
282
283 void HYDROGUI_CalculationOp::onStricklerTableSelected ( const QString & theObjName )
284 {
285   bool anIsToUpdateViewer = false;
286
287   // Remove old presentations of land covers from the operation viewer  
288   Handle(HYDROData_Entity) anEntity;
289   Handle(HYDROData_LandCover) aLandCover;
290   HYDROData_SequenceOfObjects aLandCovers;
291   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
292   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
293   for ( ; anIter.More(); anIter.Next() )
294   {
295     anEntity = anIter.Value();
296     if ( !anEntity.IsNull() )
297     {
298       aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
299       if ( !aLandCover.IsNull() )
300       {
301         aLandCovers.Append( aLandCover );
302         setObjectVisibility( aLandCover, false );
303         anIsToUpdateViewer = true;
304       }
305     }
306   }
307
308   // Set the selected Strickler table to the calculation case
309   Handle(HYDROData_StricklerTable) aNewStricklerTable = Handle(HYDROData_StricklerTable)::DownCast(
310     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_STRICKLER_TABLE ) );
311   myEditedObject->SetStricklerTable( aNewStricklerTable );
312
313   if ( myPreviewViewManager )
314   {
315     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
316     if ( aViewer )
317     {
318       if ( !aNewStricklerTable.IsNull() )
319       {
320         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
321         if ( !aCtx.IsNull() )
322         {
323           HYDROData_SequenceOfObjects::Iterator anIter( aLandCovers );
324           for ( ; anIter.More(); anIter.Next() )
325           {
326             anEntity = anIter.Value();
327             if ( !anEntity.IsNull() )
328             {
329               aLandCover = Handle(HYDROData_LandCover)::DownCast( anEntity );
330               if ( !aLandCover.IsNull() )
331               {
332                 setObjectVisibility( aLandCover, true );
333                 anIsToUpdateViewer = true;
334               }
335             }
336           }          
337         }
338       }
339
340       if ( anIsToUpdateViewer )
341         module()->update( UF_OCCViewer );
342     }
343   }
344 }
345
346 void HYDROGUI_CalculationOp::onObjectsSelected()
347 {
348   HYDROGUI_CalculationDlg* aPanel = 
349     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
350
351   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
352   QMap<QString, bool> aSelectedObjsMap;
353   foreach( QString aName, aSelectedObjs )
354     aSelectedObjsMap[aName] = true;
355
356
357   // Select the appropriate geometry object shape in the viewer
358   selectionMgr()->clearSelected();
359
360   // Unhighlight all objects except selected
361   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
362   Handle(HYDROData_Entity) anEntity;
363   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
364   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
365   bool isSelected;
366   QString aName;
367   for ( ; anIter.More(); anIter.Next() )
368   {
369     anEntity = anIter.Value();
370     if ( !anEntity.IsNull() )
371     {
372       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
373       if ( aShape )
374       {
375         aName = anEntity->GetName();
376         isSelected = aSelectedObjsMap.contains( aName );
377         aShape->highlight( isSelected, false );
378         aShape->update( false, false );
379         aLastShape = aShape;
380       }
381     }
382   }
383   if( aLastShape )
384     aLastShape->update( true, false );
385 }
386
387 void HYDROGUI_CalculationOp::onLandCoversSelected()
388 {
389   HYDROGUI_CalculationDlg* aPanel = 
390     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
391
392   QStringList aSelectedObjs = aPanel->getSelectedLandCovers();
393   QMap<QString, bool> aSelectedObjsMap;
394   foreach( QString aName, aSelectedObjs )
395     aSelectedObjsMap[aName] = true;
396
397
398   // Select the appropriate land cover shape in the viewer
399   selectionMgr()->clearSelected();
400
401   // Unhighlight all land covers except selected
402   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
403   Handle(HYDROData_Entity) anEntity;
404   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
405   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
406   bool isSelected;
407   QString aName;
408   for ( ; anIter.More(); anIter.Next() )
409   {
410     anEntity = anIter.Value();
411     if ( !anEntity.IsNull() )
412     {
413       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
414       if ( aShape )
415       {
416         aName = anEntity->GetName();
417         isSelected = aSelectedObjsMap.contains( aName );
418         aShape->highlight( isSelected, false );
419         aShape->update( false, false );
420         aLastShape = aShape;
421       }
422     }
423   }
424   if( aLastShape )
425     aLastShape->update( true, false );
426 }
427
428 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
429 {
430   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
431   HYDROGUI_Zone* aZoneItem;
432   selectionMgr()->clearSelected();
433   if ( aRegionItem )
434   {
435     // Select a region in preview
436     SUIT_DataOwnerPtrList aList( true );
437     DataObjectList aZones = aRegionItem->children();
438     for ( int i = 0; i < aZones.length(); i++ )
439     {
440       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
441       if ( aZoneItem )
442       {
443         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
444       }
445     }
446     selectionMgr()->setSelected( aList );
447   }
448   else
449   {
450     // select a single zone
451     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
452     if ( aZoneItem )
453     {
454       SUIT_DataOwnerPtrList aList( true );
455       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
456       selectionMgr()->setSelected( aList );
457     }
458   }
459 }
460
461 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
462 {
463   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
464   if ( aRegion )
465   {
466     QList<HYDROGUI_Zone*> aZonesList;
467     HYDROGUI_Zone* aZone;
468     // Get a list of dropped zones
469     for ( int i = 0; i < theZonesList.length(); i++ )
470     {
471       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
472       if ( aZone )
473       {
474         aZonesList.append( aZone );
475       }
476     }
477     if ( aZonesList.length() > 0 )
478     {
479       aRegion->addZones( aZonesList );
480       HYDROGUI_CalculationDlg* aPanel = 
481         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
482       if ( aPanel )
483       {
484         aPanel->refreshZonesBrowser();
485       }
486       createPreview();
487     }
488   }
489 }
490
491 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
492 {
493   QList<HYDROGUI_Zone*> aZonesList;
494   HYDROGUI_Zone* aZone;
495   // Get a list of dropped zones
496   for ( int i = 0; i < theZonesList.length(); i++ )
497   {
498     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
499     if ( aZone )
500     {
501       aZonesList.append( aZone );
502     }
503   }
504   if ( aZonesList.length() > 0 )
505   {
506     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
507     HYDROGUI_CalculationDlg* aPanel = 
508       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
509     if ( aPanel )
510     {
511       aPanel->refreshZonesBrowser();
512     }
513     createPreview();
514   }
515 }
516
517 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theAltitudeName )
518 {
519   HYDROGUI_CalculationDlg* aPanel = 
520     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
521   if ( aPanel )
522   {
523     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
524     if ( aZone )
525     {
526       aZone->setMergeType( theMergeType, theAltitudeName );
527       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
528       if ( aShape )
529       {
530         aShape->update( true, false );
531       }
532     }
533     aPanel->refreshZonesBrowser();
534   }
535 }
536
537 void HYDROGUI_CalculationOp::onAddObjects()
538 {
539   HYDROGUI_CalculationDlg* aPanel = 
540     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
541   if ( !aPanel )
542     return;
543
544   // Add geometry objects selected in the module browser to the calculation case
545   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
546   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
547     return;
548
549   QStringList anAddedList;
550   for (int i = 0; i < aSelectedList.length(); i++)
551   {
552     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
553       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
554     if ( anObject.IsNull() )
555       continue;
556
557     if ( myEditedObject->AddGeometryObject( anObject ) )
558       anAddedList.append( anObject->GetName() );
559   }
560
561   if ( !anAddedList.isEmpty() )
562   {
563     aPanel->includeGeomObjects( anAddedList );
564     createPreview();
565   }
566 }
567
568 void HYDROGUI_CalculationOp::onRemoveObjects()
569 {
570   // Remove selected objects from the calculation case
571   HYDROGUI_CalculationDlg* aPanel = 
572     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
573   if ( !aPanel )
574     return;
575
576   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
577   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
578     return;
579
580   for (int i = 0; i < aSelectedList.length(); i++)
581   {
582     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
583       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
584     if ( anObject.IsNull() )
585       continue;
586
587     setObjectVisibility( anObject, false );
588     myEditedObject->RemoveGeometryObject( anObject );
589   }
590
591   module()->update( UF_OCCViewer );
592   aPanel->excludeGeomObjects( aSelectedList );
593 }
594
595 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
596 {
597   // Check if the case is already modified or not
598   bool isConfirmed = myEditedObject->IsMustBeUpdated();
599   if ( !isConfirmed )
600   {
601     // If not modified check if the case has already defined regions with zones
602     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
603     if ( aSeq.Length() > 0 )
604     {
605       // If there are already defined zones then ask a user to confirm zones recalculation
606       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
607                                tr( "REGIONS_CHANGED" ),
608                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
609                                QMessageBox::Yes | QMessageBox::No,
610                                QMessageBox::No ) == QMessageBox::Yes );
611     }
612     else
613     {
614       isConfirmed = true; // No regions - no zones - nothing to recalculate
615     }
616   }
617   return isConfirmed;
618 }
619
620 bool HYDROGUI_CalculationOp::confirmOrderChange() const
621 {
622   // Check if the case is already modified or not
623   bool isConfirmed = myEditedObject->IsMustBeUpdated();
624   if ( !isConfirmed )
625   {
626     // If not modified check if the case has already defined regions with zones
627     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
628     if ( aSeq.Length() > 0 )
629     {
630       // If there are already defined zones then ask a user to confirm zones recalculation
631       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
632                                tr( "ORDER_CHANGED" ),
633                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
634                                QMessageBox::Yes | QMessageBox::No,
635                                QMessageBox::No ) == QMessageBox::Yes );
636     }
637     else
638     {
639       isConfirmed = true; // No regions - no zones - nothing to recalculate
640     }
641   }
642   return isConfirmed;
643 }
644
645 bool HYDROGUI_CalculationOp::confirmModeChange() const
646 {
647   // Check if the case is already modified or not
648   bool isConfirmed = myEditedObject->IsMustBeUpdated();
649   if ( !isConfirmed )
650   {
651     // If not modified check if the case has already defined regions with zones
652     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
653     if ( aSeq.Length() > 0 )
654     {
655       // If there are already defined zones then ask a user to confirm zones recalculation
656       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
657                                tr( "MODE_CHANGED" ),
658                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
659                                QMessageBox::Yes | QMessageBox::No,
660                                QMessageBox::No ) == QMessageBox::Yes );
661     }
662     else
663     {
664       isConfirmed = true; // No regions - no zones - nothing to recalculate
665     }
666   }
667   return isConfirmed;
668 }
669
670 bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
671 {
672   HYDROData_WarningType aType = theWarning.Type;
673   if ( aType == WARN_OK ) {
674     return true;
675   }
676
677   QString aTitle;
678   QString aMsg;
679   switch ( aType )
680   {
681     case WARN_EMPTY_REGIONS:
682       aTitle = tr( "EMPTY_REGIONS" );
683       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
684       break;
685     default:
686       aTitle = tr( "WARNING" );
687       aMsg = theWarning.Data;
688   }
689
690
691   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
692                                            aTitle, aMsg,
693                                            QMessageBox::Yes | QMessageBox::No,
694                                            QMessageBox::No );
695
696   return ( anAnswer == QMessageBox::Yes );
697 }
698
699 bool HYDROGUI_CalculationOp::confirmLandCoverModeChange() const
700 {
701   // Check if the case is already modified or not
702   bool isConfirmed = myEditedObject->IsMustBeUpdated();
703   if ( !isConfirmed )
704   {
705     // If not modified check if the case has already defined regions with zones
706     // TODO: adapt HYDROData_CalculationCase class to process regions constructed for land covers
707     /*HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCoverRegions();
708     if ( aSeq.Length() > 0 )
709     {*/
710       // If there are already defined zones then ask a user to confirm zones recalculation
711       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
712                                tr( "MODE_CHANGED" ),
713                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_MODE" ),
714                                QMessageBox::Yes | QMessageBox::No,
715                                QMessageBox::No ) == QMessageBox::Yes );
716     /*}
717     else
718     {
719       isConfirmed = true; // No regions - no zones - nothing to recalculate
720     }*/
721   }
722   return isConfirmed;
723 }
724
725 bool HYDROGUI_CalculationOp::confirmLandCoverOrderChange() const
726 {
727   // Check if the case is already modified or not
728   bool isConfirmed = myEditedObject->IsMustBeUpdated();
729   if ( !isConfirmed )
730   {
731     // If not modified check if the case has already defined regions with zones
732     // TODO: adapt HYDROData_CalculationCase class to process regions constructed for land covers
733     /*HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCoverRegions();
734     if ( aSeq.Length() > 0 )
735     {*/
736       // If there are already defined zones then ask a user to confirm zones recalculation
737       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
738                                tr( "ORDER_CHANGED" ),
739                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
740                                QMessageBox::Yes | QMessageBox::No,
741                                QMessageBox::No ) == QMessageBox::Yes );
742     /*}
743     else
744     {
745       isConfirmed = true; // No regions - no zones - nothing to recalculate
746     }*/
747   }
748   return isConfirmed;
749 }
750
751 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
752                                            QString& theErrorMsg,
753                                            QStringList& theBrowseObjectsEntries )
754 {
755   HYDROGUI_CalculationDlg* aPanel = 
756     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
757   if ( !aPanel )
758     return false;
759
760   if( !myIsEdit )
761   {
762     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
763     theBrowseObjectsEntries.append( anEntry );
764   }
765
766   // For manual mode priority rules are redundant
767   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
768     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
769   }
770   if ( aPanel->getLandCoverMode() == HYDROData_CalculationCase::MANUAL ) {
771     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, false );
772   }
773  
774   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
775
776   return true;
777 }
778
779 void HYDROGUI_CalculationOp::onApply()
780 {
781   // Check warnings
782   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
783   if ( aWarning.Type != WARN_OK ) {
784     if ( !confirmContinueWithWarning( aWarning ) ) {
785       // Go back to the first page
786       HYDROGUI_CalculationDlg* aPanel = 
787         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
788       if ( aPanel ) {
789         aPanel->onFirstPage();
790       }
791       return;
792     }
793   }
794
795   QApplication::setOverrideCursor( Qt::WaitCursor );
796
797   int anUpdateFlags = 0;
798   QString anErrorMsg;
799   QStringList aBrowseObjectsEntries;
800
801   bool aResult = false;
802   
803   try
804   {
805     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
806   }
807   catch ( Standard_Failure )
808   {
809     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
810     anErrorMsg = aFailure->GetMessageString();
811     aResult = false;
812   }
813   catch ( ... )
814   {
815     aResult = false;
816   }
817   
818   QApplication::restoreOverrideCursor();
819
820   if ( aResult )
821   {
822     module()->update( anUpdateFlags );
823     commit();
824     browseObjects( aBrowseObjectsEntries );
825   }
826   else
827   {
828     abort();
829     QString aMsg = tr( "INPUT_VALID_DATA" );
830     if( !anErrorMsg.isEmpty() )
831       aMsg.prepend( anErrorMsg + "\n" );
832     SUIT_MessageBox::critical( module()->getApp()->desktop(),
833                                tr( "INSUFFICIENT_INPUT_DATA" ),
834                                aMsg ); 
835   }
836 }
837
838 void HYDROGUI_CalculationOp::onNext( const int theIndex )
839 {
840   if( theIndex==1 )
841   {
842     setAvailableGroups();
843   }
844   else if( theIndex==2 )
845   {
846     // Land covers panel
847      HYDROGUI_CalculationDlg* aPanel = 
848       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
849     if ( !aPanel )
850       return;
851
852     QStringList aList;
853     QStringList anEntryList;
854     HYDROData_SequenceOfObjects aSeq;
855
856     Handle(HYDROData_StricklerTable) aStricklerTableObj;
857     QString aStricklerTableName;
858
859     // Get all Strickler table objects to fill in combo-box
860     aList.clear();
861     anEntryList.clear();
862     HYDROData_Iterator anIter( doc(), KIND_STRICKLER_TABLE );      
863     for ( ; anIter.More(); anIter.Next() )
864     {
865       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
866
867       if ( !aStricklerTableObj.IsNull() )
868       { 
869         aStricklerTableName = aStricklerTableObj->GetName();
870         if ( !aStricklerTableName.isEmpty() )
871         {
872           aList.append( aStricklerTableName );
873           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
874         }        
875       }
876     }
877     aPanel->setStricklerTableNames( aList, anEntryList );
878     if ( !aList.isEmpty() )
879       aPanel->setStricklerTable( aList.at( 0 ), false );
880
881     // Fill in list widget with all available land covers
882     aSeq = HYDROGUI_Tool::GetLandCovers( module() );
883     getNamesAndEntries( aSeq, aList, anEntryList );
884     aPanel->setAllLandCovers( aList, anEntryList );
885
886     // Set list of included land covers
887     aSeq = myEditedObject->GetLandCovers();
888     getNamesAndEntries( aSeq, aList, anEntryList );
889     aPanel->includeLandCovers( aList, true );
890     
891     if ( !myEditedObject.IsNull() )
892     {
893       if ( myIsEdit )
894       {      
895         // Select the certain Strickler table object in combo-box
896         aStricklerTableObj = myEditedObject->GetStricklerTable();
897         if ( aStricklerTableObj.IsNull() )
898         {
899           aPanel->setStricklerTable( QString() );
900         }
901         else
902         {
903           aStricklerTableName = aStricklerTableObj->GetName();
904           aPanel->setStricklerTable( aStricklerTableName );
905         }
906
907         // Set mode (Auto or Manual) to defined priority of land covers
908         aPanel->setLandCoverMode( myEditedObject->GetAssignmentLandCoverMode() );
909
910         // Set rules defined on land covers
911         setRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
912       }
913       else
914       {
915         myEditedObject->SetAssignmentLandCoverMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getLandCoverMode() );
916       }
917     }
918   }
919   else if( theIndex==3 )
920   {
921     HYDROGUI_CalculationDlg* aPanel = 
922       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
923     if ( !aPanel )
924       return;
925
926     QApplication::setOverrideCursor( Qt::WaitCursor );
927   
928     QString aNewCaseName = aPanel->getObjectName();
929     QString anOldCaseName = myEditedObject->GetName();
930     bool isNameChanged = anOldCaseName != aNewCaseName;
931   
932     bool anIsToUpdateOb = isNameChanged;
933
934     // At first we must to update the case name because of 
935     // automatic names generation for regions and zones
936     myEditedObject->SetName( aNewCaseName );
937     
938     // Set parameters for automatic mode
939     int aMode = aPanel->getMode();
940     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
941     {
942       // Set objects in the specified order
943       if( myEditedObject->IsMustBeUpdated() )
944       {
945         myEditedObject->RemoveGeometryObjects();
946         foreach ( const QString& aName, aPanel->getAllGeomObjects() )
947         {
948           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
949             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
950           if ( anObject.IsNull() )
951           {
952             continue;
953           }
954           myEditedObject->AddGeometryObject( anObject );
955         }
956
957         // Clear priority rules
958         //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
959           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
960         //@ASL }
961         // Set priority rules
962         foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
963           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
964                                   aRule.Object2, aRule.MergeType,
965                                   HYDROData_CalculationCase::DataTag_CustomRules );
966         }
967       }
968     }
969     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
970          
971     if ( myEditedObject->IsMustBeUpdated() )
972     {
973       myShowZones = true;
974       myEditedObject->Update();
975       
976       AssignDefaultZonesColors();
977
978       //aPanel->setEditedObject( myEditedObject );
979       aPanel->refreshZonesBrowser();
980     
981       closePreview();
982       createPreview();
983
984       anIsToUpdateOb = true;
985
986       myEditedObject->SetToUpdate( true );
987     }
988     else
989     {
990       setZonesVisible( true );
991
992       if ( isNameChanged ) {
993         module()->getDataModel()->updateObjectTree( myEditedObject );
994       }
995     }
996
997     // Move this code to processing od the last panel (see below)
998     if ( anIsToUpdateOb ) {
999       SUIT_DataBrowser* anObjBrowser = ((LightApp_Application*)module()->application())->objectBrowser();
1000       if ( anObjBrowser ) {
1001         anObjBrowser->updateTree( module()->getDataModel()->getDataObject( myEditedObject ), false );
1002       }
1003     }
1004
1005     QApplication::restoreOverrideCursor();
1006   }
1007   else if( theIndex==4 )
1008   {
1009     // Partition of Land covers panel
1010     HYDROGUI_CalculationDlg* aPanel = 
1011       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1012     if ( !aPanel )
1013       return;
1014
1015     QApplication::setOverrideCursor( Qt::WaitCursor );
1016
1017     // Set parameters for automatic mode
1018     int aMode = aPanel->getLandCoverMode();
1019     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
1020     {
1021       // Set objects in the specified order
1022       if( myEditedObject->IsMustBeUpdated() )
1023       {
1024         myEditedObject->RemoveLandCovers();
1025         foreach ( const QString& aName, aPanel->getAllLandCovers() )
1026         {
1027           Handle(HYDROData_LandCover) aLandCover = Handle(HYDROData_LandCover)::DownCast( 
1028             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
1029           if ( aLandCover.IsNull() )
1030           {
1031             continue;
1032           }
1033           myEditedObject->AddLandCover( aLandCover );
1034         }
1035
1036         // Clear priority rules
1037         myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, true );
1038         // Set priority rules
1039         foreach ( const HYDROData_CustomRule& aRule, aPanel->getLandCoverRules() ) {
1040           myEditedObject->AddRule( aRule.Object1, aRule.Priority,
1041                                    aRule.Object2, HYDROData_Zone::Merge_Object,
1042                                    HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
1043         }
1044       }
1045     }
1046     aPanel->setEditLandCoversEnabled( aMode == HYDROData_CalculationCase::MANUAL );
1047
1048     QApplication::restoreOverrideCursor();
1049   }
1050 }
1051
1052 void HYDROGUI_CalculationOp::onHideZones()
1053 {
1054   setZonesVisible( false );
1055 }
1056
1057 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
1058 {
1059   myShowZones = theIsVisible;
1060   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1061   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1062   HYDROData_SequenceOfObjects aZones;
1063   Handle(HYDROData_Region) aRegion;
1064   if ( myPreviewViewManager ) 
1065   {
1066     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1067     {
1068       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1069       if ( !aCtx.IsNull() )
1070       {
1071         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1072         {
1073           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1074           if ( !aRegion.IsNull() )
1075           {
1076             aZones = aRegion->GetZones();
1077             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1078             for ( ; aZonesIter.More(); aZonesIter.Next() )
1079             {
1080               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1081             }
1082           }
1083         }
1084       }
1085
1086       module()->update( UF_OCCViewer );
1087     }
1088   }
1089 }
1090
1091 void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
1092 {
1093   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1094   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1095   HYDROData_SequenceOfObjects aZones;
1096   Handle(HYDROData_Region) aRegion;
1097   if ( myPreviewViewManager ) 
1098   {
1099     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1100     {
1101       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1102       if ( !aCtx.IsNull() )
1103       {
1104         int aCounter = 0;        
1105         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1106         {
1107           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1108           if ( !aRegion.IsNull() )
1109           {
1110             aZones = aRegion->GetZones();
1111             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1112             for ( ; aZonesIter.More(); aZonesIter.Next() )
1113             {
1114               // Zone
1115               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1116               if ( !aZone.IsNull() )
1117               {
1118                 QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
1119                 while (aFillingColor == Qt::red)
1120                   aFillingColor = GenerateDefaultZonesColor(++aCounter);
1121                 
1122                 aZone->SetColor(aFillingColor);
1123               }
1124             }
1125           }
1126         }
1127       }
1128     }
1129   }
1130 }
1131
1132 QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
1133                                                           float theSaturation/* = 0.5*/,
1134                                                           float theValue/* = 0.95*/ ) const
1135 {
1136   float aGoldenRatioConjugate = (float)(360./582.);
1137   float aHue = (float)rand();
1138   aHue += aGoldenRatioConjugate*theIndex;
1139   aHue -= floor(aHue);
1140
1141   float aR = 0., aG = 0., aB = 0.;
1142   int aHueInt = (int)(aHue*6.);
1143   float aF = aHue*6. - aHueInt;
1144   float aP = theValue * (1. - theSaturation);
1145   float aQ = theValue * (1. - aF*theSaturation);
1146   float aT = theValue * (1. - (1. - aF) * theSaturation);
1147   switch (aHueInt)
1148   {
1149   case 0: { aR = theValue; aG = aT; aB = aP; break; }
1150   case 1: { aR = aQ; aG = theValue; aB = aP; break; }
1151   case 2: { aR = aP; aG = theValue; aB = aT; break; }
1152   case 3: { aR = aP; aG = aQ; aB = theValue; break; }
1153   case 4: { aR = aT; aG = aP; aB = theValue; break; }
1154   case 5: { aR = theValue; aG = aP; aB = aQ; break; }
1155   default: break;
1156   }
1157
1158   QColor aColor = QColor( (int)(aR*256.), (int)(aG*256.), (int)(aB*256.) );
1159   return ( aColor.isValid() ? aColor : HYDROData_ImmersibleZone::DefaultFillingColor() );
1160 }
1161
1162 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1163 {
1164   HYDROGUI_CalculationDlg* aPanel = 
1165     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1166   if ( !aPanel )
1167     return;
1168
1169   HYDROData_ListOfRules aRules;
1170   Handle(HYDROData_Entity) anObject1, anObject2;
1171   HYDROData_PriorityType aPriority;
1172   HYDROData_Zone::MergeAltitudesType aMergeType;
1173   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1174   for ( int anIndex = 0; ; anIndex++ )
1175   {
1176     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1177       HYDROData_CustomRule aRule;
1178       aRule.Object1 = anObject1;
1179       aRule.Object2 = anObject2;
1180       aRule.Priority = aPriority;
1181       aRule.MergeType = aMergeType;
1182
1183       aRules << aRule;
1184     }
1185     else
1186       break;
1187   }
1188
1189   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1190     aPanel->setRules( aRules );
1191   else if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomLandCoverRules )
1192     aPanel->setLandCoverRules( aRules );
1193 }
1194
1195 void HYDROGUI_CalculationOp::createPreview()
1196 {
1197   LightApp_Application* anApp = module()->getApp();
1198   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1199   Handle(HYDROData_Entity) anEntity;
1200
1201   if ( myShowZones )
1202   {
1203     // Gather zones for displaying
1204     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1205     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1206     HYDROData_SequenceOfObjects aZones;
1207     Handle(HYDROData_Region) aRegion;
1208     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1209     {
1210       anEntity = aRegionsIter.Value();
1211       if ( !anEntity.IsNull() )
1212       {
1213         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1214         if ( !aRegion.IsNull() )
1215         {
1216           aZones = aRegion->GetZones();
1217           aSeq.Append( aZones );
1218         }
1219       }
1220     }
1221   }
1222
1223   // Get a boundary polyline if any
1224   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
1225
1226   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1227
1228   if ( !myActiveViewManager )
1229   {
1230     if ( aSeq.IsEmpty() )
1231       return;
1232
1233     myActiveViewManager = anApp->activeViewManager();
1234   }
1235
1236   if ( !myPreviewViewManager )
1237   {
1238     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1239       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1240     if ( myPreviewViewManager )
1241     {
1242       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1243                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1244
1245       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1246       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1247     }
1248   }
1249
1250   if ( !myPreviewViewManager )
1251     return;
1252
1253   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1254   {
1255     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1256     if ( !aCtx.IsNull() )
1257     {
1258       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1259       for ( ; anIter.More(); anIter.Next() )
1260       {
1261         setObjectVisibility( anIter.Value(), true );
1262       }
1263
1264       //Process the draw events for viewer
1265       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1266       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
1267         vw->onTopView();
1268     }
1269
1270     module()->update( UF_OCCViewer | UF_FitAll );
1271   }
1272 }
1273
1274 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1275 {
1276   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1277     return;
1278   }
1279
1280   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1281   if ( aViewer ) {
1282     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1283   }
1284 }
1285
1286 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1287 {
1288   closePreview();
1289 }
1290
1291 void HYDROGUI_CalculationOp::closePreview()
1292 {
1293   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1294   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1295   QShortcut* aShortcut;
1296   foreach( aShortcut, aShortcuts )
1297   {
1298     if ( aShortcut->key() == 
1299       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1300       SUIT_DataBrowser::RenameShortcut ) ) )
1301     {
1302       aShortcut->setEnabled( true );
1303     }
1304   }
1305
1306
1307   if( myPreviewViewManager )
1308   {
1309     // Hide all the displayed objects in the preview view
1310     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1311     if ( aViewer ) {
1312       size_t aViewId = (size_t)aViewer;
1313       HYDROData_Iterator anIterator( doc() );
1314       for( ; anIterator.More(); anIterator.Next() ) {
1315         Handle(HYDROData_Entity) anObject = anIterator.Current();
1316         if( !anObject.IsNull() ) {
1317           module()->setObjectVisible( aViewId, anObject, false );
1318         }
1319       }
1320     }
1321
1322     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1323                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1324
1325     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1326     myPreviewViewManager = NULL;
1327   }
1328
1329   if( myActiveViewManager )
1330   {
1331     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1332     myActiveViewManager = NULL;
1333   }
1334 }
1335
1336 void HYDROGUI_CalculationOp::setAvailableGroups()
1337 {
1338   HYDROGUI_CalculationDlg* aPanel = 
1339       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1340
1341   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1342   QStringList aList, anEntryList;
1343   getNamesAndEntries( aSeq, aList, anEntryList );
1344
1345   QStringList aGroupsNames;
1346
1347   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1348   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1349   {
1350     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
1351     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1352     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1353     {
1354       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
1355       aGroupsNames.append( aGroup->GetName() );
1356     }
1357   }
1358   if( myEditedObject->IsMustBeUpdated() ) {
1359     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1360       Handle(HYDROData_ShapesGroup) aGeomGroup =
1361         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1362       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1363         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1364       }
1365     }
1366   }
1367
1368   aPanel->setAvailableGroups( aGroupsNames );
1369   aPanel->includeGroups( aList );
1370
1371   bool isUpdated = myEditedObject->IsMustBeUpdated();
1372 }
1373
1374 void HYDROGUI_CalculationOp::onAddGroups()
1375 {
1376   HYDROGUI_CalculationDlg* aPanel = 
1377     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1378   if ( !aPanel )
1379     return;
1380
1381   // Add geometry objects selected in the module browser to the calculation case
1382   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1383   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1384     return;
1385
1386   QStringList anAddedList;
1387   for (int i = 0; i < aSelectedList.length(); i++)
1388   {
1389     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1390       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1391     if ( aGroup.IsNull() )
1392       continue;
1393
1394     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1395       anAddedList.append( aGroup->GetName() );
1396   }
1397
1398   if ( !anAddedList.isEmpty() )
1399   {
1400     aPanel->includeGroups( anAddedList );
1401   }
1402 }
1403
1404 void HYDROGUI_CalculationOp::onRemoveGroups()
1405 {
1406   // Remove selected objects from the calculation case
1407   HYDROGUI_CalculationDlg* aPanel = 
1408     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1409   if ( !aPanel )
1410     return;
1411
1412   QStringList aSelectedList = aPanel->getSelectedGroups();
1413   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1414     return;
1415
1416   for (int i = 0; i < aSelectedList.length(); i++)
1417   {
1418     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1419       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1420     if ( aGroup.IsNull() )
1421       continue;
1422
1423     myEditedObject->RemoveGeometryGroup( aGroup );
1424   }
1425
1426   aPanel->excludeGroups( aSelectedList );
1427 }
1428
1429 void HYDROGUI_CalculationOp::onChangeLandCoverMode( int theMode )
1430 {
1431   HYDROGUI_CalculationDlg* aPanel = 
1432     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1433   if ( !aPanel )
1434     return;
1435
1436   if ( !confirmLandCoverModeChange() ) {
1437     aPanel->setLandCoverMode( myEditedObject->GetAssignmentLandCoverMode() );
1438     return;
1439   }
1440
1441   myEditedObject->SetAssignmentLandCoverMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1442   aPanel->setLandCoverMode( theMode );
1443 }
1444
1445 void HYDROGUI_CalculationOp::onAddLandCovers()
1446 {
1447   HYDROGUI_CalculationDlg* aPanel = 
1448     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1449   if ( !aPanel )
1450     return;
1451
1452   // Add land covers selected in the module browser to the calculation case
1453   QStringList aSelectedList = aPanel->getSelectedAvailableLandCovers();
1454   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1455     return;
1456
1457   QStringList anAddedList;
1458   for (int i = 0; i < aSelectedList.length(); i++)
1459   {
1460     Handle(HYDROData_LandCover) anObject = Handle(HYDROData_LandCover)::DownCast( 
1461       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1462     if ( anObject.IsNull() )
1463       continue;
1464
1465     if ( myEditedObject->AddLandCover( anObject ) )
1466       anAddedList.append( anObject->GetName() );
1467   }
1468
1469   if ( !anAddedList.isEmpty() )
1470   {
1471     aPanel->includeLandCovers( anAddedList, false );
1472     // TODO: create preview of included land covers
1473     //createPreview();
1474   }
1475 }
1476
1477 void HYDROGUI_CalculationOp::onRemoveLandCovers()
1478 {
1479   // Remove selected objects from the calculation case
1480   HYDROGUI_CalculationDlg* aPanel = 
1481     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1482   if ( !aPanel )
1483     return;
1484
1485   QStringList aSelectedList = aPanel->getSelectedLandCovers();
1486   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1487     return;
1488
1489   for (int i = 0; i < aSelectedList.length(); i++)
1490   {
1491     Handle(HYDROData_LandCover) anObject = Handle(HYDROData_LandCover)::DownCast( 
1492       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1493     if ( anObject.IsNull() )
1494       continue;
1495
1496     setObjectVisibility( anObject, false );
1497     myEditedObject->RemoveLandCover( anObject );
1498   }
1499
1500   module()->update( UF_OCCViewer );
1501   aPanel->excludeLandCovers( aSelectedList );
1502 }
1503
1504 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1505 {
1506   HYDROGUI_CalculationDlg* aPanel = 
1507     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1508   if ( !aPanel )
1509     return;
1510
1511   if ( !confirmModeChange() ) {
1512     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1513     return;
1514   }
1515
1516   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1517   aPanel->setMode( theMode );
1518 }
1519
1520 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1521 {
1522   HYDROGUI_CalculationDlg* aPanel = 
1523     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1524   if ( !aPanel )
1525     return;
1526
1527   isConfirmed = confirmOrderChange();
1528   if( isConfirmed )
1529     myEditedObject->SetToUpdate( true );
1530 }
1531
1532 void HYDROGUI_CalculationOp::onOrderLandCoverChanged( bool& isConfirmed )
1533 {
1534   HYDROGUI_CalculationDlg* aPanel = 
1535     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1536   if ( !aPanel )
1537     return;
1538
1539   isConfirmed = confirmLandCoverOrderChange();
1540   if( isConfirmed )
1541     myEditedObject->SetToUpdate( true );
1542 }