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