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_scale_map.h"
13 #include <qpainter.h>
14 #include <qpixmap.h>
15 #include <qmath.h>
16 
17 static 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 
52 {
53 public:
55  margin( 5 )
56  {
57  }
58 
60  int margin;
61 
62  QPixmap pixmap;
63 };
64 
81  QwtPlotItem( QwtText( "Label" ) )
82 {
83  d_data = new PrivateData;
84 
87 
88  setZ( 150 );
89 }
90 
93 {
94  delete d_data;
95 }
96 
99 {
101 }
102 
114 {
115  if ( d_data->text != text )
116  {
117  d_data->text = text;
118 
119  invalidateCache();
120  itemChanged();
121  }
122 }
123 
129 {
130  return d_data->text;
131 }
132 
145 {
146  margin = qMax( margin, 0 );
147  if ( d_data->margin != margin )
148  {
149  d_data->margin = margin;
150  itemChanged();
151  }
152 }
153 
159 {
160  return d_data->margin;
161 }
162 
174 void 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 #if QT_VERSION >= 0x050000
220  const qreal pixelRatio = QwtPainter::devicePixelRatio( painter->device() );
221  const QSize scaledSize = pixmapRect.size() * pixelRatio;
222 #else
223  const QSize scaledSize = pixmapRect.size();
224 #endif
225 
226  if ( d_data->pixmap.isNull() ||
227  ( scaledSize != d_data->pixmap.size() ) )
228  {
229  d_data->pixmap = QPixmap( scaledSize );
230 #if QT_VERSION >= 0x050000
231  d_data->pixmap.setDevicePixelRatio( pixelRatio );
232 #endif
233  d_data->pixmap.fill( Qt::transparent );
234 
235  const QRect r( pw, pw,
236  pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );
237 
238  QPainter pmPainter( &d_data->pixmap );
239  d_data->text.draw( &pmPainter, r );
240  }
241 
242  painter->drawPixmap( pixmapRect, d_data->pixmap );
243  }
244  else
245  {
246  d_data->text.draw( painter, rect );
247  }
248 }
249 
262  const QRectF &rect, const QSizeF &textSize ) const
263 {
264  return qwtItemRect( d_data->text.renderFlags(), rect, textSize );
265 }
266 
269 {
270  d_data->pixmap = QPixmap();
271 }
static QRect qwtItemRect(int renderFlags, const QRectF &rect, const QSizeF &itemSize)
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const
int renderFlags() const
Definition: qwt_text.cpp:284
QwtPlotTextLabel()
Constructor.
void setMargin(int margin)
virtual int rtti() const
PrivateData * d_data
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
TFSIMD_FORCE_INLINE const tfScalar & y() const
The item is represented on the legend.
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
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 &)
TFSIMD_FORCE_INLINE const tfScalar & x() const
A scale map.
Definition: qwt_scale_map.h:30
virtual void itemChanged()
void invalidateCache()
Invalidate all internal cache.
QPen borderPen() const
Definition: qwt_text.cpp:397
For QwtPlotTextLabel.
QwtText text() const
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
void setItemAttribute(ItemAttribute, bool on=true)
virtual ~QwtPlotTextLabel()
Destructor.
static bool roundingAlignment()
Definition: qwt_painter.h:176


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17