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