source: ntrip/trunk/BNC/qwt/qwt_plot_textlabel.cpp@ 10573

Last change on this file since 10573 was 9383, checked in by stoecker, 4 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 6.1 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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_scale_map.h"
13#include <qpainter.h>
14#include <qpixmap.h>
15#include <qmath.h>
16
17static QRect qwtItemRect( int renderFlags,
18 const QRectF &rect, const QSizeF &itemSize )
19{
20 int x;
21 if ( renderFlags & Qt::AlignLeft )
22 {
23 x = rect.left();
24 }
25 else if ( renderFlags & Qt::AlignRight )
26 {
27 x = rect.right() - itemSize.width();
28 }
29 else
30 {
31 x = rect.center().x() - 0.5 * itemSize.width();
32 }
33
34 int y;
35 if ( renderFlags & Qt::AlignTop )
36 {
37 y = rect.top();
38 }
39 else if ( renderFlags & Qt::AlignBottom )
40 {
41 y = rect.bottom() - itemSize.height();
42 }
43 else
44 {
45 y = rect.center().y() - 0.5 * itemSize.height();
46 }
47
48 return QRect( x, y, itemSize.width(), itemSize.height() );
49}
50
51class QwtPlotTextLabel::PrivateData
52{
53public:
54 PrivateData():
55 margin( 5 )
56 {
57 }
58
59 QwtText text;
60 int margin;
61
62 QPixmap pixmap;
63};
64
65/*!
66 \brief Constructor
67
68 Initializes an text label with an empty text
69
70 Sets the following item attributes:
71
72 - QwtPlotItem::AutoScale: true
73 - QwtPlotItem::Legend: false
74
75 The z value is initialized by 150
76
77 \sa QwtPlotItem::setItemAttribute(), QwtPlotItem::setZ()
78*/
79
80QwtPlotTextLabel::QwtPlotTextLabel():
81 QwtPlotItem( QwtText( "Label" ) )
82{
83 d_data = new PrivateData;
84
85 setItemAttribute( QwtPlotItem::AutoScale, false );
86 setItemAttribute( QwtPlotItem::Legend, false );
87
88 setZ( 150 );
89}
90
91//! Destructor
92QwtPlotTextLabel::~QwtPlotTextLabel()
93{
94 delete d_data;
95}
96
97//! \return QwtPlotItem::Rtti_PlotTextLabel
98int QwtPlotTextLabel::rtti() const
99{
100 return QwtPlotItem::Rtti_PlotTextLabel;
101}
102
103/*!
104 Set the text
105
106 The label will be aligned to the plot canvas according to
107 the alignment flags of text.
108
109 \param text Text to be displayed
110
111 \sa text(), QwtText::renderFlags()
112*/
113void QwtPlotTextLabel::setText( const QwtText &text )
114{
115 if ( d_data->text != text )
116 {
117 d_data->text = text;
118
119 invalidateCache();
120 itemChanged();
121 }
122}
123
124/*!
125 \return Text to be displayed
126 \sa setText()
127*/
128QwtText QwtPlotTextLabel::text() const
129{
130 return d_data->text;
131}
132
133/*!
134 Set the margin
135
136 The margin is the distance between the contentsRect()
137 of the plot canvas and the rectangle where the label can
138 be displayed.
139
140 \param margin Margin
141
142 \sa margin(), textRect()
143 */
144void QwtPlotTextLabel::setMargin( int margin )
145{
146 margin = qMax( margin, 0 );
147 if ( d_data->margin != margin )
148 {
149 d_data->margin = margin;
150 itemChanged();
151 }
152}
153
154/*!
155 \return Margin added to the contentsMargins() of the canvas
156 \sa setMargin()
157*/
158int QwtPlotTextLabel::margin() const
159{
160 return d_data->margin;
161}
162
163/*!
164 Draw the text label
165
166 \param painter Painter
167 \param xMap x Scale Map
168 \param yMap y Scale Map
169 \param canvasRect Contents rectangle of the canvas in painter coordinates
170
171 \sa textRect()
172*/
173
174void QwtPlotTextLabel::draw( QPainter *painter,
175 const QwtScaleMap &xMap, const QwtScaleMap &yMap,
176 const QRectF &canvasRect ) const
177{
178 Q_UNUSED( xMap );
179 Q_UNUSED( yMap );
180
181 const int m = d_data->margin;
182
183 const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
184 d_data->text.textSize( painter->font() ) );
185
186 bool doCache = QwtPainter::roundingAlignment( painter );
187 if ( doCache )
188 {
189 switch( painter->paintEngine()->type() )
190 {
191 case QPaintEngine::Picture:
192 case QPaintEngine::User: // usually QwtGraphic
193 {
194 // don't use a cache for record/replay devices
195 doCache = false;
196 break;
197 }
198 default:;
199 }
200 }
201
202 if ( doCache )
203 {
204 // when the paint device is aligning it is not one
205 // where scalability matters ( PDF, SVG ).
206 // As rendering a text label is an expensive operation
207 // we use a cache.
208
209 int pw = 0;
210 if ( d_data->text.borderPen().style() != Qt::NoPen )
211 pw = qMax( d_data->text.borderPen().width(), 1 );
212
213 QRect pixmapRect;
214 pixmapRect.setLeft( qFloor( rect.left() ) - pw );
215 pixmapRect.setTop( qFloor( rect.top() ) - pw );
216 pixmapRect.setRight( qCeil( rect.right() ) + pw );
217 pixmapRect.setBottom( qCeil( rect.bottom() ) + pw );
218
219#define QWT_HIGH_DPI 1
220
221#if QT_VERSION >= 0x050100 && QWT_HIGH_DPI
222 const qreal pixelRatio = painter->device()->devicePixelRatio();
223 const QSize scaledSize = pixmapRect.size() * pixelRatio;
224#else
225 const QSize scaledSize = pixmapRect.size();
226#endif
227 if ( d_data->pixmap.isNull() ||
228 ( scaledSize != d_data->pixmap.size() ) )
229 {
230 d_data->pixmap = QPixmap( scaledSize );
231#if QT_VERSION >= 0x050100 && QWT_HIGH_DPI
232 d_data->pixmap.setDevicePixelRatio( pixelRatio );
233#endif
234 d_data->pixmap.fill( Qt::transparent );
235
236 const QRect r( pw, pw,
237 pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );
238
239 QPainter pmPainter( &d_data->pixmap );
240 d_data->text.draw( &pmPainter, r );
241 }
242
243 painter->drawPixmap( pixmapRect, d_data->pixmap );
244 }
245 else
246 {
247 d_data->text.draw( painter, rect );
248 }
249}
250
251/*!
252 \brief Align the text label
253
254 \param rect Canvas rectangle with margins subtracted
255 \param textSize Size required to draw the text
256
257 \return A rectangle aligned according the the alignment flags of
258 the text.
259
260 \sa setMargin(), QwtText::renderFlags(), QwtText::textSize()
261 */
262QRectF QwtPlotTextLabel::textRect(
263 const QRectF &rect, const QSizeF &textSize ) const
264{
265 return qwtItemRect( d_data->text.renderFlags(), rect, textSize );
266}
267
268//! Invalidate all internal cache
269void QwtPlotTextLabel::invalidateCache()
270{
271 d_data->pixmap = QPixmap();
272}
Note: See TracBrowser for help on using the repository browser.