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