Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Zone.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_Zone.h"
20
21 #include <HYDROData_Zone.h>
22 #include <HYDROData_Object.h>
23 #include <HYDROData_IAltitudeObject.h>
24 #include <HYDROData_LandCover.h>
25
26 #include <SUIT_DataObject.h>
27 #include <QSet>
28
29 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent, 
30                                           Handle(HYDROData_Zone) theData,
31                                           const QString& theParentEntry,
32                                           const bool theIsInOperation )
33 : HYDROGUI_DataObject( theParent, theData, theParentEntry, theIsInOperation ), CAM_DataObject( theParent )
34 {
35 }
36
37 QString HYDROGUI_Zone::text( const int theColumnId ) const
38 {
39   QString aRes;
40   if( !modelObject().IsNull() )
41   {
42     switch ( theColumnId )
43     {
44       case RefObjectId:
45         // Get Ref.Object name
46         aRes = getRefObjectNames();
47         break;
48       case AltitudeObjId:
49         // Get altitude/land cover object name
50         aRes = getObjectName();
51         break;
52       default:
53         aRes = LightApp_DataObject::text( theColumnId );
54     }
55   }
56   return aRes;
57 }
58
59 QString HYDROGUI_Zone::getRefObjectNames() const
60 {
61   QString aRes;
62   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
63   if ( !aZone.IsNull() )
64   {
65     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
66     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
67     for ( ; anIter.More(); anIter.Next() )
68     {
69       Handle(HYDROData_Entity) aRefGeomObj =
70         Handle(HYDROData_Entity)::DownCast( anIter.Value() );
71       if ( !aRefGeomObj.IsNull() )
72       {
73         // Get Ref.Object name
74         aRes += aRefGeomObj->GetName() + ", ";
75       }
76     }
77   }
78   if ( aRes.length() > 1 )
79   {
80     aRes.remove( aRes.length() - 2, 2 );
81   }
82   return aRes;
83 }
84
85 QString HYDROGUI_Zone::getObjectName() const
86 {
87   QString aRes;
88   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
89   if ( !aZone.IsNull() )
90   {
91     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
92     bool isMergingNeed = aZone->IsMergingNeed();
93     if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) 
94       || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
95     {
96       // Collect all used altitudes names when merging is necessary
97       // or just get the name of altitude of a single geometry object
98       // or just get the name of a single altitude
99       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
100       QSet<QString> aNamesSet;
101       QString aName;
102       for ( ; anIter.More(); anIter.Next() )
103       {
104         aName.clear();
105         Handle(HYDROData_Object) aRefGeomObj =
106           Handle(HYDROData_Object)::DownCast( anIter.Value() );
107         if ( !aRefGeomObj.IsNull() )
108         {
109           // Get altitude object name
110           Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
111           if ( !anAltitudeObj.IsNull() )
112           {
113             aName = anAltitudeObj->GetName();
114             if ( !isMergingNeed )
115             {
116               // Get the first geometry object's altitude name and go out
117               aRes = aName;
118               break;
119             }
120           }
121         }
122         else
123         {
124           Handle(HYDROData_LandCover) aRefLandCoverObj =
125             Handle(HYDROData_LandCover)::DownCast( anIter.Value() );
126           if ( !aRefLandCoverObj.IsNull() )
127           {
128             // Get name of land cover object
129             aName = aRefLandCoverObj->GetName();
130             if ( !isMergingNeed )
131             {
132               // Get the first land cover object's altitude name and go out
133               aRes = aName;
134               break;
135             }
136           }        
137         }
138
139         if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
140         {
141           aRes += aName + ", ";
142           aNamesSet.insert( aName );
143         }
144       }
145       // Remove the last comma if necessary
146       if ( isMergingNeed && ( aRes.length() > 1 ) )
147       {
148         aRes.remove( aRes.length() - 2, 2 );
149       }
150     }
151     else
152     {
153       switch( aZone->GetMergeType() )
154       {
155         case HYDROData_Zone::Merge_ZMIN:    // The minimum values
156           aRes = QObject::tr( "MERGE_ZMIN" );
157           break;
158         case HYDROData_Zone::Merge_ZMAX:    // The maximum values
159           aRes = QObject::tr( "MERGE_ZMAX" );
160           break;
161         case HYDROData_Zone::Merge_Object:   // Only one altitude/land cover will be taken into account
162         {
163           Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
164           if ( !aMergeObj.IsNull() )
165             aRes = aMergeObj->GetName();
166           break;
167         }
168         default:
169           aRes = QObject::tr( "MERGE_UNKNOWN" );
170       }
171     }
172   }
173   return aRes;
174 }
175
176 bool HYDROGUI_Zone::isMergingNeed() const
177 {
178   bool aRes = false;
179   if( !modelObject().IsNull() )
180   {
181     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
182     if ( !aZone.IsNull() ) 
183     {
184       aRes = aZone->IsMergingNeed();
185     }
186   }
187   return aRes;
188 }
189
190 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
191 {
192   // Implement red color for altitude conflicts in case creation dialog
193   QColor aRes;
194   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
195   if ( !aZone.IsNull() )
196   {
197     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
198     {
199       switch( theColorRole )
200       {
201         case Text:            // editor foreground (text) color
202         case Foreground:      // foreground (text) color
203           aRes = Qt::red;
204           break;
205         case HighlightedText: // highlighted foreground (text) color
206           aRes = Qt::black;
207           break;
208         case Base:            // editor background color
209         case Background:      // background color
210         case Highlight:       // highlight background color
211         default:
212           aRes = Qt::red;
213       }
214     }
215   }
216   if ( !aRes.isValid() )
217   {
218     aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
219   }
220   return aRes;
221 }
222
223 QStringList HYDROGUI_Zone::getObjects() const
224 {
225   QStringList aRes;
226   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
227   if ( !aZone.IsNull() )
228   {
229     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
230     // Collect all used altitudes/land cover names when merging is necessary
231     // or just get the name of altitude/land cover of a single object
232     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
233     for ( ; anIter.More(); anIter.Next() )
234     {
235       Handle(HYDROData_Object) aRefGeomObj =
236         Handle(HYDROData_Object)::DownCast( anIter.Value() );
237       if ( !aRefGeomObj.IsNull() )
238       {
239         Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
240         if ( !anAltitudeObj.IsNull() && !aRes.contains( anAltitudeObj->GetName() ) )
241           aRes.append( anAltitudeObj->GetName() );
242       } else {
243         aRes.append( anIter.Value()->GetName() );
244       }
245     }
246   }
247   return aRes;
248 }
249
250 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
251 {
252   HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
253   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
254   if ( !aZone.IsNull() )
255   {
256     aRes = aZone->GetMergeType();
257   }
258   return aRes;
259 }
260
261 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
262 {
263   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
264   if ( !aZone.IsNull() )
265   {
266     HYDROData_Zone::MergeType aMergeType = 
267       ( HYDROData_Zone::MergeType )theMergeType;
268     aZone->SetMergeType( aMergeType );
269     if ( aMergeType == HYDROData_Zone::Merge_Object )
270     {
271       // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
272       HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
273       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
274       for ( ; anIter.More(); anIter.Next() )
275       {
276         Handle(HYDROData_Entity) aMergeObject;
277
278         Handle(HYDROData_Object) aRefGeomObj =
279           Handle(HYDROData_Object)::DownCast( anIter.Value() );
280         if ( !aRefGeomObj.IsNull() )
281         {
282           // Get altitude object
283           aMergeObject = aRefGeomObj->GetAltitudeObject();
284         } else {
285           aMergeObject = Handle(HYDROData_LandCover)::DownCast( anIter.Value() );
286         }
287
288         if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
289         {
290           aZone->SetMergeObject( aMergeObject );
291           break;
292         }
293       }
294     }
295   }
296 }
297
298 /*!
299   \brief Check if this object is can't be renamed in place
300
301   \param id column id
302   \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
303 */
304 bool HYDROGUI_Zone::renameAllowed( const int id ) const
305 {
306   if ( id == NameId && isInOperation() )
307   {
308     return true;
309   }
310   return HYDROGUI_DataObject::renameAllowed( id );
311 }
312
313 ///*!
314 //  \brief Set name of this object.
315 //
316 //  \return \c true if rename operation finished successfully, \c false otherwise.
317 //*/
318 //bool HYDROGUI_Zone::setName(const QString& theName)
319 //{
320 //  if ( isInOperation() )
321 //  {
322 //    bool aRes = false;
323 //    if ( !theName.isEmpty() )
324 //    {
325 //      Handle(HYDROData_Entity) anEntity = modelObject();
326 //      CAM_Module* aModule = module();
327 //      if( anEntity->GetName() != theName && aModule )
328 //      {
329 //        // check that there are no other objects with the same name in the document
330 //        Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
331 //        if ( anObject.IsNull() )
332 //        {
333 //          anEntity->SetName( theName );
334 //          aRes = true;
335 //        }
336 //        else
337 //        {
338 //          // Inform the user that the name is already used
339 //          QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
340 //          QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
341 //          SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
342 //        }
343 //      }
344 //    }
345 //  }
346 //  else
347 //  {
348 //    aRes = HYDROGUI_DataObject::setName( theName );
349 //  }
350 //  return aRes;
351 //}