Salome HOME
Fix for the feature #6: Update of objects (T 1.2): For objects which need updating...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Zone.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Zone.h"
24
25 #include <HYDROData_Zone.h>
26 #include <HYDROData_Object.h>
27 #include <HYDROData_Bathymetry.h>
28
29 #include <SUIT_DataObject.h>
30 #include <QSet>
31
32 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent, 
33                                           Handle(HYDROData_Zone) theData,
34                                           const QString& theParentEntry )
35 : HYDROGUI_DataObject( theParent, theData, theParentEntry ), CAM_DataObject( theParent )
36 {
37 }
38
39 QString HYDROGUI_Zone::text( const int theColumnId ) const
40 {
41   QString aRes;
42   if( !modelObject().IsNull() )
43   {
44     switch ( theColumnId )
45     {
46       case RefObjectId:
47         // Get Ref.Object name
48         aRes = getRefObjectNames();
49         break;
50       case BathymetryId:
51         // Get bathymetry name
52         aRes = getBathimetryName();
53         break;
54       default:
55         aRes = LightApp_DataObject::text( theColumnId );
56     }
57   }
58   return aRes;
59 }
60
61 QString HYDROGUI_Zone::getRefObjectNames() const
62 {
63   QString aRes;
64   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
65   if ( !aZone.IsNull() )
66   {
67     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
68     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
69     for ( ; anIter.More(); anIter.Next() )
70     {
71       Handle(HYDROData_Object) aRefGeomObj =
72         Handle(HYDROData_Object)::DownCast( anIter.Value() );
73       if ( !aRefGeomObj.IsNull() )
74       {
75         // Get Ref.Object name
76         aRes += aRefGeomObj->GetName() + ", ";
77       }
78     }
79   }
80   if ( aRes.length() > 1 )
81   {
82     aRes.remove( aRes.length() - 2, 2 );
83   }
84   return aRes;
85 }
86
87 QString HYDROGUI_Zone::getBathimetryName() const
88 {
89   QString aRes;
90   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
91   if ( !aZone.IsNull() )
92   {
93     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
94     bool isMergingNeed = aZone->IsMergingNeed();
95     if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) 
96       || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
97     {
98       // Collect all used bathymetries names when merging is necessary
99       // or just get the name of bathymetry of a single geometry object
100       // or just get the name of a single bathymetry
101       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
102       QSet<QString> aNamesSet;
103       QString aName;
104       for ( ; anIter.More(); anIter.Next() )
105       {
106         Handle(HYDROData_Object) aRefGeomObj =
107           Handle(HYDROData_Object)::DownCast( anIter.Value() );
108         if ( !aRefGeomObj.IsNull() )
109         {
110           // Get bathymetry name
111           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
112           if ( !aBathymetry.IsNull() )
113           {
114             aName = aBathymetry->GetName();
115             if ( !isMergingNeed )
116             {
117               // Get the first geometry object's bathymetry name and go out
118               aRes = aName;
119               break;
120             }
121
122             if ( !aNamesSet.contains( aName ) )
123             {
124               aRes += aName + ", ";
125               aNamesSet.insert( aName );
126             }
127           }
128         }
129       }
130       // Remove the last comma if necessary
131       if ( isMergingNeed && ( aRes.length() > 1 ) )
132       {
133         aRes.remove( aRes.length() - 2, 2 );
134       }
135     }
136     else
137     {
138       switch( aZone->GetMergeType() )
139       {
140         case HYDROData_Zone::Merge_ZMIN:    // The minimum values
141           aRes = QObject::tr( "MERGE_ZMIN" );
142           break;
143         case HYDROData_Zone::Merge_ZMAX:    // The maximum values
144           aRes = QObject::tr( "MERGE_ZMAX" );
145           break;
146         case HYDROData_Zone::Merge_Object:   // Only one bathymetry will be taken into account
147         {
148           Handle(HYDROData_Bathymetry) aBathymetry = aZone->GetMergeBathymetry();
149           if ( !aBathymetry.IsNull() )
150           {
151             aRes = aBathymetry->GetName();
152           }
153           break;
154         }
155         default:
156           aRes = QObject::tr( "MERGE_UNKNOWN" );
157       }
158     }
159   }
160   return aRes;
161 }
162
163 bool HYDROGUI_Zone::isMergingNeed() const
164 {
165   bool aRes = false;
166   if( !modelObject().IsNull() )
167   {
168     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
169     if ( !aZone.IsNull() ) 
170     {
171       aRes = aZone->IsMergingNeed();
172     }
173   }
174   return aRes;
175 }
176
177 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
178 {
179   // Implement red color for bathymetry conflicts in case creation dialog
180   QColor aRes;
181   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
182   if ( !aZone.IsNull() )
183   {
184     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
185     {
186       switch( theColorRole )
187       {
188         case Text:            // editor foreground (text) color
189         case Foreground:      // foreground (text) color
190           aRes = Qt::red;
191           break;
192         case HighlightedText: // highlighted foreground (text) color
193           aRes = Qt::black;
194           break;
195         case Base:            // editor background color
196         case Background:      // background color
197         case Highlight:       // highlight background color
198         default:
199           aRes = Qt::red;
200       }
201     }
202   }
203   if ( !aRes.isValid() )
204   {
205     aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
206   }
207   return aRes;
208 }
209
210 QStringList HYDROGUI_Zone::getBathymetries() const
211 {
212   QStringList aRes;
213   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
214   if ( !aZone.IsNull() )
215   {
216     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
217     // Collect all used bathymetries names when merging is necessary
218     // or just get the name of bathymetry of a single geometry object
219     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
220     for ( ; anIter.More(); anIter.Next() )
221     {
222       Handle(HYDROData_Object) aRefGeomObj =
223         Handle(HYDROData_Object)::DownCast( anIter.Value() );
224       if ( !aRefGeomObj.IsNull() )
225       {
226         // Get bathymetry name
227         Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
228         if ( !aBathymetry.IsNull() && !aRes.contains( aBathymetry->GetName() ))
229         {
230           aRes.append( aBathymetry->GetName() );
231         }
232       }
233     }
234   }
235   return aRes;
236 }
237
238 HYDROData_Zone::MergeBathymetriesType HYDROGUI_Zone::getMergeType() const
239 {
240   HYDROData_Zone::MergeBathymetriesType aRes = HYDROData_Zone::Merge_UNKNOWN;
241   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
242   if ( !aZone.IsNull() )
243   {
244     aRes = aZone->GetMergeType();
245   }
246   return aRes;
247 }
248
249 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theBathymetryName )
250 {
251   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
252   if ( !aZone.IsNull() )
253   {
254     HYDROData_Zone::MergeBathymetriesType aMergeType = 
255       ( HYDROData_Zone::MergeBathymetriesType )theMergeType;
256     aZone->SetMergeType( aMergeType );
257     if ( aMergeType == HYDROData_Zone::Merge_Object )
258     {
259       // Find a bathymetry by the given name and set it as the zone's merge bathymetry
260       HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
261       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
262       for ( ; anIter.More(); anIter.Next() )
263       {
264         Handle(HYDROData_Object) aRefGeomObj =
265           Handle(HYDROData_Object)::DownCast( anIter.Value() );
266         if ( !aRefGeomObj.IsNull() )
267         {
268           // Get bathymetry name
269           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
270           if ( !aBathymetry.IsNull() && theBathymetryName == aBathymetry->GetName() )
271           {
272             aZone->SetMergeBathymetry( aBathymetry );
273             break;
274           }
275         }
276       }
277     }
278   }
279 }