Salome HOME
Initial version
[modules/gui.git] / src / Plot2d / Plot2d_Curve.cxx
1 #include "Plot2d_Curve.h"
2
3 /*!
4   Constructor
5 */
6 Plot2d_Curve::Plot2d_Curve()
7 : myHorTitle( "" ), myVerTitle( "" ), 
8 myHorUnits( "" ), myVerUnits( "" ), 
9 myAutoAssign( true ), myColor( 0,0,0 ), myMarker( Circle ), myLine( Solid ), myLineWidth( 0 ),
10 myYAxis( QwtPlot::yLeft )
11 {
12 }
13
14 /*!
15   Destructor
16 */
17 Plot2d_Curve::~Plot2d_Curve()
18 {
19 }
20
21 /*!
22   Copy constructor. Makes deep copy of data.
23 */
24 Plot2d_Curve::Plot2d_Curve( const Plot2d_Curve& curve )
25 {
26   myAutoAssign = curve.isAutoAssign();
27   myHorTitle   = curve.getHorTitle();
28   myVerTitle   = curve.getVerTitle();
29   myHorUnits   = curve.getHorUnits();
30   myVerUnits   = curve.getVerUnits();
31   myColor      = curve.getColor();
32   myMarker     = curve.getMarker();
33   myLine       = curve.getLine();
34   myLineWidth  = curve.getLineWidth();
35   myPoints     = curve.getPointList();
36 }
37
38 /*!
39   operator=. Makes deep copy of data.
40 */
41 Plot2d_Curve& Plot2d_Curve::operator=( const Plot2d_Curve& curve )
42 {
43   myAutoAssign = curve.isAutoAssign();
44   myHorTitle   = curve.getHorTitle();
45   myVerTitle   = curve.getVerTitle();
46   myHorUnits   = curve.getHorUnits();
47   myVerUnits   = curve.getVerUnits();
48   myColor      = curve.getColor();
49   myMarker     = curve.getMarker();
50   myLine       = curve.getLine();
51   myLineWidth  = curve.getLineWidth();
52   myPoints     = curve.getPointList();
53   return *this;
54 }
55
56 /*!
57   Sets curve's horizontal title
58 */
59 void Plot2d_Curve::setHorTitle( const QString& title )
60 {
61   myHorTitle = title;
62 }
63
64 /*!
65   Gets curve's horizontal title
66 */
67 QString Plot2d_Curve::getHorTitle() const
68 {
69   return myHorTitle;
70 }
71
72 /*!
73   Sets curve's vertical title
74 */
75 void Plot2d_Curve::setVerTitle( const QString& title )
76 {
77   myVerTitle = title;
78 }
79
80 /*!
81   Gets curve's vertical title
82 */
83 QString Plot2d_Curve::getVerTitle() const
84 {
85   return myVerTitle;
86 }
87
88 /*!
89   Sets curve's horizontal units
90 */
91 void Plot2d_Curve::setHorUnits( const QString& units )
92 {
93   myHorUnits = units;
94 }
95
96 /*!
97   Gets curve's horizontal units
98 */
99 QString Plot2d_Curve::getHorUnits() const
100 {
101   return myHorUnits;
102 }
103
104 /*!
105   Sets curve's vertical units
106 */
107 void Plot2d_Curve::setVerUnits( const QString& units )
108 {
109   myVerUnits = units;
110 }
111
112 /*!
113   Gets curve's vertical units
114 */
115 QString Plot2d_Curve::getVerUnits() const
116 {
117   return myVerUnits;
118 }
119
120 /*!
121   Adds one point for curve.
122 */
123 void Plot2d_Curve::addPoint(double theX, double theY)
124 {
125   Plot2d_Point aPoint;
126   aPoint.x = theX;
127   aPoint.y = theY;
128   myPoints.append(aPoint);
129 }
130
131 /*!
132   Insert one point for curve on some position.
133 */
134 void Plot2d_Curve::insertPoint(int thePos, double theX, double theY)
135 {
136   Plot2d_Point aPoint;
137   aPoint.x = theX;
138   aPoint.y = theY;
139
140   QValueList<Plot2d_Point>::iterator aIt;
141   int aCurrent = 0;
142   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
143     if (thePos == aCurrent) {
144       myPoints.insert(aIt, aPoint);
145       return;
146     }
147     aCurrent++;  
148   }
149   myPoints.append(aPoint);
150 }
151
152 /*!
153   Delete one point for curve on some position.
154 */
155 void Plot2d_Curve::deletePoint(int thePos)
156 {
157   QValueList<Plot2d_Point>::iterator aIt;
158   int aCurrent = 0;
159   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
160     if (thePos == aCurrent) {
161       myPoints.remove(aIt);
162       return;
163     }
164     aCurrent++;  
165   }
166 }
167
168 /*!
169   Remove all points for curve.
170 */
171 void Plot2d_Curve::clearAllPoints()
172 {
173   myPoints.clear();
174 }
175
176 /*!
177   Gets curve's data : abscissas of points
178 */
179 pointList Plot2d_Curve::getPointList() const
180 {
181   return myPoints;
182 }
183
184 /*!
185   Gets curve's data : abscissas of points
186 */
187 double* Plot2d_Curve::horData() const
188 {
189   int aNPoints = nbPoints();
190   double* aX = new double[aNPoints];
191   for (int i = 0; i < aNPoints; i++) {
192     aX[i] = myPoints[i].x;
193   }
194   return aX;
195 }
196
197 /*!
198   Gets curve's data : ordinates of points
199 */
200 double* Plot2d_Curve::verData() const
201 {
202   int aNPoints = nbPoints();
203   double* aY = new double[aNPoints];
204   for (int i = 0; i < aNPoints; i++) {
205     aY[i] = myPoints[i].y;
206   }
207   return aY;
208 }
209
210 /*!
211   Gets curve's data : number of points
212 */
213 int Plot2d_Curve::nbPoints() const
214 {
215   return myPoints.count();
216 }
217
218 /*!
219   Returns true if curve has no data
220 */
221 bool Plot2d_Curve::isEmpty() const
222 {
223   return myPoints.isEmpty();
224 }
225
226 /*!
227   Sets curve's AutoAssign flag - in this case attributes will be set automatically
228 */
229 void Plot2d_Curve::setAutoAssign( bool on )
230 {
231   myAutoAssign = on;
232 }
233
234 /*!
235   Gets curve's AutoAssign flag state
236 */
237 bool Plot2d_Curve::isAutoAssign() const
238 {
239   return myAutoAssign;
240 }
241
242 /*!
243   Sets curve's color ( and resets AutoAssign flag )
244 */
245 void Plot2d_Curve::setColor( const QColor color )
246 {
247   myColor = color;
248   myAutoAssign = false;
249 }
250
251 /*!
252   Gets curve's color
253 */
254 QColor Plot2d_Curve::getColor() const
255 {
256   return myColor;
257 }
258
259 /*!
260   Sets curve's marker ( and resets AutoAssign flag )
261 */
262 void Plot2d_Curve::setMarker( MarkerType marker )
263 {
264   myMarker = marker;
265   myAutoAssign = false;
266 }
267
268 /*!
269   Gets curve's marker
270 */
271 Plot2d_Curve::MarkerType Plot2d_Curve::getMarker() const
272 {
273   return myMarker;
274 }
275
276 /*!
277   Sets curve's line type and width ( and resets AutoAssign flag )
278   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
279          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
280          algorithm for diagonals. 
281          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
282 */
283 void Plot2d_Curve::setLine( LineType line, const int lineWidth )
284 {
285   myLine = line;
286   myLineWidth = lineWidth;
287   if ( myLineWidth < 0 ) myLineWidth = 0;
288   myAutoAssign = false;
289 }
290
291 /*!
292   Gets curve's line type
293 */
294 Plot2d_Curve::LineType Plot2d_Curve::getLine() const
295 {
296   return myLine;
297 }
298
299 /*!
300   Gets curve's line width
301 */
302 int Plot2d_Curve::getLineWidth() const
303 {
304   return myLineWidth;
305 }
306
307 /*!
308   Sets curve's y axis
309 */
310 void Plot2d_Curve::setYAxis(QwtPlot::Axis theYAxis)
311 {
312   if(theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
313     myYAxis = theYAxis;
314 }
315
316 /*!
317   Gets curve's y axis
318 */
319 QwtPlot::Axis Plot2d_Curve::getYAxis() const
320 {
321   return myYAxis;
322 }