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