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