Salome HOME
Image positioning by two points.
[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
31 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent, 
32                                           Handle(HYDROData_Zone) theData,
33                                           const QString& theParentEntry )
34 : HYDROGUI_DataObject( theParent, theData, theParentEntry ), CAM_DataObject( theParent )
35 {
36 }
37
38 QString HYDROGUI_Zone::text( const int theColumnId ) const
39 {
40   QString aRes;
41   if( !modelObject().IsNull() )
42   {
43     switch ( theColumnId )
44     {
45       case RefObjectId:
46         // Get Ref.Object name
47         aRes = getRefObjectNames();
48         break;
49       case BathymetryId:
50         // Get bathymetry name
51         aRes = getBathimetryName();
52         break;
53       default:
54         aRes = LightApp_DataObject::text( theColumnId );
55     }
56   }
57   return aRes;
58 }
59
60 QString HYDROGUI_Zone::getRefObjectNames() const
61 {
62   QString aRes;
63   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
64   if ( !aZone.IsNull() )
65   {
66     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
67     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
68     for ( ; anIter.More(); anIter.Next() )
69     {
70       Handle(HYDROData_Object) aRefGeomObj =
71         Handle(HYDROData_Object)::DownCast( 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::getBathimetryName() 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->GetGeometryObjects();
93     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) 
94       || aSeq.Length() == 1 )
95     {
96       // Collect all used bathymetries names when merging is necessary
97       // or just get the name of bathymetry of a single geometry object
98       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
99       for ( ; anIter.More(); anIter.Next() )
100       {
101         Handle(HYDROData_Object) aRefGeomObj =
102           Handle(HYDROData_Object)::DownCast( anIter.Value() );
103         if ( !aRefGeomObj.IsNull() )
104         {
105           // Get bathymetry name
106           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
107           if ( !aBathymetry.IsNull() )
108           {
109             aRes += aBathymetry->GetName() + ", ";
110           }
111         }
112       }
113       if ( aRes.length() > 1 )
114       {
115         aRes.remove( aRes.length() - 2, 2 );
116       }
117     }
118     else
119     {
120       switch( aZone->GetMergeType() )
121       {
122         case HYDROData_Zone::Merge_ZMIN:    // The minimum values
123           aRes = QObject::tr( "MERGE_ZMIN" );
124           break;
125         case HYDROData_Zone::Merge_ZMAX:    // The maximum values
126           aRes = QObject::tr( "MERGE_ZMAX" );
127           break;
128         case HYDROData_Zone::Merge_Object:   // Only one bathymetry will be taken into account
129         {
130           Handle(HYDROData_Bathymetry) aBathymetry = aZone->GetMergeBathymetry();
131           if ( !aBathymetry.IsNull() )
132           {
133             aRes = aBathymetry->GetName();
134           }
135           break;
136         }
137         default:
138           aRes = QObject::tr( "MERGE_UNKNOWN" );
139       }
140     }
141   }
142   return aRes;
143 }
144
145 bool HYDROGUI_Zone::isMergingNeed() const
146 {
147   bool aRes = false;
148   if( !modelObject().IsNull() )
149   {
150     Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
151     if ( !aZone.IsNull() ) 
152     {
153       aRes = aZone->IsMergingNeed();
154     }
155   }
156   return aRes;
157 }
158
159 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
160 {
161   // Implement red color for bathymetry conflicts in case creation dialog
162   QColor aRes;
163   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
164   if ( !aZone.IsNull() )
165   {
166     if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
167     {
168       switch( theColorRole )
169       {
170         case Text:            // editor foreground (text) color
171         case Foreground:      // foreground (text) color
172           aRes = Qt::red;
173           break;
174         case HighlightedText: // highlighted foreground (text) color
175           aRes = Qt::black;
176           break;
177         case Base:            // editor background color
178         case Background:      // background color
179         case Highlight:       // highlight background color
180         default:
181           aRes = Qt::red;
182       }
183     }
184   }
185   if ( !aRes.isValid() )
186   {
187     aRes = LightApp_DataObject::color( theColorRole, theColumnId );
188   }
189   return aRes;
190 }
191
192 QStringList HYDROGUI_Zone::getBathymetries() const
193 {
194   QStringList aRes;
195   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
196   if ( !aZone.IsNull() )
197   {
198     HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
199     // Collect all used bathymetries names when merging is necessary
200     // or just get the name of bathymetry of a single geometry object
201     HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
202     for ( ; anIter.More(); anIter.Next() )
203     {
204       Handle(HYDROData_Object) aRefGeomObj =
205         Handle(HYDROData_Object)::DownCast( anIter.Value() );
206       if ( !aRefGeomObj.IsNull() )
207       {
208         // Get bathymetry name
209         Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
210         if ( !aBathymetry.IsNull() && !aRes.contains( aBathymetry->GetName() ))
211         {
212           aRes.append( aBathymetry->GetName() );
213         }
214       }
215     }
216   }
217   return aRes;
218 }
219
220 HYDROData_Zone::MergeBathymetriesType HYDROGUI_Zone::getMergeType() const
221 {
222   HYDROData_Zone::MergeBathymetriesType aRes = HYDROData_Zone::Merge_UNKNOWN;
223   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
224   if ( !aZone.IsNull() )
225   {
226     aRes = aZone->GetMergeType();
227   }
228   return aRes;
229 }
230
231 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theBathymetryName )
232 {
233   Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
234   if ( !aZone.IsNull() )
235   {
236     HYDROData_Zone::MergeBathymetriesType aMergeType = 
237       ( HYDROData_Zone::MergeBathymetriesType )theMergeType;
238     aZone->SetMergeType( aMergeType );
239     if ( aMergeType == HYDROData_Zone::Merge_Object )
240     {
241       // Find a bathymetry by the given name and set it as the zone's merge bathymetry
242       HYDROData_SequenceOfObjects aSeq = aZone->GetGeometryObjects();
243       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
244       for ( ; anIter.More(); anIter.Next() )
245       {
246         Handle(HYDROData_Object) aRefGeomObj =
247           Handle(HYDROData_Object)::DownCast( anIter.Value() );
248         if ( !aRefGeomObj.IsNull() )
249         {
250           // Get bathymetry name
251           Handle(HYDROData_Bathymetry) aBathymetry = aRefGeomObj->GetBathymetry();
252           if ( !aBathymetry.IsNull() && theBathymetryName == aBathymetry->GetName() )
253           {
254             aZone->SetMergeBathymetry( aBathymetry );
255             break;
256           }
257         }
258       }
259     }
260   }
261 }