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