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