Loading...
Searching...
No Matches
qwt_plot_textlabel.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_textlabel.h"
#include "qwt_painter.h"
#include "qwt_text.h"
#include "qwt_math.h"
#include <qpainter.h>
#include <qpaintengine.h>
#include <qpixmap.h>
static QRect qwtItemRect( int renderFlags,
const QRectF& rect, const QSizeF& itemSize )
{
int x;
if ( renderFlags & Qt::AlignLeft )
{
x = rect.left();
}
else if ( renderFlags & Qt::AlignRight )
{
x = rect.right() - itemSize.width();
}
else
{
x = rect.center().x() - 0.5 * itemSize.width();
}
int y;
if ( renderFlags & Qt::AlignTop )
{
y = rect.top();
}
else if ( renderFlags & Qt::AlignBottom )
{
y = rect.bottom() - itemSize.height();
}
else
{
y = rect.center().y() - 0.5 * itemSize.height();
}
return QRect( x, y, itemSize.width(), itemSize.height() );
}
class QwtPlotTextLabel::PrivateData
{
public:
PrivateData()
: margin( 5 )
{
}
QwtText text;
int margin;
QPixmap pixmap;
};
QwtPlotTextLabel::QwtPlotTextLabel()
: QwtPlotItem( QwtText( "Label" ) )
{
m_data = new PrivateData;
setItemAttribute( QwtPlotItem::AutoScale, false );
setItemAttribute( QwtPlotItem::Legend, false );
setZ( 150 );
}
QwtPlotTextLabel::~QwtPlotTextLabel()
{
delete m_data;
}
int QwtPlotTextLabel::rtti() const
{
return QwtPlotItem::Rtti_PlotTextLabel;
}
void QwtPlotTextLabel::setText( const QwtText& text )
{
if ( m_data->text != text )
{
m_data->text = text;
invalidateCache();
itemChanged();
}
}
QwtText QwtPlotTextLabel::text() const
{
return m_data->text;
}
void QwtPlotTextLabel::setMargin( int margin )
{
margin = qMax( margin, 0 );
if ( m_data->margin != margin )
{
m_data->margin = margin;
itemChanged();
}
}
int QwtPlotTextLabel::margin() const
{
return m_data->margin;
}
void QwtPlotTextLabel::draw( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect ) const
{
Q_UNUSED( xMap );
Q_UNUSED( yMap );
const int m = m_data->margin;
const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
m_data->text.textSize( painter->font() ) );
bool doCache = QwtPainter::roundingAlignment( painter );
if ( doCache )
{
switch( painter->paintEngine()->type() )
{
case QPaintEngine::Picture:
case QPaintEngine::User: // usually QwtGraphic
{
// don't use a cache for record/replay devices
doCache = false;
break;
}
default:;
}
}
if ( doCache )
{
// when the paint device is aligning it is not one
// where scalability matters ( PDF, SVG ).
// As rendering a text label is an expensive operation
// we use a cache.
int pw = 0;
if ( m_data->text.borderPen().style() != Qt::NoPen )
pw = qMax( m_data->text.borderPen().width(), 1 );
QRect pixmapRect;
pixmapRect.setLeft( qwtFloor( rect.left() ) - pw );
pixmapRect.setTop( qwtFloor( rect.top() ) - pw );
pixmapRect.setRight( qwtCeil( rect.right() ) + pw );
pixmapRect.setBottom( qwtCeil( rect.bottom() ) + pw );
#if QT_VERSION >= 0x050000
const qreal pixelRatio = QwtPainter::devicePixelRatio( painter->device() );
const QSize scaledSize = pixmapRect.size() * pixelRatio;
#else
const QSize scaledSize = pixmapRect.size();
#endif
if ( m_data->pixmap.isNull() ||
( scaledSize != m_data->pixmap.size() ) )
{
m_data->pixmap = QPixmap( scaledSize );
#if QT_VERSION >= 0x050000
m_data->pixmap.setDevicePixelRatio( pixelRatio );
#endif
m_data->pixmap.fill( Qt::transparent );
const QRect r( pw, pw,
pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );
QPainter pmPainter( &m_data->pixmap );
m_data->text.draw( &pmPainter, r );
}
painter->drawPixmap( pixmapRect, m_data->pixmap );
}
else
{
m_data->text.draw( painter, rect );
}
}
QRectF QwtPlotTextLabel::textRect(
const QRectF& rect, const QSizeF& textSize ) const
{
return qwtItemRect( m_data->text.renderFlags(), rect, textSize );
}
void QwtPlotTextLabel::invalidateCache()
{
m_data->pixmap = QPixmap();
}
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition qwt_painter.cpp:1481
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition qwt_painter.h:183
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition qwt_plot_item.cpp:228
QwtPlotItem::Rtti_PlotTextLabel
@ Rtti_PlotTextLabel
For QwtPlotTextLabel.
Definition qwt_plot_item.h:123
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition qwt_plot_item.h:150
QwtPlotItem::AutoScale
@ AutoScale
Definition qwt_plot_item.h:157
QwtPlotTextLabel::text
QwtText text() const
Definition qwt_plot_textlabel.cpp:130
QwtPlotTextLabel::QwtPlotTextLabel
QwtPlotTextLabel()
Constructor.
Definition qwt_plot_textlabel.cpp:82
QwtPlotTextLabel::~QwtPlotTextLabel
virtual ~QwtPlotTextLabel()
Destructor.
Definition qwt_plot_textlabel.cpp:94
QwtPlotTextLabel::setMargin
void setMargin(int margin)
Definition qwt_plot_textlabel.cpp:146
QwtPlotTextLabel::margin
int margin() const
Definition qwt_plot_textlabel.cpp:160
QwtPlotTextLabel::invalidateCache
void invalidateCache()
Invalidate all internal cache.
Definition qwt_plot_textlabel.cpp:270
QwtPlotTextLabel::draw
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &) const override
Definition qwt_plot_textlabel.cpp:176
QwtPlotTextLabel::textRect
virtual QRectF textRect(const QRectF &, const QSizeF &) const
Align the text label.
Definition qwt_plot_textlabel.cpp:263
QwtPlotTextLabel::setText
void setText(const QwtText &)
Definition qwt_plot_textlabel.cpp:115
QwtPlotTextLabel::rtti
virtual int rtti() const override
Definition qwt_plot_textlabel.cpp:100
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::textSize
QSizeF textSize() const
Definition qwt_text.cpp:570
QwtText::renderFlags
int renderFlags() const
Definition qwt_text.cpp:317
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615
QwtText::borderPen
QPen borderPen() const
Definition qwt_text.cpp:430