Salome HOME
selection on object browser for shapes in fitShapesPointsToMesheEdges dialog
[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_Tool2.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 #include <HYDROData_StricklerTable.h>
36 #include <HYDROData_BCPolygon.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <OCCViewer_ViewWindow.h>
41
42 #include <LightApp_Application.h>
43 #include <LightApp_UpdateFlags.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_DataOwner.h>
46
47 #include <SUIT_MessageBox.h>
48 #include <SUIT_Desktop.h>
49 #include <SUIT_DataBrowser.h>
50
51 #include <QApplication>
52 #include <QKeySequence>
53 #include <QShortcut>
54 #include <QPushButton>
55 #include <BRep_Builder.hxx>
56 #include <TopoDS.hxx>
57 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopExp.hxx>
59 #include <TopoDS.hxx>
60
61 #include <HYDROData_CompleteCalcCase.h>
62
63 //#define _DEVDEBUG_
64 #include "HYDRO_trace.hxx"
65
66
67 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit, bool IsComplete )
68 : HYDROGUI_Operation( theModule ),
69   myIsEdit( theIsEdit ),
70   myActiveViewManager( NULL ),
71   myPreviewViewManager( NULL ),
72   myShowGeomObjects( true ),
73   myShowLandCoverMap( false ),
74   myShowZones( false ),
75   myIsComplete (IsComplete)
76 {
77   QString aDlgName;
78   if (myIsComplete)
79     aDlgName =  tr( "COMPLETE_CALCULATION" );
80   else
81     aDlgName = myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" );
82   setName( aDlgName );
83 }
84
85 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
86 {
87   closePreview();
88 }
89
90 void HYDROGUI_CalculationOp::startOperation()
91 {
92   HYDROGUI_Operation::startOperation();
93   
94   // Begin transaction
95   startDocOperation();
96
97   HYDROGUI_CalculationDlg* aPanel = 
98     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
99   if ( !aPanel )
100     return;
101
102   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
103   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
104   QShortcut* aShortcut;
105   foreach( aShortcut, aShortcuts )
106   {
107     if ( aShortcut->key() == 
108       QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
109       SUIT_DataBrowser::RenameShortcut ) ) )
110     {
111       aShortcut->setEnabled( false );
112     }
113   }
114
115   aPanel->reset();
116   QStringList aList;
117   QStringList anEntryList;
118   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
119   getNamesAndEntries( aSeq, aList, anEntryList );
120   
121   QStringList List1;
122   QStringList EList1;
123
124   //add intersection polylines
125   AddInterPolylinesToList(aList, anEntryList);
126   
127   aPanel->setAllGeomObjects( aList, anEntryList );
128
129   // Get all polylines
130   aList.clear();
131   anEntryList.clear();
132   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
133   Handle(HYDROData_PolylineXY) aPolylineObj;
134   QString aPolylineName;
135   for ( ; anIter.More(); anIter.Next() )
136   {
137     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
138
139     if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed(false) )
140     { 
141 //      // Check the polyline shape
142 //      TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
143 //      if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
144         aPolylineName = aPolylineObj->GetName();
145         if ( !aPolylineName.isEmpty() )
146         {
147           aList.append( aPolylineName );
148           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
149         }
150 //      }
151     }
152   }
153   aPanel->setPolylineNames( aList, anEntryList );
154
155   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
156
157   myEditedObject.Nullify();
158
159   if ( myIsEdit )
160   {
161     myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
162     if ( !myEditedObject.IsNull() )
163     {
164       aPanel->setMode( myEditedObject->GetAssignmentMode() );
165       anObjectName = myEditedObject->GetName();
166       aPolylineObj = myEditedObject->GetBoundaryPolyline();
167       if ( aPolylineObj.IsNull() )
168       {
169         aPanel->setBoundary( QString() );
170       }
171       else
172       {
173         aPolylineName = aPolylineObj->GetName();
174         aPanel->setBoundary( aPolylineName );
175       }
176
177       aSeq = myEditedObject->GetGeometryObjects();      
178       HYDROData_SequenceOfObjects anInterPolyList = myEditedObject->GetInterPolyObjects();
179       aSeq.Append(anInterPolyList);
180
181       getNamesAndEntries( aSeq, aList, anEntryList );
182       if (!myIsComplete)
183         aPanel->includeGeomObjects( aList );
184       else 
185       {
186         //aPanel->includeGeomObjects( aList );
187         aPanel->setAlreadyAddedGeomObjects( aList );
188         //myIncObjAtStart = aSeq;
189         aPanel->hideAvailableGeomObjects( aList );
190         aPanel->setGeomOrderingEnabled(true);
191       }
192
193       // set rules
194       setRules( HYDROData_CalculationCase::DataTag_CustomRules );      
195     }
196   }
197   else
198   {
199     myEditedObject =
200       Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
201     myEditedObject->SetName( anObjectName );
202     myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getMode() );
203   }
204
205   aPanel->setObjectName( anObjectName );
206   aPanel->setEditedObject( myEditedObject );
207
208   setGeomObjectsVisible( true );
209
210   createPreview( false );
211 }
212
213 void HYDROGUI_CalculationOp::AddInterPolylinesToList(QStringList& theList, QStringList& theEntryList)
214 {
215   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
216   Handle(HYDROData_PolylineXY) aPolylineObj;
217   QString aPolylineName;
218   for ( ; anIter.More(); anIter.Next() )
219   {
220     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
221     if ( !aPolylineObj.IsNull())
222     { 
223       aPolylineName = aPolylineObj->GetName();
224       if ( !aPolylineName.isEmpty() && !theList.contains(aPolylineName) )
225       {
226         theList.append( aPolylineName );
227         theEntryList.append(HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ));
228       }
229     }
230   }
231 }
232
233 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
234                                                 QStringList& theNames, QStringList& theEntries ) const
235 {
236  
237   theNames.clear();
238   theEntries.clear();
239   HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
240   for ( ; anIter.More(); anIter.Next() )
241   {
242     Handle(HYDROData_Entity) anEntity = anIter.Value();
243     //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
244     //  continue;
245
246     theNames.append( anEntity->GetName() );
247     theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
248   }
249 }
250
251 void HYDROGUI_CalculationOp::abortOperation()
252 {
253   closePreview();
254   // Abort transaction
255   abortDocOperation();
256   HYDROGUI_Operation::abortOperation();
257   module()->getApp()->updateObjectBrowser();
258 }
259
260 void HYDROGUI_CalculationOp::commitOperation()
261 {
262   closePreview();
263   // Commit transaction
264   commitDocOperation();
265   HYDROGUI_Operation::commitOperation();
266 }
267
268 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
269 {
270   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName(), myIsComplete );
271
272   // Connect signals and slots
273   connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );  
274   connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
275   connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
276   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
277   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
278
279   connect( aPanel, SIGNAL( addBoundaryPolygons() ), SLOT( onAddBoundaryPolygons() ) );
280   connect( aPanel, SIGNAL( removeBoundaryPolygons() ), SLOT( onRemoveBoundaryPolygons() ) );
281
282   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
283
284   connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
285
286   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
287   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones( const int ) ) );
288   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
289   connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
290   connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
291     SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
292   connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
293     SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
294   connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
295     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
296   connect( aPanel, SIGNAL( objectsSelected() ), 
297            SLOT( onObjectsSelected() ) );
298   connect( aPanel, SIGNAL( landCoverMapSelected( const QString & ) ), 
299            SLOT( onLandCoverMapSelected( const QString & ) ) );
300   connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
301     SLOT( onBoundarySelected( const QString & ) ) );
302   connect( aPanel, SIGNAL( StricklerTableSelected( const QString & ) ), 
303     SLOT( onStricklerTableSelected( const QString & ) ) );
304
305   connect( aPanel, SIGNAL( regenerateColors() ), this, 
306     SLOT( onRegenerateColors() ) );
307
308   return aPanel;
309 }
310
311 void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
312 {
313   bool anIsToUpdateViewer = false;
314
315   // Remove the old boundary from the operation viewer
316   Handle(HYDROData_PolylineXY) aPrevPolyline = 
317     myEditedObject->GetBoundaryPolyline();
318   if ( !aPrevPolyline.IsNull() )
319   {
320     setObjectVisibility( aPrevPolyline, false );
321     anIsToUpdateViewer = true;
322   }
323
324   // Set the selected boundary polyline to the calculation case
325   Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
326     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
327   myEditedObject->SetBoundaryPolyline( aNewPolyline );
328
329   if ( myPreviewViewManager )
330   {
331     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
332     if ( aViewer )
333     {
334       if ( !aNewPolyline.IsNull() )
335       {
336         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
337         if ( !aCtx.IsNull() )
338         {
339           setObjectVisibility( aNewPolyline, true );
340           anIsToUpdateViewer = true;
341         }
342       }
343
344       if ( anIsToUpdateViewer )
345         module()->update( UF_OCCViewer );
346     }
347   }
348 }
349
350 void HYDROGUI_CalculationOp::onStricklerTableSelected ( const QString & theObjName )
351 {
352   bool anIsToUpdateViewer = false;
353
354   // Remove old presentation of land cover map from the operation viewer  
355   Handle(HYDROData_LandCoverMap) aLandCoverMap = myEditedObject->GetLandCoverMap();
356   if ( !aLandCoverMap.IsNull() )
357   {
358     setObjectVisibility( aLandCoverMap, false );
359     anIsToUpdateViewer = true;
360   }
361
362   // Set the selected Strickler table to the calculation case
363   Handle(HYDROData_StricklerTable) aNewStricklerTable = Handle(HYDROData_StricklerTable)::DownCast(
364     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_STRICKLER_TABLE ) );
365   myEditedObject->SetStricklerTable( aNewStricklerTable );
366
367   if ( myPreviewViewManager )
368   {
369     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
370     if ( aViewer )
371     {
372       if ( !aNewStricklerTable.IsNull() )
373       {
374         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
375         if ( !aCtx.IsNull() )
376         {
377           if ( !aLandCoverMap.IsNull() )
378           {
379             setObjectVisibility( aLandCoverMap, true );
380             anIsToUpdateViewer = true;
381           }          
382         }
383       }
384
385       if ( anIsToUpdateViewer )
386         module()->update( UF_OCCViewer );
387     }
388   }
389 }
390
391 void HYDROGUI_CalculationOp::onObjectsSelected()
392 {
393   DEBTRACE("onObjectsSelected");
394   HYDROGUI_CalculationDlg* aPanel = 
395     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
396
397   QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
398   QMap<QString, bool> aSelectedObjsMap;
399   foreach( QString aName, aSelectedObjs )
400     aSelectedObjsMap[aName] = true;
401
402
403   // Select the appropriate geometry object shape in the viewer
404   selectionMgr()->clearSelected();
405
406   // Unhighlight all objects except selected
407   HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
408   Handle(HYDROData_Entity) anEntity;
409   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
410   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
411   bool isSelected;
412   QString aName;
413   for ( ; anIter.More(); anIter.Next() )
414   {
415     anEntity = anIter.Value();
416     if ( !anEntity.IsNull() )
417     {
418       aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
419       if ( aShape )
420       {
421         aName = anEntity->GetName();
422         isSelected = aSelectedObjsMap.contains( aName );
423         aShape->highlight( isSelected, false );
424         aShape->update( false, false );
425         aLastShape = aShape;
426       }
427     }
428   }
429   if( aLastShape )
430     aLastShape->update( true, false );
431 }
432
433 void HYDROGUI_CalculationOp::onLandCoverMapSelected( const QString & theObjName )
434 {
435   bool anIsToUpdateViewer = false;
436   
437   // Remove old presentation of land cover map from the operation viewer  
438   Handle(HYDROData_LandCoverMap) aPrevLandCoverMap = myEditedObject->GetLandCoverMap();
439   if ( !aPrevLandCoverMap.IsNull() )
440   {
441     setObjectVisibility( aPrevLandCoverMap, false );
442     anIsToUpdateViewer = true;
443   }
444
445   // Select the appropriate land cover map shape in the viewer
446   selectionMgr()->clearSelected();
447
448   // Set the selected land cover map to the calculation case
449   Handle(HYDROData_LandCoverMap) aNewLandCoverMap = Handle(HYDROData_LandCoverMap)::DownCast(
450     HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_LAND_COVER_MAP ) );
451   myEditedObject->SetLandCoverMap( aNewLandCoverMap );
452
453   createPreview( true );
454 }
455
456 void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
457 {
458   DEBTRACE("onClickedInZonesBrowser");
459   HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
460   HYDROGUI_Zone* aZoneItem;
461   selectionMgr()->clearSelected();
462   if ( aRegionItem )
463   {
464     DEBTRACE("Region");
465     // Select a region in preview
466     SUIT_DataOwnerPtrList aList( true );
467     DataObjectList aZones = aRegionItem->children();
468     for ( int i = 0; i < aZones.length(); i++ )
469     {
470       aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
471       if ( aZoneItem )
472       {
473         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
474       }
475     }
476     selectionMgr()->setSelected( aList );
477   }
478   else
479   {
480     // select a single zone
481     aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
482     if ( aZoneItem )
483     {
484       DEBTRACE("Zone");
485       SUIT_DataOwnerPtrList aList( true );
486       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
487       selectionMgr()->setSelected( aList );
488     }
489   }
490 }
491
492 void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem,
493                                           const QList<SUIT_DataObject*>& theZonesList )
494 {
495   HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
496   if ( aRegion )
497   {
498     QList<HYDROGUI_Zone*> aZonesList;
499     HYDROGUI_Zone* aZone;
500     // Get a list of dropped zones
501     for ( int i = 0; i < theZonesList.length(); i++ )
502     {
503       aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
504       if ( aZone )
505       {
506         aZonesList.append( aZone );
507       }
508     }
509     if ( aZonesList.length() > 0 )
510     {
511       aRegion->addZones( aZonesList );
512       HYDROGUI_CalculationDlg* aPanel = 
513         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
514       if ( aPanel )
515         aPanel->refreshZonesBrowser();
516       createPreview( false, false, false );
517     }
518   }
519 }
520
521 void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
522 {
523   if ( createRegion( theZonesList ) )
524   {
525     HYDROGUI_CalculationDlg* aPanel = 
526       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
527     if ( aPanel )
528     {
529       aPanel->refreshZonesBrowser();
530     }
531     createPreview( false, false, false  );
532   }
533 }
534
535 void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theMergeObjectName )
536 {
537   HYDROGUI_CalculationDlg* aPanel = 
538     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
539   if ( aPanel )
540   {
541     HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
542     if ( aZone )
543     {
544       aZone->setMergeType( theMergeType, theMergeObjectName );
545       HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
546       if ( aShape )
547       {
548         aShape->update( true, false );
549       }
550     }
551     aPanel->refreshZonesBrowser();
552   }
553 }
554
555 void HYDROGUI_CalculationOp::onAddObjects()
556 {
557   HYDROGUI_CalculationDlg* aPanel = 
558     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
559   if ( !aPanel )
560     return;
561
562   // Add geometry objects selected in the module browser to the calculation case
563   QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
564   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
565     return;
566
567   QStringList anAddedList;
568   for (int i = 0; i < aSelectedList.length(); i++)
569   {
570     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
571       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
572     if ( anObject.IsNull() )
573       continue;
574
575     if ( myEditedObject->AddGeometryObject( anObject ) )
576       anAddedList.append( anObject->GetName() );
577   }
578
579   for (int i = 0; i < aSelectedList.length(); i++)
580   {
581     Handle(HYDROData_PolylineXY) aPoly = Handle(HYDROData_PolylineXY)::DownCast( 
582       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
583     if ( aPoly.IsNull() )
584       continue;
585
586     if ( myEditedObject->AddInterPoly( aPoly ) )
587       anAddedList.append( aPoly->GetName() );
588   }
589
590   if ( !anAddedList.isEmpty() )
591   {
592     aPanel->includeGeomObjects( anAddedList );
593     createPreview( false );
594   }
595 }
596
597 void HYDROGUI_CalculationOp::onRemoveObjects()
598 {
599   // Remove selected objects from the calculation case
600   HYDROGUI_CalculationDlg* aPanel = 
601     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
602   if ( !aPanel )
603     return;
604
605   QStringList aSelectedList = aPanel->getSelectedGeomObjects();
606
607   if (myIsComplete)
608   {
609     QSet<QString> selMap = aSelectedList.toSet();
610   }
611
612
613   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
614     return;
615
616   for (int i = 0; i < aSelectedList.length(); i++)
617   {
618     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
619       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
620     if ( anObject.IsNull() )
621       continue;
622
623     setObjectVisibility( anObject, false );
624     myEditedObject->RemoveGeometryObject( anObject );
625   }
626
627   for (int i = 0; i < aSelectedList.length(); i++)
628   {
629     Handle(HYDROData_PolylineXY) aPoly = Handle(HYDROData_PolylineXY)::DownCast( 
630       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
631     if ( aPoly.IsNull() )
632       continue;
633
634     setObjectVisibility( aPoly, false );
635     myEditedObject->RemoveInterPolyObject ( aPoly ); 
636   }
637
638   module()->update( UF_OCCViewer );
639   aPanel->excludeGeomObjects( aSelectedList );
640 }
641
642 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
643 {
644
645   // Check if the case is already modified or not
646   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
647
648   if ( !isConfirmed )
649   {
650   
651    if (myIsComplete)
652    {
653      //SUIT_MessageBox::information(module()->getApp()->desktop(),
654      //                         tr( "REGIONS_CHANGED" ),
655      //                         tr( "COMPLETE_OP_WILL_BE_PERFORMED" ));
656      return true;
657    }
658
659     // If not modified check if the case has already defined regions with zones
660     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
661     if ( aSeq.Length() > 0 )
662     {
663       // If there are already defined zones then ask a user to confirm zones recalculation
664       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
665                                tr( "REGIONS_CHANGED" ),
666                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
667                                QMessageBox::Yes | QMessageBox::No,
668                                QMessageBox::No ) == QMessageBox::Yes );
669     }
670     else
671     {
672       isConfirmed = true; // No regions - no zones - nothing to recalculate
673     }
674   }
675   return isConfirmed;
676 }
677
678 bool HYDROGUI_CalculationOp::confirmOrderChange() const
679 {
680   // Check if the case is already modified or not
681   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
682   if ( !isConfirmed )
683   {
684     // If not modified check if the case has already defined regions with zones
685     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
686     if ( aSeq.Length() > 0 )
687     {
688       // If there are already defined zones then ask a user to confirm zones recalculation
689       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
690                                tr( "ORDER_CHANGED" ),
691                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
692                                QMessageBox::Yes | QMessageBox::No,
693                                QMessageBox::No ) == QMessageBox::Yes );
694     }
695     else
696     {
697       isConfirmed = true; // No regions - no zones - nothing to recalculate
698     }
699   }
700   return isConfirmed;
701 }
702
703 bool HYDROGUI_CalculationOp::confirmRuleChange() const
704 {
705   // Check if the case is already modified or not
706   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
707   if ( !isConfirmed )
708   {
709     // If not modified check if the case has already defined regions with zones
710     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
711     if ( aSeq.Length() > 0 )
712     {
713       // If there are already defined zones then ask a user to confirm zones recalculation
714       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
715                                tr( "RULE_CHANGED" ),
716                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
717                                QMessageBox::Yes | QMessageBox::No,
718                                QMessageBox::No ) == QMessageBox::Yes );
719     }
720     else
721     {
722       isConfirmed = true; // No regions - no zones - nothing to recalculate
723     }
724   }
725   return isConfirmed;
726 }
727
728 bool HYDROGUI_CalculationOp::confirmModeChange() const
729 {
730   // Check if the case is already modified or not
731   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
732   if ( !isConfirmed )
733   {
734     // If not modified check if the case has already defined regions with zones
735     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
736     if ( aSeq.Length() > 0 )
737     {
738       // If there are already defined zones then ask a user to confirm zones recalculation
739       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
740                                tr( "MODE_CHANGED" ),
741                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
742                                QMessageBox::Yes | QMessageBox::No,
743                                QMessageBox::No ) == QMessageBox::Yes );
744     }
745     else
746     {
747       isConfirmed = true; // No regions - no zones - nothing to recalculate
748     }
749   }
750   return isConfirmed;
751 }
752
753 bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
754 {
755   HYDROData_WarningType aType = theWarning.Type;
756   if ( aType == WARN_OK ) {
757     return true;
758   }
759
760   QString aTitle;
761   QString aMsg;
762   switch ( aType )
763   {
764     case WARN_EMPTY_REGIONS:
765       aTitle = tr( "EMPTY_REGIONS" );
766       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
767       break;
768     default:
769       aTitle = tr( "WARNING" );
770       aMsg = theWarning.Data;
771   }
772
773
774   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
775                                            aTitle, aMsg,
776                                            QMessageBox::Yes | QMessageBox::No,
777                                            QMessageBox::No );
778
779   return ( anAnswer == QMessageBox::Yes );
780 }
781
782 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
783                                            QString& theErrorMsg,
784                                            QStringList& theBrowseObjectsEntries )
785 {
786   HYDROGUI_CalculationDlg* aPanel = 
787     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
788   if ( !aPanel )
789     return false;
790
791   if( !myIsEdit )
792   {
793     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
794     theBrowseObjectsEntries.append( anEntry );
795   }
796
797   // For manual mode priority rules are redundant
798   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
799     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
800   }
801  
802   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
803
804   return true;
805 }
806
807 void HYDROGUI_CalculationOp::onApply()
808 {
809   // Check warnings
810   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
811   if ( aWarning.Type != WARN_OK ) {
812     if ( !confirmContinueWithWarning( aWarning ) ) {
813       // Go back to the first page
814       HYDROGUI_CalculationDlg* aPanel = 
815         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
816       if ( aPanel ) {
817         aPanel->onFirstPage();
818       }
819       return;
820     }
821   }
822
823   QApplication::setOverrideCursor( Qt::WaitCursor );
824
825   int anUpdateFlags = 0;
826   QString anErrorMsg;
827   QStringList aBrowseObjectsEntries;
828
829   bool aResult = false;
830   
831   try
832   {
833     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
834   }
835   catch ( Standard_Failure& e )
836   {
837     anErrorMsg = e.GetMessageString();
838     aResult = false;
839   }
840   catch ( ... )
841   {
842     aResult = false;
843   }
844   
845   QApplication::restoreOverrideCursor();
846
847   if ( aResult )
848   {
849     module()->update( anUpdateFlags );
850     commit();
851     browseObjects( aBrowseObjectsEntries );
852   }
853   else
854   {
855     abort();
856     QString aMsg = tr( "INPUT_VALID_DATA" );
857     if( !anErrorMsg.isEmpty() )
858       aMsg.prepend( anErrorMsg + "\n" );
859     SUIT_MessageBox::critical( module()->getApp()->desktop(),
860                                tr( "INSUFFICIENT_INPUT_DATA" ),
861                                aMsg ); 
862   }
863 }
864 #include <HYDROData_CompleteCalcCase.h>
865 void HYDROGUI_CalculationOp::onNext( const int theIndex )
866 {
867   if( theIndex==1 && !myIsComplete)
868   {
869     setAvailableGroups();
870   }
871   else if( theIndex==2 && !myIsComplete)
872   {
873     setAvailableBoundaryPolygons();
874   }
875   else if( theIndex==3 && !myIsComplete)
876   {
877     // Land cover map panel
878      HYDROGUI_CalculationDlg* aPanel = 
879       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
880     if ( !aPanel )
881       return;
882
883     setLandCoverMapVisible( true );
884
885     QStringList aList;
886     QStringList anEntryList;
887
888     // Get all land cover map objects to fill in combo-box
889     Handle(HYDROData_LandCoverMap) aLandCoverMapObj;
890     QString aLandCoverMapName;
891
892     aList.clear();
893     anEntryList.clear();
894     HYDROData_Iterator anIter( doc(), KIND_LAND_COVER_MAP );
895     for ( ; anIter.More(); anIter.Next() )
896     {
897       aLandCoverMapObj = Handle(HYDROData_LandCoverMap)::DownCast( anIter.Current() );
898
899       if ( !aLandCoverMapObj.IsNull() )
900       { 
901         aLandCoverMapName = aLandCoverMapObj->GetName();
902         if ( !aLandCoverMapName.isEmpty() )
903         {
904           aList.append( aLandCoverMapName );
905           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj ) );
906         }
907       }
908     }
909     aPanel->setLandCoverMapsNames( aList, anEntryList );
910     aLandCoverMapObj = myEditedObject->GetLandCoverMap();
911     if ( !aList.isEmpty() )
912     {
913       if ( aLandCoverMapObj.IsNull() )
914         aPanel->setLandCoverMap( aList.at( 0 ), false );
915       else if ( myIsEdit )
916         aPanel->setLandCoverMap( aList.at( 0 ), true );
917       else
918         aPanel->setLandCoverMap( aList.at( aList.indexOf( aLandCoverMapObj->GetName() ) ), true );
919     }
920
921     // Get all Strickler table objects to fill in combo-box
922     Handle(HYDROData_StricklerTable) aStricklerTableObj;
923     QString aStricklerTableName;
924
925     aList.clear();
926     anEntryList.clear();
927     anIter = HYDROData_Iterator( doc(), KIND_STRICKLER_TABLE );
928     for ( ; anIter.More(); anIter.Next() )
929     {
930       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
931
932       if ( !aStricklerTableObj.IsNull() )
933       { 
934         aStricklerTableName = aStricklerTableObj->GetName();
935         if ( !aStricklerTableName.isEmpty() )
936         {
937           aList.append( aStricklerTableName );
938           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
939         }        
940       }
941     }
942     aPanel->setStricklerTableNames( aList, anEntryList );
943     //@ASL: bool anUpdateState = myEditedObject->IsMustBeUpdated();
944     if ( !aList.isEmpty() )
945       aPanel->setStricklerTable( aList.at( 0 ), false );
946     //@ASL: myEditedObject->SetToUpdate( anUpdateState );
947
948     if ( !myEditedObject.IsNull() )
949     {
950       if ( myIsEdit )
951       {      
952         // Select the certain Strickler table object in combo-box
953         aStricklerTableObj = myEditedObject->GetStricklerTable();
954         if ( aStricklerTableObj.IsNull() )
955         {
956           aPanel->setStricklerTable( QString() );
957         }
958         else
959         {
960           aStricklerTableName = aStricklerTableObj->GetName();
961           aPanel->setStricklerTable( aStricklerTableName );
962         }
963
964         // Select the certain land cover map object in combo-box
965         if ( aLandCoverMapObj.IsNull() )
966         {
967           aPanel->setLandCoverMap( QString() );
968         }
969         else
970         {
971           aLandCoverMapName = aLandCoverMapObj->GetName();
972           aPanel->setLandCoverMap( aLandCoverMapName );
973         }
974       }
975     }
976
977     closePreview( false );
978     createPreview( true );
979   }
980   else if( theIndex==4 || myIsComplete)
981   {
982     HYDROGUI_CalculationDlg* aPanel = 
983       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
984     if ( !aPanel )
985       return;
986
987     QApplication::setOverrideCursor( Qt::WaitCursor );
988
989     setGeomObjectsVisible( false );
990     setLandCoverMapVisible( false );
991
992     QString aNewCaseName = aPanel->getObjectName();
993     QString anOldCaseName = myEditedObject->GetName();
994     bool isNameChanged = anOldCaseName != aNewCaseName;
995   
996     bool anIsToUpdateOb = isNameChanged;
997
998     // At first we must to update the case name because of 
999     // automatic names generation for regions and zones
1000     myEditedObject->SetName( aNewCaseName );
1001     
1002     // Zones: set parameters for automatic mode
1003     int aMode = aPanel->getMode();
1004     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
1005     {
1006       // Set objects in the specified order
1007       if( myEditedObject->IsMustBeUpdated(  HYDROData_Entity::Geom_2d ) )
1008       {
1009         myEditedObject->RemoveGeometryObjects();
1010         QStringList aGeomObjNames;
1011         if (myIsComplete) //erase objects ?
1012           aGeomObjNames << aPanel->getAlreadyAddedGeomObjects();
1013         aGeomObjNames << aPanel->getAllGeomObjects();
1014         foreach ( const QString& aName, aGeomObjNames )
1015         {
1016           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
1017             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
1018           if ( anObject.IsNull() )
1019           {
1020             continue;
1021           }
1022           myEditedObject->AddGeometryObject( anObject );
1023         }
1024
1025         if (!myIsComplete)
1026         {
1027           // Clear priority rules
1028           //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
1029           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
1030           //@ASL }
1031           // Set priority rules
1032           foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
1033             myEditedObject->AddRule( aRule.Object1, aRule.Priority,
1034               aRule.Object2, aRule.MergeType,
1035               HYDROData_CalculationCase::DataTag_CustomRules );
1036           }
1037         }
1038       }
1039     }
1040     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
1041
1042     if (myIsComplete)
1043       aPanel->BackButton()->setEnabled(false); //force disable; to prevent moving to the first page
1044
1045     if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
1046     {
1047       myShowZones = true;
1048       
1049       if (myIsComplete)
1050       {
1051         //abortDocOperation();
1052         //module()->getApp()->updateObjectBrowser();
1053         QList<Handle(HYDROData_Entity)> newGeomObj = aPanel->getGeometryObjects(false);
1054         //QSet<Handle(HYDROData_Entity)> includedObjAtStartSet = myIncObjAtStart.toSet();
1055         NCollection_Sequence<Handle(HYDROData_Entity)> theNewObjects;
1056         foreach (Handle(HYDROData_Entity) obj, newGeomObj)
1057           theNewObjects.Append(obj);
1058
1059
1060         bool is_int = false;
1061         bool UseOrigNamingOfNewRegions = true;
1062         NCollection_Sequence<Handle(HYDROData_Region)> theNewRegions;
1063         QSet<QString> newRegionEntries;
1064         HYDROData_CompleteCalcCase::AddObjects(doc(), myEditedObject, theNewObjects, UseOrigNamingOfNewRegions, is_int, theNewRegions);
1065         if (is_int)        
1066           SUIT_MessageBox::information(module()->getApp()->desktop(),
1067           tr( "COMPLETE_CASE" ),
1068           tr( "COMPLETE_CASE_INTERSECTION_DETECTED" ));
1069
1070         myEditedObject->ClearChanged();
1071         for (int k=1;k<=theNewRegions.Size();k++)
1072         {
1073           QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theNewRegions(k) );
1074           newRegionEntries.insert(anEntry);
1075         }
1076         AssignDefaultZonesColors(&newRegionEntries);
1077       }
1078       else
1079       {
1080         myEditedObject->Update();
1081         AssignDefaultZonesColors(NULL);
1082       }
1083       
1084
1085       //aPanel->setEditedObject( myEditedObject );
1086       aPanel->refreshZonesBrowser();
1087     
1088       closePreview( false );
1089       createPreview( false );
1090
1091       anIsToUpdateOb = true;
1092     }
1093     else
1094     {
1095       // Show zones
1096       setZonesVisible( true );
1097
1098       if ( isNameChanged ) {
1099         module()->getDataModel()->updateObjectTree( myEditedObject );
1100       }
1101     }
1102
1103     QApplication::restoreOverrideCursor();
1104   }  
1105 }
1106
1107 void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
1108 {
1109   if (myIsComplete)
1110   {
1111     //theIndex == 0
1112   //  setGeomObjectsVisible( true );
1113   // // HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1114   // // QList<Handle(HYDROData_Entity)> geomObjList =  aPanel->getGeometryObjects(false);
1115   // // foreach ( Handle(HYDROData_Entity) geomObj, geomObjList)
1116   // //   setObjectVisibility( geomObj, true );
1117   //
1118   //  setZonesVisible( false );
1119   //  closePreview( false );
1120   //  createPreview( false );
1121   }
1122   else
1123   {
1124     if( theIndex==1 )
1125     {
1126       setGeomObjectsVisible( true );
1127
1128       closePreview( false );
1129       createPreview( false );
1130     }
1131     if( theIndex==3 )
1132     {
1133       setLandCoverMapVisible( true );
1134
1135       closePreview( false );
1136       createPreview( true );
1137
1138       // Hide zones
1139       setZonesVisible( false );
1140     }
1141     else if( theIndex==4 )
1142     {
1143       AssignDefaultZonesColors();
1144
1145       closePreview( false );
1146       createPreview( false );
1147
1148       // Show zones
1149       setZonesVisible( true );
1150     }
1151   }
1152 }
1153
1154 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
1155 {
1156   myShowZones = theIsVisible;
1157   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1158   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1159   HYDROData_SequenceOfObjects aZones;
1160   Handle(HYDROData_Region) aRegion;
1161   if ( myPreviewViewManager ) 
1162   {
1163     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1164     {
1165       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1166       if ( !aCtx.IsNull() )
1167       {
1168         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1169         {
1170           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1171           if ( !aRegion.IsNull() )
1172           {
1173             aZones = aRegion->GetZones();
1174             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1175             for ( ; aZonesIter.More(); aZonesIter.Next() )
1176             {
1177               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1178             }
1179           }
1180         }
1181       }
1182
1183       module()->update( UF_OCCViewer );
1184     }
1185   }
1186 }
1187
1188 void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
1189 {
1190   myShowGeomObjects = theIsVisible;
1191
1192   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1193
1194   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1195   for ( ; anIter.More(); anIter.Next() ) {
1196     setObjectVisibility( anIter.Value(), theIsVisible );
1197   }
1198 }
1199
1200 void HYDROGUI_CalculationOp::setLandCoverMapVisible( bool theIsVisible )
1201 {
1202   myShowLandCoverMap = theIsVisible;
1203   setObjectVisibility( myEditedObject->GetLandCoverMap(), theIsVisible );  
1204 }
1205
1206 void HYDROGUI_CalculationOp::AssignDefaultZonesColors(const QSet<QString>* theRegionsEntriesToColored)
1207 {
1208   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1209   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1210   HYDROData_SequenceOfObjects aZones;
1211   Handle(HYDROData_Region) aRegion;
1212   if ( myPreviewViewManager ) 
1213   {
1214     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1215     {
1216       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1217       if ( !aCtx.IsNull() )
1218       {     
1219         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1220         {
1221           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1222           QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aRegion );
1223
1224           if (theRegionsEntriesToColored && !theRegionsEntriesToColored->contains(anEntry))
1225             continue;
1226
1227           if ( !aRegion.IsNull() )
1228           {
1229             aZones = aRegion->GetZones();
1230             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1231             for ( ; aZonesIter.More(); aZonesIter.Next() )
1232             {
1233               // Zone
1234               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1235               if ( !aZone.IsNull() )
1236               {
1237                 QColor aFillingColor = HYDROData_Tool::GenerateRandColor();
1238                 while (aFillingColor == Qt::red)
1239                   aFillingColor = HYDROData_Tool::GenerateRandColor();
1240                 
1241                 aZone->SetColor(aFillingColor);
1242               }
1243             }
1244           }
1245         }
1246       }
1247     }
1248   }
1249 }
1250
1251 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1252 {
1253   HYDROGUI_CalculationDlg* aPanel = 
1254     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1255   if ( !aPanel )
1256     return;
1257
1258   HYDROData_ListOfRules aRules;
1259   Handle(HYDROData_Entity) anObject1, anObject2;
1260   HYDROData_PriorityType aPriority;
1261   HYDROData_Zone::MergeType aMergeType;
1262   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1263   for ( int anIndex = 0; ; anIndex++ )
1264   {
1265     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1266       HYDROData_CustomRule aRule;
1267       aRule.Object1 = anObject1;
1268       aRule.Object2 = anObject2;
1269       aRule.Priority = aPriority;
1270       aRule.MergeType = aMergeType;
1271
1272       aRules << aRule;
1273     }
1274     else
1275       break;
1276   }
1277
1278   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1279     aPanel->setRules( aRules );  
1280 }
1281
1282 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList )
1283 {
1284   bool aRetValue = false;
1285
1286   QList<HYDROGUI_Zone*> aZonesList;
1287   HYDROGUI_Zone* aZone;
1288   // Get a list of dropped zones
1289   for ( int i = 0; i < theZonesList.length(); i++ )
1290   {
1291     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1292     if ( aZone )
1293     {
1294       aZonesList.append( aZone );
1295     }
1296   }
1297   if ( aZonesList.length() > 0 )
1298   {
1299     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
1300     aRetValue = true;
1301   }
1302    
1303   return aRetValue;
1304 }
1305
1306 void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap, bool fitAllFlag, bool onTopViewAndFit )
1307 {
1308   DEBTRACE("createPreview " << theLandCoverMap << " " << fitAllFlag << " " << onTopViewAndFit);
1309   LightApp_Application* anApp = module()->getApp();
1310   HYDROData_SequenceOfObjects aSeq;
1311   if ( theLandCoverMap && myShowLandCoverMap )
1312   {
1313     aSeq.Append( myEditedObject->GetLandCoverMap() );
1314   }
1315   else if ( !theLandCoverMap && myShowGeomObjects )
1316   {
1317     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1318     HYDROData_SequenceOfObjects aSeqP = myEditedObject->GetInterPolyObjects();
1319     aSeq.Append( aSeqGO );
1320     aSeq.Append( aSeqP );
1321   }
1322
1323   Handle(HYDROData_Entity) anEntity;
1324
1325   if ( myShowZones )
1326   {
1327     // Gather zones for displaying
1328     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1329     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1330     HYDROData_SequenceOfObjects aZones;
1331     Handle(HYDROData_Region) aRegion;
1332     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1333     {
1334       anEntity = aRegionsIter.Value();
1335       if ( !anEntity.IsNull() )
1336       {
1337         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1338         if ( !aRegion.IsNull() )
1339         {
1340           aZones = aRegion->GetZones();
1341           aSeq.Append( aZones );
1342         }
1343       }
1344     }
1345   }
1346
1347   // Get a boundary polyline if any
1348   if (Handle(HYDROData_PolylineXY) aBPoly = myEditedObject->GetBoundaryPolyline())
1349     aSeq.Append( aBPoly );
1350
1351   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1352
1353   if ( !myActiveViewManager )
1354   {
1355     if ( aSeq.IsEmpty() )
1356       return;
1357
1358     myActiveViewManager = anApp->activeViewManager();
1359   }
1360
1361   if ( !myPreviewViewManager )
1362   {
1363     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1364       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1365     DEBTRACE("  createViewManager " << myPreviewViewManager);
1366     if ( myPreviewViewManager )
1367     {
1368       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1369                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1370
1371       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1372       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1373     }
1374   }
1375
1376   if ( !myPreviewViewManager )
1377     return;
1378   restoreOCCViewerSelection(myPreviewViewManager);
1379
1380   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1381   {
1382     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1383     if ( !aCtx.IsNull() )
1384     {
1385       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1386       for ( ; anIter.More(); anIter.Next() )
1387       {
1388         const Handle(HYDROData_Entity)& ent = anIter.Value();
1389         if (!ent.IsNull())
1390           setObjectVisibility( ent, true );
1391       }
1392
1393       //Process the draw events for viewer
1394       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1395     }
1396
1397     int UpdateFlags = UF_OCCViewer;
1398     if (fitAllFlag)
1399       UpdateFlags |= UF_FitAll;
1400     module()->update( UpdateFlags );
1401
1402     OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
1403     if ( onTopViewAndFit && vw )
1404       vw->onTopView();
1405   }
1406 }
1407
1408 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1409 {
1410   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1411     return;
1412   }
1413
1414   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1415   if ( aViewer ) {
1416     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1417   }
1418 }
1419
1420 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1421 {
1422   closePreview();
1423 }
1424
1425 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1426 {
1427   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1428   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1429   QShortcut* aShortcut;
1430   foreach( aShortcut, aShortcuts )
1431   {
1432     if ( aShortcut->key() == 
1433       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1434       SUIT_DataBrowser::RenameShortcut ) ) )
1435     {
1436       aShortcut->setEnabled( true );
1437     }
1438   }
1439
1440
1441   if( myPreviewViewManager )
1442   {
1443     // Hide all the displayed objects in the preview view
1444     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1445     if ( aViewer ) {
1446       size_t aViewId = (size_t)aViewer;
1447       HYDROData_Iterator anIterator( doc() );
1448       for( ; anIterator.More(); anIterator.Next() ) {
1449         Handle(HYDROData_Entity) anObject = anIterator.Current();
1450         if( !anObject.IsNull() ) {
1451           module()->setObjectVisible( aViewId, anObject, false );
1452         }
1453       }
1454     }
1455
1456     if ( theRemoveViewManager )
1457     {
1458       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1459                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1460
1461       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1462       myPreviewViewManager = NULL;
1463     }
1464   }
1465
1466   if( myActiveViewManager && theRemoveViewManager )
1467   {
1468     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1469     myActiveViewManager = NULL;
1470   }
1471 }
1472
1473 void HYDROGUI_CalculationOp::setAvailableGroups()
1474 {
1475   HYDROGUI_CalculationDlg* aPanel = 
1476       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1477
1478   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1479   QStringList aList, anEntryList;
1480   getNamesAndEntries( aSeq, aList, anEntryList );
1481
1482   QStringList aGroupsNames;
1483
1484   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1485   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1486   {
1487     Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( anObjs.Value( anIndex ) );
1488     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1489     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1490     {
1491       Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( aGIndex ) );
1492       aGroupsNames.append( aGroup->GetName() );
1493     }
1494   }
1495   if( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) ) {
1496     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1497       Handle(HYDROData_ShapesGroup) aGeomGroup =
1498         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1499       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1500         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1501       }
1502     }
1503   }
1504
1505   aPanel->setAvailableGroups( aGroupsNames );
1506   aPanel->includeGroups( aList );
1507
1508   //@ASL: bool isUpdated = myEditedObject->IsMustBeUpdated();
1509 }
1510
1511 void HYDROGUI_CalculationOp::onAddGroups()
1512 {
1513   HYDROGUI_CalculationDlg* aPanel = 
1514     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1515   if ( !aPanel )
1516     return;
1517
1518   // Add geometry objects selected in the module browser to the calculation case
1519   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1520   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1521     return;
1522
1523   QStringList anAddedList;
1524   for (int i = 0; i < aSelectedList.length(); i++)
1525   {
1526     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1527       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1528     if ( aGroup.IsNull() )
1529       continue;
1530
1531     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1532       anAddedList.append( aGroup->GetName() );
1533   }
1534
1535   if ( !anAddedList.isEmpty() )
1536   {
1537     aPanel->includeGroups( anAddedList );
1538   }
1539 }
1540
1541 void HYDROGUI_CalculationOp::onRemoveGroups()
1542 {
1543   // Remove selected objects from the calculation case
1544   HYDROGUI_CalculationDlg* aPanel = 
1545     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1546   if ( !aPanel )
1547     return;
1548
1549   QStringList aSelectedList = aPanel->getSelectedGroups();
1550   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1551     return;
1552
1553   for (int i = 0; i < aSelectedList.length(); i++)
1554   {
1555     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1556       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1557     if ( aGroup.IsNull() )
1558       continue;
1559
1560     myEditedObject->RemoveGeometryGroup( aGroup );
1561   }
1562
1563   aPanel->excludeGroups( aSelectedList );
1564 }
1565
1566
1567 void HYDROGUI_CalculationOp::setAvailableBoundaryPolygons()
1568 {
1569   HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1570
1571   HYDROData_Iterator anIter( doc(), KIND_BC_POLYGON );
1572   Handle(HYDROData_BCPolygon) aBCP;
1573   QStringList aNames;
1574   QVector<int> aTypes; 
1575   for (;anIter.More(); anIter.Next())
1576   {
1577     aBCP = Handle(HYDROData_BCPolygon)::DownCast( anIter.Current() );
1578     aNames.append(aBCP->GetName());
1579     aTypes.append(aBCP->GetBoundaryType());
1580   }
1581   HYDROData_SequenceOfObjects aBCPSeq = myEditedObject->GetBoundaryPolygons();
1582
1583   QStringList aListCut, aListIS;
1584   for (int i=1; i<=aBCPSeq.Size();i++)
1585   {
1586     int type = Handle(HYDROData_BCPolygon)::DownCast(aBCPSeq(i))->GetBoundaryType();
1587     if (type == 1)
1588       aListCut << aBCPSeq(i)->GetName();
1589     else if (type == 2 || type == 3)
1590       aListIS << aBCPSeq(i)->GetName();
1591   }
1592
1593   aPanel->setAvailableBoundaryPolygons( aNames, aTypes );
1594   aPanel->includeBoundaryPolygons( aListCut );
1595   aPanel->includeISBoundaryPolygons( aListIS );
1596 }
1597
1598 void HYDROGUI_CalculationOp::onRemoveBoundaryPolygons()
1599 {
1600   HYDROGUI_CalculationDlg* aPanel = 
1601     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1602   if ( !aPanel )
1603     return;
1604
1605   QStringList aSelectedListCut = aPanel->getSelectedBoundaryPolygons();
1606   QStringList aSelectedListIS = aPanel->getSelectedISBoundaryPolygons();
1607
1608   if ( aSelectedListCut.isEmpty() && aSelectedListIS.isEmpty() )
1609     return;
1610
1611   for (int i = 0; i < aSelectedListCut.length(); i++)
1612   {
1613     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1614       HYDROGUI_Tool::FindObjectByName( module(), aSelectedListCut.at(i) ) );
1615     if ( aBCPoly.IsNull() )
1616       continue;
1617     myEditedObject->RemoveBoundaryPolygon( aBCPoly );
1618   }
1619
1620   for (int i = 0; i < aSelectedListIS.length(); i++)
1621   {
1622     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1623       HYDROGUI_Tool::FindObjectByName( module(), aSelectedListIS.at(i) ) );
1624     if ( aBCPoly.IsNull() )
1625       continue;
1626     myEditedObject->RemoveBoundaryPolygon( aBCPoly );
1627   }
1628
1629   aPanel->excludeBoundaryPolygons( aSelectedListCut );
1630   aPanel->excludeISBoundaryPolygons( aSelectedListIS );
1631 }
1632
1633  
1634 void HYDROGUI_CalculationOp::onAddBoundaryPolygons()
1635 {
1636   HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1637   if ( !aPanel )
1638     return;
1639
1640   QStringList aSelectedList = aPanel->getSelectedAvailableBoundaryPolygons();
1641   if ( aSelectedList.isEmpty())
1642     return;
1643
1644   QStringList anAddedListCut, anAddedListIS;
1645   for (int i = 0; i < aSelectedList.length(); i++)
1646   {
1647     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1648       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1649     if ( aBCPoly.IsNull() )
1650       continue;
1651    
1652    if ( myEditedObject->AddBoundaryPolygon( aBCPoly ) )
1653    {
1654      QString aName = aBCPoly->GetName();
1655      int type = aBCPoly->GetBoundaryType(); 
1656      if (type == 1)
1657        anAddedListCut.append(aName);
1658      else if (type == 2 || type == 3)
1659        anAddedListIS.append(aName);
1660    }
1661
1662   }
1663
1664   if ( !anAddedListCut.isEmpty() )
1665     aPanel->includeBoundaryPolygons(anAddedListCut);
1666   if ( !anAddedListIS.isEmpty() )
1667     aPanel->includeISBoundaryPolygons(anAddedListIS);
1668 }
1669
1670 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1671 {
1672   HYDROGUI_CalculationDlg* aPanel = 
1673     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1674   if ( !aPanel )
1675     return;
1676
1677   if ( !confirmModeChange() ) {
1678     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1679     return;
1680   }
1681
1682   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1683   aPanel->setMode( theMode );
1684 }
1685
1686 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1687 {
1688   HYDROGUI_CalculationDlg* aPanel = 
1689     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1690   if ( !aPanel )
1691     return;
1692
1693   isConfirmed = confirmOrderChange();
1694   if( isConfirmed )
1695     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1696 }
1697
1698 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1699 {
1700   HYDROGUI_CalculationDlg* aPanel = 
1701     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1702   if ( !aPanel )
1703     return;
1704
1705   isConfirmed = confirmRuleChange();
1706   if( isConfirmed )
1707     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1708 }
1709
1710 void HYDROGUI_CalculationOp::onRegenerateColors()
1711 {
1712   // For geometry zones
1713   AssignDefaultZonesColors();
1714   setZonesVisible( false );
1715   setZonesVisible( true );
1716 }