qwt_plot_textlabel.cpp
Go to the documentation of this file.
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 
54 {
55  public:
57  : margin( 5 )
58  {
59  }
60 
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 
101 {
103 }
104 
116 {
117  if ( m_data->text != text )
118  {
119  m_data->text = text;
120 
121  invalidateCache();
122  itemChanged();
123  }
124 }
125 
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 
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 
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 }
qwtItemRect
static QRect qwtItemRect(int renderFlags, const QRectF &rect, const QSizeF &itemSize)
Definition: qwt_plot_textlabel.cpp:19
QwtPlotTextLabel::textRect
virtual QRectF textRect(const QRectF &, const QSizeF &) const
Align the text label.
Definition: qwt_plot_textlabel.cpp:263
QwtPlotTextLabel::PrivateData::margin
int margin
Definition: qwt_plot_textlabel.cpp:62
QwtPlotTextLabel::~QwtPlotTextLabel
virtual ~QwtPlotTextLabel()
Destructor.
Definition: qwt_plot_textlabel.cpp:94
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QwtPlotTextLabel::margin
int margin() const
Definition: qwt_plot_textlabel.cpp:160
QwtPlotItem::Rtti_PlotTextLabel
@ Rtti_PlotTextLabel
For QwtPlotTextLabel.
Definition: qwt_plot_item.h:123
qwt_math.h
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
QwtPlotTextLabel::QwtPlotTextLabel
QwtPlotTextLabel()
Constructor.
Definition: qwt_plot_textlabel.cpp:82
QwtPlotTextLabel::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_textlabel.cpp:56
QwtText::textSize
QSizeF textSize() const
Definition: qwt_text.cpp:570
qwt_plot_textlabel.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtPlotTextLabel::PrivateData
Definition: qwt_plot_textlabel.cpp:53
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:66
QwtText::renderFlags
int renderFlags() const
Definition: qwt_text.cpp:317
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
qwt_painter.h
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
qwtFloor
int qwtFloor(qreal value)
Definition: qwt_math.h:275
QwtPlotTextLabel::PrivateData::pixmap
QPixmap pixmap
Definition: qwt_plot_textlabel.cpp:64
qwtCeil
int qwtCeil(qreal value)
Definition: qwt_math.h:266
QwtText::borderPen
QPen borderPen() const
Definition: qwt_text.cpp:430
QwtPlotTextLabel::setText
void setText(const QwtText &)
Definition: qwt_plot_textlabel.cpp:115
QwtPlotTextLabel::invalidateCache
void invalidateCache()
Invalidate all internal cache.
Definition: qwt_plot_textlabel.cpp:270
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:615
QwtPlotTextLabel::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_textlabel.cpp:100
QwtPlotTextLabel::PrivateData::text
QwtText text
Definition: qwt_plot_textlabel.cpp:61
QwtPlotTextLabel::m_data
PrivateData * m_data
Definition: qwt_plot_textlabel.h:71
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition: qwt_painter.cpp:1491
qwt_text.h
QwtPlotTextLabel::setMargin
void setMargin(int margin)
Definition: qwt_plot_textlabel.cpp:146
QwtPlotTextLabel::draw
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const QWT_OVERRIDE
Definition: qwt_plot_textlabel.cpp:176
QwtPlotTextLabel::text
QwtText text() const
Definition: qwt_plot_textlabel.cpp:130


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46