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