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