Salome HOME
Update names of regions in calculation case if its order has been changed (Bug #47).
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
1
2 #include "HYDROData_Region.h"
3
4 #include "HYDROData_CalculationCase.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_Zone.h"
8
9 #include <QStringList>
10
11 #define PYTHON_REGION_ID "KIND_REGION"
12
13 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
14 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
15
16
17 HYDROData_Region::HYDROData_Region()
18  : HYDROData_Entity()
19 {
20 }
21
22 HYDROData_Region::~HYDROData_Region()
23 {
24 }
25
26 QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
27 {
28   QStringList aResList;
29
30   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
31   if ( aDocument.IsNull() )
32     return aResList;
33
34   QString aDocName = aDocument->GetDocPyName();
35   QString aRegionName = GetName();
36
37   aResList << QString( "%1 = %2.CreateObject( %3 );" )
38               .arg( aRegionName ).arg( aDocName ).arg( PYTHON_REGION_ID );
39   aResList << QString( "%1.SetName( \"%2\" );" )
40               .arg( aRegionName ).arg( aRegionName );
41   aResList << QString( "" );
42
43   HYDROData_SequenceOfObjects aZones = GetZones();
44   HYDROData_SequenceOfObjects::Iterator anIter( aZones );
45   for ( ; anIter.More(); anIter.Next() )
46   {
47     Handle(HYDROData_Zone) aRefZone =
48       Handle(HYDROData_Zone)::DownCast( anIter.Value() );
49     if ( !aRefZone.IsNull() )
50       setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
51   }
52   aResList << QString( "" );
53
54   return aResList;
55 }
56
57 void HYDROData_Region::Remove()
58 {
59   Handle(HYDROData_CalculationCase) aFatherCalc = 
60     Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
61
62   HYDROData_Entity::Remove();
63
64   if ( !aFatherCalc.IsNull() )
65     aFatherCalc->UpdateRegionsOrder();
66 }
67
68 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
69 {
70   if ( theZone.IsNull() )
71     return false;
72   
73   if ( HasReference( theZone, DataTag_Zone ) )
74     return false; // Object is already in reference list
75
76   // Move the zone from other region
77   Handle(HYDROData_Region) aFatherRegion = 
78     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
79   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
80   {
81     Handle(HYDROData_Zone) aNewZone = addNewZone();
82     theZone->CopyTo( aNewZone );
83
84     aFatherRegion->RemoveZone( theZone );
85
86     theZone->SetLabel( aNewZone->Label() );
87   }
88   else
89   {
90     AddReferenceObject( theZone, DataTag_Zone );
91   }
92
93   return true;
94 }
95
96 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
97 {
98   return GetReferenceObjects( DataTag_Zone );
99 }
100
101 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
102 {
103   if ( theZone.IsNull() )
104     return;
105
106   RemoveReferenceObject( theZone->Label(), DataTag_Zone );
107
108   // Remove zone from data model
109   Handle(HYDROData_Region) aFatherRegion = 
110     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
111   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
112     theZone->Remove();
113
114   // If the last zone has been removed from region we remove this region
115   HYDROData_SequenceOfObjects aRefZones = GetZones();
116   if ( aRefZones.IsEmpty() )
117     Remove();
118 }
119
120 void HYDROData_Region::RemoveZones()
121 {
122   ClearReferenceObjects( DataTag_Zone );
123   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
124 }
125
126 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
127 {
128   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
129
130   Handle(HYDROData_Zone) aNewZone =
131     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
132   AddZone( aNewZone );
133
134   return aNewZone;
135 }
136