Salome HOME
Still an error on ZLayers related to edit CalculationCase The call to createViewManag...
[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   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
607     return;
608
609   for (int i = 0; i < aSelectedList.length(); i++)
610   {
611     Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
612       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
613     if ( anObject.IsNull() )
614       continue;
615
616     setObjectVisibility( anObject, false );
617     myEditedObject->RemoveGeometryObject( anObject );
618   }
619
620   for (int i = 0; i < aSelectedList.length(); i++)
621   {
622     Handle(HYDROData_PolylineXY) aPoly = Handle(HYDROData_PolylineXY)::DownCast( 
623       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
624     if ( aPoly.IsNull() )
625       continue;
626
627     setObjectVisibility( aPoly, false );
628     myEditedObject->RemoveInterPolyObject ( aPoly ); 
629   }
630
631   module()->update( UF_OCCViewer );
632   aPanel->excludeGeomObjects( aSelectedList );
633 }
634
635 bool HYDROGUI_CalculationOp::confirmRegionsChange() const
636 {
637
638   // Check if the case is already modified or not
639   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
640
641   if ( !isConfirmed )
642   {
643   
644    if (myIsComplete)
645    {
646      //SUIT_MessageBox::information(module()->getApp()->desktop(),
647      //                         tr( "REGIONS_CHANGED" ),
648      //                         tr( "COMPLETE_OP_WILL_BE_PERFORMED" ));
649      return true;
650    }
651
652     // If not modified check if the case has already defined regions with zones
653     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
654     if ( aSeq.Length() > 0 )
655     {
656       // If there are already defined zones then ask a user to confirm zones recalculation
657       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
658                                tr( "REGIONS_CHANGED" ),
659                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
660                                QMessageBox::Yes | QMessageBox::No,
661                                QMessageBox::No ) == QMessageBox::Yes );
662     }
663     else
664     {
665       isConfirmed = true; // No regions - no zones - nothing to recalculate
666     }
667   }
668   return isConfirmed;
669 }
670
671 bool HYDROGUI_CalculationOp::confirmOrderChange() const
672 {
673   // Check if the case is already modified or not
674   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
675   if ( !isConfirmed )
676   {
677     // If not modified check if the case has already defined regions with zones
678     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
679     if ( aSeq.Length() > 0 )
680     {
681       // If there are already defined zones then ask a user to confirm zones recalculation
682       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
683                                tr( "ORDER_CHANGED" ),
684                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
685                                QMessageBox::Yes | QMessageBox::No,
686                                QMessageBox::No ) == QMessageBox::Yes );
687     }
688     else
689     {
690       isConfirmed = true; // No regions - no zones - nothing to recalculate
691     }
692   }
693   return isConfirmed;
694 }
695
696 bool HYDROGUI_CalculationOp::confirmRuleChange() const
697 {
698   // Check if the case is already modified or not
699   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
700   if ( !isConfirmed )
701   {
702     // If not modified check if the case has already defined regions with zones
703     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
704     if ( aSeq.Length() > 0 )
705     {
706       // If there are already defined zones then ask a user to confirm zones recalculation
707       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
708                                tr( "RULE_CHANGED" ),
709                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
710                                QMessageBox::Yes | QMessageBox::No,
711                                QMessageBox::No ) == QMessageBox::Yes );
712     }
713     else
714     {
715       isConfirmed = true; // No regions - no zones - nothing to recalculate
716     }
717   }
718   return isConfirmed;
719 }
720
721 bool HYDROGUI_CalculationOp::confirmModeChange() const
722 {
723   // Check if the case is already modified or not
724   bool isConfirmed = myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d );
725   if ( !isConfirmed )
726   {
727     // If not modified check if the case has already defined regions with zones
728     HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions();
729     if ( aSeq.Length() > 0 )
730     {
731       // If there are already defined zones then ask a user to confirm zones recalculation
732       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
733                                tr( "MODE_CHANGED" ),
734                                tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
735                                QMessageBox::Yes | QMessageBox::No,
736                                QMessageBox::No ) == QMessageBox::Yes );
737     }
738     else
739     {
740       isConfirmed = true; // No regions - no zones - nothing to recalculate
741     }
742   }
743   return isConfirmed;
744 }
745
746 bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
747 {
748   HYDROData_WarningType aType = theWarning.Type;
749   if ( aType == WARN_OK ) {
750     return true;
751   }
752
753   QString aTitle;
754   QString aMsg;
755   switch ( aType )
756   {
757     case WARN_EMPTY_REGIONS:
758       aTitle = tr( "EMPTY_REGIONS" );
759       aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
760       break;
761     default:
762       aTitle = tr( "WARNING" );
763       aMsg = theWarning.Data;
764   }
765
766
767   int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
768                                            aTitle, aMsg,
769                                            QMessageBox::Yes | QMessageBox::No,
770                                            QMessageBox::No );
771
772   return ( anAnswer == QMessageBox::Yes );
773 }
774
775 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
776                                            QString& theErrorMsg,
777                                            QStringList& theBrowseObjectsEntries )
778 {
779   HYDROGUI_CalculationDlg* aPanel = 
780     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
781   if ( !aPanel )
782     return false;
783
784   if( !myIsEdit )
785   {
786     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
787     theBrowseObjectsEntries.append( anEntry );
788   }
789
790   // For manual mode priority rules are redundant
791   if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
792     myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, false );
793   }
794  
795   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
796
797   return true;
798 }
799
800 void HYDROGUI_CalculationOp::onApply()
801 {
802   // Check warnings
803   HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
804   if ( aWarning.Type != WARN_OK ) {
805     if ( !confirmContinueWithWarning( aWarning ) ) {
806       // Go back to the first page
807       HYDROGUI_CalculationDlg* aPanel = 
808         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
809       if ( aPanel ) {
810         aPanel->onFirstPage();
811       }
812       return;
813     }
814   }
815
816   QApplication::setOverrideCursor( Qt::WaitCursor );
817
818   int anUpdateFlags = 0;
819   QString anErrorMsg;
820   QStringList aBrowseObjectsEntries;
821
822   bool aResult = false;
823   
824   try
825   {
826     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
827   }
828   catch ( Standard_Failure& e )
829   {
830     anErrorMsg = e.GetMessageString();
831     aResult = false;
832   }
833   catch ( ... )
834   {
835     aResult = false;
836   }
837   
838   QApplication::restoreOverrideCursor();
839
840   if ( aResult )
841   {
842     module()->update( anUpdateFlags );
843     commit();
844     browseObjects( aBrowseObjectsEntries );
845   }
846   else
847   {
848     abort();
849     QString aMsg = tr( "INPUT_VALID_DATA" );
850     if( !anErrorMsg.isEmpty() )
851       aMsg.prepend( anErrorMsg + "\n" );
852     SUIT_MessageBox::critical( module()->getApp()->desktop(),
853                                tr( "INSUFFICIENT_INPUT_DATA" ),
854                                aMsg ); 
855   }
856 }
857 #include <HYDROData_CompleteCalcCase.h>
858 void HYDROGUI_CalculationOp::onNext( const int theIndex )
859 {
860   if( theIndex==1 && !myIsComplete)
861   {
862     setAvailableGroups();
863   }
864   else if( theIndex==2 && !myIsComplete)
865   {
866     setAvailableBoundaryPolygons();
867   }
868   else if( theIndex==3 && !myIsComplete)
869   {
870     // Land cover map panel
871      HYDROGUI_CalculationDlg* aPanel = 
872       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
873     if ( !aPanel )
874       return;
875
876     setLandCoverMapVisible( true );
877
878     QStringList aList;
879     QStringList anEntryList;
880
881     // Get all land cover map objects to fill in combo-box
882     Handle(HYDROData_LandCoverMap) aLandCoverMapObj;
883     QString aLandCoverMapName;
884
885     aList.clear();
886     anEntryList.clear();
887     HYDROData_Iterator anIter( doc(), KIND_LAND_COVER_MAP );
888     for ( ; anIter.More(); anIter.Next() )
889     {
890       aLandCoverMapObj = Handle(HYDROData_LandCoverMap)::DownCast( anIter.Current() );
891
892       if ( !aLandCoverMapObj.IsNull() )
893       { 
894         aLandCoverMapName = aLandCoverMapObj->GetName();
895         if ( !aLandCoverMapName.isEmpty() )
896         {
897           aList.append( aLandCoverMapName );
898           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj ) );
899         }
900       }
901     }
902     aPanel->setLandCoverMapsNames( aList, anEntryList );
903     aLandCoverMapObj = myEditedObject->GetLandCoverMap();
904     if ( !aList.isEmpty() )
905     {
906       if ( aLandCoverMapObj.IsNull() )
907         aPanel->setLandCoverMap( aList.at( 0 ), false );
908       else if ( myIsEdit )
909         aPanel->setLandCoverMap( aList.at( 0 ), true );
910       else
911         aPanel->setLandCoverMap( aList.at( aList.indexOf( aLandCoverMapObj->GetName() ) ), true );
912     }
913
914     // Get all Strickler table objects to fill in combo-box
915     Handle(HYDROData_StricklerTable) aStricklerTableObj;
916     QString aStricklerTableName;
917
918     aList.clear();
919     anEntryList.clear();
920     anIter = HYDROData_Iterator( doc(), KIND_STRICKLER_TABLE );
921     for ( ; anIter.More(); anIter.Next() )
922     {
923       aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIter.Current() );
924
925       if ( !aStricklerTableObj.IsNull() )
926       { 
927         aStricklerTableName = aStricklerTableObj->GetName();
928         if ( !aStricklerTableName.isEmpty() )
929         {
930           aList.append( aStricklerTableName );
931           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aStricklerTableObj ) );
932         }        
933       }
934     }
935     aPanel->setStricklerTableNames( aList, anEntryList );
936     //@ASL: bool anUpdateState = myEditedObject->IsMustBeUpdated();
937     if ( !aList.isEmpty() )
938       aPanel->setStricklerTable( aList.at( 0 ), false );
939     //@ASL: myEditedObject->SetToUpdate( anUpdateState );
940
941     if ( !myEditedObject.IsNull() )
942     {
943       if ( myIsEdit )
944       {      
945         // Select the certain Strickler table object in combo-box
946         aStricklerTableObj = myEditedObject->GetStricklerTable();
947         if ( aStricklerTableObj.IsNull() )
948         {
949           aPanel->setStricklerTable( QString() );
950         }
951         else
952         {
953           aStricklerTableName = aStricklerTableObj->GetName();
954           aPanel->setStricklerTable( aStricklerTableName );
955         }
956
957         // Select the certain land cover map object in combo-box
958         if ( aLandCoverMapObj.IsNull() )
959         {
960           aPanel->setLandCoverMap( QString() );
961         }
962         else
963         {
964           aLandCoverMapName = aLandCoverMapObj->GetName();
965           aPanel->setLandCoverMap( aLandCoverMapName );
966         }
967       }
968     }
969
970     closePreview( false );
971     createPreview( true );
972   }
973   else if( theIndex==4 || myIsComplete)
974   {
975     HYDROGUI_CalculationDlg* aPanel = 
976       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
977     if ( !aPanel )
978       return;
979
980     QApplication::setOverrideCursor( Qt::WaitCursor );
981
982     setGeomObjectsVisible( false );
983     setLandCoverMapVisible( false );
984
985     QString aNewCaseName = aPanel->getObjectName();
986     QString anOldCaseName = myEditedObject->GetName();
987     bool isNameChanged = anOldCaseName != aNewCaseName;
988   
989     bool anIsToUpdateOb = isNameChanged;
990
991     // At first we must to update the case name because of 
992     // automatic names generation for regions and zones
993     myEditedObject->SetName( aNewCaseName );
994     
995     // Zones: set parameters for automatic mode
996     int aMode = aPanel->getMode();
997     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
998     {
999       // Set objects in the specified order
1000       if( myEditedObject->IsMustBeUpdated(  HYDROData_Entity::Geom_2d ) )
1001       {
1002         myEditedObject->RemoveGeometryObjects();
1003         QStringList aGeomObjNames;
1004         if (myIsComplete) //erase objects ?
1005           aGeomObjNames << aPanel->getAlreadyAddedGeomObjects();
1006         aGeomObjNames << aPanel->getAllGeomObjects();
1007         foreach ( const QString& aName, aGeomObjNames )
1008         {
1009           Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
1010             HYDROGUI_Tool::FindObjectByName( module(), aName ) );
1011           if ( anObject.IsNull() )
1012           {
1013             continue;
1014           }
1015           myEditedObject->AddGeometryObject( anObject );
1016         }
1017
1018         if (!myIsComplete)
1019         {
1020           // Clear priority rules
1021           //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
1022           myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomRules, true );
1023           //@ASL }
1024           // Set priority rules
1025           foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
1026             myEditedObject->AddRule( aRule.Object1, aRule.Priority,
1027               aRule.Object2, aRule.MergeType,
1028               HYDROData_CalculationCase::DataTag_CustomRules );
1029           }
1030         }
1031       }
1032     }
1033     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
1034
1035     if (myIsComplete)
1036       aPanel->BackButton()->setEnabled(false); //force disable; to prevent moving to the first page
1037
1038     if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
1039     {
1040       myShowZones = true;
1041       
1042       if (myIsComplete)
1043       {
1044         //abortDocOperation();
1045         //module()->getApp()->updateObjectBrowser();
1046         QList<Handle(HYDROData_Entity)> newGeomObj = aPanel->getGeometryObjects(false);
1047         //QSet<Handle(HYDROData_Entity)> includedObjAtStartSet = myIncObjAtStart.toSet();
1048         NCollection_Sequence<Handle(HYDROData_Entity)> theNewObjects;
1049         foreach (Handle(HYDROData_Entity) obj, newGeomObj)
1050           theNewObjects.Append(obj);
1051
1052
1053         bool is_int = false;
1054         bool UseOrigNamingOfNewRegions = true;
1055         NCollection_Sequence<Handle(HYDROData_Region)> theNewRegions;
1056         QSet<QString> newRegionEntries;
1057         HYDROData_CompleteCalcCase::AddObjects(doc(), myEditedObject, theNewObjects, UseOrigNamingOfNewRegions, is_int, theNewRegions);
1058         if (is_int)        
1059           SUIT_MessageBox::information(module()->getApp()->desktop(),
1060           tr( "COMPLETE_CASE" ),
1061           tr( "COMPLETE_CASE_INTERSECTION_DETECTED" ));
1062
1063         myEditedObject->ClearChanged();
1064         for (int k=1;k<=theNewRegions.Size();k++)
1065         {
1066           QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theNewRegions(k) );
1067           newRegionEntries.insert(anEntry);
1068         }
1069         AssignDefaultZonesColors(&newRegionEntries);
1070       }
1071       else
1072       {
1073         myEditedObject->Update();
1074         AssignDefaultZonesColors(NULL);
1075       }
1076       
1077
1078       //aPanel->setEditedObject( myEditedObject );
1079       aPanel->refreshZonesBrowser();
1080     
1081       closePreview( false );
1082       createPreview( false );
1083
1084       anIsToUpdateOb = true;
1085     }
1086     else
1087     {
1088       // Show zones
1089       setZonesVisible( true );
1090
1091       if ( isNameChanged ) {
1092         module()->getDataModel()->updateObjectTree( myEditedObject );
1093       }
1094     }
1095
1096     QApplication::restoreOverrideCursor();
1097   }  
1098 }
1099
1100 void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
1101 {
1102   if (myIsComplete)
1103   {
1104     //theIndex == 0
1105   //  setGeomObjectsVisible( true );
1106   // // HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1107   // // QList<Handle(HYDROData_Entity)> geomObjList =  aPanel->getGeometryObjects(false);
1108   // // foreach ( Handle(HYDROData_Entity) geomObj, geomObjList)
1109   // //   setObjectVisibility( geomObj, true );
1110   //
1111   //  setZonesVisible( false );
1112   //  closePreview( false );
1113   //  createPreview( false );
1114   }
1115   else
1116   {
1117     if( theIndex==1 )
1118     {
1119       setGeomObjectsVisible( true );
1120
1121       closePreview( false );
1122       createPreview( false );
1123     }
1124     if( theIndex==3 )
1125     {
1126       setLandCoverMapVisible( true );
1127
1128       closePreview( false );
1129       createPreview( true );
1130
1131       // Hide zones
1132       setZonesVisible( false );
1133     }
1134     else if( theIndex==4 )
1135     {
1136       AssignDefaultZonesColors();
1137
1138       closePreview( false );
1139       createPreview( false );
1140
1141       // Show zones
1142       setZonesVisible( true );
1143     }
1144   }
1145 }
1146
1147 void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
1148 {
1149   myShowZones = theIsVisible;
1150   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1151   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1152   HYDROData_SequenceOfObjects aZones;
1153   Handle(HYDROData_Region) aRegion;
1154   if ( myPreviewViewManager ) 
1155   {
1156     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1157     {
1158       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1159       if ( !aCtx.IsNull() )
1160       {
1161         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1162         {
1163           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1164           if ( !aRegion.IsNull() )
1165           {
1166             aZones = aRegion->GetZones();
1167             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1168             for ( ; aZonesIter.More(); aZonesIter.Next() )
1169             {
1170               setObjectVisibility( aZonesIter.Value(), theIsVisible );
1171             }
1172           }
1173         }
1174       }
1175
1176       module()->update( UF_OCCViewer );
1177     }
1178   }
1179 }
1180
1181 void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
1182 {
1183   myShowGeomObjects = theIsVisible;
1184
1185   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
1186
1187   HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1188   for ( ; anIter.More(); anIter.Next() ) {
1189     setObjectVisibility( anIter.Value(), theIsVisible );
1190   }
1191 }
1192
1193 void HYDROGUI_CalculationOp::setLandCoverMapVisible( bool theIsVisible )
1194 {
1195   myShowLandCoverMap = theIsVisible;
1196   setObjectVisibility( myEditedObject->GetLandCoverMap(), theIsVisible );  
1197 }
1198
1199 void HYDROGUI_CalculationOp::AssignDefaultZonesColors(const QSet<QString>* theRegionsEntriesToColored)
1200 {
1201   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1202   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1203   HYDROData_SequenceOfObjects aZones;
1204   Handle(HYDROData_Region) aRegion;
1205   if ( myPreviewViewManager ) 
1206   {
1207     if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1208     {
1209       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1210       if ( !aCtx.IsNull() )
1211       {     
1212         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1213         {
1214           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
1215           QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aRegion );
1216
1217           if (theRegionsEntriesToColored && !theRegionsEntriesToColored->contains(anEntry))
1218             continue;
1219
1220           if ( !aRegion.IsNull() )
1221           {
1222             aZones = aRegion->GetZones();
1223             HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1224             for ( ; aZonesIter.More(); aZonesIter.Next() )
1225             {
1226               // Zone
1227               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1228               if ( !aZone.IsNull() )
1229               {
1230                 QColor aFillingColor = HYDROData_Tool::GenerateRandColor();
1231                 while (aFillingColor == Qt::red)
1232                   aFillingColor = HYDROData_Tool::GenerateRandColor();
1233                 
1234                 aZone->SetColor(aFillingColor);
1235               }
1236             }
1237           }
1238         }
1239       }
1240     }
1241   }
1242 }
1243
1244 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
1245 {
1246   HYDROGUI_CalculationDlg* aPanel = 
1247     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1248   if ( !aPanel )
1249     return;
1250
1251   HYDROData_ListOfRules aRules;
1252   Handle(HYDROData_Entity) anObject1, anObject2;
1253   HYDROData_PriorityType aPriority;
1254   HYDROData_Zone::MergeType aMergeType;
1255   HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
1256   for ( int anIndex = 0; ; anIndex++ )
1257   {
1258     if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType, theDataTag ) ) {
1259       HYDROData_CustomRule aRule;
1260       aRule.Object1 = anObject1;
1261       aRule.Object2 = anObject2;
1262       aRule.Priority = aPriority;
1263       aRule.MergeType = aMergeType;
1264
1265       aRules << aRule;
1266     }
1267     else
1268       break;
1269   }
1270
1271   if ( theDataTag == HYDROData_CalculationCase::DataTag_CustomRules )
1272     aPanel->setRules( aRules );  
1273 }
1274
1275 bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZonesList )
1276 {
1277   bool aRetValue = false;
1278
1279   QList<HYDROGUI_Zone*> aZonesList;
1280   HYDROGUI_Zone* aZone;
1281   // Get a list of dropped zones
1282   for ( int i = 0; i < theZonesList.length(); i++ )
1283   {
1284     aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
1285     if ( aZone )
1286     {
1287       aZonesList.append( aZone );
1288     }
1289   }
1290   if ( aZonesList.length() > 0 )
1291   {
1292     module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
1293     aRetValue = true;
1294   }
1295    
1296   return aRetValue;
1297 }
1298
1299 void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap, bool fitAllFlag, bool onTopViewAndFit )
1300 {
1301   LightApp_Application* anApp = module()->getApp();
1302   HYDROData_SequenceOfObjects aSeq;
1303   if ( theLandCoverMap && myShowLandCoverMap )
1304   {
1305     aSeq.Append( myEditedObject->GetLandCoverMap() );
1306   }
1307   else if ( !theLandCoverMap && myShowGeomObjects )
1308   {
1309     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
1310     HYDROData_SequenceOfObjects aSeqP = myEditedObject->GetInterPolyObjects();
1311     aSeq.Append( aSeqGO );
1312     aSeq.Append( aSeqP );
1313   }
1314
1315   Handle(HYDROData_Entity) anEntity;
1316
1317   if ( myShowZones )
1318   {
1319     // Gather zones for displaying
1320     HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
1321     HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
1322     HYDROData_SequenceOfObjects aZones;
1323     Handle(HYDROData_Region) aRegion;
1324     for ( ; aRegionsIter.More(); aRegionsIter.Next() )
1325     {
1326       anEntity = aRegionsIter.Value();
1327       if ( !anEntity.IsNull() )
1328       {
1329         aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
1330         if ( !aRegion.IsNull() )
1331         {
1332           aZones = aRegion->GetZones();
1333           aSeq.Append( aZones );
1334         }
1335       }
1336     }
1337   }
1338
1339   // Get a boundary polyline if any
1340   if (Handle(HYDROData_PolylineXY) aBPoly = myEditedObject->GetBoundaryPolyline())
1341     aSeq.Append( aBPoly );
1342
1343   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
1344
1345   if ( !myActiveViewManager )
1346   {
1347     if ( aSeq.IsEmpty() )
1348       return;
1349
1350     myActiveViewManager = anApp->activeViewManager();
1351   }
1352
1353   if ( !myPreviewViewManager )
1354   {
1355     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
1356       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
1357     if ( myPreviewViewManager )
1358     {
1359       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1360                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1361
1362       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
1363       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
1364     }
1365   }
1366
1367   if ( !myPreviewViewManager )
1368     return;
1369
1370   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
1371   {
1372     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
1373     if ( !aCtx.IsNull() )
1374     {
1375       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
1376       for ( ; anIter.More(); anIter.Next() )
1377       {
1378         const Handle(HYDROData_Entity)& ent = anIter.Value();
1379         if (!ent.IsNull())
1380           setObjectVisibility( ent, true );
1381       }
1382
1383       //Process the draw events for viewer
1384       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
1385     }
1386
1387     int UpdateFlags = UF_OCCViewer;
1388     if (fitAllFlag)
1389       UpdateFlags |= UF_FitAll;
1390     module()->update( UpdateFlags );
1391
1392     OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
1393     if ( onTopViewAndFit && vw )
1394       vw->onTopView();
1395   }
1396 }
1397
1398 void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
1399 {
1400   if ( theEntity.IsNull() || !myPreviewViewManager ) {
1401     return;
1402   }
1403
1404   OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1405   if ( aViewer ) {
1406     module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
1407   }
1408 }
1409
1410 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
1411 {
1412   closePreview();
1413 }
1414
1415 void HYDROGUI_CalculationOp::closePreview( bool theRemoveViewManager )
1416 {
1417   SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
1418   QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
1419   QShortcut* aShortcut;
1420   foreach( aShortcut, aShortcuts )
1421   {
1422     if ( aShortcut->key() == 
1423       QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
1424       SUIT_DataBrowser::RenameShortcut ) ) )
1425     {
1426       aShortcut->setEnabled( true );
1427     }
1428   }
1429
1430
1431   if( myPreviewViewManager )
1432   {
1433     // Hide all the displayed objects in the preview view
1434     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
1435     if ( aViewer ) {
1436       size_t aViewId = (size_t)aViewer;
1437       HYDROData_Iterator anIterator( doc() );
1438       for( ; anIterator.More(); anIterator.Next() ) {
1439         Handle(HYDROData_Entity) anObject = anIterator.Current();
1440         if( !anObject.IsNull() ) {
1441           module()->setObjectVisible( aViewId, anObject, false );
1442         }
1443       }
1444     }
1445
1446     if ( theRemoveViewManager )
1447     {
1448       disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1449                   this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
1450
1451       module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
1452       myPreviewViewManager = NULL;
1453     }
1454   }
1455
1456   if( myActiveViewManager && theRemoveViewManager )
1457   {
1458     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
1459     myActiveViewManager = NULL;
1460   }
1461 }
1462
1463 void HYDROGUI_CalculationOp::setAvailableGroups()
1464 {
1465   HYDROGUI_CalculationDlg* aPanel = 
1466       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1467
1468   HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
1469   QStringList aList, anEntryList;
1470   getNamesAndEntries( aSeq, aList, anEntryList );
1471
1472   QStringList aGroupsNames;
1473
1474   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
1475   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
1476   {
1477     Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( anObjs.Value( anIndex ) );
1478     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
1479     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
1480     {
1481       Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( aGIndex ) );
1482       aGroupsNames.append( aGroup->GetName() );
1483     }
1484   }
1485   if( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) ) {
1486     for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
1487       Handle(HYDROData_ShapesGroup) aGeomGroup =
1488         Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
1489       if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
1490         myEditedObject->RemoveGeometryGroup( aGeomGroup );
1491       }
1492     }
1493   }
1494
1495   aPanel->setAvailableGroups( aGroupsNames );
1496   aPanel->includeGroups( aList );
1497
1498   //@ASL: bool isUpdated = myEditedObject->IsMustBeUpdated();
1499 }
1500
1501 void HYDROGUI_CalculationOp::onAddGroups()
1502 {
1503   HYDROGUI_CalculationDlg* aPanel = 
1504     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1505   if ( !aPanel )
1506     return;
1507
1508   // Add geometry objects selected in the module browser to the calculation case
1509   QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
1510   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1511     return;
1512
1513   QStringList anAddedList;
1514   for (int i = 0; i < aSelectedList.length(); i++)
1515   {
1516     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1517       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1518     if ( aGroup.IsNull() )
1519       continue;
1520
1521     if ( myEditedObject->AddGeometryGroup( aGroup ) )
1522       anAddedList.append( aGroup->GetName() );
1523   }
1524
1525   if ( !anAddedList.isEmpty() )
1526   {
1527     aPanel->includeGroups( anAddedList );
1528   }
1529 }
1530
1531 void HYDROGUI_CalculationOp::onRemoveGroups()
1532 {
1533   // Remove selected objects from the calculation case
1534   HYDROGUI_CalculationDlg* aPanel = 
1535     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1536   if ( !aPanel )
1537     return;
1538
1539   QStringList aSelectedList = aPanel->getSelectedGroups();
1540   if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
1541     return;
1542
1543   for (int i = 0; i < aSelectedList.length(); i++)
1544   {
1545     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
1546       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
1547     if ( aGroup.IsNull() )
1548       continue;
1549
1550     myEditedObject->RemoveGeometryGroup( aGroup );
1551   }
1552
1553   aPanel->excludeGroups( aSelectedList );
1554 }
1555
1556
1557 void HYDROGUI_CalculationOp::setAvailableBoundaryPolygons()
1558 {
1559   HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1560
1561   HYDROData_Iterator anIter( doc(), KIND_BC_POLYGON );
1562   Handle(HYDROData_BCPolygon) aBCP;
1563   QStringList aNames;
1564   QVector<int> aTypes; 
1565   for (;anIter.More(); anIter.Next())
1566   {
1567     aBCP = Handle(HYDROData_BCPolygon)::DownCast( anIter.Current() );
1568     aNames.append(aBCP->GetName());
1569     aTypes.append(aBCP->GetBoundaryType());
1570   }
1571   HYDROData_SequenceOfObjects aBCPSeq = myEditedObject->GetBoundaryPolygons();
1572
1573   QStringList aListCut, aListIS;
1574   for (int i=1; i<=aBCPSeq.Size();i++)
1575   {
1576     int type = Handle(HYDROData_BCPolygon)::DownCast(aBCPSeq(i))->GetBoundaryType();
1577     if (type == 1)
1578       aListCut << aBCPSeq(i)->GetName();
1579     else if (type == 2 || type == 3)
1580       aListIS << aBCPSeq(i)->GetName();
1581   }
1582
1583   aPanel->setAvailableBoundaryPolygons( aNames, aTypes );
1584   aPanel->includeBoundaryPolygons( aListCut );
1585   aPanel->includeISBoundaryPolygons( aListIS );
1586 }
1587
1588 void HYDROGUI_CalculationOp::onRemoveBoundaryPolygons()
1589 {
1590   HYDROGUI_CalculationDlg* aPanel = 
1591     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1592   if ( !aPanel )
1593     return;
1594
1595   QStringList aSelectedListCut = aPanel->getSelectedBoundaryPolygons();
1596   QStringList aSelectedListIS = aPanel->getSelectedISBoundaryPolygons();
1597
1598   if ( aSelectedListCut.isEmpty() && aSelectedListIS.isEmpty() )
1599     return;
1600
1601   for (int i = 0; i < aSelectedListCut.length(); i++)
1602   {
1603     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1604       HYDROGUI_Tool::FindObjectByName( module(), aSelectedListCut.at(i) ) );
1605     if ( aBCPoly.IsNull() )
1606       continue;
1607     myEditedObject->RemoveBoundaryPolygon( aBCPoly );
1608   }
1609
1610   for (int i = 0; i < aSelectedListIS.length(); i++)
1611   {
1612     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1613       HYDROGUI_Tool::FindObjectByName( module(), aSelectedListIS.at(i) ) );
1614     if ( aBCPoly.IsNull() )
1615       continue;
1616     myEditedObject->RemoveBoundaryPolygon( aBCPoly );
1617   }
1618
1619   aPanel->excludeBoundaryPolygons( aSelectedListCut );
1620   aPanel->excludeISBoundaryPolygons( aSelectedListIS );
1621 }
1622
1623  
1624 void HYDROGUI_CalculationOp::onAddBoundaryPolygons()
1625 {
1626   HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1627   if ( !aPanel )
1628     return;
1629
1630   QStringList aSelectedList = aPanel->getSelectedAvailableBoundaryPolygons();
1631   if ( aSelectedList.isEmpty())
1632     return;
1633
1634   QStringList anAddedListCut, anAddedListIS;
1635   for (int i = 0; i < aSelectedList.length(); i++)
1636   {
1637     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
1638       HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
1639     if ( aBCPoly.IsNull() )
1640       continue;
1641    
1642    if ( myEditedObject->AddBoundaryPolygon( aBCPoly ) )
1643    {
1644      QString aName = aBCPoly->GetName();
1645      int type = aBCPoly->GetBoundaryType(); 
1646      if (type == 1)
1647        anAddedListCut.append(aName);
1648      else if (type == 2 || type == 3)
1649        anAddedListIS.append(aName);
1650    }
1651
1652   }
1653
1654   if ( !anAddedListCut.isEmpty() )
1655     aPanel->includeBoundaryPolygons(anAddedListCut);
1656   if ( !anAddedListIS.isEmpty() )
1657     aPanel->includeISBoundaryPolygons(anAddedListIS);
1658 }
1659
1660 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
1661 {
1662   HYDROGUI_CalculationDlg* aPanel = 
1663     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1664   if ( !aPanel )
1665     return;
1666
1667   if ( !confirmModeChange() ) {
1668     aPanel->setMode( myEditedObject->GetAssignmentMode() );
1669     return;
1670   }
1671
1672   myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
1673   aPanel->setMode( theMode );
1674 }
1675
1676 void HYDROGUI_CalculationOp::onOrderChanged( bool& isConfirmed )
1677 {
1678   HYDROGUI_CalculationDlg* aPanel = 
1679     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1680   if ( !aPanel )
1681     return;
1682
1683   isConfirmed = confirmOrderChange();
1684   if( isConfirmed )
1685     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1686 }
1687
1688 void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
1689 {
1690   HYDROGUI_CalculationDlg* aPanel = 
1691     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
1692   if ( !aPanel )
1693     return;
1694
1695   isConfirmed = confirmRuleChange();
1696   if( isConfirmed )
1697     myEditedObject->Changed( HYDROData_Entity::Geom_2d );
1698 }
1699
1700 void HYDROGUI_CalculationOp::onRegenerateColors()
1701 {
1702   // For geometry zones
1703   AssignDefaultZonesColors();
1704   setZonesVisible( false );
1705   setZonesVisible( true );
1706 }