Qwt User's Guide  6.2.0
qwt_plot_item.cpp
1 /******************************************************************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_plot_item.h"
11 #include "qwt_text.h"
12 #include "qwt_plot.h"
13 #include "qwt_legend_data.h"
14 #include "qwt_scale_map.h"
15 #include "qwt_graphic.h"
16 
17 #include <qpainter.h>
18 
19 class QwtPlotItem::PrivateData
20 {
21  public:
22  PrivateData()
23  : plot( NULL )
24  , isVisible( true )
25  , renderThreadCount( 1 )
26  , z( 0.0 )
27  , xAxisId( QwtAxis::XBottom )
28  , yAxisId( QwtAxis::YLeft )
29  , legendIconSize( 8, 8 )
30  {
31  }
32 
33  mutable QwtPlot* plot;
34 
35  bool isVisible;
36 
37  QwtPlotItem::ItemAttributes attributes;
39 
40  QwtPlotItem::RenderHints renderHints;
41  uint renderThreadCount;
42 
43  double z;
44 
45  QwtAxisId xAxisId;
46  QwtAxisId yAxisId;
47 
48  QwtText title;
49  QSize legendIconSize;
50 };
51 
56 {
57  m_data = new PrivateData;
58 }
59 
64 QwtPlotItem::QwtPlotItem( const QString& title )
65 {
66  m_data = new PrivateData;
67  m_data->title = title;
68 }
69 
75 {
76  m_data = new PrivateData;
77  m_data->title = title;
78 }
79 
82 {
83  attach( NULL );
84  delete m_data;
85 }
86 
99 {
100  if ( plot == m_data->plot )
101  return;
102 
103  if ( m_data->plot )
104  m_data->plot->attachItem( this, false );
105 
106  m_data->plot = plot;
107 
108  if ( m_data->plot )
109  m_data->plot->attachItem( this, true );
110 }
111 
120 {
121  attach( NULL );
122 }
123 
136 int QwtPlotItem::rtti() const
137 {
138  return Rtti_PlotItem;
139 }
140 
143 {
144  return m_data->plot;
145 }
146 
152 double QwtPlotItem::z() const
153 {
154  return m_data->z;
155 }
156 
165 void QwtPlotItem::setZ( double z )
166 {
167  if ( m_data->z != z )
168  {
169  if ( m_data->plot ) // update the z order
170  m_data->plot->attachItem( this, false );
171 
172  m_data->z = z;
173 
174  if ( m_data->plot )
175  m_data->plot->attachItem( this, true );
176 
177  itemChanged();
178  }
179 }
180 
187 void QwtPlotItem::setTitle( const QString& title )
188 {
189  setTitle( QwtText( title ) );
190 }
191 
198 void QwtPlotItem::setTitle( const QwtText& title )
199 {
200  if ( m_data->title != title )
201  {
202  m_data->title = title;
203 
204  legendChanged();
205 #if 0
206  itemChanged();
207 #endif
208  }
209 }
210 
216 {
217  return m_data->title;
218 }
219 
229 {
230  if ( m_data->attributes.testFlag( attribute ) != on )
231  {
232  if ( on )
233  m_data->attributes |= attribute;
234  else
235  m_data->attributes &= ~attribute;
236 
237  if ( attribute == QwtPlotItem::Legend )
238  {
239  if ( on )
240  {
241  legendChanged();
242  }
243  else
244  {
245  /*
246  In the special case of taking an item from
247  the legend we can't use legendChanged() as
248  it depends on QwtPlotItem::Legend being enabled
249  */
250  if ( m_data->plot )
251  m_data->plot->updateLegend( this );
252  }
253  }
254 
255  itemChanged();
256  }
257 }
258 
267 {
268  return m_data->attributes.testFlag( attribute );
269 }
270 
280 {
281  if ( m_data->interests.testFlag( interest ) != on )
282  {
283  if ( on )
284  m_data->interests |= interest;
285  else
286  m_data->interests &= ~interest;
287 
288  itemChanged();
289  }
290 }
291 
300 {
301  return m_data->interests.testFlag( interest );
302 }
303 
313 {
314  if ( m_data->renderHints.testFlag( hint ) != on )
315  {
316  if ( on )
317  m_data->renderHints |= hint;
318  else
319  m_data->renderHints &= ~hint;
320 
321  itemChanged();
322  }
323 }
324 
333 {
334  return m_data->renderHints.testFlag( hint );
335 }
336 
350 void QwtPlotItem::setRenderThreadCount( uint numThreads )
351 {
352  m_data->renderThreadCount = numThreads;
353 }
354 
361 {
362  return m_data->renderThreadCount;
363 }
364 
373 void QwtPlotItem::setLegendIconSize( const QSize& size )
374 {
375  if ( m_data->legendIconSize != size )
376  {
377  m_data->legendIconSize = size;
378  legendChanged();
379  }
380 }
381 
387 {
388  return m_data->legendIconSize;
389 }
390 
403  int index, const QSizeF& size ) const
404 {
405  Q_UNUSED( index )
406  Q_UNUSED( size )
407 
408  return QwtGraphic();
409 }
410 
423  const QBrush& brush, const QSizeF& size ) const
424 {
425  QwtGraphic icon;
426  if ( !size.isEmpty() )
427  {
428  icon.setDefaultSize( size );
429 
430  QRectF r( 0, 0, size.width(), size.height() );
431 
432  QPainter painter( &icon );
433  painter.fillRect( r, brush );
434  }
435 
436  return icon;
437 }
438 
441 {
442  setVisible( true );
443 }
444 
447 {
448  setVisible( false );
449 }
450 
457 void QwtPlotItem::setVisible( bool on )
458 {
459  if ( on != m_data->isVisible )
460  {
461  m_data->isVisible = on;
462  itemChanged();
463  }
464 }
465 
471 {
472  return m_data->isVisible;
473 }
474 
482 {
483  if ( m_data->plot )
484  m_data->plot->autoRefresh();
485 }
486 
492 {
493  if ( testItemAttribute( QwtPlotItem::Legend ) && m_data->plot )
494  m_data->plot->updateLegend( this );
495 }
496 
507 void QwtPlotItem::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
508 {
509  if ( QwtAxis::isXAxis( xAxisId ) )
510  m_data->xAxisId = xAxisId;
511 
512  if ( QwtAxis::isYAxis( yAxisId ) )
513  m_data->yAxisId = yAxisId;
514 
515  itemChanged();
516 }
517 
526 void QwtPlotItem::setXAxis( QwtAxisId axisId )
527 {
528  if ( QwtAxis::isXAxis( axisId ) )
529  {
530  m_data->xAxisId = axisId;
531  itemChanged();
532  }
533 }
534 
543 void QwtPlotItem::setYAxis( QwtAxisId axisId )
544 {
545  if ( QwtAxis::isYAxis( axisId ) )
546  {
547  m_data->yAxisId = axisId;
548  itemChanged();
549  }
550 }
551 
553 QwtAxisId QwtPlotItem::xAxis() const
554 {
555  return m_data->xAxisId;
556 }
557 
559 QwtAxisId QwtPlotItem::yAxis() const
560 {
561  return m_data->yAxisId;
562 }
563 
569 {
570  return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
571 }
572 
596  const QwtScaleMap& yMap, const QRectF& canvasRect,
597  double& left, double& top, double& right, double& bottom ) const
598 {
599  Q_UNUSED( xMap );
600  Q_UNUSED( yMap );
601  Q_UNUSED( canvasRect );
602 
603  // use QMargins, when we don't need to support Qt < 4.6 anymore
604  left = top = right = bottom = 0.0;
605 }
606 
627 {
628  QwtLegendData data;
629 
630  QwtText label = title();
631  label.setRenderFlags( label.renderFlags() & Qt::AlignLeft );
632 
633  data.setValue( QwtLegendData::TitleRole,
634  QVariant::fromValue( label ) );
635 
636  const QwtGraphic graphic = legendIcon( 0, legendIconSize() );
637  if ( !graphic.isNull() )
638  {
639  data.setValue( QwtLegendData::IconRole,
640  QVariant::fromValue( graphic ) );
641  }
642 
644  list += data;
645 
646  return list;
647 }
648 
666  const QwtScaleDiv& yScaleDiv )
667 {
668  Q_UNUSED( xScaleDiv );
669  Q_UNUSED( yScaleDiv );
670 }
671 
691  const QList< QwtLegendData >& data )
692 {
693  Q_UNUSED( item );
694  Q_UNUSED( data );
695 }
696 
706  const QwtScaleMap& yMap ) const
707 {
708  return QRectF( xMap.s1(), yMap.s1(),
709  xMap.sDist(), yMap.sDist() );
710 }
711 
721  const QwtScaleMap& yMap ) const
722 {
723  const QRectF rect( xMap.p1(), yMap.p1(),
724  xMap.pDist(), yMap.pDist() );
725 
726  return rect;
727 }
A paint device for scalable graphics.
Definition: qwt_graphic.h:76
bool isNull() const
void setDefaultSize(const QSizeF &)
Set a default size.
Attributes of an entry on a legend.
void setValue(int role, const QVariant &)
A 2-D plotting widget.
Definition: qwt_plot.h:79
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
QwtAxisId yAxis() const
Return yAxis.
void setLegendIconSize(const QSize &)
QRectF paintRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding paint rectangle of 2 maps.
QFlags< ItemInterest > ItemInterests
void setRenderThreadCount(uint numThreads)
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
void setTitle(const QString &title)
bool isVisible() const
void hide()
Hide the item.
void setYAxis(QwtAxisId)
virtual ~QwtPlotItem()
Destroy the QwtPlotItem.
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
virtual void legendChanged()
const QwtText & title() const
void setXAxis(QwtAxisId)
QRectF scaleRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding scale rectangle of 2 maps.
QFlags< RenderHint > RenderHints
QFlags< ItemAttribute > ItemAttributes
void setZ(double z)
Set the z value.
void setItemAttribute(ItemAttribute, bool on=true)
virtual void setVisible(bool)
QwtPlot * plot() const
Return attached plot.
double z() const
virtual int rtti() const
QwtAxisId xAxis() const
Return xAxis.
bool testItemInterest(ItemInterest) const
void show()
Show the item.
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
Definition: qwt_plot_item.h:78
void detach()
This method detaches a QwtPlotItem from any QwtPlot it has been associated with.
void setItemInterest(ItemInterest, bool on=true)
RenderHint
Render hints.
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &)
Update the item to changes of the axes scale division.
bool testItemAttribute(ItemAttribute) const
void setAxes(QwtAxisId xAxis, QwtAxisId yAxis)
bool testRenderHint(RenderHint) const
void setRenderHint(RenderHint, bool on=true)
virtual void itemChanged()
ItemAttribute
Plot Item Attributes.
@ Legend
The item is represented on the legend.
uint renderThreadCount() const
virtual void getCanvasMarginHint(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const
Calculate a hint for the canvas margin.
void attach(QwtPlot *plot)
Attach the item to a plot.
virtual QRectF boundingRect() const
virtual void updateLegend(const QwtPlotItem *, const QList< QwtLegendData > &)
Update the item to changes of the legend info.
QSize legendIconSize() const
ItemInterest
Plot Item Interests.
A class representing a scale division.
Definition: qwt_scale_div.h:34
A scale map.
Definition: qwt_scale_map.h:27
double pDist() const
double p1() const
Definition: qwt_scale_map.h:99
double s1() const
Definition: qwt_scale_map.h:83
double sDist() const
A class representing a text.
Definition: qwt_text.h:52
int renderFlags() const
Definition: qwt_text.cpp:317
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
bool isYAxis(int axisPos)
Definition: qwt_axis.h:57
@ XBottom
X axis below the canvas.
Definition: qwt_axis.h:30
@ YLeft
Y axis left of the canvas.
Definition: qwt_axis.h:24
bool isXAxis(int axisPos)
Definition: qwt_axis.h:51