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