Qwt User's Guide 6.2.0

qwt_plot_textlabel.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_textlabel.h"
11 #include "qwt_painter.h"
12 #include "qwt_text.h"
13 #include "qwt_math.h"
14
15 #include <qpainter.h>
16 #include <qpaintengine.h>
17 #include <qpixmap.h>
18
19 static QRect qwtItemRect( int renderFlags,
20 const QRectF& rect, const QSizeF& itemSize )
21 {
22 int x;
23 if ( renderFlags & Qt::AlignLeft )
24 {
25 x = rect.left();
26 }
27 else if ( renderFlags & Qt::AlignRight )
28 {
29 x = rect.right() - itemSize.width();
30 }
31 else
32 {
33 x = rect.center().x() - 0.5 * itemSize.width();
34 }
35
36 int y;
37 if ( renderFlags & Qt::AlignTop )
38 {
39 y = rect.top();
40 }
41 else if ( renderFlags & Qt::AlignBottom )
42 {
43 y = rect.bottom() - itemSize.height();
44 }
45 else
46 {
47 y = rect.center().y() - 0.5 * itemSize.height();
48 }
49
50 return QRect( x, y, itemSize.width(), itemSize.height() );
51 }
52
53 class QwtPlotTextLabel::PrivateData
54 {
55 public:
56 PrivateData()
57 : margin( 5 )
58 {
59 }
60
61 QwtText text;
62 int margin;
63
64 QPixmap pixmap;
65 };
66
82 QwtPlotTextLabel::QwtPlotTextLabel()
83 : QwtPlotItem( QwtText( "Label" ) )
84 {
85 m_data = new PrivateData;
86
87 setItemAttribute( QwtPlotItem::AutoScale, false );
88 setItemAttribute( QwtPlotItem::Legend, false );
89
90 setZ( 150 );
91 }
92
94 QwtPlotTextLabel::~QwtPlotTextLabel()
95 {
96 delete m_data;
97 }
98
100 int QwtPlotTextLabel::rtti() const
101 {
102 return QwtPlotItem::Rtti_PlotTextLabel;
103 }
104
115 void QwtPlotTextLabel::setText( const QwtText& text )
116 {
117 if ( m_data->text != text )
118 {
119 m_data->text = text;
120
121 invalidateCache();
122 itemChanged();
123 }
124 }
125
130 QwtText QwtPlotTextLabel::text() const
131 {
132 return m_data->text;
133 }
134
146 void QwtPlotTextLabel::setMargin( int margin )
147 {
148 margin = qMax( margin, 0 );
149 if ( m_data->margin != margin )
150 {
151 m_data->margin = margin;
152 itemChanged();
153 }
154 }
155
160 int QwtPlotTextLabel::margin() const
161 {
162 return m_data->margin;
163 }
164
176 void QwtPlotTextLabel::draw( QPainter* painter,
177 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
178 const QRectF& canvasRect ) const
179 {
180 Q_UNUSED( xMap );
181 Q_UNUSED( yMap );
182
183 const int m = m_data->margin;
184
185 const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
186 m_data->text.textSize( painter->font() ) );
187
188 bool doCache = QwtPainter::roundingAlignment( painter );
189 if ( doCache )
190 {
191 switch( painter->paintEngine()->type() )
192 {
193 case QPaintEngine::Picture:
194 case QPaintEngine::User: // usually QwtGraphic
195 {
196 // don't use a cache for record/replay devices
197 doCache = false;
198 break;
199 }
200 default:;
201 }
202 }
203
204 if ( doCache )
205 {
206 // when the paint device is aligning it is not one
207 // where scalability matters ( PDF, SVG ).
208 // As rendering a text label is an expensive operation
209 // we use a cache.
210
211 int pw = 0;
212 if ( m_data->text.borderPen().style() != Qt::NoPen )
213 pw = qMax( m_data->text.borderPen().width(), 1 );
214
215 QRect pixmapRect;
216 pixmapRect.setLeft( qwtFloor( rect.left() ) - pw );
217 pixmapRect.setTop( qwtFloor( rect.top() ) - pw );
218 pixmapRect.setRight( qwtCeil( rect.right() ) + pw );
219 pixmapRect.setBottom( qwtCeil( rect.bottom() ) + pw );
220
221 #if QT_VERSION >= 0x050000
222 const qreal pixelRatio = QwtPainter::devicePixelRatio( painter->device() );
223 const QSize scaledSize = pixmapRect.size() * pixelRatio;
224 #else
225 const QSize scaledSize = pixmapRect.size();
226 #endif
227
228 if ( m_data->pixmap.isNull() ||
229 ( scaledSize != m_data->pixmap.size() ) )
230 {
231 m_data->pixmap = QPixmap( scaledSize );
232 #if QT_VERSION >= 0x050000
233 m_data->pixmap.setDevicePixelRatio( pixelRatio );
234 #endif
235 m_data->pixmap.fill( Qt::transparent );
236
237 const QRect r( pw, pw,
238 pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );
239
240 QPainter pmPainter( &m_data->pixmap );
241 m_data->text.draw( &pmPainter, r );
242 }
243
244 painter->drawPixmap( pixmapRect, m_data->pixmap );
245 }
246 else
247 {
248 m_data->text.draw( painter, rect );
249 }
250 }
251
263 QRectF QwtPlotTextLabel::textRect(
264 const QRectF& rect, const QSizeF& textSize ) const
265 {
266 return qwtItemRect( m_data->text.renderFlags(), rect, textSize );
267 }
268
270 void QwtPlotTextLabel::invalidateCache()
271 {
272 m_data->pixmap = QPixmap();
273 }
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition: qwt_painter.cpp:1491
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
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::Rtti_PlotTextLabel
@ Rtti_PlotTextLabel
For QwtPlotTextLabel.
Definition: qwt_plot_item.h:123
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
QwtPlotTextLabel::text
QwtText text() const
Definition: qwt_plot_textlabel.cpp:130
QwtPlotTextLabel::QwtPlotTextLabel
QwtPlotTextLabel()
Constructor.
Definition: qwt_plot_textlabel.cpp:82
QwtPlotTextLabel::~QwtPlotTextLabel
virtual ~QwtPlotTextLabel()
Destructor.
Definition: qwt_plot_textlabel.cpp:94
QwtPlotTextLabel::setMargin
void setMargin(int margin)
Definition: qwt_plot_textlabel.cpp:146
QwtPlotTextLabel::margin
int margin() const
Definition: qwt_plot_textlabel.cpp:160
QwtPlotTextLabel::invalidateCache
void invalidateCache()
Invalidate all internal cache.
Definition: qwt_plot_textlabel.cpp:270
QwtPlotTextLabel::draw
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const override
Definition: qwt_plot_textlabel.cpp:176
QwtPlotTextLabel::textRect
virtual QRectF textRect(const QRectF &, const QSizeF &) const
Align the text label.
Definition: qwt_plot_textlabel.cpp:263
QwtPlotTextLabel::setText
void setText(const QwtText &)
Definition: qwt_plot_textlabel.cpp:115
QwtPlotTextLabel::rtti
virtual int rtti() const override
Definition: qwt_plot_textlabel.cpp:100
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtText
A class representing a text.
Definition: qwt_text.h:52