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