]> SALOME platform Git repositories - modules/gui.git/blob - src/Plot2d/Plot2d_Curve.cxx
Salome HOME
4c5d02bd19472f79b1fc503ba9eb058e4c6a78aa
[modules/gui.git] / src / Plot2d / Plot2d_Curve.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "Plot2d_Curve.h"
20 #include <qcolor.h>
21
22 /*!
23   Constructor
24 */
25 Plot2d_Curve::Plot2d_Curve()
26 : myHorTitle( "" ), myVerTitle( "" ), 
27 myHorUnits( "" ), myVerUnits( "" ), 
28 myAutoAssign( true ), myColor( 0,0,0 ), myMarker( Circle ), myLine( Solid ), myLineWidth( 0 ),
29 myYAxis( QwtPlot::yLeft )
30 {
31 }
32
33 /*!
34   Destructor
35 */
36 Plot2d_Curve::~Plot2d_Curve()
37 {
38 }
39
40 /*!
41   Copy constructor. Makes deep copy of data.
42 */
43 Plot2d_Curve::Plot2d_Curve( const Plot2d_Curve& curve )
44 {
45   myAutoAssign = curve.isAutoAssign();
46   myHorTitle   = curve.getHorTitle();
47   myVerTitle   = curve.getVerTitle();
48   myHorUnits   = curve.getHorUnits();
49   myVerUnits   = curve.getVerUnits();
50   myColor      = curve.getColor();
51   myMarker     = curve.getMarker();
52   myLine       = curve.getLine();
53   myLineWidth  = curve.getLineWidth();
54   myPoints     = curve.getPointList();
55 }
56
57 /*!
58   operator=. Makes deep copy of data.
59 */
60 Plot2d_Curve& Plot2d_Curve::operator=( const Plot2d_Curve& curve )
61 {
62   myAutoAssign = curve.isAutoAssign();
63   myHorTitle   = curve.getHorTitle();
64   myVerTitle   = curve.getVerTitle();
65   myHorUnits   = curve.getHorUnits();
66   myVerUnits   = curve.getVerUnits();
67   myColor      = curve.getColor();
68   myMarker     = curve.getMarker();
69   myLine       = curve.getLine();
70   myLineWidth  = curve.getLineWidth();
71   myPoints     = curve.getPointList();
72   return *this;
73 }
74
75 /*!
76   \return title of table
77 */
78 QString Plot2d_Curve::getTableTitle() const
79 {
80   return QString();
81 }
82
83 /*!
84   Sets curve's horizontal title
85 */
86 void Plot2d_Curve::setHorTitle( const QString& title )
87 {
88   myHorTitle = title;
89 }
90
91 /*!
92   Gets curve's horizontal title
93 */
94 QString Plot2d_Curve::getHorTitle() const
95 {
96   return myHorTitle;
97 }
98
99 /*!
100   Sets curve's vertical title
101 */
102 void Plot2d_Curve::setVerTitle( const QString& title )
103 {
104   myVerTitle = title;
105 }
106
107 /*!
108   Gets curve's vertical title
109 */
110 QString Plot2d_Curve::getVerTitle() const
111 {
112   return myVerTitle;
113 }
114
115 /*!
116   Sets curve's horizontal units
117 */
118 void Plot2d_Curve::setHorUnits( const QString& units )
119 {
120   myHorUnits = units;
121 }
122
123 /*!
124   Gets curve's horizontal units
125 */
126 QString Plot2d_Curve::getHorUnits() const
127 {
128   return myHorUnits;
129 }
130
131 /*!
132   Sets curve's vertical units
133 */
134 void Plot2d_Curve::setVerUnits( const QString& units )
135 {
136   myVerUnits = units;
137 }
138
139 /*!
140   Gets curve's vertical units
141 */
142 QString Plot2d_Curve::getVerUnits() const
143 {
144   return myVerUnits;
145 }
146
147 /*!
148   Adds one point for curve.
149 */
150 void Plot2d_Curve::addPoint(double theX, double theY)
151 {
152   Plot2d_Point aPoint;
153   aPoint.x = theX;
154   aPoint.y = theY;
155   myPoints.append(aPoint);
156 }
157
158 /*!
159   Insert one point for curve on some position.
160 */
161 void Plot2d_Curve::insertPoint(int thePos, double theX, double theY)
162 {
163   Plot2d_Point aPoint;
164   aPoint.x = theX;
165   aPoint.y = theY;
166
167   QValueList<Plot2d_Point>::iterator aIt;
168   int aCurrent = 0;
169   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
170     if (thePos == aCurrent) {
171       myPoints.insert(aIt, aPoint);
172       return;
173     }
174     aCurrent++;  
175   }
176   myPoints.append(aPoint);
177 }
178
179 /*!
180   Delete one point for curve on some position.
181 */
182 void Plot2d_Curve::deletePoint(int thePos)
183 {
184   QValueList<Plot2d_Point>::iterator aIt;
185   int aCurrent = 0;
186   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
187     if (thePos == aCurrent) {
188       myPoints.remove(aIt);
189       return;
190     }
191     aCurrent++;  
192   }
193 }
194
195 /*!
196   Remove all points for curve.
197 */
198 void Plot2d_Curve::clearAllPoints()
199 {
200   myPoints.clear();
201 }
202
203 /*!
204   Gets curve's data : abscissas of points
205 */
206 pointList Plot2d_Curve::getPointList() const
207 {
208   return myPoints;
209 }
210
211 /*!
212   Sets curve's data. 
213 */
214 void Plot2d_Curve::setData( const double* hData, const double* vData, long size )
215 {
216   clearAllPoints();
217   for(long i = 0; i < size; i++) addPoint(hData[i], vData[i]);
218 }
219
220 /*!
221   Gets curve's data : abscissas of points
222 */
223 double* Plot2d_Curve::horData() const
224 {
225   int aNPoints = nbPoints();
226   double* aX = new double[aNPoints];
227   for (int i = 0; i < aNPoints; i++) {
228     aX[i] = myPoints[i].x;
229   }
230   return aX;
231 }
232
233 /*!
234   Gets curve's data : ordinates of points
235 */
236 double* Plot2d_Curve::verData() const
237 {
238   int aNPoints = nbPoints();
239   double* aY = new double[aNPoints];
240   for (int i = 0; i < aNPoints; i++) {
241     aY[i] = myPoints[i].y;
242   }
243   return aY;
244 }
245
246 /*!
247   Gets curve's data : number of points
248 */
249 int Plot2d_Curve::nbPoints() const
250 {
251   return myPoints.count();
252 }
253
254 /*!
255   Returns true if curve has no data
256 */
257 bool Plot2d_Curve::isEmpty() const
258 {
259   return myPoints.isEmpty();
260 }
261
262 /*!
263   Sets curve's AutoAssign flag - in this case attributes will be set automatically
264 */
265 void Plot2d_Curve::setAutoAssign( bool on )
266 {
267   myAutoAssign = on;
268 }
269
270 /*!
271   Gets curve's AutoAssign flag state
272 */
273 bool Plot2d_Curve::isAutoAssign() const
274 {
275   return myAutoAssign;
276 }
277
278 /*!
279   Sets curve's color ( and resets AutoAssign flag )
280 */
281 void Plot2d_Curve::setColor( const QColor& color )
282 {
283   myColor = color;
284   myAutoAssign = false;
285 }
286
287 /*!
288   Gets curve's color
289 */
290 QColor Plot2d_Curve::getColor() const
291 {
292   return myColor;
293 }
294
295 /*!
296   Sets curve's marker ( and resets AutoAssign flag )
297 */
298 void Plot2d_Curve::setMarker( MarkerType marker )
299 {
300   myMarker = marker;
301   myAutoAssign = false;
302 }
303
304 /*!
305   Gets curve's marker
306 */
307 Plot2d_Curve::MarkerType Plot2d_Curve::getMarker() const
308 {
309   return myMarker;
310 }
311
312 /*!
313   Sets curve's line type and width ( and resets AutoAssign flag )
314   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
315          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
316          algorithm for diagonals. 
317          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
318 */
319 void Plot2d_Curve::setLine( LineType line, const int lineWidth )
320 {
321   myLine = line;
322   myLineWidth = lineWidth;
323   if ( myLineWidth < 0 ) myLineWidth = 0;
324   myAutoAssign = false;
325 }
326
327 /*!
328   Gets curve's line type
329 */
330 Plot2d_Curve::LineType Plot2d_Curve::getLine() const
331 {
332   return myLine;
333 }
334
335 /*!
336   Gets curve's line width
337 */
338 int Plot2d_Curve::getLineWidth() const
339 {
340   return myLineWidth;
341 }
342
343 /*!
344   Sets curve's y axis
345 */
346 void Plot2d_Curve::setYAxis(QwtPlot::Axis theYAxis)
347 {
348   if(theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
349     myYAxis = theYAxis;
350 }
351
352 /*!
353   Gets curve's y axis
354 */
355 QwtPlot::Axis Plot2d_Curve::getYAxis() const
356 {
357   return myYAxis;
358 }
359
360 /*!
361   Gets curve's minimal abscissa
362 */
363 double Plot2d_Curve::getMinX() const
364 {
365   QValueList<Plot2d_Point>::const_iterator aIt;
366   double aMinX = 1e150;
367   int aCurrent = 0;
368   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
369     if ( (*aIt).x < aMinX )
370       aMinX = (*aIt).x;
371   }
372   return aMinX;
373 }
374
375 /*!
376   Gets curve's minimal ordinate
377 */
378 double Plot2d_Curve::getMinY() const
379 {
380   QValueList<Plot2d_Point>::const_iterator aIt;
381   double aMinY = 1e150;
382   int aCurrent = 0;
383   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
384     if ( (*aIt).y < aMinY )
385       aMinY = (*aIt).y;
386   }
387   return aMinY;
388 }