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