Salome HOME
Updated copyright comment
[modules/gui.git] / src / Plot2d / Plot2d_Object.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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_Object.cxx
23 //  Author : Natalia ERMOLAEVA, Open CASCADE S.A.S. (natalia.donis@opencascade.com)
24 //
25
26 #include "Plot2d_Object.h"
27
28 // Static members
29 QColor Plot2d_Object::mySelectionColor;
30 QColor Plot2d_Object::myHighlightedLegendTextColor;
31
32
33 /*!
34   Constructor
35 */
36 Plot2d_Object::Plot2d_Object()
37 : myAutoAssign( true ),
38   myHorTitle( "" ), myVerTitle( "" ),
39   myHorUnits( "" ), myVerUnits( "" ),
40   myName( "" ),
41   myXAxis( QwtPlot::xBottom ),
42   myYAxis( QwtPlot::yLeft ),
43   myScale ( 1.0 ),
44   myIsSelected(false)
45 {
46 }
47
48 /*!
49   Destructor
50 */
51 Plot2d_Object::~Plot2d_Object()
52 {
53 }
54
55 /*!
56   Copy constructor. Makes deep copy of data.
57 */
58 Plot2d_Object::Plot2d_Object( const Plot2d_Object& object )
59 {
60   myAutoAssign = object.isAutoAssign();
61   myHorTitle   = object.getHorTitle();
62   myVerTitle   = object.getVerTitle();
63   myHorUnits   = object.getHorUnits();
64   myVerUnits   = object.getVerUnits();
65   myName       = object.getName();
66   myXAxis      = object.getXAxis();
67   myYAxis      = object.getYAxis();
68   myPoints     = object.getPointList();
69   myScale      = object.getScale();
70 }
71
72 /*!
73   operator=. Makes deep copy of data.
74 */
75 Plot2d_Object& Plot2d_Object::operator=( const Plot2d_Object& object )
76 {
77   myAutoAssign = object.isAutoAssign();
78   myHorTitle   = object.getHorTitle();
79   myVerTitle   = object.getVerTitle();
80   myHorUnits   = object.getHorUnits();
81   myVerUnits   = object.getVerUnits();
82   myName       = object.getName();
83   myXAxis      = object.getXAxis();
84   myYAxis      = object.getYAxis();
85   myPoints     = object.getPointList();
86   myScale      = object.getScale();
87   return *this;
88 }
89
90 /*!
91   Auto fill parameters of object by plot view
92 */
93 void Plot2d_Object::autoFill( const QwtPlot* )
94 {
95 }
96
97 /*!
98  * Updates object fields
99  */
100 void Plot2d_Object::updatePlotItem( QwtPlotItem* theItem )
101 {
102   if ( !theItem || theItem->rtti() != rtti() )
103     return;
104
105   if ( theItem->yAxis() != getYAxis() || theItem->xAxis() != getXAxis() ) {
106     theItem->setAxes( getXAxis(), getYAxis() );
107
108     QwtPlot* aPlot = theItem->plot();
109     if ( aPlot ) {
110       theItem->detach();
111       theItem->attach( aPlot );
112     }
113   }
114   QString name = !getName().isEmpty() ? getName() : getVerTitle();
115   if( myScale != 1.0 )
116       name = name + QString("( *%1 )").arg(myScale);
117   theItem->setTitle( name );
118 }
119
120 /*!
121   \return title of table
122 */
123 QString Plot2d_Object::getTableTitle() const
124 {
125   return QString();
126 }
127
128 /*!
129   Sets object's horizontal title
130 */
131 void Plot2d_Object::setHorTitle( const QString& title )
132 {
133   myHorTitle = title;
134 }
135
136 /*!
137   Gets object's horizontal title
138 */
139 QString Plot2d_Object::getHorTitle() const
140 {
141   return myHorTitle;
142 }
143
144 /*!
145   Sets object's vertical title
146 */
147 void Plot2d_Object::setVerTitle( const QString& title )
148 {
149   myVerTitle = title;
150 }
151
152 /*!
153   Gets object's vertical title
154 */
155 QString Plot2d_Object::getVerTitle() const
156 {
157   return myVerTitle;
158 }
159
160 /*!
161   Sets object's horizontal units
162 */
163 void Plot2d_Object::setHorUnits( const QString& units )
164 {
165   myHorUnits = units;
166 }
167
168 /*!
169   Gets object's horizontal units
170 */
171 QString Plot2d_Object::getHorUnits() const
172 {
173   return myHorUnits;
174 }
175
176 /*!
177   Sets object's vertical units
178 */
179 void Plot2d_Object::setVerUnits( const QString& units )
180 {
181   myVerUnits = units;
182 }
183
184 /*!
185   Gets object's vertical units
186 */
187 QString Plot2d_Object::getVerUnits() const
188 {
189   return myVerUnits;
190 }
191
192 /*!
193   Sets object's name
194  */
195 void Plot2d_Object::setName( const QString& theName )
196 {
197   myName = theName;
198 }
199 /*!
200   Gets object's name
201  */
202 QString Plot2d_Object::getName() const
203 {
204   return myName;
205 }
206
207 /*!
208   Sets object's scale factor
209  */
210 void Plot2d_Object::setScale( double theScale )
211 {
212   myScale = theScale;
213 }
214 /*!
215   Gets object's scale factor
216  */
217 double Plot2d_Object::getScale() const
218 {
219   return myScale;
220 }
221
222 /*!
223   Adds one point for object.
224 */
225 void Plot2d_Object::addPoint( double theX, double theY, const QString& theText )
226 {
227   addPoint( Plot2d_Point( theX, theY, theText ) );
228 }
229
230 /*!
231   Adds one point for object.
232 */
233 void Plot2d_Object::addPoint( const Plot2d_Point& thePoint )
234 {
235   myPoints.append( thePoint );
236 }
237
238 /*!
239   Insert one point for object on some position.
240 */
241 void Plot2d_Object::insertPoint( int thePos, double theX, double theY,
242                                  const QString& theText )
243 {
244   insertPoint( thePos, Plot2d_Point( theX, theY, theText ) );
245 }
246
247 /*!
248   Insert one point for object on some position.
249 */
250 void Plot2d_Object::insertPoint( int thePos, const Plot2d_Point& thePoint )
251 {
252   if ( thePos < 0 )
253     myPoints.append( thePoint );
254   else
255     myPoints.insert( thePos, thePoint );
256 }
257
258 /*!
259   Delete one point for object on some position.
260 */
261 void Plot2d_Object::deletePoint(int thePos)
262 {
263   if ( thePos >= 0 && thePos < myPoints.count() )
264     myPoints.removeAt( thePos );
265 }
266
267 /*!
268   Remove all points for object.
269 */
270 void Plot2d_Object::clearAllPoints()
271 {
272   myPoints.clear();
273 }
274
275 /*!
276   Gets object's data : abscissas of points
277 */
278 pointList Plot2d_Object::getPointList() const
279 {
280   return myPoints;
281 }
282
283 /*!
284   Gets points by index.
285 */
286
287 Plot2d_Point& Plot2d_Object::getPoint(int index) {
288         return myPoints[index];
289 }
290
291 /*!
292   Gets object's data : abscissas of points
293 */
294 void Plot2d_Object::setPointList( const pointList& points )
295 {
296   myPoints = points;
297 }
298
299 /*!
300   Sets object's data.
301 */
302 void Plot2d_Object::setData( const double* hData, const double* vData, long size, const QStringList& lst )
303 {
304   clearAllPoints();
305   QStringList::const_iterator anIt = lst.begin(), aLast = lst.end();
306   for ( long i = 0; i < size; i++, anIt++ )
307     addPoint( hData[i], vData[i], anIt==aLast ? QString() : *anIt );
308 }
309
310 /*!
311   Gets object's data : abscissas of points
312 */
313 double* Plot2d_Object::horData() const
314 {
315   int aNPoints = nbPoints();
316   double* aX = new double[aNPoints];
317   for (int i = 0; i < aNPoints; i++) {
318     aX[i] = myPoints[i].x;
319   }
320   return aX;
321 }
322
323 /*!
324   Gets object's data : ordinates of points
325 */
326 double* Plot2d_Object::verData() const
327 {
328   int aNPoints = nbPoints();
329   double* aY = new double[aNPoints];
330   for (int i = 0; i < aNPoints; i++) {
331     aY[i] = myScale * myPoints[i].y;
332   }
333   return aY;
334 }
335
336 /*!
337   Gets object's data
338 */
339 long Plot2d_Object::getData( double** theX, double** theY ) const
340 {
341   int aNPoints = nbPoints();
342   *theX = new double[aNPoints];
343   *theY = new double[aNPoints];
344   for (int i = 0; i < aNPoints; i++) {
345     (*theX)[i] = myPoints[i].x;
346     (*theY)[i] = myScale * myPoints[i].y;
347   }
348   return aNPoints;
349 }
350
351 /*!
352   Changes text assigned to point of object
353   \param ind -- index of point
354   \param txt -- new text
355 */
356 void Plot2d_Object::setText( const int ind, const QString& txt )
357 {
358   if ( ind >= 0 && ind < myPoints.count() )
359     myPoints[ind].text = txt;
360 }
361
362 /*!
363   \return text assigned to point
364   \param ind -- index of point
365 */
366 QString Plot2d_Object::text( const int ind ) const
367 {
368   return ( ind >= 0 && ind < myPoints.count() ) ? myPoints[ind].text : QString();
369 }
370
371 /*!
372   Gets object's data : number of points
373 */
374 int Plot2d_Object::nbPoints() const
375 {
376   return myPoints.count();
377 }
378
379 /*!
380   Returns true if object has no data
381 */
382 bool Plot2d_Object::isEmpty() const
383 {
384   return myPoints.isEmpty();
385 }
386
387 /*!
388   Sets object's AutoAssign flag - in this case attributes will be set automatically
389 */
390 void Plot2d_Object::setAutoAssign( bool on )
391 {
392   myAutoAssign = on;
393 }
394
395 /*!
396   Gets object's AutoAssign flag state
397 */
398 bool Plot2d_Object::isAutoAssign() const
399 {
400   return myAutoAssign;
401 }
402
403 /*!
404   Sets object's x axis
405 */
406 void Plot2d_Object::setXAxis(QwtPlot::Axis theXAxis)
407 {
408   if (theXAxis == QwtPlot::xBottom || theXAxis == QwtPlot::xTop)
409     myXAxis = theXAxis;
410 }
411
412 /*!
413   Gets object's x axis
414 */
415 QwtPlot::Axis Plot2d_Object::getXAxis() const
416 {
417   return myXAxis;
418 }
419
420 /*!
421   Sets object's y axis
422 */
423 void Plot2d_Object::setYAxis(QwtPlot::Axis theYAxis)
424 {
425   if (theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
426     myYAxis = theYAxis;
427 }
428
429 /*!
430   Gets object's y axis
431 */
432 QwtPlot::Axis Plot2d_Object::getYAxis() const
433 {
434   return myYAxis;
435 }
436
437 /*!
438   Gets object's minimal abscissa
439 */
440 double Plot2d_Object::getMinX() const
441 {
442   double aMinX = 1e150;
443   pointList::const_iterator aIt;
444   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt)
445     aMinX = qMin( aMinX, (*aIt).x );
446   return aMinX;
447 }
448
449 /*!
450   Gets object's maximal abscissa
451 */
452 double Plot2d_Object::getMaxX() const
453 {
454   double aMaxX = -1e150;
455   pointList::const_iterator aIt;
456   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt)
457     aMaxX = qMax( aMaxX, (*aIt).x );
458   return aMaxX;
459 }
460
461 /*!
462   Gets object's minimal ordinate
463 */
464 double Plot2d_Object::getMinY() const
465 {
466   double aMinY = 1e150;
467   pointList::const_iterator aIt;
468   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt)
469     aMinY = qMin( aMinY, myScale * (*aIt).y );
470   return aMinY;
471 }
472
473 /*!
474   Gets object's maximal ordinate
475 */
476 double Plot2d_Object::getMaxY() const
477 {
478   double aMaxY = -1e150;
479   pointList::const_iterator aIt;
480   for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt)
481     aMaxY = qMax( aMaxY, myScale * (*aIt).y );
482   return aMaxY;
483 }
484
485 /*!
486   Sets object's selected property
487 */
488 void Plot2d_Object::setSelected(const bool on) {
489   myIsSelected = on;
490 }
491
492 /*!
493   Gets object's selected property
494 */
495 bool Plot2d_Object::isSelected() const {
496   return myIsSelected;
497 }
498
499 /*!
500  * Sets selection color of the object.
501 */
502 void Plot2d_Object::setSelectionColor(const QColor& c) {
503   mySelectionColor = c;
504 }
505
506 /*!
507  * Return selection color of the object.
508 */
509 QColor Plot2d_Object::selectionColor() {
510   return mySelectionColor;
511 }
512
513 /*!
514  * Sets font color of the selected legend item.
515 */
516 void Plot2d_Object::setHighlightedLegendTextColor(const QColor& c) {
517   myHighlightedLegendTextColor = c;
518 }
519
520 /*!
521  * Sets font color of the selected legend item.
522 */
523 QColor Plot2d_Object::highlightedLegendTextColor() {
524   return myHighlightedLegendTextColor;
525 }