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