]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_CalculationOp.cxx
Salome HOME
Fix for the bug #49: Message box on objects list change is absent
[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   }
493   return isConfirmed;
494 }
495
496 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
497                                            QString& theErrorMsg )
498 {
499   HYDROGUI_CalculationDlg* aPanel = 
500     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
501   if ( !aPanel )
502     return false;
503
504   theUpdateFlags = UF_Model;
505
506   return true;
507 }
508
509 void HYDROGUI_CalculationOp::onApply()
510 {
511   QApplication::setOverrideCursor( Qt::WaitCursor );
512
513   int anUpdateFlags = 0;
514   QString anErrorMsg;
515
516   bool aResult = false;
517   
518   try
519   {
520     aResult = processApply( anUpdateFlags, anErrorMsg );
521   }
522   catch ( Standard_Failure )
523   {
524     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
525     anErrorMsg = aFailure->GetMessageString();
526     aResult = false;
527   }
528   catch ( ... )
529   {
530     aResult = false;
531   }
532   
533   QApplication::restoreOverrideCursor();
534
535   if ( aResult )
536   {
537     module()->update( anUpdateFlags );
538     commit();
539   }
540   else
541   {
542     abort();
543     QString aMsg = tr( "INPUT_VALID_DATA" );
544     if( !anErrorMsg.isEmpty() )
545       aMsg.prepend( anErrorMsg + "\n" );
546     SUIT_MessageBox::critical( module()->getApp()->desktop(),
547                                tr( "INSUFFICIENT_INPUT_DATA" ),
548                                aMsg ); 
549   }
550 }
551
552 void HYDROGUI_CalculationOp::onSplitZones()
553 {
554   QApplication::setOverrideCursor( Qt::WaitCursor );
555
556   if ( myEditedObject->IsMustBeUpdated() )
557   {
558     myShowZones = true;
559     myEditedObject->SplitGeometryObjects();
560
561     HYDROGUI_CalculationDlg* aPanel = 
562       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
563     if ( aPanel )
564     {
565       aPanel->setEditedObject( myEditedObject );
566     }
567
568     createPreview();
569   }
570   else
571   {
572     setZonesVisible( true );
573   }
574
575   QApplication::restoreOverrideCursor();
576 }
577
578 void HYDROGUI_CalculationOp::onHideZones()
579 {
580   setZonesVisible( false );
581 }
582
583 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
584 {
585   myShowZones = theIsVisible;
586   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
587   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
588   HYDROData_SequenceOfObjects aZones;
589   Handle(HYDROData_Region) aRegion;
590   if ( myPreviewViewManager ) 
591   {
592     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
593     {
594       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
595       if ( !aCtx.IsNull() )
596       {
597         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
598         {
599           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
600           if ( !aRegion.IsNull() )
601           {
602             aZones = aRegion->GetZones();
603             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
604             for ( ; aZonesIter.More(); aZonesIter.Next() )
605             {
606               if ( theIsVisible )
607               {
608                 showObject( aZonesIter.Value(), aCtx );
609               }
610               else
611               {
612                 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
613               }
614             }
615           }
616         }
617       }
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 }
698
699 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
700 {
701   if ( !theEntity.IsNull() )
702   {
703     HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
704     if ( !aShape )
705     {
706       aShape = new HYDROGUI_Shape( theCtx, theEntity );
707       module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
708     }
709     aShape->update();
710   }
711 }
712
713 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
714 {
715   closePreview();
716 }
717
718 void HYDROGUI_CalculationOp::closePreview()
719 {
720   if( myPreviewViewManager )
721   {
722     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
723                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
724
725     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
726     myPreviewViewManager = NULL;
727   }
728
729   if( myActiveViewManager )
730   {
731     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
732     myActiveViewManager = NULL;
733   }
734 }
735
736