Salome HOME
bug #237: fatal error on profile
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Zone.cxx
1 // Copyright (C) 2007-2013  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.
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_Bathymetry.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 BathymetryId:
52         // Get bathymetry name
53         aRes = getBathimetryName();
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::getBathimetryName() 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 bathymetries names when merging is necessary
100       // or just get the name of bathymetry of a single geometry object
101       // or just get the name of a single bathymetry
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 bathymetry name
112           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
113           if ( !aBathymetry.IsNull() )
114           {
115             aName = aBathymetry->GetName();
116             if ( !isMergingNeed )
117             {
118               // Get the first geometry object's bathymetry 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 bathymetry will be taken into account
148         {
149           Handle(HYDROData_Bathymetry) aBathymetry = aZone->GetMergeBathymetry();
150           if ( !aBathymetry.IsNull() )
151           {
152             aRes = aBathymetry->GetName();
153           }
154           break;
155         }
156         default:
157           aRes = QObject::tr( "MERGE_UNKNOWN" );
158       }
159     }
160   }
161   return aRes;
162 }
163
164 bool HYDROGUI_Zone::isMergingNeed() const
165 {
166   bool aRes = false;
167   if( !modelObject().IsNull() )
168   {
169     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
170     if ( !aZone.IsNull() ) 
171     {
172       aRes = aZone->IsMergingNeed();
173     }
174   }
175   return aRes;
176 }
177
178 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
179 {
180   // Implement red color for bathymetry conflicts in case creation dialog
181   QColor aRes;
182   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
183   if ( !aZone.IsNull() )
184   {
185     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
186     {
187       switch( theColorRole )
188       {
189         case Text:            // editor foreground (text) color
190         case Foreground:      // foreground (text) color
191           aRes = Qt::red;
192           break;
193         case HighlightedText: // highlighted foreground (text) color
194           aRes = Qt::black;
195           break;
196         case Base:            // editor background color
197         case Background:      // background color
198         case Highlight:       // highlight background color
199         default:
200           aRes = Qt::red;
201       }
202     }
203   }
204   if ( !aRes.isValid() )
205   {
206     aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
207   }
208   return aRes;
209 }
210
211 QStringList HYDROGUI_Zone::getBathymetries() const
212 {
213   QStringList aRes;
214   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
215   if ( !aZone.IsNull() )
216   {
217     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
218     // Collect all used bathymetries names when merging is necessary
219     // or just get the name of bathymetry of a single geometry object
220     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
221     for ( ; anIter.More(); anIter.Next() )
222     {
223       Handle(HYDROData_Object) aRefGeomObj =
224         Handle(HYDROData_Object)::DownCast( anIter.Value() );
225       if ( !aRefGeomObj.IsNull() )
226       {
227         // Get bathymetry name
228         Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
229         if ( !aBathymetry.IsNull() && !aRes.contains( aBathymetry->GetName() ))
230         {
231           aRes.append( aBathymetry->GetName() );
232         }
233       }
234     }
235   }
236   return aRes;
237 }
238
239 HYDROData_Zone::MergeBathymetriesType HYDROGUI_Zone::getMergeType() const
240 {
241   HYDROData_Zone::MergeBathymetriesType aRes = HYDROData_Zone::Merge_UNKNOWN;
242   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
243   if ( !aZone.IsNull() )
244   {
245     aRes = aZone->GetMergeType();
246   }
247   return aRes;
248 }
249
250 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theBathymetryName )
251 {
252   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
253   if ( !aZone.IsNull() )
254   {
255     HYDROData_Zone::MergeBathymetriesType aMergeType = 
256       ( HYDROData_Zone::MergeBathymetriesType )theMergeType;
257     aZone->SetMergeType( aMergeType );
258     if ( aMergeType == HYDROData_Zone::Merge_Object )
259     {
260       // Find a bathymetry by the given name and set it as the zone's merge bathymetry
261       HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
262       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
263       for ( ; anIter.More(); anIter.Next() )
264       {
265         Handle(HYDROData_Object) aRefGeomObj =
266           Handle(HYDROData_Object)::DownCast( anIter.Value() );
267         if ( !aRefGeomObj.IsNull() )
268         {
269           // Get bathymetry name
270           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
271           if ( !aBathymetry.IsNull() && theBathymetryName == aBathymetry->GetName() )
272           {
273             aZone->SetMergeBathymetry( aBathymetry );
274             break;
275           }
276         }
277       }
278     }
279   }
280 }
281
282 /*!
283   \brief Check if this object is can't be renamed in place
284
285   \param id column id
286   \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
287 */
288 bool HYDROGUI_Zone::renameAllowed( const int id ) const
289 {
290   if ( id == NameId && isInOperation() )
291   {
292     return true;
293   }
294   return HYDROGUI_DataObject::renameAllowed( id );
295 }
296
297 ///*!
298 //  \brief Set name of this object.
299 //
300 //  \return \c true if rename operation finished successfully, \c false otherwise.
301 //*/
302 //bool HYDROGUI_Zone::setName(const QString& theName)
303 //{
304 //  if ( isInOperation() )
305 //  {
306 //    bool aRes = false;
307 //    if ( !theName.isEmpty() )
308 //    {
309 //      Handle(HYDROData_Entity) anEntity = modelObject();
310 //      CAM_Module* aModule = module();
311 //      if( anEntity->GetName() != theName && aModule )
312 //      {
313 //        // check that there are no other objects with the same name in the document
314 //        Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
315 //        if ( anObject.IsNull() )
316 //        {
317 //          anEntity->SetName( theName );
318 //          aRes = true;
319 //        }
320 //        else
321 //        {
322 //          // Inform the user that the name is already used
323 //          QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
324 //          QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
325 //          SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
326 //        }
327 //      }
328 //    }
329 //  }
330 //  else
331 //  {
332 //    aRes = HYDROGUI_DataObject::setName( theName );
333 //  }
334 //  return aRes;
335 //}