Salome HOME
Preferences
[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   Sets curve's data. 
186 */
187 void Plot2d_Curve::setData( const double* hData, const double* vData, long size )
188 {
189   clearAllPoints();
190   for(long i = 0; i < size; i++) addPoint(hData[i], vData[i]);
191 }
192
193 /*!
194   Gets curve's data : abscissas of points
195 */
196 double* Plot2d_Curve::horData() const
197 {
198   int aNPoints = nbPoints();
199   double* aX = new double[aNPoints];
200   for (int i = 0; i < aNPoints; i++) {
201     aX[i] = myPoints[i].x;
202   }
203   return aX;
204 }
205
206 /*!
207   Gets curve's data : ordinates of points
208 */
209 double* Plot2d_Curve::verData() const
210 {
211   int aNPoints = nbPoints();
212   double* aY = new double[aNPoints];
213   for (int i = 0; i < aNPoints; i++) {
214     aY[i] = myPoints[i].y;
215   }
216   return aY;
217 }
218
219 /*!
220   Gets curve's data : number of points
221 */
222 int Plot2d_Curve::nbPoints() const
223 {
224   return myPoints.count();
225 }
226
227 /*!
228   Returns true if curve has no data
229 */
230 bool Plot2d_Curve::isEmpty() const
231 {
232   return myPoints.isEmpty();
233 }
234
235 /*!
236   Sets curve's AutoAssign flag - in this case attributes will be set automatically
237 */
238 void Plot2d_Curve::setAutoAssign( bool on )
239 {
240   myAutoAssign = on;
241 }
242
243 /*!
244   Gets curve's AutoAssign flag state
245 */
246 bool Plot2d_Curve::isAutoAssign() const
247 {
248   return myAutoAssign;
249 }
250
251 /*!
252   Sets curve's color ( and resets AutoAssign flag )
253 */
254 void Plot2d_Curve::setColor( const QColor color )
255 {
256   myColor = color;
257   myAutoAssign = false;
258 }
259
260 /*!
261   Gets curve's color
262 */
263 QColor Plot2d_Curve::getColor() const
264 {
265   return myColor;
266 }
267
268 /*!
269   Sets curve's marker ( and resets AutoAssign flag )
270 */
271 void Plot2d_Curve::setMarker( MarkerType marker )
272 {
273   myMarker = marker;
274   myAutoAssign = false;
275 }
276
277 /*!
278   Gets curve's marker
279 */
280 Plot2d_Curve::MarkerType Plot2d_Curve::getMarker() const
281 {
282   return myMarker;
283 }
284
285 /*!
286   Sets curve's line type and width ( and resets AutoAssign flag )
287   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
288          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
289          algorithm for diagonals. 
290          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
291 */
292 void Plot2d_Curve::setLine( LineType line, const int lineWidth )
293 {
294   myLine = line;
295   myLineWidth = lineWidth;
296   if ( myLineWidth < 0 ) myLineWidth = 0;
297   myAutoAssign = false;
298 }
299
300 /*!
301   Gets curve's line type
302 */
303 Plot2d_Curve::LineType Plot2d_Curve::getLine() const
304 {
305   return myLine;
306 }
307
308 /*!
309   Gets curve's line width
310 */
311 int Plot2d_Curve::getLineWidth() const
312 {
313   return myLineWidth;
314 }
315
316 /*!
317   Sets curve's y axis
318 */
319 void Plot2d_Curve::setYAxis(QwtPlot::Axis theYAxis)
320 {
321   if(theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
322     myYAxis = theYAxis;
323 }
324
325 /*!
326   Gets curve's y axis
327 */
328 QwtPlot::Axis Plot2d_Curve::getYAxis() const
329 {
330   return myYAxis;
331 }