Salome HOME
PR: quadtree
[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_ShapesGroup.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Object.h>
37 #include <HYDROData_Tool.h>
38
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42
43 #include <LightApp_Application.h>
44 #include <LightApp_UpdateFlags.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <LightApp_DataOwner.h>
47
48 #include <SUIT_MessageBox.h>
49 #include <SUIT_Desktop.h>
50 #include <SUIT_DataBrowser.h>
51
52 #include <QApplication>
53 #include <QKeySequence>
54 #include <QShortcut>
55
56 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
57 : HYDROGUI_Operation( theModule ),
58   myIsEdit( theIsEdit ),
59   myActiveViewManager( NULL ),
60   myPreviewViewManager( NULL )
61 {
62   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
63 }
64
65 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
66 {
67   closePreview();
68 }
69
70 void HYDROGUI_CalculationOp::startOperation()
71 {
72   HYDROGUI_Operation::startOperation();
73   
74   // Begin transaction
75   startDocOperation();
76
77   HYDROGUI_CalculationDlg* aPanel = 
78     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
79   if ( !aPanel )
80     return;
81
82   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
83   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
84   QShortcut* aShortcut;
85   foreach( aShortcut, aShortcuts )
86   {
87     if ( aShortcut->key() == 
88       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
89       SUIT_DataBrowser::RenameShortcut ) ) )
90     {
91       aShortcut->setEnabled( false );
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
148       aSeq = myEditedObject->GetGeometryObjects();
149       getNamesAndEntries( aSeq, aList, anEntryList );
150       aPanel->includeGeomObjects( aList );
151     }
152   }
153   else
154   {
155     myEditedObject =
156       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
157     myEditedObject->SetName(anObjectName);
158   }
159
160   aPanel->setObjectName( anObjectName );
161   aPanel->setEditedObject( myEditedObject );
162
163   createPreview();
164 }
165
166 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
167                                                 QStringList& theNames, QStringList& theEntries ) const
168 {
169  
170   theNames.clear();
171   theEntries.clear();
172   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
173   for ( ; anIter.More(); anIter.Next() )
174   {
175     Handle(HYDROData_Entity) anEntity = anIter.Value();
176     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
177     //  continue;
178
179     theNames.append( anEntity->GetName() );
180     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
181   }
182 }
183
184 void HYDROGUI_CalculationOp::abortOperation()
185 {
186   closePreview();
187   // Abort transaction
188   abortDocOperation();
189   HYDROGUI_Operation::abortOperation();
190   module()->getApp()->updateObjectBrowser();
191 }
192
193 void HYDROGUI_CalculationOp::commitOperation()
194 {
195   closePreview();
196   // Commit transaction
197   commitDocOperation();
198   HYDROGUI_Operation::commitOperation();
199 }
200
201 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
202 {
203   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
204
205   // Connect signals and slots
206   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
207   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
208   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
209   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
210   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
211   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones() ) );
212   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
213   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
214   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
215     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
216   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
217     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
218   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
219     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
220   connect( aPanel, SIGNAL( objectsSelected() ), 
221            SLOT( onObjectsSelected() ) );
222   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
223     SLOT( onBoundarySelected( const QString & ) ) );
224
225   return aPanel;
226 }
227
228 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
229 {
230   bool anIsToUpdateViewer = false;
231
232   // Remove the old boundary from the operation viewer
233   Handle(HYDROData_PolylineXY) aPrevPolyline = 
234     myEditedObject->GetBoundaryPolyline();
235   if ( !aPrevPolyline.IsNull() )
236   {
237     setObjectVisibility( aPrevPolyline, false );
238     anIsToUpdateViewer = true;
239   }
240
241   // Set the selected boundary polyline to the calculation case
242   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
243     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
244   myEditedObject->SetBoundaryPolyline( aNewPolyline );
245
246   if ( myPreviewViewManager )
247   {
248     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
249     if ( aViewer )
250     {
251       if ( !aNewPolyline.IsNull() )
252       {
253         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
254         if ( !aCtx.IsNull() )
255         {
256           setObjectVisibility( aNewPolyline, true );
257           anIsToUpdateViewer = true;
258         }
259       }
260
261       if ( anIsToUpdateViewer )
262         module()->update( UF_OCCViewer );
263     }
264   }
265 }
266
267 void HYDROGUI_CalculationOp::onObjectsSelected()
268 {
269   HYDROGUI_CalculationDlg* aPanel = 
270     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
271
272   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
273   QMap<QString, bool> aSelectedObjsMap;
274   foreach( QString aName, aSelectedObjs )
275     aSelectedObjsMap[aName] = true;
276
277
278   // Select the appropriate geometry object shape in the viewer
279   selectionMgr()->clearSelected();
280
281   // Unhighlight all objects except selected
282   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
283   Handle(HYDROData_Entity) anEntity;
284   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
285   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
286   bool isSelected;
287   QString aName;
288   for ( ; anIter.More(); anIter.Next() )
289   {
290     anEntity = anIter.Value();
291     if ( !anEntity.IsNull() )
292     {
293       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
294       if ( aShape )
295       {
296         aName = anEntity->GetName();
297         isSelected = aSelectedObjsMap.contains( aName );
298         aShape->highlight( isSelected, false );
299         aShape->update( false );
300         aLastShape = aShape;
301       }
302     }
303   }
304   if( aLastShape )
305     aLastShape->update( true );
306 }
307
308 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
309 {
310   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
311   HYDROGUI_Zone* aZoneItem;
312   selectionMgr()->clearSelected();
313   if ( aRegionItem )
314   {
315     // Select a region in preview
316     SUIT_DataOwnerPtrList aList( true );
317     DataObjectList aZones = aRegionItem->children();
318     for ( int i = 0; i < aZones.length(); i++ )
319     {
320       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
321       if ( aZoneItem )
322       {
323         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
324       }
325     }
326     selectionMgr()->setSelected( aList );
327   }
328   else
329   {
330     // select a single zone
331     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
332     if ( aZoneItem )
333     {
334       SUIT_DataOwnerPtrList aList( true );
335       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
336       selectionMgr()->setSelected( aList );
337     }
338   }
339 }
340
341 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
342 {
343   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
344   if ( aRegion )
345   {
346     QList<HYDROGUI_Zone*> aZonesList;
347     HYDROGUI_Zone* aZone;
348     // Get a list of dropped zones
349     for ( int i = 0; i < theZonesList.length(); i++ )
350     {
351       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
352       if ( aZone )
353       {
354         aZonesList.append( aZone );
355       }
356     }
357     if ( aZonesList.length() > 0 )
358     {
359       aRegion->addZones( aZonesList );
360       HYDROGUI_CalculationDlg* aPanel = 
361         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
362       if ( aPanel )
363       {
364         aPanel->refreshZonesBrowser();
365       }
366       createPreview();
367     }
368   }
369 }
370
371 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
372 {
373   QList<HYDROGUI_Zone*> aZonesList;
374   HYDROGUI_Zone* aZone;
375   // Get a list of dropped zones
376   for ( int i = 0; i < theZonesList.length(); i++ )
377   {
378     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
379     if ( aZone )
380     {
381       aZonesList.append( aZone );
382     }
383   }
384   if ( aZonesList.length() > 0 )
385   {
386     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
387     HYDROGUI_CalculationDlg* aPanel = 
388       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
389     if ( aPanel )
390     {
391       aPanel->refreshZonesBrowser();
392     }
393     createPreview();
394   }
395 }
396
397 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theAltitudeName )
398 {
399   HYDROGUI_CalculationDlg* aPanel = 
400     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
401   if ( aPanel )
402   {
403     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
404     if ( aZone )
405     {
406       aZone->setMergeType( theMergeType, theAltitudeName );
407       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
408       if ( aShape )
409       {
410         aShape->update();
411       }
412     }
413     aPanel->refreshZonesBrowser();
414   }
415 }
416
417 void HYDROGUI_CalculationOp::onAddObjects()
418 {
419   HYDROGUI_CalculationDlg* aPanel = 
420     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
421   if ( !aPanel )
422     return;
423
424   // Add geometry objects selected in the module browser to the calculation case
425   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
426   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
427     return;
428
429   QStringList anAddedList;
430   for (int i = 0; i < aSelectedList.length(); i++)
431   {
432     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
433       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
434     if ( anObject.IsNull() )
435       continue;
436
437     if ( myEditedObject->AddGeometryObject( anObject ) )
438       anAddedList.append( anObject->GetName() );
439   }
440
441   if ( !anAddedList.isEmpty() )
442   {
443     aPanel->includeGeomObjects( anAddedList );
444     createPreview();
445   }
446 }
447
448 void HYDROGUI_CalculationOp::onRemoveObjects()
449 {
450   // Remove selected objects from the calculation case
451   HYDROGUI_CalculationDlg* aPanel = 
452     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
453   if ( !aPanel )
454     return;
455
456   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
457   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
458     return;
459
460   for (int i = 0; i < aSelectedList.length(); i++)
461   {
462     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
463       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
464     if ( anObject.IsNull() )
465       continue;
466
467     setObjectVisibility( anObject, false );
468     myEditedObject->RemoveGeometryObject( anObject );
469   }
470
471   module()->update( UF_OCCViewer );
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 | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
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::onNext( const int theIndex )
557 {
558   if( theIndex==1 )
559   {
560     setAvailableGroups();
561   }
562   else if( theIndex==2 )
563   {
564     HYDROGUI_CalculationDlg* aPanel = 
565       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
566     if ( !aPanel )
567       return;
568
569     QApplication::setOverrideCursor( Qt::WaitCursor );
570   
571     QString aNewCaseName = aPanel->getObjectName();
572     QString anOldCaseName = myEditedObject->GetName();
573   
574     bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
575   
576     // At first we must to update the case name because of 
577     // automatic names generation for regions and zones
578     myEditedObject->SetName( aNewCaseName );
579   
580     if ( myEditedObject->IsMustBeUpdated() )
581     {
582       myShowZones = true;
583       myEditedObject->Update();
584
585       //aPanel->setEditedObject( myEditedObject );
586       aPanel->refreshZonesBrowser();
587
588       createPreview();
589     }
590     else
591     {
592       setZonesVisible( true );
593     }
594
595     if ( anIsToUpdateOb )
596       module()->getApp()->updateObjectBrowser( false );
597
598     QApplication::restoreOverrideCursor();
599   }
600 }
601
602 void HYDROGUI_CalculationOp::onHideZones()
603 {
604   setZonesVisible( false );
605 }
606
607 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
608 {
609   myShowZones = theIsVisible;
610   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
611   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
612   HYDROData_SequenceOfObjects aZones;
613   Handle(HYDROData_Region) aRegion;
614   if ( myPreviewViewManager ) 
615   {
616     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
617     {
618       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
619       if ( !aCtx.IsNull() )
620       {
621         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
622         {
623           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
624           if ( !aRegion.IsNull() )
625           {
626             aZones = aRegion->GetZones();
627             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
628             for ( ; aZonesIter.More(); aZonesIter.Next() )
629             {
630               setObjectVisibility( aZonesIter.Value(), theIsVisible );
631             }
632           }
633         }
634       }
635
636       module()->update( UF_OCCViewer );
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         setObjectVisibility( anIter.Value(), true );
708       }
709
710       //Process the draw events for viewer
711       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
712       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
713         vw->onTopView();
714     }
715
716     module()->update( UF_OCCViewer | UF_FitAll );
717   }
718 }
719
720 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
721 {
722   if ( theEntity.IsNull() || !myPreviewViewManager ) {
723     return;
724   }
725
726   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
727   if ( aViewer ) {
728     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
729   }
730 }
731
732 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
733 {
734   closePreview();
735 }
736
737 void HYDROGUI_CalculationOp::closePreview()
738 {
739   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
740   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
741   QShortcut* aShortcut;
742   foreach( aShortcut, aShortcuts )
743   {
744     if ( aShortcut->key() == 
745       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
746       SUIT_DataBrowser::RenameShortcut ) ) )
747     {
748       aShortcut->setEnabled( true );
749     }
750   }
751
752
753   if( myPreviewViewManager )
754   {
755     // Hide all the displayed objects in the preview view
756     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
757     if ( aViewer ) {
758       size_t aViewId = (size_t)aViewer;
759       HYDROData_Iterator anIterator( doc() );
760       for( ; anIterator.More(); anIterator.Next() ) {
761         Handle(HYDROData_Entity) anObject = anIterator.Current();
762         if( !anObject.IsNull() ) {
763           module()->setObjectVisible( aViewId, anObject, false );
764         }
765       }
766     }
767
768     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
769                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
770
771     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
772     myPreviewViewManager = NULL;
773   }
774
775   if( myActiveViewManager )
776   {
777     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
778     myActiveViewManager = NULL;
779   }
780 }
781
782 void HYDROGUI_CalculationOp::setAvailableGroups()
783 {
784   HYDROGUI_CalculationDlg* aPanel = 
785       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
786
787   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
788   QStringList aList, anEntryList;
789   getNamesAndEntries( aSeq, aList, anEntryList );
790
791   QStringList aGroupsNames;
792
793   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
794   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
795   {
796     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
797     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
798     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
799     {
800       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
801       aGroupsNames.append( aGroup->GetName() );
802     }
803   }
804   if( myEditedObject->IsMustBeUpdated() ) {
805     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
806       Handle(HYDROData_ShapesGroup) aGeomGroup =
807         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
808       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
809         myEditedObject->RemoveGeometryGroup( aGeomGroup );
810       }
811     }
812   }
813
814   aPanel->setAvailableGroups( aGroupsNames );
815   aPanel->includeGroups( aList );
816
817   bool isUpdated = myEditedObject->IsMustBeUpdated();
818 }
819
820 void HYDROGUI_CalculationOp::onAddGroups()
821 {
822   HYDROGUI_CalculationDlg* aPanel = 
823     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
824   if ( !aPanel )
825     return;
826
827   // Add geometry objects selected in the module browser to the calculation case
828   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
829   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
830     return;
831
832   QStringList anAddedList;
833   for (int i = 0; i < aSelectedList.length(); i++)
834   {
835     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
836       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
837     if ( aGroup.IsNull() )
838       continue;
839
840     if ( myEditedObject->AddGeometryGroup( aGroup ) )
841       anAddedList.append( aGroup->GetName() );
842   }
843
844   if ( !anAddedList.isEmpty() )
845   {
846     aPanel->includeGroups( anAddedList );
847   }
848 }
849
850 void HYDROGUI_CalculationOp::onRemoveGroups()
851 {
852   // Remove selected objects from the calculation case
853   HYDROGUI_CalculationDlg* aPanel = 
854     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
855   if ( !aPanel )
856     return;
857
858   QStringList aSelectedList = aPanel->getSelectedGroups();
859   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
860     return;
861
862   for (int i = 0; i < aSelectedList.length(); i++)
863   {
864     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
865       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
866     if ( aGroup.IsNull() )
867       continue;
868
869     myEditedObject->RemoveGeometryGroup( aGroup );
870   }
871
872   aPanel->excludeGroups( aSelectedList );
873 }