Salome HOME
bug #243: GUI for groups
[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( onNext( const int ) ) );
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( objectsSelected() ), 
218            SLOT( onObjectsSelected() ) );
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::onObjectsSelected()
265 {
266   HYDROGUI_CalculationDlg* aPanel = 
267     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
268
269   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
270   QMap<QString, bool> aSelectedObjsMap;
271   foreach( QString aName, aSelectedObjs )
272     aSelectedObjsMap[aName] = true;
273
274
275   // Select the appropriate geometry object shape in the viewer
276   selectionMgr()->clearSelected();
277
278   // Unhighlight all objects except selected
279   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
280   Handle(HYDROData_Entity) anEntity;
281   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
282   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
283   bool isSelected;
284   QString aName;
285   for ( ; anIter.More(); anIter.Next() )
286   {
287     anEntity = anIter.Value();
288     if ( !anEntity.IsNull() )
289     {
290       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
291       if ( aShape )
292       {
293         aName = anEntity->GetName();
294         isSelected = aSelectedObjsMap.contains( aName );
295         aShape->highlight( isSelected, false );
296         aShape->update( false );
297         aLastShape = aShape;
298       }
299     }
300   }
301   if( aLastShape )
302     aLastShape->update( true );
303 }
304
305 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
306 {
307   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
308   HYDROGUI_Zone* aZoneItem;
309   selectionMgr()->clearSelected();
310   if ( aRegionItem )
311   {
312     // Select a region in preview
313     SUIT_DataOwnerPtrList aList( true );
314     DataObjectList aZones = aRegionItem->children();
315     for ( int i = 0; i < aZones.length(); i++ )
316     {
317       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
318       if ( aZoneItem )
319       {
320         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
321       }
322     }
323     selectionMgr()->setSelected( aList );
324   }
325   else
326   {
327     // select a single zone
328     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
329     if ( aZoneItem )
330     {
331       SUIT_DataOwnerPtrList aList( true );
332       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
333       selectionMgr()->setSelected( aList );
334     }
335   }
336 }
337
338 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
339 {
340   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
341   if ( aRegion )
342   {
343     QList<HYDROGUI_Zone*> aZonesList;
344     HYDROGUI_Zone* aZone;
345     // Get a list of dropped zones
346     for ( int i = 0; i < theZonesList.length(); i++ )
347     {
348       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
349       if ( aZone )
350       {
351         aZonesList.append( aZone );
352       }
353     }
354     if ( aZonesList.length() > 0 )
355     {
356       aRegion->addZones( aZonesList );
357       HYDROGUI_CalculationDlg* aPanel = 
358         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
359       if ( aPanel )
360       {
361         aPanel->refreshZonesBrowser();
362       }
363       createPreview();
364     }
365   }
366 }
367
368 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
369 {
370   QList<HYDROGUI_Zone*> aZonesList;
371   HYDROGUI_Zone* aZone;
372   // Get a list of dropped zones
373   for ( int i = 0; i < theZonesList.length(); i++ )
374   {
375     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
376     if ( aZone )
377     {
378       aZonesList.append( aZone );
379     }
380   }
381   if ( aZonesList.length() > 0 )
382   {
383     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
384     HYDROGUI_CalculationDlg* aPanel = 
385       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
386     if ( aPanel )
387     {
388       aPanel->refreshZonesBrowser();
389     }
390     createPreview();
391   }
392 }
393
394 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathymetryName )
395 {
396   HYDROGUI_CalculationDlg* aPanel = 
397     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
398   if ( aPanel )
399   {
400     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
401     if ( aZone )
402     {
403       aZone->setMergeType( theMergeType, theBathymetryName );
404       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
405       if ( aShape )
406       {
407         aShape->update();
408       }
409     }
410     aPanel->refreshZonesBrowser();
411   }
412 }
413
414 void HYDROGUI_CalculationOp::onAddObjects()
415 {
416   HYDROGUI_CalculationDlg* aPanel = 
417     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
418   if ( !aPanel )
419     return;
420
421   // Add geometry objects selected in the module browser to the calculation case
422   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
423   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
424     return;
425
426   QStringList anAddedList;
427   for (int i = 0; i < aSelectedList.length(); i++)
428   {
429     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
430       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
431     if ( anObject.IsNull() )
432       continue;
433
434     if ( myEditedObject->AddGeometryObject( anObject ) )
435       anAddedList.append( anObject->GetName() );
436   }
437
438   if ( !anAddedList.isEmpty() )
439   {
440     aPanel->includeGeomObjects( anAddedList );
441     createPreview();
442   }
443 }
444
445 void HYDROGUI_CalculationOp::onRemoveObjects()
446 {
447   // Remove selected objects from the calculation case
448   HYDROGUI_CalculationDlg* aPanel = 
449     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
450   if ( !aPanel )
451     return;
452
453   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
454   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
455     return;
456
457   for (int i = 0; i < aSelectedList.length(); i++)
458   {
459     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
460       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
461     if ( anObject.IsNull() )
462       continue;
463
464     module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
465     myEditedObject->RemoveGeometryObject( anObject );
466   }
467
468   aPanel->excludeGeomObjects( aSelectedList );
469 }
470
471 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
472 {
473   // Check if the case is already modified or not
474   bool isConfirmed = myEditedObject->IsMustBeUpdated();
475   if ( !isConfirmed )
476   {
477     // If not modified check if the case has already defined regions with zones
478     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
479     if ( aSeq.Length() > 0 )
480     {
481       // If there are already defined zones then ask a user to confirm zones recalculation
482       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
483                                tr( "REGIONS_CHANGED" ),
484                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
485                                QMessageBox::Yes | QMessageBox::No,
486                                QMessageBox::No ) == QMessageBox::Yes );
487     }
488     else
489     {
490       isConfirmed = true; // No regions - no zones - nothing to recalculate
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 | UF_OCCViewer | UF_OCC_Forced;
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::onNext( const int theIndex )
553 {
554   if( theIndex==1 )
555   {
556     setAvailableGroups();
557   }
558   else if( theIndex==2 )
559   {
560     HYDROGUI_CalculationDlg* aPanel = 
561       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
562     if ( !aPanel )
563       return;
564
565     QApplication::setOverrideCursor( Qt::WaitCursor );
566   
567     QString aNewCaseName = aPanel->getObjectName();
568     QString anOldCaseName = myEditedObject->GetName();
569   
570     bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
571   
572     // At first we must to update the case name because of 
573     // automatic names generation for regions and zones
574     myEditedObject->SetName( aNewCaseName );
575   
576     if ( myEditedObject->IsMustBeUpdated() )
577     {
578       myShowZones = true;
579       myEditedObject->Update();
580
581       //aPanel->setEditedObject( myEditedObject );
582       aPanel->refreshZonesBrowser();
583
584       createPreview();
585     }
586     else
587     {
588       setZonesVisible( true );
589     }
590
591     if ( anIsToUpdateOb )
592       module()->getApp()->updateObjectBrowser( false );
593
594     QApplication::restoreOverrideCursor();
595   }
596 }
597
598 void HYDROGUI_CalculationOp::onHideZones()
599 {
600   setZonesVisible( false );
601 }
602
603 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
604 {
605   myShowZones = theIsVisible;
606   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
607   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
608   HYDROData_SequenceOfObjects aZones;
609   Handle(HYDROData_Region) aRegion;
610   if ( myPreviewViewManager ) 
611   {
612     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
613     {
614       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
615       if ( !aCtx.IsNull() )
616       {
617         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
618         {
619           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
620           if ( !aRegion.IsNull() )
621           {
622             aZones = aRegion->GetZones();
623             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
624             for ( ; aZonesIter.More(); aZonesIter.Next() )
625             {
626               if ( theIsVisible )
627               {
628                 showObject( aZonesIter.Value(), aCtx );
629               }
630               else
631               {
632                 module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
633               }
634             }
635           }
636         }
637       }
638
639       aViewer->update();
640     }
641   }
642 }
643
644 void HYDROGUI_CalculationOp::createPreview()
645 {
646   LightApp_Application* anApp = module()->getApp();
647   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
648   Handle(HYDROData_Entity) anEntity;
649
650   if ( myShowZones )
651   {
652     // Gather zones for displaying
653     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
654     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
655     HYDROData_SequenceOfObjects aZones;
656     Handle(HYDROData_Region) aRegion;
657     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
658     {
659       anEntity = aRegionsIter.Value();
660       if ( !anEntity.IsNull() )
661       {
662         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
663         if ( !aRegion.IsNull() )
664         {
665           aZones = aRegion->GetZones();
666           aSeq.Append( aZones );
667         }
668       }
669     }
670   }
671
672   // Get a boundary polyline if any
673   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
674
675   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
676
677   if ( !myActiveViewManager )
678   {
679     if ( aSeq.IsEmpty() )
680       return;
681
682     myActiveViewManager = anApp->activeViewManager();
683   }
684
685   if ( !myPreviewViewManager )
686   {
687     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
688       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
689     if ( myPreviewViewManager )
690     {
691       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
692                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
693
694       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
695       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
696     }
697   }
698
699   if ( !myPreviewViewManager )
700     return;
701
702   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
703   {
704     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
705     if ( !aCtx.IsNull() )
706     {
707       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
708       for ( ; anIter.More(); anIter.Next() )
709       {
710         showObject( anIter.Value(), aCtx );
711       }
712
713       //Process the draw events for viewer
714       QApplication::processEvents();
715       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
716         vw->onTopView();
717     }
718
719     aViewer->update();
720   }
721 }
722
723 void HYDROGUI_CalculationOp::showObject( Handle(HYDROData_Entity) theEntity, Handle(AIS_InteractiveContext) theCtx )
724 {
725   if ( !theEntity.IsNull() )
726   {
727     HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity );
728     if ( !aShape )
729     {
730       aShape = new HYDROGUI_Shape( theCtx, theEntity );
731       module()->setObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, theEntity, aShape );
732     }
733     aShape->update( false );
734   }
735 }
736
737 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
738 {
739   closePreview();
740 }
741
742 void HYDROGUI_CalculationOp::closePreview()
743 {
744   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
745   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
746   QShortcut* aShortcut;
747   foreach( aShortcut, aShortcuts )
748   {
749     if ( aShortcut->key() == 
750       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
751       SUIT_DataBrowser::RenameShortcut ) ) )
752     {
753       aShortcut->setEnabled( true );
754     }
755   }
756
757
758   if( myPreviewViewManager )
759   {
760     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
761                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
762
763     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
764     myPreviewViewManager = NULL;
765   }
766
767   if( myActiveViewManager )
768   {
769     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
770     myActiveViewManager = NULL;
771   }
772 }
773
774 void HYDROGUI_CalculationOp::setAvailableGroups()
775 {
776   HYDROGUI_CalculationDlg* aPanel = 
777     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
778
779   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
780   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
781   {
782     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
783     //TODO: anObj->GetGroups();
784   }
785 }