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