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