Qwt User's Guide: qwt_plot_item.cpp Source File

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;
38 QwtPlotItem::ItemInterests interests;
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
55 QwtPlotItem::QwtPlotItem()
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
74 QwtPlotItem::QwtPlotItem( const QwtText& title )
75 {
76 m_data = new PrivateData;
77 m_data->title = title;
78 }
79
81 QwtPlotItem::~QwtPlotItem()
82 {
83 attach( NULL );
84 delete m_data;
85 }
86
98 void QwtPlotItem::attach( QwtPlot* plot )
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
119 void QwtPlotItem::detach()
120 {
121 attach( NULL );
122 }
123
136 int QwtPlotItem::rtti() const
137 {
138 return Rtti_PlotItem;
139 }
140
142 QwtPlot* QwtPlotItem::plot() const
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
215 const QwtText& QwtPlotItem::title() const
216 {
217 return m_data->title;
218 }
219
228 void QwtPlotItem::setItemAttribute( ItemAttribute attribute, bool on )
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
266 bool QwtPlotItem::testItemAttribute( ItemAttribute attribute ) const
267 {
268 return m_data->attributes.testFlag( attribute );
269 }
270
279 void QwtPlotItem::setItemInterest( ItemInterest interest, bool on )
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
299 bool QwtPlotItem::testItemInterest( ItemInterest interest ) const
300 {
301 return m_data->interests.testFlag( interest );
302 }
303
312 void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
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
332 bool QwtPlotItem::testRenderHint( RenderHint hint ) const
333 {
334 return m_data->renderHints.testFlag( hint );
335 }
336
350 void QwtPlotItem::setRenderThreadCount( uint numThreads )
351 {
352 m_data->renderThreadCount = numThreads;
353 }
354
360 uint QwtPlotItem::renderThreadCount() const
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
386 QSize QwtPlotItem::legendIconSize() const
387 {
388 return m_data->legendIconSize;
389 }
390
402 QwtGraphic QwtPlotItem::legendIcon(
403 int index, const QSizeF& size ) const
404 {
405 Q_UNUSED( index )
406 Q_UNUSED( size )
407
408 return QwtGraphic();
409 }
410
422 QwtGraphic QwtPlotItem::defaultIcon(
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
440 void QwtPlotItem::show()
441 {
442 setVisible( true );
443 }
444
446 void QwtPlotItem::hide()
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
470 bool QwtPlotItem::isVisible() const
471 {
472 return m_data->isVisible;
473 }
474
481 void QwtPlotItem::itemChanged()
482 {
483 if ( m_data->plot )
484 m_data->plot->autoRefresh();
485 }
486
491 void QwtPlotItem::legendChanged()
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
568 QRectF QwtPlotItem::boundingRect() const
569 {
570 return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
571 }
572
595 void QwtPlotItem::getCanvasMarginHint( const QwtScaleMap& xMap,
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
626 QList< QwtLegendData > QwtPlotItem::legendData() const
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
643 QList< QwtLegendData > list;
644 list += data;
645
646 return list;
647 }
648
665 void QwtPlotItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
666 const QwtScaleDiv& yScaleDiv )
667 {
668 Q_UNUSED( xScaleDiv );
669 Q_UNUSED( yScaleDiv );
670 }
671
690 void QwtPlotItem::updateLegend( const QwtPlotItem* item,
691 const QList< QwtLegendData >& data )
692 {
693 Q_UNUSED( item );
694 Q_UNUSED( data );
695 }
696
705 QRectF QwtPlotItem::scaleRect( const QwtScaleMap& xMap,
706 const QwtScaleMap& yMap ) const
707 {
708 return QRectF( xMap.s1(), yMap.s1(),
709 xMap.sDist(), yMap.sDist() );
710 }
711
720 QRectF QwtPlotItem::paintRect( const QwtScaleMap& xMap,
721 const QwtScaleMap& yMap ) const
722 {
723 const QRectF rect( xMap.p1(), yMap.p1(),
724 xMap.pDist(), yMap.pDist() );
725
726 return rect;
727 }
QList
Definition: qwt_abstract_legend.h:17
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:76
QwtGraphic::isNull
bool isNull() const
Definition: qwt_graphic.cpp:413
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtLegendData
Attributes of an entry on a legend.
Definition: qwt_legend_data.h:37
QwtLegendData::setValue
void setValue(int role, const QVariant &)
Definition: qwt_legend_data.cpp:64
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:79
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
QwtPlotItem::QwtPlotItem
QwtPlotItem()
Definition: qwt_plot_item.cpp:55
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition: qwt_plot_item.cpp:559
QwtPlotItem::setLegendIconSize
void setLegendIconSize(const QSize &)
Definition: qwt_plot_item.cpp:373
QwtPlotItem::paintRect
QRectF paintRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding paint rectangle of 2 maps.
Definition: qwt_plot_item.cpp:720
QwtPlotItem::ItemInterests
QFlags< ItemInterest > ItemInterests
Definition: qwt_plot_item.h:200
QwtPlotItem::setRenderThreadCount
void setRenderThreadCount(uint numThreads)
Definition: qwt_plot_item.cpp:350
QwtPlotItem::legendIcon
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
Definition: qwt_plot_item.cpp:402
QwtPlotItem::setTitle
void setTitle(const QString &title)
Definition: qwt_plot_item.cpp:187
QwtPlotItem::isVisible
bool isVisible() const
Definition: qwt_plot_item.cpp:470
QwtPlotItem::hide
void hide()
Hide the item.
Definition: qwt_plot_item.cpp:446
QwtPlotItem::setYAxis
void setYAxis(QwtAxisId)
Definition: qwt_plot_item.cpp:543
QwtPlotItem::~QwtPlotItem
virtual ~QwtPlotItem()
Destroy the QwtPlotItem.
Definition: qwt_plot_item.cpp:81
QwtPlotItem::legendData
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
Definition: qwt_plot_item.cpp:626
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotItem::title
const QwtText & title() const
Definition: qwt_plot_item.cpp:215
QwtPlotItem::setXAxis
void setXAxis(QwtAxisId)
Definition: qwt_plot_item.cpp:526
QwtPlotItem::scaleRect
QRectF scaleRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding scale rectangle of 2 maps.
Definition: qwt_plot_item.cpp:705
QwtPlotItem::RenderHints
QFlags< RenderHint > RenderHints
Definition: qwt_plot_item.h:209
QwtPlotItem::ItemAttributes
QFlags< ItemAttribute > ItemAttributes
Definition: qwt_plot_item.h:167
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
QwtPlotItem::setVisible
virtual void setVisible(bool)
Definition: qwt_plot_item.cpp:457
QwtPlotItem::plot
QwtPlot * plot() const
Return attached plot.
Definition: qwt_plot_item.cpp:142
QwtPlotItem::z
double z() const
Definition: qwt_plot_item.cpp:152
QwtPlotItem::rtti
virtual int rtti() const
Definition: qwt_plot_item.cpp:136
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition: qwt_plot_item.cpp:553
QwtPlotItem::testItemInterest
bool testItemInterest(ItemInterest) const
Definition: qwt_plot_item.cpp:299
QwtPlotItem::show
void show()
Show the item.
Definition: qwt_plot_item.cpp:440
QwtPlotItem::defaultIcon
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
Definition: qwt_plot_item.cpp:422
QwtPlotItem::Rtti_PlotItem
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
Definition: qwt_plot_item.h:78
QwtPlotItem::detach
void detach()
This method detaches a QwtPlotItem from any QwtPlot it has been associated with.
Definition: qwt_plot_item.cpp:119
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition: qwt_plot_item.cpp:279
QwtPlotItem::RenderHint
RenderHint
Render hints.
Definition: qwt_plot_item.h:204
QwtPlotItem::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &)
Update the item to changes of the axes scale division.
Definition: qwt_plot_item.cpp:665
QwtPlotItem::testItemAttribute
bool testItemAttribute(ItemAttribute) const
Definition: qwt_plot_item.cpp:266
QwtPlotItem::setAxes
void setAxes(QwtAxisId xAxis, QwtAxisId yAxis)
Definition: qwt_plot_item.cpp:507
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
QwtPlotItem::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition: qwt_plot_item.cpp:312
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::ItemAttribute
ItemAttribute
Plot Item Attributes.
Definition: qwt_plot_item.h:148
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotItem::renderThreadCount
uint renderThreadCount() const
Definition: qwt_plot_item.cpp:360
QwtPlotItem::getCanvasMarginHint
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.
Definition: qwt_plot_item.cpp:595
QwtPlotItem::attach
void attach(QwtPlot *plot)
Attach the item to a plot.
Definition: qwt_plot_item.cpp:98
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition: qwt_plot_item.cpp:568
QwtPlotItem::updateLegend
virtual void updateLegend(const QwtPlotItem *, const QList< QwtLegendData > &)
Update the item to changes of the legend info.
Definition: qwt_plot_item.cpp:690
QwtPlotItem::legendIconSize
QSize legendIconSize() const
Definition: qwt_plot_item.cpp:386
QwtPlotItem::ItemInterest
ItemInterest
Plot Item Interests.
Definition: qwt_plot_item.h:180
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:34
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::pDist
double pDist() const
Definition: qwt_scale_map.h:115
QwtScaleMap::p1
double p1() const
Definition: qwt_scale_map.h:99
QwtScaleMap::s1
double s1() const
Definition: qwt_scale_map.h:83
QwtScaleMap::sDist
double sDist() const
Definition: qwt_scale_map.h:123
QwtText
A class representing a text.
Definition: qwt_text.h:52
QwtText::renderFlags
int renderFlags() const
Definition: qwt_text.cpp:317
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
QwtAxis
Definition: qwt_axis.h:19
QwtAxis::isYAxis
bool isYAxis(int axisPos)
Definition: qwt_axis.h:57
QwtAxis::XBottom
@ XBottom
X axis below the canvas.
Definition: qwt_axis.h:30
QwtAxis::YLeft
@ YLeft
Y axis left of the canvas.
Definition: qwt_axis.h:24
QwtAxis::isXAxis
bool isXAxis(int axisPos)
Definition: qwt_axis.h:51