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