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