Salome HOME
refs #430: incorrect coordinates in dump polyline
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_CalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_CalculationDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30 #include "HYDROGUI_Zone.h"
31 #include "HYDROGUI_Region.h"
32
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_ShapesGroup.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Object.h>
37 #include <HYDROData_Tool.h>
38
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42
43 #include <LightApp_Application.h>
44 #include <LightApp_UpdateFlags.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <LightApp_DataOwner.h>
47
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
50 #include <SUIT_DataBrowser.h>
51
52 #include <QApplication>
53 #include <QKeySequence>
54 #include <QShortcut>
55
56 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
57 : HYDROGUI_Operation( theModule ),
58   myIsEdit( theIsEdit ),
59   myActiveViewManager( NULL ),
60   myPreviewViewManager( NULL )
61 {
62   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
63 }
64
65 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
66 {
67   closePreview();
68 }
69
70 void HYDROGUI_CalculationOp::startOperation()
71 {
72   HYDROGUI_Operation::startOperation();
73   
74   // Begin transaction
75   startDocOperation();
76
77   HYDROGUI_CalculationDlg* aPanel = 
78     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
79   if ( !aPanel )
80     return;
81
82   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
83   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
84   QShortcut* aShortcut;
85   foreach( aShortcut, aShortcuts )
86   {
87     if ( aShortcut->key() == 
88       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
89       SUIT_DataBrowser::RenameShortcut ) ) )
90     {
91       aShortcut->setEnabled( false );
92     }
93   }
94
95   aPanel->reset();
96   QStringList aList;
97   QStringList anEntryList;
98   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
99   getNamesAndEntries( aSeq, aList, anEntryList );
100   aPanel->setAllGeomObjects( aList, anEntryList );
101
102   // Get all polylines
103   aList.clear();
104   anEntryList.clear();
105   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
106   Handle(HYDROData_PolylineXY) aPolylineObj;
107   QString aPolylineName;
108   for ( ; anIter.More(); anIter.Next() )
109   {
110     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
111
112     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
113     { 
114       // Check the polyline shape
115       TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
116       if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
117         aPolylineName = aPolylineObj->GetName();
118         if ( !aPolylineName.isEmpty() )
119         {
120           aList.append( aPolylineName );
121           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
122         }
123       }
124     }
125   }
126   aPanel->setPolylineNames( aList, anEntryList );
127
128   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
129
130   myEditedObject.Nullify();
131   if ( myIsEdit )
132   {
133     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
134     if ( !myEditedObject.IsNull() )
135     {
136       anObjectName = myEditedObject->GetName();
137       aPolylineObj = myEditedObject->GetBoundaryPolyline();
138       if ( aPolylineObj.IsNull() )
139       {
140         aPanel->setBoundary( QString() );
141       }
142       else
143       {
144         aPolylineName = aPolylineObj->GetName();
145         aPanel->setBoundary( aPolylineName );
146       }
147
148       aSeq = myEditedObject->GetGeometryObjects();
149       getNamesAndEntries( aSeq, aList, anEntryList );
150       aPanel->includeGeomObjects( aList );
151     }
152   }
153   else
154   {
155     myEditedObject =
156       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
157     myEditedObject->SetName(anObjectName);
158   }
159
160   aPanel->setObjectName( anObjectName );
161   aPanel->setEditedObject( myEditedObject );
162
163   createPreview();
164 }
165
166 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
167                                                 QStringList& theNames, QStringList& theEntries ) const
168 {
169  
170   theNames.clear();
171   theEntries.clear();
172   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
173   for ( ; anIter.More(); anIter.Next() )
174   {
175     Handle(HYDROData_Entity) anEntity = anIter.Value();
176     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
177     //  continue;
178
179     theNames.append( anEntity->GetName() );
180     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
181   }
182 }
183
184 void HYDROGUI_CalculationOp::abortOperation()
185 {
186   closePreview();
187   // Abort transaction
188   abortDocOperation();
189   HYDROGUI_Operation::abortOperation();
190   module()->getApp()->updateObjectBrowser();
191 }
192
193 void HYDROGUI_CalculationOp::commitOperation()
194 {
195   closePreview();
196   // Commit transaction
197   commitDocOperation();
198   HYDROGUI_Operation::commitOperation();
199 }
200
201 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
202 {
203   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
204
205   // Connect signals and slots
206   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
207   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
208   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
209   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
210   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
211   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones() ) );
212   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
213   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
214   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
215     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
216   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
217     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
218   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
219     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
220   connect( aPanel, SIGNAL( objectsSelected() ), 
221            SLOT( onObjectsSelected() ) );
222   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
223     SLOT( onBoundarySelected( const QString & ) ) );
224
225   return aPanel;
226 }
227
228 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
229 {
230   bool anIsToUpdateViewer = false;
231
232   // Remove the old boundary from the operation viewer
233   Handle(HYDROData_PolylineXY) aPrevPolyline = 
234     myEditedObject->GetBoundaryPolyline();
235   if ( !aPrevPolyline.IsNull() )
236   {
237     setObjectVisibility( aPrevPolyline, false );
238     anIsToUpdateViewer = true;
239   }
240
241   // Set the selected boundary polyline to the calculation case
242   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
243     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
244   myEditedObject->SetBoundaryPolyline( aNewPolyline );
245
246   if ( myPreviewViewManager )
247   {
248     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
249     if ( aViewer )
250     {
251       if ( !aNewPolyline.IsNull() )
252       {
253         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
254         if ( !aCtx.IsNull() )
255         {
256           setObjectVisibility( aNewPolyline, true );
257           anIsToUpdateViewer = true;
258         }
259       }
260
261       if ( anIsToUpdateViewer )
262         module()->update( UF_OCCViewer );
263     }
264   }
265 }
266
267 void HYDROGUI_CalculationOp::onObjectsSelected()
268 {
269   HYDROGUI_CalculationDlg* aPanel = 
270     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
271
272   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
273   QMap<QString, bool> aSelectedObjsMap;
274   foreach( QString aName, aSelectedObjs )
275     aSelectedObjsMap[aName] = true;
276
277
278   // Select the appropriate geometry object shape in the viewer
279   selectionMgr()->clearSelected();
280
281   // Unhighlight all objects except selected
282   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
283   Handle(HYDROData_Entity) anEntity;
284   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
285   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
286   bool isSelected;
287   QString aName;
288   for ( ; anIter.More(); anIter.Next() )
289   {
290     anEntity = anIter.Value();
291     if ( !anEntity.IsNull() )
292     {
293       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
294       if ( aShape )
295       {
296         aName = anEntity->GetName();
297         isSelected = aSelectedObjsMap.contains( aName );
298         aShape->highlight( isSelected, false );
299         aShape->update( false );
300         aLastShape = aShape;
301       }
302     }
303   }
304   if( aLastShape )
305     aLastShape->update( true );
306 }
307
308 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
309 {
310   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
311   HYDROGUI_Zone* aZoneItem;
312   selectionMgr()->clearSelected();
313   if ( aRegionItem )
314   {
315     // Select a region in preview
316     SUIT_DataOwnerPtrList aList( true );
317     DataObjectList aZones = aRegionItem->children();
318     for ( int i = 0; i < aZones.length(); i++ )
319     {
320       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
321       if ( aZoneItem )
322       {
323         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
324       }
325     }
326     selectionMgr()->setSelected( aList );
327   }
328   else
329   {
330     // select a single zone
331     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
332     if ( aZoneItem )
333     {
334       SUIT_DataOwnerPtrList aList( true );
335       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
336       selectionMgr()->setSelected( aList );
337     }
338   }
339 }
340
341 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
342 {
343   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
344   if ( aRegion )
345   {
346     QList<HYDROGUI_Zone*> aZonesList;
347     HYDROGUI_Zone* aZone;
348     // Get a list of dropped zones
349     for ( int i = 0; i < theZonesList.length(); i++ )
350     {
351       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
352       if ( aZone )
353       {
354         aZonesList.append( aZone );
355       }
356     }
357     if ( aZonesList.length() > 0 )
358     {
359       aRegion->addZones( aZonesList );
360       HYDROGUI_CalculationDlg* aPanel = 
361         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
362       if ( aPanel )
363       {
364         aPanel->refreshZonesBrowser();
365       }
366       createPreview();
367     }
368   }
369 }
370
371 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
372 {
373   QList<HYDROGUI_Zone*> aZonesList;
374   HYDROGUI_Zone* aZone;
375   // Get a list of dropped zones
376   for ( int i = 0; i < theZonesList.length(); i++ )
377   {
378     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
379     if ( aZone )
380     {
381       aZonesList.append( aZone );
382     }
383   }
384   if ( aZonesList.length() > 0 )
385   {
386     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
387     HYDROGUI_CalculationDlg* aPanel = 
388       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
389     if ( aPanel )
390     {
391       aPanel->refreshZonesBrowser();
392     }
393     createPreview();
394   }
395 }
396
397 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theAltitudeName )
398 {
399   HYDROGUI_CalculationDlg* aPanel = 
400     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
401   if ( aPanel )
402   {
403     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
404     if ( aZone )
405     {
406       aZone->setMergeType( theMergeType, theAltitudeName );
407       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
408       if ( aShape )
409       {
410         aShape->update();
411       }
412     }
413     aPanel->refreshZonesBrowser();
414   }
415 }
416
417 void HYDROGUI_CalculationOp::onAddObjects()
418 {
419   HYDROGUI_CalculationDlg* aPanel = 
420     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
421   if ( !aPanel )
422     return;
423
424   // Add geometry objects selected in the module browser to the calculation case
425   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
426   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
427     return;
428
429   QStringList anAddedList;
430   for (int i = 0; i < aSelectedList.length(); i++)
431   {
432     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
433       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
434     if ( anObject.IsNull() )
435       continue;
436
437     if ( myEditedObject->AddGeometryObject( anObject ) )
438       anAddedList.append( anObject->GetName() );
439   }
440
441   if ( !anAddedList.isEmpty() )
442   {
443     aPanel->includeGeomObjects( anAddedList );
444     createPreview();
445   }
446 }
447
448 void HYDROGUI_CalculationOp::onRemoveObjects()
449 {
450   // Remove selected objects from the calculation case
451   HYDROGUI_CalculationDlg* aPanel = 
452     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
453   if ( !aPanel )
454     return;
455
456   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
457   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
458     return;
459
460   for (int i = 0; i < aSelectedList.length(); i++)
461   {
462     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
463       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
464     if ( anObject.IsNull() )
465       continue;
466
467     setObjectVisibility( anObject, false );
468     myEditedObject->RemoveGeometryObject( anObject );
469   }
470
471   module()->update( UF_OCCViewer );
472   aPanel->excludeGeomObjects( aSelectedList );
473 }
474
475 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
476 {
477   // Check if the case is already modified or not
478   bool isConfirmed = myEditedObject->IsMustBeUpdated();
479   if ( !isConfirmed )
480   {
481     // If not modified check if the case has already defined regions with zones
482     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
483     if ( aSeq.Length() > 0 )
484     {
485       // If there are already defined zones then ask a user to confirm zones recalculation
486       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
487                                tr( "REGIONS_CHANGED" ),
488                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
489                                QMessageBox::Yes | QMessageBox::No,
490                                QMessageBox::No ) == QMessageBox::Yes );
491     }
492     else
493     {
494       isConfirmed = true; // No regions - no zones - nothing to recalculate
495     }
496   }
497   return isConfirmed;
498 }
499
500 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
501                                            QString& theErrorMsg,
502                                            QStringList& theBrowseObjectsEntries )
503 {
504   HYDROGUI_CalculationDlg* aPanel = 
505     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
506   if ( !aPanel )
507     return false;
508
509   if( !myIsEdit )
510   {
511     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
512     theBrowseObjectsEntries.append( anEntry );
513   }
514
515   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
516
517   return true;
518 }
519
520 void HYDROGUI_CalculationOp::onApply()
521 {
522   QApplication::setOverrideCursor( Qt::WaitCursor );
523
524   int anUpdateFlags = 0;
525   QString anErrorMsg;
526   QStringList aBrowseObjectsEntries;
527
528   bool aResult = false;
529   
530   try
531   {
532     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
533   }
534   catch ( Standard_Failure )
535   {
536     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
537     anErrorMsg = aFailure->GetMessageString();
538     aResult = false;
539   }
540   catch ( ... )
541   {
542     aResult = false;
543   }
544   
545   QApplication::restoreOverrideCursor();
546
547   if ( aResult )
548   {
549     module()->update( anUpdateFlags );
550     commit();
551     browseObjects( aBrowseObjectsEntries );
552   }
553   else
554   {
555     abort();
556     QString aMsg = tr( "INPUT_VALID_DATA" );
557     if( !anErrorMsg.isEmpty() )
558       aMsg.prepend( anErrorMsg + "\n" );
559     SUIT_MessageBox::critical( module()->getApp()->desktop(),
560                                tr( "INSUFFICIENT_INPUT_DATA" ),
561                                aMsg ); 
562   }
563 }
564
565 void HYDROGUI_CalculationOp::onNext( const int theIndex )
566 {
567   if( theIndex==1 )
568   {
569     setAvailableGroups();
570   }
571   else if( theIndex==2 )
572   {
573     HYDROGUI_CalculationDlg* aPanel = 
574       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
575     if ( !aPanel )
576       return;
577
578     QApplication::setOverrideCursor( Qt::WaitCursor );
579   
580     QString aNewCaseName = aPanel->getObjectName();
581     QString anOldCaseName = myEditedObject->GetName();
582   
583     bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
584   
585     // At first we must to update the case name because of 
586     // automatic names generation for regions and zones
587     myEditedObject->SetName( aNewCaseName );
588   
589     if ( myEditedObject->IsMustBeUpdated() )
590     {
591       myShowZones = true;
592       myEditedObject->Update();
593
594       //aPanel->setEditedObject( myEditedObject );
595       aPanel->refreshZonesBrowser();
596
597       createPreview();
598     }
599     else
600     {
601       setZonesVisible( true );
602     }
603
604     if ( anIsToUpdateOb )
605       module()->getApp()->updateObjectBrowser( false );
606
607     QApplication::restoreOverrideCursor();
608   }
609 }
610
611 void HYDROGUI_CalculationOp::onHideZones()
612 {
613   setZonesVisible( false );
614 }
615
616 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
617 {
618   myShowZones = theIsVisible;
619   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
620   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
621   HYDROData_SequenceOfObjects aZones;
622   Handle(HYDROData_Region) aRegion;
623   if ( myPreviewViewManager ) 
624   {
625     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
626     {
627       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
628       if ( !aCtx.IsNull() )
629       {
630         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
631         {
632           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
633           if ( !aRegion.IsNull() )
634           {
635             aZones = aRegion->GetZones();
636             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
637             for ( ; aZonesIter.More(); aZonesIter.Next() )
638             {
639               setObjectVisibility( aZonesIter.Value(), theIsVisible );
640             }
641           }
642         }
643       }
644
645       module()->update( UF_OCCViewer );
646     }
647   }
648 }
649
650 void HYDROGUI_CalculationOp::createPreview()
651 {
652   LightApp_Application* anApp = module()->getApp();
653   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
654   Handle(HYDROData_Entity) anEntity;
655
656   if ( myShowZones )
657   {
658     // Gather zones for displaying
659     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
660     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
661     HYDROData_SequenceOfObjects aZones;
662     Handle(HYDROData_Region) aRegion;
663     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
664     {
665       anEntity = aRegionsIter.Value();
666       if ( !anEntity.IsNull() )
667       {
668         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
669         if ( !aRegion.IsNull() )
670         {
671           aZones = aRegion->GetZones();
672           aSeq.Append( aZones );
673         }
674       }
675     }
676   }
677
678   // Get a boundary polyline if any
679   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
680
681   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
682
683   if ( !myActiveViewManager )
684   {
685     if ( aSeq.IsEmpty() )
686       return;
687
688     myActiveViewManager = anApp->activeViewManager();
689   }
690
691   if ( !myPreviewViewManager )
692   {
693     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
694       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
695     if ( myPreviewViewManager )
696     {
697       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
698                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
699
700       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
701       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
702     }
703   }
704
705   if ( !myPreviewViewManager )
706     return;
707
708   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
709   {
710     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
711     if ( !aCtx.IsNull() )
712     {
713       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
714       for ( ; anIter.More(); anIter.Next() )
715       {
716         setObjectVisibility( anIter.Value(), true );
717       }
718
719       //Process the draw events for viewer
720       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
721       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
722         vw->onTopView();
723     }
724
725     module()->update( UF_OCCViewer | UF_FitAll );
726   }
727 }
728
729 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
730 {
731   if ( theEntity.IsNull() || !myPreviewViewManager ) {
732     return;
733   }
734
735   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
736   if ( aViewer ) {
737     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
738   }
739 }
740
741 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
742 {
743   closePreview();
744 }
745
746 void HYDROGUI_CalculationOp::closePreview()
747 {
748   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
749   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
750   QShortcut* aShortcut;
751   foreach( aShortcut, aShortcuts )
752   {
753     if ( aShortcut->key() == 
754       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
755       SUIT_DataBrowser::RenameShortcut ) ) )
756     {
757       aShortcut->setEnabled( true );
758     }
759   }
760
761
762   if( myPreviewViewManager )
763   {
764     // Hide all the displayed objects in the preview view
765     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
766     if ( aViewer ) {
767       size_t aViewId = (size_t)aViewer;
768       HYDROData_Iterator anIterator( doc() );
769       for( ; anIterator.More(); anIterator.Next() ) {
770         Handle(HYDROData_Entity) anObject = anIterator.Current();
771         if( !anObject.IsNull() ) {
772           module()->setObjectVisible( aViewId, anObject, false );
773         }
774       }
775     }
776
777     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
778                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
779
780     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
781     myPreviewViewManager = NULL;
782   }
783
784   if( myActiveViewManager )
785   {
786     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
787     myActiveViewManager = NULL;
788   }
789 }
790
791 void HYDROGUI_CalculationOp::setAvailableGroups()
792 {
793   HYDROGUI_CalculationDlg* aPanel = 
794       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
795
796   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
797   QStringList aList, anEntryList;
798   getNamesAndEntries( aSeq, aList, anEntryList );
799
800   QStringList aGroupsNames;
801
802   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
803   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
804   {
805     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
806     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
807     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
808     {
809       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
810       aGroupsNames.append( aGroup->GetName() );
811     }
812   }
813   if( myEditedObject->IsMustBeUpdated() ) {
814     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
815       Handle(HYDROData_ShapesGroup) aGeomGroup =
816         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
817       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
818         myEditedObject->RemoveGeometryGroup( aGeomGroup );
819       }
820     }
821   }
822
823   aPanel->setAvailableGroups( aGroupsNames );
824   aPanel->includeGroups( aList );
825
826   bool isUpdated = myEditedObject->IsMustBeUpdated();
827 }
828
829 void HYDROGUI_CalculationOp::onAddGroups()
830 {
831   HYDROGUI_CalculationDlg* aPanel = 
832     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
833   if ( !aPanel )
834     return;
835
836   // Add geometry objects selected in the module browser to the calculation case
837   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
838   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
839     return;
840
841   QStringList anAddedList;
842   for (int i = 0; i < aSelectedList.length(); i++)
843   {
844     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
845       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
846     if ( aGroup.IsNull() )
847       continue;
848
849     if ( myEditedObject->AddGeometryGroup( aGroup ) )
850       anAddedList.append( aGroup->GetName() );
851   }
852
853   if ( !anAddedList.isEmpty() )
854   {
855     aPanel->includeGroups( anAddedList );
856   }
857 }
858
859 void HYDROGUI_CalculationOp::onRemoveGroups()
860 {
861   // Remove selected objects from the calculation case
862   HYDROGUI_CalculationDlg* aPanel = 
863     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
864   if ( !aPanel )
865     return;
866
867   QStringList aSelectedList = aPanel->getSelectedGroups();
868   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
869     return;
870
871   for (int i = 0; i < aSelectedList.length(); i++)
872   {
873     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
874       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
875     if ( aGroup.IsNull() )
876       continue;
877
878     myEditedObject->RemoveGeometryGroup( aGroup );
879   }
880
881   aPanel->excludeGroups( aSelectedList );
882 }