qwt_plot_textlabel.cpp
Go to the documentation of this file.
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_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 
54 {
55 public:
57  margin( 5 )
58  {
59  }
60 
62  int margin;
63 
64  QPixmap pixmap;
65 };
66 
83  QwtPlotItem( QwtText( "Label" ) )
84 {
85  d_data = new PrivateData;
86 
89 
90  setZ( 150 );
91 }
92 
95 {
96  delete d_data;
97 }
98 
101 {
103 }
104 
116 {
117  if ( d_data->text != text )
118  {
119  d_data->text = text;
120 
121  invalidateCache();
122  itemChanged();
123  }
124 }
125 
131 {
132  return d_data->text;
133 }
134 
147 {
148  margin = qMax( margin, 0 );
149  if ( d_data->margin != margin )
150  {
151  d_data->margin = margin;
152  itemChanged();
153  }
154 }
155 
161 {
162  return d_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 = d_data->margin;
184 
185  const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
186  d_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 ( d_data->text.borderPen().style() != Qt::NoPen )
213  pw = qMax( d_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 ( d_data->pixmap.isNull() ||
229  ( scaledSize != d_data->pixmap.size() ) )
230  {
231  d_data->pixmap = QPixmap( scaledSize );
232 #if QT_VERSION >= 0x050000
233  d_data->pixmap.setDevicePixelRatio( pixelRatio );
234 #endif
235  d_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( &d_data->pixmap );
241  d_data->text.draw( &pmPainter, r );
242  }
243 
244  painter->drawPixmap( pixmapRect, d_data->pixmap );
245  }
246  else
247  {
248  d_data->text.draw( painter, rect );
249  }
250 }
251 
264  const QRectF &rect, const QSizeF &textSize ) const
265 {
266  return qwtItemRect( d_data->text.renderFlags(), rect, textSize );
267 }
268 
271 {
272  d_data->pixmap = QPixmap();
273 }
static QRect qwtItemRect(int renderFlags, const QRectF &rect, const QSizeF &itemSize)
QSizeF textSize() const
Definition: qwt_text.cpp:547
int renderFlags() const
Definition: qwt_text.cpp:294
QwtPlotTextLabel()
Constructor.
void setMargin(int margin)
PrivateData * d_data
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:592
virtual int rtti() const QWT_OVERRIDE
int qwtFloor(qreal value)
Definition: qwt_math.h:271
The item is represented on the legend.
virtual QRectF textRect(const QRectF &, const QSizeF &) const
Align the text label.
static qreal devicePixelRatio(const QPaintDevice *)
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
void setText(const QwtText &)
A scale map.
Definition: qwt_scale_map.h:26
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const QWT_OVERRIDE
virtual void itemChanged()
void invalidateCache()
Invalidate all internal cache.
QPen borderPen() const
Definition: qwt_text.cpp:407
For QwtPlotTextLabel.
QwtText text() const
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:65
void setItemAttribute(ItemAttribute, bool on=true)
virtual ~QwtPlotTextLabel()
Destructor.
int qwtCeil(qreal value)
Definition: qwt_math.h:262
static bool roundingAlignment()
Definition: qwt_painter.h:181


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10