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