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