Salome HOME
refs# #1339 debug
[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, false, 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, false, 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         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1104         {
1105           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1106           if ( !aRegion.IsNull() )
1107           {
1108             aZones = aRegion->GetZones();
1109             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1110             for ( ; aZonesIter.More(); aZonesIter.Next() )
1111             {
1112               // Zone
1113               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1114               if ( !aZone.IsNull() )
1115               {
1116                 QColor aFillingColor = HYDROData_Tool::GenerateRandColor();
1117                 while (aFillingColor == Qt::red)
1118                   aFillingColor = HYDROData_Tool::GenerateRandColor();
1119                 
1120                 aZone->SetColor(aFillingColor);
1121               }
1122             }
1123           }
1124         }
1125       }
1126     }
1127   }
1128 }
1129
1130 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1131 {
1132   HYDROGUI_CalculationDlg* aPanel = 
1133     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1134   if ( !aPanel )
1135     return;
1136
1137   HYDROData_ListOfRules aRules;
1138   Handle(HYDROData_Entity) anObject1, anObject2;
1139   HYDROData_PriorityType aPriority;
1140   HYDROData_Zone::MergeType aMergeType;
1141   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1142   for ( int anIndex = 0; ; anIndex++ )
1143   {
1144     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1145       HYDROData_CustomRule aRule;
1146       aRule.Object1 = anObject1;
1147       aRule.Object2 = anObject2;
1148       aRule.Priority = aPriority;
1149       aRule.MergeType = aMergeType;
1150
1151       aRules << aRule;
1152     }
1153     else
1154       break;
1155   }
1156
1157   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1158     aPanel->setRules( aRules );  
1159 }
1160
1161 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList )
1162 {
1163   bool aRetValue = false;
1164
1165   QList<HYDROGUI_Zone*> aZonesList;
1166   HYDROGUI_Zone* aZone;
1167   // Get a list of dropped zones
1168   for ( int i = 0; i < theZonesList.length(); i++ )
1169   {
1170     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1171     if ( aZone )
1172     {
1173       aZonesList.append( aZone );
1174     }
1175   }
1176   if ( aZonesList.length() > 0 )
1177   {
1178     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
1179     aRetValue = true;
1180   }
1181    
1182   return aRetValue;
1183 }
1184
1185 void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap, bool fitAllFlag, bool onTopViewAndFit )
1186 {
1187   LightApp_Application* anApp = module()->getApp();
1188   HYDROData_SequenceOfObjects aSeq;
1189   if ( theLandCoverMap && myShowLandCoverMap )
1190   {
1191     aSeq.Append( myEditedObject->GetLandCoverMap() );
1192   }
1193   else if ( !theLandCoverMap && myShowGeomObjects )
1194   {
1195     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1196     HYDROData_SequenceOfObjects aSeqP = myEditedObject->GetInterPolyObjects();
1197     aSeq.Append( aSeqGO );
1198     aSeq.Append( aSeqP );
1199   }
1200
1201   Handle(HYDROData_Entity) anEntity;
1202
1203   if ( myShowZones )
1204   {
1205     // Gather zones for displaying
1206     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1207     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1208     HYDROData_SequenceOfObjects aZones;
1209     Handle(HYDROData_Region) aRegion;
1210     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1211     {
1212       anEntity = aRegionsIter.Value();
1213       if ( !anEntity.IsNull() )
1214       {
1215         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1216         if ( !aRegion.IsNull() )
1217         {
1218           aZones = aRegion->GetZones();
1219           aSeq.Append( aZones );
1220         }
1221       }
1222     }
1223   }
1224
1225   // Get a boundary polyline if any
1226   if (Handle(HYDROData_PolylineXY) aBPoly = myEditedObject->GetBoundaryPolyline())
1227     aSeq.Append( aBPoly );
1228
1229   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1230
1231   if ( !myActiveViewManager )
1232   {
1233     if ( aSeq.IsEmpty() )
1234       return;
1235
1236     myActiveViewManager = anApp->activeViewManager();
1237   }
1238
1239   if ( !myPreviewViewManager )
1240   {
1241     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1242       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1243     if ( myPreviewViewManager )
1244     {
1245       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1246                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1247
1248       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1249       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1250     }
1251   }
1252
1253   if ( !myPreviewViewManager )
1254     return;
1255
1256   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1257   {
1258     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1259     if ( !aCtx.IsNull() )
1260     {
1261       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1262       for ( ; anIter.More(); anIter.Next() )
1263       {
1264         const Handle(HYDROData_Entity)& ent = anIter.Value();
1265         if (!ent.IsNull())
1266           setObjectVisibility( ent, true );
1267       }
1268
1269       //Process the draw events for viewer
1270       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1271     }
1272
1273     int UpdateFlags = UF_OCCViewer;
1274     if (fitAllFlag)
1275       UpdateFlags |= UF_FitAll;
1276     module()->update( UpdateFlags );
1277
1278     OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
1279     if ( onTopViewAndFit && vw )
1280       vw->onTopView();
1281   }
1282 }
1283
1284 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1285 {
1286   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1287     return;
1288   }
1289
1290   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1291   if ( aViewer ) {
1292     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1293   }
1294 }
1295
1296 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1297 {
1298   closePreview();
1299 }
1300
1301 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1302 {
1303   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1304   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1305   QShortcut* aShortcut;
1306   foreach( aShortcut, aShortcuts )
1307   {
1308     if ( aShortcut->key() == 
1309       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1310       SUIT_DataBrowser::RenameShortcut ) ) )
1311     {
1312       aShortcut->setEnabled( true );
1313     }
1314   }
1315
1316
1317   if( myPreviewViewManager )
1318   {
1319     // Hide all the displayed objects in the preview view
1320     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1321     if ( aViewer ) {
1322       size_t aViewId = (size_t)aViewer;
1323       HYDROData_Iterator anIterator( doc() );
1324       for( ; anIterator.More(); anIterator.Next() ) {
1325         Handle(HYDROData_Entity) anObject = anIterator.Current();
1326         if( !anObject.IsNull() ) {
1327           module()->setObjectVisible( aViewId, anObject, false );
1328         }
1329       }
1330     }
1331
1332     if ( theRemoveViewManager )
1333     {
1334       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1335                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1336
1337       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1338       myPreviewViewManager = NULL;
1339     }
1340   }
1341
1342   if( myActiveViewManager && theRemoveViewManager )
1343   {
1344     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1345     myActiveViewManager = NULL;
1346   }
1347 }
1348
1349 void HYDROGUI_CalculationOp::setAvailableGroups()
1350 {
1351   HYDROGUI_CalculationDlg* aPanel = 
1352       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1353
1354   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1355   QStringList aList, anEntryList;
1356   getNamesAndEntries( aSeq, aList, anEntryList );
1357
1358   QStringList aGroupsNames;
1359
1360   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1361   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1362   {
1363     Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( anObjs.Value( anIndex ) );
1364     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1365     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1366     {
1367       Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( aGIndex ) );
1368       aGroupsNames.append( aGroup->GetName() );
1369     }
1370   }
1371   if( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) ) {
1372     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1373       Handle(HYDROData_ShapesGroup) aGeomGroup =
1374         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1375       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1376         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1377       }
1378     }
1379   }
1380
1381   aPanel->setAvailableGroups( aGroupsNames );
1382   aPanel->includeGroups( aList );
1383
1384   //@ASL: bool isUpdated = myEditedObject->IsMustBeUpdated();
1385 }
1386
1387 void HYDROGUI_CalculationOp::onAddGroups()
1388 {
1389   HYDROGUI_CalculationDlg* aPanel = 
1390     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1391   if ( !aPanel )
1392     return;
1393
1394   // Add geometry objects selected in the module browser to the calculation case
1395   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1396   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1397     return;
1398
1399   QStringList anAddedList;
1400   for (int i = 0; i < aSelectedList.length(); i++)
1401   {
1402     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1403       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1404     if ( aGroup.IsNull() )
1405       continue;
1406
1407     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1408       anAddedList.append( aGroup->GetName() );
1409   }
1410
1411   if ( !anAddedList.isEmpty() )
1412   {
1413     aPanel->includeGroups( anAddedList );
1414   }
1415 }
1416
1417 void HYDROGUI_CalculationOp::onRemoveGroups()
1418 {
1419   // Remove selected objects from the calculation case
1420   HYDROGUI_CalculationDlg* aPanel = 
1421     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1422   if ( !aPanel )
1423     return;
1424
1425   QStringList aSelectedList = aPanel->getSelectedGroups();
1426   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1427     return;
1428
1429   for (int i = 0; i < aSelectedList.length(); i++)
1430   {
1431     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1432       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1433     if ( aGroup.IsNull() )
1434       continue;
1435
1436     myEditedObject->RemoveGeometryGroup( aGroup );
1437   }
1438
1439   aPanel->excludeGroups( aSelectedList );
1440 }
1441
1442 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1443 {
1444   HYDROGUI_CalculationDlg* aPanel = 
1445     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1446   if ( !aPanel )
1447     return;
1448
1449   if ( !confirmModeChange() ) {
1450     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1451     return;
1452   }
1453
1454   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1455   aPanel->setMode( theMode );
1456 }
1457
1458 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1459 {
1460   HYDROGUI_CalculationDlg* aPanel = 
1461     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1462   if ( !aPanel )
1463     return;
1464
1465   isConfirmed = confirmOrderChange();
1466   if( isConfirmed )
1467     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1468 }
1469
1470 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1471 {
1472   HYDROGUI_CalculationDlg* aPanel = 
1473     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1474   if ( !aPanel )
1475     return;
1476
1477   isConfirmed = confirmRuleChange();
1478   if( isConfirmed )
1479     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1480 }
1481
1482 void HYDROGUI_CalculationOp::onRegenerateColors()
1483 {
1484   // For geometry zones
1485   AssignDefaultZonesColors();
1486   setZonesVisible( false );
1487   setZonesVisible( true );
1488 }