Salome HOME
f62389382bba0841b35cbfbd3c21aba9a311b266
[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       aPanel->setMode( myEditedObject->GetAssignmentMode() );
137       anObjectName = myEditedObject->GetName();
138       aPolylineObj = myEditedObject->GetBoundaryPolyline();
139       if ( aPolylineObj.IsNull() )
140       {
141         aPanel->setBoundary( QString() );
142       }
143       else
144       {
145         aPolylineName = aPolylineObj->GetName();
146         aPanel->setBoundary( aPolylineName );
147       }
148
149       aSeq = myEditedObject->GetGeometryObjects();
150       getNamesAndEntries( aSeq, aList, anEntryList );
151       aPanel->includeGeomObjects( aList );
152     }
153   }
154   else
155   {
156     myEditedObject =
157       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
158     myEditedObject->SetName( anObjectName );
159     myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getMode() );
160   }
161
162   aPanel->setObjectName( anObjectName );
163   aPanel->setEditedObject( myEditedObject );
164
165   createPreview();
166 }
167
168 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
169                                                 QStringList& theNames, QStringList& theEntries ) const
170 {
171  
172   theNames.clear();
173   theEntries.clear();
174   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
175   for ( ; anIter.More(); anIter.Next() )
176   {
177     Handle(HYDROData_Entity) anEntity = anIter.Value();
178     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
179     //  continue;
180
181     theNames.append( anEntity->GetName() );
182     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
183   }
184 }
185
186 void HYDROGUI_CalculationOp::abortOperation()
187 {
188   closePreview();
189   // Abort transaction
190   abortDocOperation();
191   HYDROGUI_Operation::abortOperation();
192   module()->getApp()->updateObjectBrowser();
193 }
194
195 void HYDROGUI_CalculationOp::commitOperation()
196 {
197   closePreview();
198   // Commit transaction
199   commitDocOperation();
200   HYDROGUI_Operation::commitOperation();
201 }
202
203 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
204 {
205   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
206
207   // Connect signals and slots
208   connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );
209   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
210   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
211   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
212   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
213   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
214   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones() ) );
215   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
216   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
217   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
218     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
219   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
220     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
221   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
222     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
223   connect( aPanel, SIGNAL( objectsSelected() ), 
224            SLOT( onObjectsSelected() ) );
225   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
226     SLOT( onBoundarySelected( const QString & ) ) );
227
228   return aPanel;
229 }
230
231 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
232 {
233   bool anIsToUpdateViewer = false;
234
235   // Remove the old boundary from the operation viewer
236   Handle(HYDROData_PolylineXY) aPrevPolyline = 
237     myEditedObject->GetBoundaryPolyline();
238   if ( !aPrevPolyline.IsNull() )
239   {
240     setObjectVisibility( aPrevPolyline, false );
241     anIsToUpdateViewer = true;
242   }
243
244   // Set the selected boundary polyline to the calculation case
245   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
246     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
247   myEditedObject->SetBoundaryPolyline( aNewPolyline );
248
249   if ( myPreviewViewManager )
250   {
251     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
252     if ( aViewer )
253     {
254       if ( !aNewPolyline.IsNull() )
255       {
256         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
257         if ( !aCtx.IsNull() )
258         {
259           setObjectVisibility( aNewPolyline, true );
260           anIsToUpdateViewer = true;
261         }
262       }
263
264       if ( anIsToUpdateViewer )
265         module()->update( UF_OCCViewer );
266     }
267   }
268 }
269
270 void HYDROGUI_CalculationOp::onObjectsSelected()
271 {
272   HYDROGUI_CalculationDlg* aPanel = 
273     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
274
275   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
276   QMap<QString, bool> aSelectedObjsMap;
277   foreach( QString aName, aSelectedObjs )
278     aSelectedObjsMap[aName] = true;
279
280
281   // Select the appropriate geometry object shape in the viewer
282   selectionMgr()->clearSelected();
283
284   // Unhighlight all objects except selected
285   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
286   Handle(HYDROData_Entity) anEntity;
287   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
288   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
289   bool isSelected;
290   QString aName;
291   for ( ; anIter.More(); anIter.Next() )
292   {
293     anEntity = anIter.Value();
294     if ( !anEntity.IsNull() )
295     {
296       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
297       if ( aShape )
298       {
299         aName = anEntity->GetName();
300         isSelected = aSelectedObjsMap.contains( aName );
301         aShape->highlight( isSelected, false );
302         aShape->update( false, false );
303         aLastShape = aShape;
304       }
305     }
306   }
307   if( aLastShape )
308     aLastShape->update( true, false );
309 }
310
311 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
312 {
313   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
314   HYDROGUI_Zone* aZoneItem;
315   selectionMgr()->clearSelected();
316   if ( aRegionItem )
317   {
318     // Select a region in preview
319     SUIT_DataOwnerPtrList aList( true );
320     DataObjectList aZones = aRegionItem->children();
321     for ( int i = 0; i < aZones.length(); i++ )
322     {
323       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
324       if ( aZoneItem )
325       {
326         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
327       }
328     }
329     selectionMgr()->setSelected( aList );
330   }
331   else
332   {
333     // select a single zone
334     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
335     if ( aZoneItem )
336     {
337       SUIT_DataOwnerPtrList aList( true );
338       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
339       selectionMgr()->setSelected( aList );
340     }
341   }
342 }
343
344 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
345 {
346   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
347   if ( aRegion )
348   {
349     QList<HYDROGUI_Zone*> aZonesList;
350     HYDROGUI_Zone* aZone;
351     // Get a list of dropped zones
352     for ( int i = 0; i < theZonesList.length(); i++ )
353     {
354       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
355       if ( aZone )
356       {
357         aZonesList.append( aZone );
358       }
359     }
360     if ( aZonesList.length() > 0 )
361     {
362       aRegion->addZones( aZonesList );
363       HYDROGUI_CalculationDlg* aPanel = 
364         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
365       if ( aPanel )
366       {
367         aPanel->refreshZonesBrowser();
368       }
369       createPreview();
370     }
371   }
372 }
373
374 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
375 {
376   QList<HYDROGUI_Zone*> aZonesList;
377   HYDROGUI_Zone* aZone;
378   // Get a list of dropped zones
379   for ( int i = 0; i < theZonesList.length(); i++ )
380   {
381     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
382     if ( aZone )
383     {
384       aZonesList.append( aZone );
385     }
386   }
387   if ( aZonesList.length() > 0 )
388   {
389     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
390     HYDROGUI_CalculationDlg* aPanel = 
391       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
392     if ( aPanel )
393     {
394       aPanel->refreshZonesBrowser();
395     }
396     createPreview();
397   }
398 }
399
400 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theAltitudeName )
401 {
402   HYDROGUI_CalculationDlg* aPanel = 
403     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
404   if ( aPanel )
405   {
406     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
407     if ( aZone )
408     {
409       aZone->setMergeType( theMergeType, theAltitudeName );
410       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
411       if ( aShape )
412       {
413         aShape->update( true, false );
414       }
415     }
416     aPanel->refreshZonesBrowser();
417   }
418 }
419
420 void HYDROGUI_CalculationOp::onAddObjects()
421 {
422   HYDROGUI_CalculationDlg* aPanel = 
423     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
424   if ( !aPanel )
425     return;
426
427   // Add geometry objects selected in the module browser to the calculation case
428   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
429   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
430     return;
431
432   QStringList anAddedList;
433   for (int i = 0; i < aSelectedList.length(); i++)
434   {
435     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
436       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
437     if ( anObject.IsNull() )
438       continue;
439
440     if ( myEditedObject->AddGeometryObject( anObject ) )
441       anAddedList.append( anObject->GetName() );
442   }
443
444   if ( !anAddedList.isEmpty() )
445   {
446     aPanel->includeGeomObjects( anAddedList );
447     createPreview();
448   }
449 }
450
451 void HYDROGUI_CalculationOp::onRemoveObjects()
452 {
453   // Remove selected objects from the calculation case
454   HYDROGUI_CalculationDlg* aPanel = 
455     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
456   if ( !aPanel )
457     return;
458
459   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
460   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
461     return;
462
463   for (int i = 0; i < aSelectedList.length(); i++)
464   {
465     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
466       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
467     if ( anObject.IsNull() )
468       continue;
469
470     setObjectVisibility( anObject, false );
471     myEditedObject->RemoveGeometryObject( anObject );
472   }
473
474   module()->update( UF_OCCViewer );
475   aPanel->excludeGeomObjects( aSelectedList );
476 }
477
478 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
479 {
480   // Check if the case is already modified or not
481   bool isConfirmed = myEditedObject->IsMustBeUpdated();
482   if ( !isConfirmed )
483   {
484     // If not modified check if the case has already defined regions with zones
485     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
486     if ( aSeq.Length() > 0 )
487     {
488       // If there are already defined zones then ask a user to confirm zones recalculation
489       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
490                                tr( "REGIONS_CHANGED" ),
491                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
492                                QMessageBox::Yes | QMessageBox::No,
493                                QMessageBox::No ) == QMessageBox::Yes );
494     }
495     else
496     {
497       isConfirmed = true; // No regions - no zones - nothing to recalculate
498     }
499   }
500   return isConfirmed;
501 }
502
503 bool HYDROGUI_CalculationOp::confirmModeChange() const
504 {
505   // Check if the case is already modified or not
506   bool isConfirmed = myEditedObject->IsMustBeUpdated();
507   if ( !isConfirmed )
508   {
509     // If not modified check if the case has already defined regions with zones
510     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
511     if ( aSeq.Length() > 0 )
512     {
513       // If there are already defined zones then ask a user to confirm zones recalculation
514       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
515                                tr( "MODE_CHANGED" ),
516                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
517                                QMessageBox::Yes | QMessageBox::No,
518                                QMessageBox::No ) == QMessageBox::Yes );
519     }
520     else
521     {
522       isConfirmed = true; // No regions - no zones - nothing to recalculate
523     }
524   }
525   return isConfirmed;
526 }
527
528 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
529                                            QString& theErrorMsg,
530                                            QStringList& theBrowseObjectsEntries )
531 {
532   HYDROGUI_CalculationDlg* aPanel = 
533     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
534   if ( !aPanel )
535     return false;
536
537   if( !myIsEdit )
538   {
539     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
540     theBrowseObjectsEntries.append( anEntry );
541   }
542
543   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
544
545   return true;
546 }
547
548 void HYDROGUI_CalculationOp::onApply()
549 {
550   QApplication::setOverrideCursor( Qt::WaitCursor );
551
552   int anUpdateFlags = 0;
553   QString anErrorMsg;
554   QStringList aBrowseObjectsEntries;
555
556   bool aResult = false;
557   
558   try
559   {
560     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
561   }
562   catch ( Standard_Failure )
563   {
564     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
565     anErrorMsg = aFailure->GetMessageString();
566     aResult = false;
567   }
568   catch ( ... )
569   {
570     aResult = false;
571   }
572   
573   QApplication::restoreOverrideCursor();
574
575   if ( aResult )
576   {
577     module()->update( anUpdateFlags );
578     commit();
579     browseObjects( aBrowseObjectsEntries );
580   }
581   else
582   {
583     abort();
584     QString aMsg = tr( "INPUT_VALID_DATA" );
585     if( !anErrorMsg.isEmpty() )
586       aMsg.prepend( anErrorMsg + "\n" );
587     SUIT_MessageBox::critical( module()->getApp()->desktop(),
588                                tr( "INSUFFICIENT_INPUT_DATA" ),
589                                aMsg ); 
590   }
591 }
592
593 void HYDROGUI_CalculationOp::onNext( const int theIndex )
594 {
595   if( theIndex==1 )
596   {
597     setAvailableGroups();
598   }
599   else if( theIndex==2 )
600   {
601     HYDROGUI_CalculationDlg* aPanel = 
602       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
603     if ( !aPanel )
604       return;
605
606     QApplication::setOverrideCursor( Qt::WaitCursor );
607   
608     QString aNewCaseName = aPanel->getObjectName();
609     QString anOldCaseName = myEditedObject->GetName();
610   
611     bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
612   
613     // At first we must to update the case name because of 
614     // automatic names generation for regions and zones
615     myEditedObject->SetName( aNewCaseName );
616
617     // Set objects in the specified order
618     int aMode = aPanel->getMode();
619     if ( aMode == HYDROData_CalculationCase::AUTOMATIC ) {
620       myEditedObject->RemoveGeometryObjects();
621       foreach ( const QString& aName, aPanel->getAllGeomObjects() ) {
622         Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
623           HYDROGUI_Tool::FindObjectByName( module(), aName ) );
624         if ( anObject.IsNull() ) {
625           continue;
626         }
627
628         myEditedObject->AddGeometryObject( anObject );
629       }
630     }
631     aPanel->setMoveZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
632      
633     if ( myEditedObject->IsMustBeUpdated() )
634     {
635       myShowZones = true;
636       myEditedObject->Update();
637
638       //aPanel->setEditedObject( myEditedObject );
639       aPanel->refreshZonesBrowser();
640
641       createPreview();
642     }
643     else
644     {
645       setZonesVisible( true );
646     }
647
648     if ( anIsToUpdateOb )
649       module()->getApp()->updateObjectBrowser( false );
650
651     QApplication::restoreOverrideCursor();
652   }
653 }
654
655 void HYDROGUI_CalculationOp::onHideZones()
656 {
657   setZonesVisible( false );
658 }
659
660 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
661 {
662   myShowZones = theIsVisible;
663   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
664   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
665   HYDROData_SequenceOfObjects aZones;
666   Handle(HYDROData_Region) aRegion;
667   if ( myPreviewViewManager ) 
668   {
669     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
670     {
671       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
672       if ( !aCtx.IsNull() )
673       {
674         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
675         {
676           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
677           if ( !aRegion.IsNull() )
678           {
679             aZones = aRegion->GetZones();
680             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
681             for ( ; aZonesIter.More(); aZonesIter.Next() )
682             {
683               setObjectVisibility( aZonesIter.Value(), theIsVisible );
684             }
685           }
686         }
687       }
688
689       module()->update( UF_OCCViewer );
690     }
691   }
692 }
693
694 void HYDROGUI_CalculationOp::createPreview()
695 {
696   LightApp_Application* anApp = module()->getApp();
697   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
698   Handle(HYDROData_Entity) anEntity;
699
700   if ( myShowZones )
701   {
702     // Gather zones for displaying
703     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
704     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
705     HYDROData_SequenceOfObjects aZones;
706     Handle(HYDROData_Region) aRegion;
707     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
708     {
709       anEntity = aRegionsIter.Value();
710       if ( !anEntity.IsNull() )
711       {
712         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
713         if ( !aRegion.IsNull() )
714         {
715           aZones = aRegion->GetZones();
716           aSeq.Append( aZones );
717         }
718       }
719     }
720   }
721
722   // Get a boundary polyline if any
723   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
724
725   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
726
727   if ( !myActiveViewManager )
728   {
729     if ( aSeq.IsEmpty() )
730       return;
731
732     myActiveViewManager = anApp->activeViewManager();
733   }
734
735   if ( !myPreviewViewManager )
736   {
737     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
738       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
739     if ( myPreviewViewManager )
740     {
741       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
742                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
743
744       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
745       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
746     }
747   }
748
749   if ( !myPreviewViewManager )
750     return;
751
752   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
753   {
754     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
755     if ( !aCtx.IsNull() )
756     {
757       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
758       for ( ; anIter.More(); anIter.Next() )
759       {
760         setObjectVisibility( anIter.Value(), true );
761       }
762
763       //Process the draw events for viewer
764       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
765       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
766         vw->onTopView();
767     }
768
769     module()->update( UF_OCCViewer | UF_FitAll );
770   }
771 }
772
773 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
774 {
775   if ( theEntity.IsNull() || !myPreviewViewManager ) {
776     return;
777   }
778
779   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
780   if ( aViewer ) {
781     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
782   }
783 }
784
785 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
786 {
787   closePreview();
788 }
789
790 void HYDROGUI_CalculationOp::closePreview()
791 {
792   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
793   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
794   QShortcut* aShortcut;
795   foreach( aShortcut, aShortcuts )
796   {
797     if ( aShortcut->key() == 
798       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
799       SUIT_DataBrowser::RenameShortcut ) ) )
800     {
801       aShortcut->setEnabled( true );
802     }
803   }
804
805
806   if( myPreviewViewManager )
807   {
808     // Hide all the displayed objects in the preview view
809     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
810     if ( aViewer ) {
811       size_t aViewId = (size_t)aViewer;
812       HYDROData_Iterator anIterator( doc() );
813       for( ; anIterator.More(); anIterator.Next() ) {
814         Handle(HYDROData_Entity) anObject = anIterator.Current();
815         if( !anObject.IsNull() ) {
816           module()->setObjectVisible( aViewId, anObject, false );
817         }
818       }
819     }
820
821     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
822                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
823
824     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
825     myPreviewViewManager = NULL;
826   }
827
828   if( myActiveViewManager )
829   {
830     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
831     myActiveViewManager = NULL;
832   }
833 }
834
835 void HYDROGUI_CalculationOp::setAvailableGroups()
836 {
837   HYDROGUI_CalculationDlg* aPanel = 
838       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
839
840   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
841   QStringList aList, anEntryList;
842   getNamesAndEntries( aSeq, aList, anEntryList );
843
844   QStringList aGroupsNames;
845
846   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
847   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
848   {
849     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
850     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
851     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
852     {
853       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
854       aGroupsNames.append( aGroup->GetName() );
855     }
856   }
857   if( myEditedObject->IsMustBeUpdated() ) {
858     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
859       Handle(HYDROData_ShapesGroup) aGeomGroup =
860         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
861       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
862         myEditedObject->RemoveGeometryGroup( aGeomGroup );
863       }
864     }
865   }
866
867   aPanel->setAvailableGroups( aGroupsNames );
868   aPanel->includeGroups( aList );
869
870   bool isUpdated = myEditedObject->IsMustBeUpdated();
871 }
872
873 void HYDROGUI_CalculationOp::onAddGroups()
874 {
875   HYDROGUI_CalculationDlg* aPanel = 
876     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
877   if ( !aPanel )
878     return;
879
880   // Add geometry objects selected in the module browser to the calculation case
881   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
882   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
883     return;
884
885   QStringList anAddedList;
886   for (int i = 0; i < aSelectedList.length(); i++)
887   {
888     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
889       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
890     if ( aGroup.IsNull() )
891       continue;
892
893     if ( myEditedObject->AddGeometryGroup( aGroup ) )
894       anAddedList.append( aGroup->GetName() );
895   }
896
897   if ( !anAddedList.isEmpty() )
898   {
899     aPanel->includeGroups( anAddedList );
900   }
901 }
902
903 void HYDROGUI_CalculationOp::onRemoveGroups()
904 {
905   // Remove selected objects from the calculation case
906   HYDROGUI_CalculationDlg* aPanel = 
907     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
908   if ( !aPanel )
909     return;
910
911   QStringList aSelectedList = aPanel->getSelectedGroups();
912   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
913     return;
914
915   for (int i = 0; i < aSelectedList.length(); i++)
916   {
917     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
918       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
919     if ( aGroup.IsNull() )
920       continue;
921
922     myEditedObject->RemoveGeometryGroup( aGroup );
923   }
924
925   aPanel->excludeGroups( aSelectedList );
926 }
927
928 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
929 {
930   HYDROGUI_CalculationDlg* aPanel = 
931     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
932   if ( !aPanel )
933     return;
934
935   if ( !confirmModeChange() ) {
936     aPanel->setMode( myEditedObject->GetAssignmentMode() );
937     return;
938   }
939
940   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
941   aPanel->setMode( theMode );
942 }