Loading...
Searching...
No Matches
qwt_text_label.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_text_label.h"
#include "qwt_text.h"
#include "qwt_painter.h"
#include "qwt_math.h"
#include <qstyle.h>
#include <qstyleoption.h>
#include <qpainter.h>
#include <qevent.h>
#include <qmargins.h>
class QwtTextLabel::PrivateData
{
public:
PrivateData()
: indent( 4 )
, margin( 0 )
{
}
int indent;
int margin;
QwtText text;
};
QwtTextLabel::QwtTextLabel( QWidget* parent )
: QFrame( parent )
{
init();
}
QwtTextLabel::QwtTextLabel( const QwtText& text, QWidget* parent )
: QFrame( parent )
{
init();
m_data->text = text;
}
QwtTextLabel::~QwtTextLabel()
{
delete m_data;
}
void QwtTextLabel::init()
{
m_data = new PrivateData();
setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
}
void QwtTextLabel::setPlainText( const QString& text )
{
setText( QwtText( text ) );
}
QString QwtTextLabel::plainText() const
{
return m_data->text.text();
}
void QwtTextLabel::setText( const QString& text,
QwtText::TextFormat textFormat )
{
m_data->text.setText( text, textFormat );
update();
updateGeometry();
}
void QwtTextLabel::setText( const QwtText& text )
{
m_data->text = text;
update();
updateGeometry();
}
const QwtText& QwtTextLabel::text() const
{
return m_data->text;
}
void QwtTextLabel::clear()
{
m_data->text = QwtText();
update();
updateGeometry();
}
int QwtTextLabel::indent() const
{
return m_data->indent;
}
void QwtTextLabel::setIndent( int indent )
{
if ( indent < 0 )
indent = 0;
m_data->indent = indent;
update();
updateGeometry();
}
int QwtTextLabel::margin() const
{
return m_data->margin;
}
void QwtTextLabel::setMargin( int margin )
{
m_data->margin = margin;
update();
updateGeometry();
}
QSize QwtTextLabel::sizeHint() const
{
return minimumSizeHint();
}
QSize QwtTextLabel::minimumSizeHint() const
{
QSizeF sz = m_data->text.textSize( font() );
const QMargins m = contentsMargins();
int mw = m.left() + m.right() + 2 * m_data->margin;
int mh = m.top() + m.bottom() + 2 * m_data->margin;
int indent = m_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
if ( indent > 0 )
{
const int align = m_data->text.renderFlags();
if ( align & Qt::AlignLeft || align & Qt::AlignRight )
mw += m_data->indent;
else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
mh += m_data->indent;
}
sz += QSizeF( mw, mh );
return QSize( qwtCeil( sz.width() ), qwtCeil( sz.height() ) );
}
int QwtTextLabel::heightForWidth( int width ) const
{
const int renderFlags = m_data->text.renderFlags();
int indent = m_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
const QMargins m = contentsMargins();
width -= m.left() + m.right() - 2 * m_data->margin;
if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
width -= indent;
int height = qwtCeil( m_data->text.heightForWidth( width, font() ) );
if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
height += indent;
height += m.top() + m.bottom() + 2 * m_data->margin;
return height;
}
void QwtTextLabel::paintEvent( QPaintEvent* event )
{
QPainter painter( this );
painter.setClipRegion( event->region() );
QStyleOption opt;
opt.initFrom(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
if ( !contentsRect().contains( event->rect() ) )
{
painter.setClipRegion( event->region() & frameRect() );
drawFrame( &painter );
}
painter.setClipRegion( event->region() & contentsRect() );
drawContents( &painter );
}
void QwtTextLabel::drawContents( QPainter* painter )
{
const QRect r = textRect();
if ( r.isEmpty() )
return;
painter->setFont( font() );
painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
drawText( painter, QRectF( r ) );
if ( hasFocus() )
{
const int m = 2;
QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
QwtPainter::drawFocusRect( painter, this, focusRect );
}
}
void QwtTextLabel::drawText( QPainter* painter, const QRectF& textRect )
{
m_data->text.draw( painter, textRect );
}
QRect QwtTextLabel::textRect() const
{
QRect r = contentsRect();
if ( !r.isEmpty() && m_data->margin > 0 )
{
const int m = m_data->margin;
r.adjust( m, m, -m, -m );
}
if ( !r.isEmpty() )
{
int indent = m_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
if ( indent > 0 )
{
const int renderFlags = m_data->text.renderFlags();
if ( renderFlags & Qt::AlignLeft )
{
r.setX( r.x() + indent );
}
else if ( renderFlags & Qt::AlignRight )
{
r.setWidth( r.width() - indent );
}
else if ( renderFlags & Qt::AlignTop )
{
r.setY( r.y() + indent );
}
else if ( renderFlags & Qt::AlignBottom )
{
r.setHeight( r.height() - indent );
}
}
}
return r;
}
int QwtTextLabel::defaultIndent() const
{
if ( frameWidth() <= 0 )
return 0;
QFont fnt;
if ( m_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
fnt = m_data->text.font();
else
fnt = font();
return QwtPainter::horizontalAdvance( QFontMetrics( fnt ), 'x' ) / 2;
}
#include "moc_qwt_text_label.cpp"
QwtPainter::drawFocusRect
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
Definition qwt_painter.cpp:805
QwtPainter::horizontalAdvance
static int horizontalAdvance(const QFontMetrics &, const QString &)
Definition qwt_painter.cpp:1369
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition qwt_text.cpp:482
QwtText::textSize
QSizeF textSize() const
Definition qwt_text.cpp:570
QwtText::renderFlags
int renderFlags() const
Definition qwt_text.cpp:317
QwtText::TextFormat
TextFormat
Text format.
Definition qwt_text.h:65
QwtText::font
QFont font() const
Return the font.
Definition qwt_text.cpp:336
QwtText::PaintUsingTextFont
@ PaintUsingTextFont
The text has an individual font.
Definition qwt_text.h:117
QwtText::setText
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition qwt_text.cpp:277
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615
QwtText::text
QString text() const
Definition qwt_text.cpp:289
QwtText::heightForWidth
double heightForWidth(double width) const
Definition qwt_text.cpp:522
QwtTextLabel::setPlainText
void setPlainText(const QString &)
Definition qwt_text_label.cpp:73
QwtTextLabel::heightForWidth
virtual int heightForWidth(int) const override
Definition qwt_text_label.cpp:209
QwtTextLabel::indent
int indent() const
Return label's text indent in pixels.
Definition qwt_text_label.cpp:133
QwtTextLabel::margin
int margin() const
Return label's text margin in pixels.
Definition qwt_text_label.cpp:154
QwtTextLabel::text
const QwtText & text() const
Return the text.
Definition qwt_text_label.cpp:118
QwtTextLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition qwt_text_label.cpp:172
QwtTextLabel::clear
void clear()
Clear the text and all QwtText attributes.
Definition qwt_text_label.cpp:124
QwtTextLabel::plainText
QString plainText() const
Definition qwt_text_label.cpp:84
QwtTextLabel::minimumSizeHint
virtual QSize minimumSizeHint() const override
Return a minimum size hint.
Definition qwt_text_label.cpp:178
QwtTextLabel::setMargin
void setMargin(int)
Definition qwt_text_label.cpp:163
QwtTextLabel::QwtTextLabel
QwtTextLabel(QWidget *parent=NULL)
Definition qwt_text_label.cpp:39
QwtTextLabel::setIndent
void setIndent(int)
Definition qwt_text_label.cpp:142
QwtTextLabel::drawContents
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
Definition qwt_text_label.cpp:257
QwtTextLabel::setText
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)
Definition qwt_text_label.cpp:96
QwtTextLabel::textRect
QRect textRect() const
Definition qwt_text_label.cpp:288
QwtTextLabel::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition qwt_text_label.cpp:236
QwtTextLabel::~QwtTextLabel
virtual ~QwtTextLabel()
Destructor.
Definition qwt_text_label.cpp:58
QwtTextLabel::drawText
virtual void drawText(QPainter *, const QRectF &)
Redraw the text.
Definition qwt_text_label.cpp:279