Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
19static 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
53class 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
83 : QwtPlotItem( QwtText( "Label" ) )
84{
85 m_data = new PrivateData;
86
89
90 setZ( 150 );
91}
92
95{
96 delete m_data;
97}
98
104
116{
117 if ( m_data->text != text )
118 {
119 m_data->text = text;
120
122 itemChanged();
123 }
124}
125
131{
132 return m_data->text;
133}
134
147{
148 margin = qMax( margin, 0 );
149 if ( m_data->margin != margin )
150 {
151 m_data->margin = margin;
152 itemChanged();
153 }
154}
155
161{
162 return m_data->margin;
163}
164
176void 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
264 const QRectF& rect, const QSizeF& textSize ) const
265{
266 return qwtItemRect( m_data->text.renderFlags(), rect, textSize );
267}
268
271{
272 m_data->pixmap = QPixmap();
273}
static qreal devicePixelRatio(const QPaintDevice *)
static bool roundingAlignment()
Base class for items on the plot canvas.
void setZ(double z)
Set the z value.
void setItemAttribute(ItemAttribute, bool on=true)
@ Rtti_PlotTextLabel
For QwtPlotTextLabel.
virtual void itemChanged()
@ Legend
The item is represented on the legend.
QwtPlotTextLabel()
Constructor.
virtual ~QwtPlotTextLabel()
Destructor.
void setMargin(int margin)
void invalidateCache()
Invalidate all internal cache.
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const override
virtual QRectF textRect(const QRectF &, const QSizeF &) const
Align the text label.
void setText(const QwtText &)
virtual int rtti() const override
A scale map.
A class representing a text.
Definition qwt_text.h:52
QSizeF textSize() const
Definition qwt_text.cpp:570
int renderFlags() const
Definition qwt_text.cpp:317
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615
QPen borderPen() const
Definition qwt_text.cpp:430