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