Salome HOME
merge BR_PORTING_OCCT_7
[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 = anIter.Value();
69       if ( !aRefGeomObj.IsNull() )
70       {
71         // Get Ref.Object name
72         aRes += aRefGeomObj->GetName() + ", ";
73       }
74     }
75   }
76   if ( aRes.length() > 1 )
77   {
78     aRes.remove( aRes.length() - 2, 2 );
79   }
80   return aRes;
81 }
82
83 QString HYDROGUI_Zone::getObjectName() const
84 {
85   QString aRes;
86   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
87   if ( !aZone.IsNull() )
88   {
89     HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
90     bool isMergingNeed = aZone->IsMergingNeed();
91     if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) 
92       || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
93     {
94       // Collect all used altitudes names when merging is necessary
95       // or just get the name of altitude of a single geometry object
96       // or just get the name of a single altitude
97       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
98       QSet<QString> aNamesSet;
99       QString aName;
100       for ( ; anIter.More(); anIter.Next() )
101       {
102         aName.clear();
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         }
120
121         if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
122         {
123           aRes += aName + ", ";
124           aNamesSet.insert( aName );
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/land cover will be taken into account
144         {
145           Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
146           if ( !aMergeObj.IsNull() )
147             aRes = aMergeObj->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::getObjects() 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->GetObjects();
212     // Collect all used altitudes/land cover names when merging is necessary
213     // or just get the name of altitude/land cover of a single 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       } else {
225         aRes.append( anIter.Value()->GetName() );
226       }
227     }
228   }
229   return aRes;
230 }
231
232 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
233 {
234   HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
235   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
236   if ( !aZone.IsNull() )
237   {
238     aRes = aZone->GetMergeType();
239   }
240   return aRes;
241 }
242
243 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
244 {
245   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
246   if ( !aZone.IsNull() )
247   {
248     HYDROData_Zone::MergeType aMergeType = 
249       ( HYDROData_Zone::MergeType )theMergeType;
250     aZone->SetMergeType( aMergeType );
251     if ( aMergeType == HYDROData_Zone::Merge_Object )
252     {
253       // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
254       HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
255       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
256       for ( ; anIter.More(); anIter.Next() )
257       {
258         Handle(HYDROData_Entity) aMergeObject;
259
260         Handle(HYDROData_Object) aRefGeomObj =
261           Handle(HYDROData_Object)::DownCast( anIter.Value() );
262         if ( !aRefGeomObj.IsNull() )
263         {
264           // Get altitude object
265           aMergeObject = aRefGeomObj->GetAltitudeObject();
266         }
267
268         if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
269         {
270           aZone->SetMergeObject( aMergeObject );
271           break;
272         }
273       }
274     }
275   }
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 //}