Salome HOME
[fix] Corrections for version value
[modules/hydro.git] / src / HYDROData / HYDROData_IPolyline.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 "HYDROData_IPolyline.h"
20
21 #include <gp_XY.hxx>
22
23 #include <TDataStd_BooleanList.hxx>
24 #include <TDataStd_ExtStringList.hxx>
25 #include <TDataStd_IntegerList.hxx>
26 #include <TDataStd_RealList.hxx>
27 #include <TDataStd_IntegerArray.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <QColor>
30
31 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_IPolyline, HYDROData_Entity)
32
33 HYDROData_IPolyline::HYDROData_IPolyline()
34 : HYDROData_Entity( Geom_2d )
35 {
36 }
37
38 HYDROData_IPolyline::~HYDROData_IPolyline()
39 {
40 }
41
42 void HYDROData_IPolyline::SetWireColor( const QColor& theColor )
43 {
44   //SetColor( theColor, DataTag_WireColor ); //DEPRECATED;
45   //set theColor for all sections
46   int nbSec = NbSections();
47   for (int i = 0; i < nbSec; i++)
48     SetSectionColor(i, theColor);
49 }
50
51 QColor HYDROData_IPolyline::GetWireColor() const
52 {
53   return GetColor( DefaultWireColor(), DataTag_WireColor );
54 }
55
56 QColor HYDROData_IPolyline::DefaultWireColor()
57 {
58   return QColor( Qt::black );
59 }
60
61 int HYDROData_IPolyline::NbPoints( const int theSectionIndex ) const
62 {
63   return GetPoints( theSectionIndex ).Length();
64 }
65
66 TopoDS_Shape HYDROData_IPolyline::GetShape() const
67 {
68   return HYDROData_Entity::GetShape( DataTag_PolylineShape );
69 }
70
71 void HYDROData_IPolyline::SetShape( const TopoDS_Shape& theShape )
72 {
73   HYDROData_Entity::SetShape( DataTag_PolylineShape, theShape );
74 }
75
76 void HYDROData_IPolyline::RemovePolylineShape()
77 {
78   SetShape( TopoDS_Shape() );
79 }
80
81 void HYDROData_IPolyline::getSectionsLists( Handle(TDataStd_ExtStringList)& theNamesList,
82                                             Handle(TDataStd_IntegerList)&   theTypesList,
83                                             Handle(TDataStd_BooleanList)&   theClosuresList,
84                                             const bool                      theIsCreate ) const
85 {
86   theNamesList.Nullify();
87   theTypesList.Nullify();
88   theClosuresList.Nullify();
89
90   TDF_Label aSectLabel = myLab.FindChild( DataTag_Sections, theIsCreate );
91   if ( aSectLabel.IsNull() )
92     return;
93
94   if ( !aSectLabel.FindAttribute( TDataStd_ExtStringList::GetID(), theNamesList ) && theIsCreate )
95   {
96     theNamesList = TDataStd_ExtStringList::Set( aSectLabel );
97     theNamesList->SetID(TDataStd_ExtStringList::GetID());
98   }
99
100   if ( !aSectLabel.FindAttribute( TDataStd_IntegerList::GetID(), theTypesList ) && theIsCreate )
101   {
102     theTypesList = TDataStd_IntegerList::Set( aSectLabel );
103     theTypesList->SetID(TDataStd_IntegerList::GetID());
104   }
105
106   if ( !aSectLabel.FindAttribute( TDataStd_BooleanList::GetID(), theClosuresList ) && theIsCreate )
107   {
108     theClosuresList = TDataStd_BooleanList::Set( aSectLabel );
109     theClosuresList->SetID(TDataStd_BooleanList::GetID());
110   }
111 }
112
113 void HYDROData_IPolyline::removeSectionsLists()
114 {
115   TDF_Label aSectsLabel = myLab.FindChild( DataTag_Sections, false );
116   if ( !aSectsLabel.IsNull() )
117     aSectsLabel.ForgetAllAttributes();
118 }
119
120 void HYDROData_IPolyline::getPointsLists( const int                  theSectionIndex,
121                                           Handle(TDataStd_RealList)& theListX,
122                                           Handle(TDataStd_RealList)& theListY,
123                                           const bool                 theIsCreate ) const
124 {
125   theListX.Nullify();
126   theListY.Nullify();
127
128   TDF_Label aLabel = myLab.FindChild( DataTag_Points, theIsCreate );
129   if ( aLabel.IsNull() )
130     return;
131
132   TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, theIsCreate );
133   if ( aSectLabel.IsNull() )
134     return;
135
136   TDF_Label aLabelX = aSectLabel.FindChild( 0, theIsCreate );
137   if ( !aLabelX.IsNull() )
138   {
139     if ( !aLabelX.FindAttribute( TDataStd_RealList::GetID(), theListX ) && theIsCreate )
140     {
141       theListX = TDataStd_RealList::Set( aLabelX );
142       theListX->SetID(TDataStd_RealList::GetID());
143     }
144   }
145
146   TDF_Label aLabelY = aSectLabel.FindChild( 1, theIsCreate );
147   if ( !aLabelY.IsNull() )
148   {
149     if ( !aLabelY.FindAttribute( TDataStd_RealList::GetID(), theListY ) && theIsCreate )
150     {
151       theListY = TDataStd_RealList::Set( aLabelY );
152       theListY->SetID(TDataStd_RealList::GetID());
153     }
154   }
155 }
156
157 void HYDROData_IPolyline::removePointsLists( const int theSectionIndex ) const
158 {
159   TDF_Label aLabel = myLab.FindChild( DataTag_Points, false );
160   if ( aLabel.IsNull() )
161     return;
162
163   if ( theSectionIndex < 0 )
164   {
165     aLabel.ForgetAllAttributes();
166   }
167   else
168   {
169     TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
170     if ( !aSectLabel.IsNull() )
171       aSectLabel.ForgetAllAttributes();
172   }
173 }
174
175 bool HYDROData_IPolyline::GetSectionColor( const int theSectionIndex, QColor &theColor) const
176 {
177   TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, false );
178   if ( aLabel.IsNull() )
179     return false;
180
181   TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
182   if ( aSectLabel.IsNull() )
183     return false;
184
185   Handle(TDataStd_IntegerArray) aColorArray;
186
187   if ( aSectLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
188   {
189     theColor.setRed(   aColorArray->Value( 1 ) );
190     theColor.setGreen( aColorArray->Value( 2 ) );
191     theColor.setBlue(  aColorArray->Value( 3 ) );
192     theColor.setAlpha( aColorArray->Value( 4 ) );
193   }
194 }
195
196 void HYDROData_IPolyline::SetSectionColor( const int theSectionIndex,
197                                            const QColor& theColor )
198 {
199   TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, true );
200   if ( aLabel.IsNull() )
201     return;
202
203   TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, true );
204   if ( aSectLabel.IsNull() )
205     return;
206
207   Handle(TDataStd_IntegerArray) aColorArray;
208
209   if ( !aSectLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
210     aColorArray = TDataStd_IntegerArray::Set( aSectLabel, 1, 4 );
211
212   aColorArray->SetValue( 1, theColor.red()   );
213   aColorArray->SetValue( 2, theColor.green() );
214   aColorArray->SetValue( 3, theColor.blue()  );
215   aColorArray->SetValue( 4, theColor.alpha() );
216 }
217
218 void HYDROData_IPolyline::removeSectionColor( const int theSectionIndex ) const
219 {
220   TDF_Label aLabel = myLab.FindChild( DataTag_SectionColors, false );
221   if ( aLabel.IsNull() )
222     return;
223
224   if ( theSectionIndex < 0 )
225   {
226     aLabel.ForgetAllAttributes();
227   }
228   else
229   {
230     TDF_Label aSectLabel = aLabel.FindChild( theSectionIndex, false );
231     if ( !aSectLabel.IsNull() )
232       aSectLabel.ForgetAllAttributes();
233   }
234 }
235
236  void HYDROData_IPolyline::setPythonPolylineSectionColor( QStringList&  theScript,
237                                                           const int     theSectIndex,
238                                                           const QColor& theColor ) const
239 {
240   QString anObjName = GetObjPyName();
241   theScript << QString( "%1.SetSectionColor( %2, QColor( %3, %4, %5, %6 ) )" )
242               .arg( anObjName ).arg( theSectIndex )
243               .arg( theColor.red()  ).arg( theColor.green() )
244               .arg( theColor.blue() ).arg( theColor.alpha() );
245 }