Salome HOME
debug of DTM object
[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
25 #include <SUIT_DataObject.h>
26 #include <QSet>
27
28 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent, 
29                                           Handle(HYDROData_Zone) theData,
30                                           const QString& theParentEntry,
31                                           const bool theIsInOperation )
32 : HYDROGUI_DataObject( theParent, theData, theParentEntry, theIsInOperation ), CAM_DataObject( theParent )
33 {
34 }
35
36 QString HYDROGUI_Zone::text( const int theColumnId ) const
37 {
38   QString aRes;
39   if( !modelObject().IsNull() )
40   {
41     switch ( theColumnId )
42     {
43       case RefObjectId:
44         // Get Ref.Object name
45         aRes = getRefObjectNames();
46         break;
47       case AltitudeObjId:
48         // Get altitude/land cover object name
49         aRes = getObjectName();
50         break;
51       default:
52         aRes = LightApp_DataObject::text( theColumnId );
53     }
54   }
55   return aRes;
56 }
57
58 QString HYDROGUI_Zone::getRefObjectNames() const
59 {
60   QString aRes;
61   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
62   if ( !aZone.IsNull() )
63   {
64     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
65     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
66     for ( ; anIter.More(); anIter.Next() )
67     {
68       Handle(HYDROData_Entity) aRefGeomObj =
69         Handle(HYDROData_Entity)::DownCast( anIter.Value() );
70       if ( !aRefGeomObj.IsNull() )
71       {
72         // Get Ref.Object name
73         aRes += aRefGeomObj->GetName() + ", ";
74       }
75     }
76   }
77   if ( aRes.length() > 1 )
78   {
79     aRes.remove( aRes.length() - 2, 2 );
80   }
81   return aRes;
82 }
83
84 QString HYDROGUI_Zone::getObjectName() const
85 {
86   QString aRes;
87   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
88   if ( !aZone.IsNull() )
89   {
90     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
91     bool isMergingNeed = aZone->IsMergingNeed();
92     if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) 
93       || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
94     {
95       // Collect all used altitudes names when merging is necessary
96       // or just get the name of altitude of a single geometry object
97       // or just get the name of a single altitude
98       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
99       QSet<QString> aNamesSet;
100       QString aName;
101       for ( ; anIter.More(); anIter.Next() )
102       {
103         aName.clear();
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         }
121
122         if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
123         {
124           aRes += aName + ", ";
125           aNamesSet.insert( aName );
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         }
268
269         if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
270         {
271           aZone->SetMergeObject( aMergeObject );
272           break;
273         }
274       }
275     }
276   }
277 }
278
279 /*!
280   \brief Check if this object is can't be renamed in place
281
282   \param id column id
283   \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
284 */
285 bool HYDROGUI_Zone::renameAllowed( const int id ) const
286 {
287   if ( id == NameId && isInOperation() )
288   {
289     return true;
290   }
291   return HYDROGUI_DataObject::renameAllowed( id );
292 }
293
294 ///*!
295 //  \brief Set name of this object.
296 //
297 //  \return \c true if rename operation finished successfully, \c false otherwise.
298 //*/
299 //bool HYDROGUI_Zone::setName(const QString& theName)
300 //{
301 //  if ( isInOperation() )
302 //  {
303 //    bool aRes = false;
304 //    if ( !theName.isEmpty() )
305 //    {
306 //      Handle(HYDROData_Entity) anEntity = modelObject();
307 //      CAM_Module* aModule = module();
308 //      if( anEntity->GetName() != theName && aModule )
309 //      {
310 //        // check that there are no other objects with the same name in the document
311 //        Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
312 //        if ( anObject.IsNull() )
313 //        {
314 //          anEntity->SetName( theName );
315 //          aRes = true;
316 //        }
317 //        else
318 //        {
319 //          // Inform the user that the name is already used
320 //          QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
321 //          QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
322 //          SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
323 //        }
324 //      }
325 //    }
326 //  }
327 //  else
328 //  {
329 //    aRes = HYDROGUI_DataObject::setName( theName );
330 //  }
331 //  return aRes;
332 //}