Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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_Polyline.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_ImmersibleZone.h>
36 #include <HYDROData_Object.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42 #include <LightApp_Application.h>
43 #include <LightApp_UpdateFlags.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_DataOwner.h>
46
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
49
50 #include <QApplication>
51
52 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
53 : HYDROGUI_Operation( theModule ),
54   myIsEdit( theIsEdit ),
55   myActiveViewManager( NULL ),
56   myPreviewViewManager( NULL )
57 {
58   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
59 }
60
61 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
62 {
63   closePreview();
64 }
65
66 void HYDROGUI_CalculationOp::startOperation()
67 {
68   HYDROGUI_Operation::startOperation();
69   
70   // Begin transaction
71   startDocOperation();
72
73   HYDROGUI_CalculationDlg* aPanel = 
74     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
75   if ( !aPanel )
76     return;
77
78   aPanel->reset();
79   QStringList aList;
80   QStringList anEntryList;
81   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
82   getNamesAndEntries( aSeq, aList, anEntryList );
83   aPanel->setAllGeomObjects( aList, anEntryList );
84
85   // Get all polylines
86   aList.clear();
87   anEntryList.clear();
88   HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
89   Handle(HYDROData_Polyline) aPolylineObj;
90   QString aPolylineName;
91   for ( ; anIter.More(); anIter.Next() )
92   {
93     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
94
95     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
96     { 
97       // Check the polyline shape
98       TopoDS_Shape aPolylineShape = aPolylineObj->GetTopShape();
99       if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
100         aPolylineName = aPolylineObj->GetName();
101         if ( !aPolylineName.isEmpty() )
102         {
103           aList.append( aPolylineName );
104           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
105         }
106       }
107     }
108   }
109   aPanel->setPolylineNames( aList, anEntryList );
110
111   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
112
113   myEditedObject.Nullify();
114   if ( myIsEdit )
115   {
116     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
117     if ( !myEditedObject.IsNull() )
118     {
119       anObjectName = myEditedObject->GetName();
120       aPolylineObj = myEditedObject->GetBoundaryPolyline();
121       if ( aPolylineObj.IsNull() )
122       {
123         aPanel->setBoundary( QString() );
124       }
125       else
126       {
127         aPolylineName = aPolylineObj->GetName();
128         aPanel->setBoundary( aPolylineName );
129       }
130       aSeq = myEditedObject->GetGeometryObjects();
131       getNamesAndEntries( aSeq, aList, anEntryList );
132       aPanel->includeGeomObjects( aList );
133     }
134   }
135   else
136   {
137     myEditedObject =
138       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
139     myEditedObject->SetName(anObjectName);
140   }
141
142   aPanel->setObjectName( anObjectName );
143   aPanel->setEditedObject( myEditedObject );
144
145   createPreview();
146 }
147
148 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
149                                                 QStringList& theNames, QStringList& theEntries ) const
150 {
151   Handle(HYDROData_Object) anObject;
152   Handle(HYDROData_Entity) anEntity;
153   theNames.clear();
154   theEntries.clear();
155   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
156   for ( ; anIter.More(); anIter.Next() )
157   {
158     anEntity = anIter.Value();
159     if ( !anEntity.IsNull() )
160     {
161       // Temporary solution will be revised later
162       //anObject = Handle(HYDROData_Object)::DownCast( anEntity );
163       anObject = Handle(HYDROData_ImmersibleZone)::DownCast( anEntity );
164       if ( !anObject.IsNull() )
165       {
166         theNames.append( anObject->GetName() );
167         theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anObject ) );
168       }
169     }
170   }
171 }
172
173 void HYDROGUI_CalculationOp::abortOperation()
174 {
175   closePreview();
176   // Abort transaction
177   abortDocOperation();
178   HYDROGUI_Operation::abortOperation();
179 }
180
181 void HYDROGUI_CalculationOp::commitOperation()
182 {
183   closePreview();
184   // Commit transaction
185   commitDocOperation();
186   HYDROGUI_Operation::commitOperation();
187 }
188
189 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
190 {
191   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
192
193   // Connect signals and slots
194   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
195   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
196   connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
197   connect( aPanel, SIGNAL( hideZones() ), SLOT( onHideZones() ) );
198   connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
199   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
200   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
201     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
202   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
203     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
204   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
205     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
206   connect( aPanel, SIGNAL( objectSelected( const QString & ) ), 
207     SLOT( onObjectSelected( const QString & ) ) );
208   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
209     SLOT( onBoundarySelected( const QString & ) ) );
210
211   return aPanel;
212 }
213
214 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
215 {
216   // Set the selected boundary polyline to the calculation case
217   Handle(HYDROData_Polyline) anObject;
218   Handle(AIS_InteractiveContext) aCtx;
219   if ( myPreviewViewManager ) 
220   {
221     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
222     {
223       aCtx = aViewer->getAISContext();
224     }
225   }
226   // Remove the old boundary from the operation viewer
227   anObject = myEditedObject->GetBoundaryPolyline();
228   if ( !anObject.IsNull() )
229   {
230     module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
231   }
232
233   if ( theObjName.trimmed().isEmpty() )
234   {
235     // No polyline is selected
236     myEditedObject->RemoveBoundaryPolyline();
237   }
238   else
239   {
240     Handle(HYDROData_Entity) anEntity = 
241       HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINE );
242     if ( !anEntity.IsNull() )
243     {
244       anObject = Handle(HYDROData_Polyline)::DownCast( anEntity );
245       if ( !anObject.IsNull() )
246       {
247         myEditedObject->SetBoundaryPolyline( anObject );
248         if ( !aCtx.IsNull() )
249         {
250           showObject( anEntity, aCtx );
251         }
252       }
253     }
254   }
255 }
256
257 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
258 {
259   // Select the appropriate geometry object shape in the viewer
260   selectionMgr()->clearSelected();
261
262   // Unhighlight all objects except selected
263   HYDROGUI_Shape* aShape;
264   HYDROGUI_Shape* aSelectedShape = 0;
265   Handle(HYDROData_Entity) anEntity;
266   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
267   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
268   bool isSelected;
269   QString aName;
270   for ( ; anIter.More(); anIter.Next() )
271   {
272     anEntity = anIter.Value();
273     if ( !anEntity.IsNull() )
274     {
275       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
276       if ( aShape )
277       {
278         aName = anEntity->GetName();
279         isSelected = ( aName == theObjName );
280         if ( isSelected )
281         {
282           aSelectedShape = aShape;
283         }
284         if ( aShape->isHighlighted() != isSelected )
285         {
286           if ( !isSelected )
287           {
288             aShape->highlight( isSelected );
289             aShape->update();
290           }
291         }
292       }
293     }
294   }
295   if ( aSelectedShape )
296   {
297     aSelectedShape->highlight( true );
298     aSelectedShape->update();
299   }
300 }
301
302 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
303 {
304   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
305   HYDROGUI_Zone* aZoneItem;
306   selectionMgr()->clearSelected();
307   if ( aRegionItem )
308   {
309     // Select a region in preview
310     SUIT_DataOwnerPtrList aList( true );
311     DataObjectList aZones = aRegionItem->children();
312     for ( int i = 0; i < aZones.length(); i++ )
313     {
314       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
315       if ( aZoneItem )
316       {
317         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
318       }
319     }
320     selectionMgr()->setSelected( aList );
321   }
322   else
323   {
324     // select a single zone
325     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
326     if ( aZoneItem )
327     {
328       SUIT_DataOwnerPtrList aList( true );
329       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
330       selectionMgr()->setSelected( aList );
331     }
332   }
333 }
334
335 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
336 {
337   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
338   if ( aRegion )
339   {
340     QList<HYDROGUI_Zone*> aZonesList;
341     HYDROGUI_Zone* aZone;
342     // Get a list of dropped zones
343     for ( int i = 0; i < theZonesList.length(); i++ )
344     {
345       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
346       if ( aZone )
347       {
348         aZonesList.append( aZone );
349       }
350     }
351     if ( aZonesList.length() > 0 )
352     {
353       aRegion->addZones( aZonesList );
354       HYDROGUI_CalculationDlg* aPanel = 
355         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
356       if ( aPanel )
357       {
358         aPanel->setEditedObject(myEditedObject);
359       }
360       createPreview();
361     }
362   }
363 }
364
365 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
366 {
367   QList<HYDROGUI_Zone*> aZonesList;
368   HYDROGUI_Zone* aZone;
369   // Get a list of dropped zones
370   for ( int i = 0; i < theZonesList.length(); i++ )
371   {
372     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
373     if ( aZone )
374     {
375       aZonesList.append( aZone );
376     }
377   }
378   if ( aZonesList.length() > 0 )
379   {
380     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
381     HYDROGUI_CalculationDlg* aPanel = 
382       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
383     if ( aPanel )
384     {
385       aPanel->setEditedObject(myEditedObject);
386     }
387     createPreview();
388   }
389 }
390
391 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
392 {
393   HYDROGUI_CalculationDlg* aPanel = 
394     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
395   if ( aPanel )
396   {
397     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
398     if ( aZone )
399     {
400       aZone->setMergeType( theMergeType, theBathymetryName );
401       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
402       if ( aShape )
403       {
404         aShape->update();
405       }
406     }
407     aPanel->refreshZonesBrowser();
408   }
409 }
410
411 void HYDROGUI_CalculationOp::onAddObjects()
412 {
413   HYDROGUI_CalculationDlg* aPanel = 
414     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
415   if ( aPanel )
416   {
417     // Add geometry objects selected in the module browser to the calculation case
418     QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
419     if ( ( !aSelectedList.isEmpty() ) && ( confirmRegionsChange() ) )
420     {
421       Handle(HYDROData_Object) anObject;
422       Handle(HYDROData_Entity) anEntity;
423       QStringList aList;
424       for (int i = 0; i < aSelectedList.length(); i++)
425       {
426         anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
427         if ( !anEntity.IsNull() )
428         {
429           anObject = Handle(HYDROData_Object)::DownCast( anEntity );
430           if ( !anObject.IsNull() )
431           {
432             if (myEditedObject->AddGeometryObject( anObject ))
433             {
434               aList.append( anObject->GetName() );
435             }
436           }
437         }
438       }
439       if ( !aList.isEmpty() )
440       {
441         aPanel->includeGeomObjects( aList );
442         createPreview();
443       }
444     }
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   {
455     QStringList aList = aPanel->getSelectedGeomObjects();
456     if ( ( !aList.isEmpty() ) && ( confirmRegionsChange() ) )
457     {
458       Handle(HYDROData_Object) anObject;
459       Handle(HYDROData_Entity) anEntity;
460       for (int i = 0; i < aList.length(); i++)
461       {
462         anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
463         if ( !anEntity.IsNull() )
464         {
465           anObject = Handle(HYDROData_Object)::DownCast( anEntity );
466           if ( !anObject.IsNull() )
467           {
468             module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
469             myEditedObject->RemoveGeometryObject( anObject );
470           }
471         }
472       }
473       if ( !aList.isEmpty() )
474       {
475         aPanel->excludeGeomObjects( aList );
476       }
477     }
478   }
479 }
480
481 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
482 {
483   // Check if the case is already modified or not
484   bool isConfirmed = myEditedObject->IsMustBeUpdated();
485   if ( !isConfirmed )
486   {
487     // If not modified check if the case has already defined regions with zones
488     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
489     if ( aSeq.Length() > 0 )
490     {
491       // If there are already defined zones then ask a user to confirm zones recalculation
492       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
493                                tr( "REGIONS_CHANGED" ),
494                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
495                                QMessageBox::Yes | QMessageBox::No,
496                                QMessageBox::No ) == QMessageBox::Yes );
497     }
498     else
499     {
500       isConfirmed = true; // No regions - no zones - nothing to recalculate
501     }
502   }
503   return isConfirmed;
504 }
505
506 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
507                                            QString& theErrorMsg )
508 {
509   HYDROGUI_CalculationDlg* aPanel = 
510     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
511   if ( !aPanel )
512     return false;
513
514   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
515
516   return true;
517 }
518
519 void HYDROGUI_CalculationOp::onApply()
520 {
521   QApplication::setOverrideCursor( Qt::WaitCursor );
522
523   int anUpdateFlags = 0;
524   QString anErrorMsg;
525
526   bool aResult = false;
527   
528   try
529   {
530     aResult = processApply( anUpdateFlags, anErrorMsg );
531   }
532   catch ( Standard_Failure )
533   {
534     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
535     anErrorMsg = aFailure->GetMessageString();
536     aResult = false;
537   }
538   catch ( ... )
539   {
540     aResult = false;
541   }
542   
543   QApplication::restoreOverrideCursor();
544
545   if ( aResult )
546   {
547     module()->update( anUpdateFlags );
548     commit();
549   }
550   else
551   {
552     abort();
553     QString aMsg = tr( "INPUT_VALID_DATA" );
554     if( !anErrorMsg.isEmpty() )
555       aMsg.prepend( anErrorMsg + "\n" );
556     SUIT_MessageBox::critical( module()->getApp()->desktop(),
557                                tr( "INSUFFICIENT_INPUT_DATA" ),
558                                aMsg ); 
559   }
560 }
561
562 void HYDROGUI_CalculationOp::onSplitZones()
563 {
564   HYDROGUI_CalculationDlg* aPanel = 
565     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
566   if ( !aPanel )
567     return;
568
569   QApplication::setOverrideCursor( Qt::WaitCursor );
570
571   QString aNewCaseName = aPanel->getObjectName();
572   QString anOldCaseName = myEditedObject->GetName();
573
574   bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
575   
576   // At first we must to update the case name because of 
577   // automatic names generation for regions and zones
578   myEditedObject->SetName( aNewCaseName );
579   
580   if ( myEditedObject->IsMustBeUpdated() )
581   {
582     myShowZones = true;
583     myEditedObject->SplitGeometryObjects();
584
585     aPanel->setEditedObject( myEditedObject );
586
587     createPreview();
588   }
589   else
590   {
591     setZonesVisible( true );
592   }
593
594   if ( anIsToUpdateOb )
595     module()->getApp()->updateObjectBrowser( false );
596
597   QApplication::restoreOverrideCursor();
598 }
599
600 void HYDROGUI_CalculationOp::onHideZones()
601 {
602   setZonesVisible( false );
603 }
604
605 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
606 {
607   myShowZones = theIsVisible;
608   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
609   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
610   HYDROData_SequenceOfObjects aZones;
611   Handle(HYDROData_Region) aRegion;
612   if ( myPreviewViewManager ) 
613   {
614     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
615     {
616       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
617       if ( !aCtx.IsNull() )
618       {
619         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
620         {
621           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
622           if ( !aRegion.IsNull() )
623           {
624             aZones = aRegion->GetZones();
625             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
626             for ( ; aZonesIter.More(); aZonesIter.Next() )
627             {
628               if ( theIsVisible )
629               {
630                 showObject( aZonesIter.Value(), aCtx );
631               }
632               else
633               {
634                 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
635               }
636             }
637           }
638         }
639       }
640     }
641   }
642 }
643
644 void HYDROGUI_CalculationOp::createPreview()
645 {
646   LightApp_Application* anApp = module()->getApp();
647   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
648   Handle(HYDROData_Entity) anEntity;
649
650   if ( myShowZones )
651   {
652     // Gather zones for displaying
653     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
654     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
655     HYDROData_SequenceOfObjects aZones;
656     Handle(HYDROData_Region) aRegion;
657     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
658     {
659       anEntity = aRegionsIter.Value();
660       if ( !anEntity.IsNull() )
661       {
662         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
663         if ( !aRegion.IsNull() )
664         {
665           aZones = aRegion->GetZones();
666           aSeq.Append( aZones );
667         }
668       }
669     }
670   }
671
672   // Get a boundary polyline if any
673   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
674
675   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
676
677   if ( !myActiveViewManager )
678   {
679     if ( aSeq.IsEmpty() )
680       return;
681
682     myActiveViewManager = anApp->activeViewManager();
683   }
684
685   if ( !myPreviewViewManager )
686   {
687     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
688       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
689     if ( myPreviewViewManager )
690     {
691       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
692                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
693
694       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
695       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
696     }
697   }
698
699   if ( !myPreviewViewManager )
700     return;
701
702   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
703   {
704     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
705     if ( !aCtx.IsNull() )
706     {
707       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
708       for ( ; anIter.More(); anIter.Next() )
709       {
710         showObject( anIter.Value(), aCtx );
711       }
712
713       //Process the draw events for viewer
714       QApplication::processEvents();
715       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
716         vw->onTopView();
717     }
718   }
719 }
720
721 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
722 {
723   if ( !theEntity.IsNull() )
724   {
725     HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
726     if ( !aShape )
727     {
728       aShape = new HYDROGUI_Shape( theCtx, theEntity );
729       module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
730     }
731     aShape->update();
732   }
733 }
734
735 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
736 {
737   closePreview();
738 }
739
740 void HYDROGUI_CalculationOp::closePreview()
741 {
742   if( myPreviewViewManager )
743   {
744     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
745                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
746
747     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
748     myPreviewViewManager = NULL;
749   }
750
751   if( myActiveViewManager )
752   {
753     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
754     myActiveViewManager = NULL;
755   }
756 }
757
758