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