Salome HOME
Copyrights update
[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 QString Plot2d_Curve::getTableTitle() const
76 {
77   return QString();
78 }
79
80 /*!
81   Sets curve's horizontal title
82 */
83 void Plot2d_Curve::setHorTitle( const QString& title )
84 {
85   myHorTitle = title;
86 }
87
88 /*!
89   Gets curve's horizontal title
90 */
91 QString Plot2d_Curve::getHorTitle() const
92 {
93   return myHorTitle;
94 }
95
96 /*!
97   Sets curve's vertical title
98 */
99 void Plot2d_Curve::setVerTitle( const QString& title )
100 {
101   myVerTitle = title;
102 }
103
104 /*!
105   Gets curve's vertical title
106 */
107 QString Plot2d_Curve::getVerTitle() const
108 {
109   return myVerTitle;
110 }
111
112 /*!
113   Sets curve's horizontal units
114 */
115 void Plot2d_Curve::setHorUnits( const QString& units )
116 {
117   myHorUnits = units;
118 }
119
120 /*!
121   Gets curve's horizontal units
122 */
123 QString Plot2d_Curve::getHorUnits() const
124 {
125   return myHorUnits;
126 }
127
128 /*!
129   Sets curve's vertical units
130 */
131 void Plot2d_Curve::setVerUnits( const QString& units )
132 {
133   myVerUnits = units;
134 }
135
136 /*!
137   Gets curve's vertical units
138 */
139 QString Plot2d_Curve::getVerUnits() const
140 {
141   return myVerUnits;
142 }
143
144 /*!
145   Adds one point for curve.
146 */
147 void Plot2d_Curve::addPoint(double theX, double theY)
148 {
149   Plot2d_Point aPoint;
150   aPoint.x = theX;
151   aPoint.y = theY;
152   myPoints.append(aPoint);
153 }
154
155 /*!
156   Insert one point for curve on some position.
157 */
158 void Plot2d_Curve::insertPoint(int thePos, double theX, double theY)
159 {
160   Plot2d_Point aPoint;
161   aPoint.x = theX;
162   aPoint.y = theY;
163
164   QValueList<Plot2d_Point>::iterator aIt;
165   int aCurrent = 0;
166   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
167     if (thePos == aCurrent) {
168       myPoints.insert(aIt, aPoint);
169       return;
170     }
171     aCurrent++;  
172   }
173   myPoints.append(aPoint);
174 }
175
176 /*!
177   Delete one point for curve on some position.
178 */
179 void Plot2d_Curve::deletePoint(int thePos)
180 {
181   QValueList<Plot2d_Point>::iterator aIt;
182   int aCurrent = 0;
183   for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
184     if (thePos == aCurrent) {
185       myPoints.remove(aIt);
186       return;
187     }
188     aCurrent++;  
189   }
190 }
191
192 /*!
193   Remove all points for curve.
194 */
195 void Plot2d_Curve::clearAllPoints()
196 {
197   myPoints.clear();
198 }
199
200 /*!
201   Gets curve's data : abscissas of points
202 */
203 pointList Plot2d_Curve::getPointList() const
204 {
205   return myPoints;
206 }
207
208 /*!
209   Sets curve's data. 
210 */
211 void Plot2d_Curve::setData( const double* hData, const double* vData, long size )
212 {
213   clearAllPoints();
214   for(long i = 0; i < size; i++) addPoint(hData[i], vData[i]);
215 }
216
217 /*!
218   Gets curve's data : abscissas of points
219 */
220 double* Plot2d_Curve::horData() const
221 {
222   int aNPoints = nbPoints();
223   double* aX = new double[aNPoints];
224   for (int i = 0; i < aNPoints; i++) {
225     aX[i] = myPoints[i].x;
226   }
227   return aX;
228 }
229
230 /*!
231   Gets curve's data : ordinates of points
232 */
233 double* Plot2d_Curve::verData() const
234 {
235   int aNPoints = nbPoints();
236   double* aY = new double[aNPoints];
237   for (int i = 0; i < aNPoints; i++) {
238     aY[i] = myPoints[i].y;
239   }
240   return aY;
241 }
242
243 /*!
244   Gets curve's data : number of points
245 */
246 int Plot2d_Curve::nbPoints() const
247 {
248   return myPoints.count();
249 }
250
251 /*!
252   Returns true if curve has no data
253 */
254 bool Plot2d_Curve::isEmpty() const
255 {
256   return myPoints.isEmpty();
257 }
258
259 /*!
260   Sets curve's AutoAssign flag - in this case attributes will be set automatically
261 */
262 void Plot2d_Curve::setAutoAssign( bool on )
263 {
264   myAutoAssign = on;
265 }
266
267 /*!
268   Gets curve's AutoAssign flag state
269 */
270 bool Plot2d_Curve::isAutoAssign() const
271 {
272   return myAutoAssign;
273 }
274
275 /*!
276   Sets curve's color ( and resets AutoAssign flag )
277 */
278 void Plot2d_Curve::setColor( const QColor& color )
279 {
280   myColor = color;
281   myAutoAssign = false;
282 }
283
284 /*!
285   Gets curve's color
286 */
287 QColor Plot2d_Curve::getColor() const
288 {
289   return myColor;
290 }
291
292 /*!
293   Sets curve's marker ( and resets AutoAssign flag )
294 */
295 void Plot2d_Curve::setMarker( MarkerType marker )
296 {
297   myMarker = marker;
298   myAutoAssign = false;
299 }
300
301 /*!
302   Gets curve's marker
303 */
304 Plot2d_Curve::MarkerType Plot2d_Curve::getMarker() const
305 {
306   return myMarker;
307 }
308
309 /*!
310   Sets curve's line type and width ( and resets AutoAssign flag )
311   NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
312          A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
313          algorithm for diagonals. 
314          For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
315 */
316 void Plot2d_Curve::setLine( LineType line, const int lineWidth )
317 {
318   myLine = line;
319   myLineWidth = lineWidth;
320   if ( myLineWidth < 0 ) myLineWidth = 0;
321   myAutoAssign = false;
322 }
323
324 /*!
325   Gets curve's line type
326 */
327 Plot2d_Curve::LineType Plot2d_Curve::getLine() const
328 {
329   return myLine;
330 }
331
332 /*!
333   Gets curve's line width
334 */
335 int Plot2d_Curve::getLineWidth() const
336 {
337   return myLineWidth;
338 }
339
340 /*!
341   Sets curve's y axis
342 */
343 void Plot2d_Curve::setYAxis(QwtPlot::Axis theYAxis)
344 {
345   if(theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
346     myYAxis = theYAxis;
347 }
348
349 /*!
350   Gets curve's y axis
351 */
352 QwtPlot::Axis Plot2d_Curve::getYAxis() const
353 {
354   return myYAxis;
355 }