Salome HOME
66b9a89bb90b6b64be9ee0ea2145308221aff204
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_CalculationOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_CalculationDlg.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_Zone.h"
27 #include "HYDROGUI_Region.h"
28
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_ShapesGroup.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_ImmersibleZone.h>
33 #include <HYDROData_Object.h>
34 #include <HYDROData_Tool.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <LightApp_DataOwner.h>
44
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_Desktop.h>
47 #include <SUIT_DataBrowser.h>
48
49 #include <QApplication>
50 #include <QKeySequence>
51 #include <QShortcut>
52
53 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
54 : HYDROGUI_Operation( theModule ),
55   myIsEdit( theIsEdit ),
56   myActiveViewManager( NULL ),
57   myPreviewViewManager( NULL )
58 {
59   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
60 }
61
62 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
63 {
64   closePreview();
65 }
66
67 void HYDROGUI_CalculationOp::startOperation()
68 {
69   HYDROGUI_Operation::startOperation();
70   
71   // Begin transaction
72   startDocOperation();
73
74   HYDROGUI_CalculationDlg* aPanel = 
75     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
76   if ( !aPanel )
77     return;
78
79   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
80   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
81   QShortcut* aShortcut;
82   foreach( aShortcut, aShortcuts )
83   {
84     if ( aShortcut->key() == 
85       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
86       SUIT_DataBrowser::RenameShortcut ) ) )
87     {
88       aShortcut->setEnabled( false );
89     }
90   }
91
92   aPanel->reset();
93   QStringList aList;
94   QStringList anEntryList;
95   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
96   getNamesAndEntries( aSeq, aList, anEntryList );
97   aPanel->setAllGeomObjects( aList, anEntryList );
98
99   // Get all polylines
100   aList.clear();
101   anEntryList.clear();
102   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
103   Handle(HYDROData_PolylineXY) aPolylineObj;
104   QString aPolylineName;
105   for ( ; anIter.More(); anIter.Next() )
106   {
107     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
108
109     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
110     { 
111       // Check the polyline shape
112       TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
113       if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
114         aPolylineName = aPolylineObj->GetName();
115         if ( !aPolylineName.isEmpty() )
116         {
117           aList.append( aPolylineName );
118           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
119         }
120       }
121     }
122   }
123   aPanel->setPolylineNames( aList, anEntryList );
124
125   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
126
127   myEditedObject.Nullify();
128   if ( myIsEdit )
129   {
130     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
131     if ( !myEditedObject.IsNull() )
132     {
133       aPanel->setMode( myEditedObject->GetAssignmentMode() );
134       anObjectName = myEditedObject->GetName();
135       aPolylineObj = myEditedObject->GetBoundaryPolyline();
136       if ( aPolylineObj.IsNull() )
137       {
138         aPanel->setBoundary( QString() );
139       }
140       else
141       {
142         aPolylineName = aPolylineObj->GetName();
143         aPanel->setBoundary( aPolylineName );
144       }
145
146       aSeq = myEditedObject->GetGeometryObjects();
147       getNamesAndEntries( aSeq, aList, anEntryList );
148       aPanel->includeGeomObjects( aList );
149
150       // set rules
151       HYDROData_ListOfRules aRules;
152       Handle(HYDROData_Object) anObject1, anObject2;
153       HYDROData_PriorityType aPriority;
154       HYDROData_Zone::MergeAltitudesType aMergeType;
155       for ( int anIndex = 0; ; anIndex++ )
156       {
157         if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType ) ) {
158           HYDROData_CustomRule aRule;
159           aRule.Object1 = anObject1;
160           aRule.Object2 = anObject2;
161           aRule.Priority = aPriority;
162           aRule.MergeType = aMergeType;
163
164           aRules << aRule;
165         }
166         else
167           break;
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   // For manual mode priority rules are redundant
591   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
592     myEditedObject->ClearRules( false );
593   }
594  
595   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
596
597   return true;
598 }
599
600 void HYDROGUI_CalculationOp::onApply()
601 {
602   // Check warnings
603   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
604   if ( aWarning.Type != WARN_OK ) {
605     if ( !confirmContinueWithWarning( aWarning ) ) {
606       // Go back to the first page
607       HYDROGUI_CalculationDlg* aPanel = 
608         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
609       if ( aPanel ) {
610         aPanel->onFirstPage();
611       }
612       return;
613     }
614   }
615
616   QApplication::setOverrideCursor( Qt::WaitCursor );
617
618   int anUpdateFlags = 0;
619   QString anErrorMsg;
620   QStringList aBrowseObjectsEntries;
621
622   bool aResult = false;
623   
624   try
625   {
626     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
627   }
628   catch ( Standard_Failure )
629   {
630     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
631     anErrorMsg = aFailure->GetMessageString();
632     aResult = false;
633   }
634   catch ( ... )
635   {
636     aResult = false;
637   }
638   
639   QApplication::restoreOverrideCursor();
640
641   if ( aResult )
642   {
643     module()->update( anUpdateFlags );
644     commit();
645     browseObjects( aBrowseObjectsEntries );
646   }
647   else
648   {
649     abort();
650     QString aMsg = tr( "INPUT_VALID_DATA" );
651     if( !anErrorMsg.isEmpty() )
652       aMsg.prepend( anErrorMsg + "\n" );
653     SUIT_MessageBox::critical( module()->getApp()->desktop(),
654                                tr( "INSUFFICIENT_INPUT_DATA" ),
655                                aMsg ); 
656   }
657 }
658
659 void HYDROGUI_CalculationOp::onNext( const int theIndex )
660 {
661   if( theIndex==1 )
662   {
663     setAvailableGroups();
664   }
665   else if( theIndex==2 )
666   {
667     HYDROGUI_CalculationDlg* aPanel = 
668       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
669     if ( !aPanel )
670       return;
671
672     QApplication::setOverrideCursor( Qt::WaitCursor );
673   
674     QString aNewCaseName = aPanel->getObjectName();
675     QString anOldCaseName = myEditedObject->GetName();
676     bool isNameChanged = anOldCaseName != aNewCaseName;
677   
678     bool anIsToUpdateOb = isNameChanged;
679
680     // At first we must to update the case name because of 
681     // automatic names generation for regions and zones
682     myEditedObject->SetName( aNewCaseName );
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       // Clear priority rules
700       //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
701         myEditedObject->ClearRules( true );
702       //@ASL }
703       // Set priority rules
704       foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
705         myEditedObject->AddRule( aRule.Object1, aRule.Priority,
706                                  aRule.Object2, aRule.MergeType );
707       }
708     }
709     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
710          
711     if ( myEditedObject->IsMustBeUpdated() )
712     {
713       myShowZones = true;
714       myEditedObject->Update();
715       
716       AssignDefaultZonesColors();
717
718       //aPanel->setEditedObject( myEditedObject );
719       aPanel->refreshZonesBrowser();
720     
721       closePreview();
722       createPreview();
723
724       anIsToUpdateOb = true;
725     }
726     else
727     {
728       setZonesVisible( true );
729
730       if ( isNameChanged ) {
731         module()->getDataModel()->updateObjectTree( myEditedObject );
732       }
733     }
734
735     if ( anIsToUpdateOb ) {
736       SUIT_DataBrowser* anObjBrowser = ((LightApp_Application*)module()->application())->objectBrowser();
737       if ( anObjBrowser ) {
738         anObjBrowser->updateTree( module()->getDataModel()->getDataObject( myEditedObject ), false );
739       }
740     }
741
742     QApplication::restoreOverrideCursor();
743   }
744 }
745
746 void HYDROGUI_CalculationOp::onHideZones()
747 {
748   setZonesVisible( false );
749 }
750
751 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
752 {
753   myShowZones = theIsVisible;
754   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
755   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
756   HYDROData_SequenceOfObjects aZones;
757   Handle(HYDROData_Region) aRegion;
758   if ( myPreviewViewManager ) 
759   {
760     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
761     {
762       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
763       if ( !aCtx.IsNull() )
764       {
765         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
766         {
767           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
768           if ( !aRegion.IsNull() )
769           {
770             aZones = aRegion->GetZones();
771             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
772             for ( ; aZonesIter.More(); aZonesIter.Next() )
773             {
774               setObjectVisibility( aZonesIter.Value(), theIsVisible );
775             }
776           }
777         }
778       }
779
780       module()->update( UF_OCCViewer );
781     }
782   }
783 }
784
785 void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
786 {
787   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
788   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
789   HYDROData_SequenceOfObjects aZones;
790   Handle(HYDROData_Region) aRegion;
791   if ( myPreviewViewManager ) 
792   {
793     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
794     {
795       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
796       if ( !aCtx.IsNull() )
797       {
798         int aCounter = 0;        
799         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
800         {
801           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
802           if ( !aRegion.IsNull() )
803           {
804             aZones = aRegion->GetZones();
805             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
806             for ( ; aZonesIter.More(); aZonesIter.Next() )
807             {
808               // Zone
809               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
810               if ( !aZone.IsNull() )
811               {
812                 QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
813                 while (aFillingColor == Qt::red)
814                   aFillingColor = GenerateDefaultZonesColor(++aCounter);
815                 
816                 aZone->SetColor(aFillingColor);
817               }
818             }
819           }
820         }
821       }
822     }
823   }
824 }
825
826 QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
827                                                           float theSaturation/* = 0.5*/,
828                                                           float theValue/* = 0.95*/ ) const
829 {
830   float aGoldenRatioConjugate = (float)(360./582.);
831   float aHue = (float)rand();
832   aHue += aGoldenRatioConjugate*theIndex;
833   aHue -= floor(aHue);
834
835   float aR = 0., aG = 0., aB = 0.;
836   int aHueInt = (int)(aHue*6.);
837   float aF = aHue*6. - aHueInt;
838   float aP = theValue * (1. - theSaturation);
839   float aQ = theValue * (1. - aF*theSaturation);
840   float aT = theValue * (1. - (1. - aF) * theSaturation);
841   switch (aHueInt)
842   {
843   case 0: { aR = theValue; aG = aT; aB = aP; break; }
844   case 1: { aR = aQ; aG = theValue; aB = aP; break; }
845   case 2: { aR = aP; aG = theValue; aB = aT; break; }
846   case 3: { aR = aP; aG = aQ; aB = theValue; break; }
847   case 4: { aR = aT; aG = aP; aB = theValue; break; }
848   case 5: { aR = theValue; aG = aP; aB = aQ; break; }
849   default: break;
850   }
851
852   QColor aColor = QColor( (int)(aR*256.), (int)(aG*256.), (int)(aB*256.) );
853   return ( aColor.isValid() ? aColor : HYDROData_ImmersibleZone::DefaultFillingColor() );
854 }
855
856 void HYDROGUI_CalculationOp::createPreview()
857 {
858   LightApp_Application* anApp = module()->getApp();
859   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
860   Handle(HYDROData_Entity) anEntity;
861
862   if ( myShowZones )
863   {
864     // Gather zones for displaying
865     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
866     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
867     HYDROData_SequenceOfObjects aZones;
868     Handle(HYDROData_Region) aRegion;
869     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
870     {
871       anEntity = aRegionsIter.Value();
872       if ( !anEntity.IsNull() )
873       {
874         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
875         if ( !aRegion.IsNull() )
876         {
877           aZones = aRegion->GetZones();
878           aSeq.Append( aZones );
879         }
880       }
881     }
882   }
883
884   // Get a boundary polyline if any
885   aSeq.Append( myEditedObject->GetBoundaryPolyline() );
886
887   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
888
889   if ( !myActiveViewManager )
890   {
891     if ( aSeq.IsEmpty() )
892       return;
893
894     myActiveViewManager = anApp->activeViewManager();
895   }
896
897   if ( !myPreviewViewManager )
898   {
899     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
900       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
901     if ( myPreviewViewManager )
902     {
903       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
904                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
905
906       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
907       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
908     }
909   }
910
911   if ( !myPreviewViewManager )
912     return;
913
914   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
915   {
916     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
917     if ( !aCtx.IsNull() )
918     {
919       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
920       for ( ; anIter.More(); anIter.Next() )
921       {
922         setObjectVisibility( anIter.Value(), true );
923       }
924
925       //Process the draw events for viewer
926       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
927       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
928         vw->onTopView();
929     }
930
931     module()->update( UF_OCCViewer | UF_FitAll );
932   }
933 }
934
935 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
936 {
937   if ( theEntity.IsNull() || !myPreviewViewManager ) {
938     return;
939   }
940
941   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
942   if ( aViewer ) {
943     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
944   }
945 }
946
947 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
948 {
949   closePreview();
950 }
951
952 void HYDROGUI_CalculationOp::closePreview()
953 {
954   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
955   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
956   QShortcut* aShortcut;
957   foreach( aShortcut, aShortcuts )
958   {
959     if ( aShortcut->key() == 
960       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
961       SUIT_DataBrowser::RenameShortcut ) ) )
962     {
963       aShortcut->setEnabled( true );
964     }
965   }
966
967
968   if( myPreviewViewManager )
969   {
970     // Hide all the displayed objects in the preview view
971     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
972     if ( aViewer ) {
973       size_t aViewId = (size_t)aViewer;
974       HYDROData_Iterator anIterator( doc() );
975       for( ; anIterator.More(); anIterator.Next() ) {
976         Handle(HYDROData_Entity) anObject = anIterator.Current();
977         if( !anObject.IsNull() ) {
978           module()->setObjectVisible( aViewId, anObject, false );
979         }
980       }
981     }
982
983     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
984                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
985
986     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
987     myPreviewViewManager = NULL;
988   }
989
990   if( myActiveViewManager )
991   {
992     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
993     myActiveViewManager = NULL;
994   }
995 }
996
997 void HYDROGUI_CalculationOp::setAvailableGroups()
998 {
999   HYDROGUI_CalculationDlg* aPanel = 
1000       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1001
1002   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1003   QStringList aList, anEntryList;
1004   getNamesAndEntries( aSeq, aList, anEntryList );
1005
1006   QStringList aGroupsNames;
1007
1008   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1009   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1010   {
1011     Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
1012     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1013     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1014     {
1015       Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
1016       aGroupsNames.append( aGroup->GetName() );
1017     }
1018   }
1019   if( myEditedObject->IsMustBeUpdated() ) {
1020     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1021       Handle(HYDROData_ShapesGroup) aGeomGroup =
1022         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1023       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1024         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1025       }
1026     }
1027   }
1028
1029   aPanel->setAvailableGroups( aGroupsNames );
1030   aPanel->includeGroups( aList );
1031
1032   bool isUpdated = myEditedObject->IsMustBeUpdated();
1033 }
1034
1035 void HYDROGUI_CalculationOp::onAddGroups()
1036 {
1037   HYDROGUI_CalculationDlg* aPanel = 
1038     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1039   if ( !aPanel )
1040     return;
1041
1042   // Add geometry objects selected in the module browser to the calculation case
1043   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1044   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1045     return;
1046
1047   QStringList anAddedList;
1048   for (int i = 0; i < aSelectedList.length(); i++)
1049   {
1050     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1051       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1052     if ( aGroup.IsNull() )
1053       continue;
1054
1055     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1056       anAddedList.append( aGroup->GetName() );
1057   }
1058
1059   if ( !anAddedList.isEmpty() )
1060   {
1061     aPanel->includeGroups( anAddedList );
1062   }
1063 }
1064
1065 void HYDROGUI_CalculationOp::onRemoveGroups()
1066 {
1067   // Remove selected objects from the calculation case
1068   HYDROGUI_CalculationDlg* aPanel = 
1069     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1070   if ( !aPanel )
1071     return;
1072
1073   QStringList aSelectedList = aPanel->getSelectedGroups();
1074   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1075     return;
1076
1077   for (int i = 0; i < aSelectedList.length(); i++)
1078   {
1079     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1080       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1081     if ( aGroup.IsNull() )
1082       continue;
1083
1084     myEditedObject->RemoveGeometryGroup( aGroup );
1085   }
1086
1087   aPanel->excludeGroups( aSelectedList );
1088 }
1089
1090 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1091 {
1092   HYDROGUI_CalculationDlg* aPanel = 
1093     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1094   if ( !aPanel )
1095     return;
1096
1097   if ( !confirmModeChange() ) {
1098     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1099     return;
1100   }
1101
1102   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1103   aPanel->setMode( theMode );
1104 }