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