Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[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_Object) aRefGeomObj =
70         Handle(HYDROData_Object)::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         Handle(HYDROData_Object) aRefGeomObj =
105           Handle(HYDROData_Object)::DownCast( anIter.Value() );
106         if ( !aRefGeomObj.IsNull() )
107         {
108           // Get altitude object name
109           Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
110           if ( !anAltitudeObj.IsNull() )
111           {
112             aName = anAltitudeObj->GetName();
113             if ( !isMergingNeed )
114             {
115               // Get the first geometry object's altitude name and go out
116               aRes = aName;
117               break;
118             }
119
120             if ( !aNamesSet.contains( aName ) )
121             {
122               aRes += aName + ", ";
123               aNamesSet.insert( aName );
124             }
125           }
126         }
127       }
128       // Remove the last comma if necessary
129       if ( isMergingNeed && ( aRes.length() > 1 ) )
130       {
131         aRes.remove( aRes.length() - 2, 2 );
132       }
133     }
134     else
135     {
136       switch( aZone->GetMergeType() )
137       {
138         case HYDROData_Zone::Merge_ZMIN:    // The minimum values
139           aRes = QObject::tr( "MERGE_ZMIN" );
140           break;
141         case HYDROData_Zone::Merge_ZMAX:    // The maximum values
142           aRes = QObject::tr( "MERGE_ZMAX" );
143           break;
144         case HYDROData_Zone::Merge_Object:   // Only one altitude/land cover will be taken into account
145         {
146           Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
147           if ( !aMergeObj.IsNull() )
148             aRes = aMergeObj->GetName();
149           break;
150         }
151         default:
152           aRes = QObject::tr( "MERGE_UNKNOWN" );
153       }
154     }
155   }
156   return aRes;
157 }
158
159 bool HYDROGUI_Zone::isMergingNeed() const
160 {
161   bool aRes = false;
162   if( !modelObject().IsNull() )
163   {
164     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
165     if ( !aZone.IsNull() ) 
166     {
167       aRes = aZone->IsMergingNeed();
168     }
169   }
170   return aRes;
171 }
172
173 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
174 {
175   // Implement red color for altitude conflicts in case creation dialog
176   QColor aRes;
177   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
178   if ( !aZone.IsNull() )
179   {
180     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
181     {
182       switch( theColorRole )
183       {
184         case Text:            // editor foreground (text) color
185         case Foreground:      // foreground (text) color
186           aRes = Qt::red;
187           break;
188         case HighlightedText: // highlighted foreground (text) color
189           aRes = Qt::black;
190           break;
191         case Base:            // editor background color
192         case Background:      // background color
193         case Highlight:       // highlight background color
194         default:
195           aRes = Qt::red;
196       }
197     }
198   }
199   if ( !aRes.isValid() )
200   {
201     aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
202   }
203   return aRes;
204 }
205
206 QStringList HYDROGUI_Zone::getObjects() const
207 {
208   QStringList aRes;
209   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
210   if ( !aZone.IsNull() )
211   {
212     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
213     // Collect all used altitudes/land cover names when merging is necessary
214     // or just get the name of altitude/land cover of a single object
215     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
216     for ( ; anIter.More(); anIter.Next() )
217     {
218       Handle(HYDROData_Object) aRefGeomObj =
219         Handle(HYDROData_Object)::DownCast( anIter.Value() );
220       if ( !aRefGeomObj.IsNull() )
221       {
222         Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
223         if ( !anAltitudeObj.IsNull() && !aRes.contains( anAltitudeObj->GetName() ) )
224           aRes.append( anAltitudeObj->GetName() );
225       } else {
226         aRes.append( anIter.Value()->GetName() );
227       }
228     }
229   }
230   return aRes;
231 }
232
233 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
234 {
235   HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
236   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
237   if ( !aZone.IsNull() )
238   {
239     aRes = aZone->GetMergeType();
240   }
241   return aRes;
242 }
243
244 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
245 {
246   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
247   if ( !aZone.IsNull() )
248   {
249     HYDROData_Zone::MergeType aMergeType = 
250       ( HYDROData_Zone::MergeType )theMergeType;
251     aZone->SetMergeType( aMergeType );
252     if ( aMergeType == HYDROData_Zone::Merge_Object )
253     {
254       // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
255       HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
256       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
257       for ( ; anIter.More(); anIter.Next() )
258       {
259         Handle(HYDROData_Entity) aMergeObject;
260
261         Handle(HYDROData_Object) aRefGeomObj =
262           Handle(HYDROData_Object)::DownCast( anIter.Value() );
263         if ( !aRefGeomObj.IsNull() )
264         {
265           // Get altitude object
266           aMergeObject = aRefGeomObj->GetAltitudeObject();
267         } else {
268           aMergeObject = Handle(HYDROData_LandCover)::DownCast( anIter.Value() );
269         }
270
271         if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
272         {
273           aZone->SetMergeObject( aMergeObject );
274           break;
275         }
276       }
277     }
278   }
279 }
280
281 /*!
282   \brief Check if this object is can't be renamed in place
283
284   \param id column id
285   \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
286 */
287 bool HYDROGUI_Zone::renameAllowed( const int id ) const
288 {
289   if ( id == NameId && isInOperation() )
290   {
291     return true;
292   }
293   return HYDROGUI_DataObject::renameAllowed( id );
294 }
295
296 ///*!
297 //  \brief Set name of this object.
298 //
299 //  \return \c true if rename operation finished successfully, \c false otherwise.
300 //*/
301 //bool HYDROGUI_Zone::setName(const QString& theName)
302 //{
303 //  if ( isInOperation() )
304 //  {
305 //    bool aRes = false;
306 //    if ( !theName.isEmpty() )
307 //    {
308 //      Handle(HYDROData_Entity) anEntity = modelObject();
309 //      CAM_Module* aModule = module();
310 //      if( anEntity->GetName() != theName && aModule )
311 //      {
312 //        // check that there are no other objects with the same name in the document
313 //        Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
314 //        if ( anObject.IsNull() )
315 //        {
316 //          anEntity->SetName( theName );
317 //          aRes = true;
318 //        }
319 //        else
320 //        {
321 //          // Inform the user that the name is already used
322 //          QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
323 //          QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
324 //          SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
325 //        }
326 //      }
327 //    }
328 //  }
329 //  else
330 //  {
331 //    aRes = HYDROGUI_DataObject::setName( theName );
332 //  }
333 //  return aRes;
334 //}