Newer
Older
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* 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 <qpainter.h>
#include <qevent.h>
#include <qmath.h>
class QwtTextLabel::PrivateData
{
public:
PrivateData():
QwtTextLabel::QwtTextLabel( QWidget *parent ):
QFrame( parent )
{
init();
}
/*!
Constructs a label that displays the text, text
\param parent Parent widget
\param text Text
*/
QwtTextLabel::QwtTextLabel( const QwtText &text, QWidget *parent ):
QFrame( parent )
{
init();
d_data->text = text;
}
//! Destructor
QwtTextLabel::~QwtTextLabel()
{
delete d_data;
}
void QwtTextLabel::init()
{
d_data = new PrivateData();
setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
}
/*!
Interface for the designer plugin - does the same as setText()
\sa plainText()
*/
void QwtTextLabel::setPlainText( const QString &text )
{
setText( QwtText( text ) );
}
/*!
Interface for the designer plugin
\return Text as plain text
\sa setPlainText(), text()
*/
QString QwtTextLabel::plainText() const
{
return d_data->text.text();
}
/*!
Change the label's text, keeping all other QwtText attributes
\param text New text
\param textFormat Format of text
\sa QwtText
*/
void QwtTextLabel::setText( const QString &text,
QwtText::TextFormat textFormat )
update();
updateGeometry();
}
/*!
Change the label's text
\param text New text
*/
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
d_data->text = text;
update();
updateGeometry();
}
//! Return the text
const QwtText &QwtTextLabel::text() const
{
return d_data->text;
}
//! Clear the text and all QwtText attributes
void QwtTextLabel::clear()
{
d_data->text = QwtText();
update();
updateGeometry();
}
//! Return label's text indent in pixels
int QwtTextLabel::indent() const
{
return d_data->indent;
}
/*!
Set label's text indent in pixels
\param indent Indentation in pixels
*/
{
if ( indent < 0 )
indent = 0;
d_data->indent = indent;
update();
updateGeometry();
}
//! Return label's text indent in pixels
int QwtTextLabel::margin() const
{
return d_data->margin;
}
/*!
Set label's margin in pixels
\param margin Margin in pixels
*/
{
d_data->margin = margin;
update();
updateGeometry();
}
//! Return label's margin in pixels
QSize QwtTextLabel::sizeHint() const
{
return minimumSizeHint();
}
//! Return a minimum size hint
QSize QwtTextLabel::minimumSizeHint() const
{
int mh = mw;
int indent = d_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
const int align = d_data->text.renderFlags();
if ( align & Qt::AlignLeft || align & Qt::AlignRight )
mw += d_data->indent;
else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
mh += d_data->indent;
}
{
const int renderFlags = d_data->text.renderFlags();
int indent = d_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
width -= 2 * frameWidth();
if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
width -= indent;
int height = qCeil( d_data->text.heightForWidth( width, font() ) );
if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
height += indent;
height += 2 * frameWidth();
return height;
}
/*!
Qt paint event
\param event Paint event
*/
void QwtTextLabel::paintEvent( QPaintEvent *event )
painter.save();
painter.setClipRegion( event->region() & frameRect() );
drawFrame( &painter );
painter.restore();
}
drawContents( &painter );
}
//! Redraw the text and focus indicator
{
const QRect r = textRect();
if ( r.isEmpty() )
return;
painter->setFont( font() );
painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
void QwtTextLabel::drawText( QPainter *painter, const QRectF &textRect )
Calculate geometry for the text in widget coordinates
\return Geometry for the text
*/
QRect QwtTextLabel::textRect() const
{
QRect r = contentsRect();
if ( !r.isEmpty() && d_data->margin > 0 )
{
r.setRect( r.x() + d_data->margin, r.y() + d_data->margin,
r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
int indent = d_data->indent;
if ( indent <= 0 )
indent = defaultIndent();
const int renderFlags = d_data->text.renderFlags();
if ( renderFlags & Qt::AlignLeft )
}
}
return r;
}
int QwtTextLabel::defaultIndent() const
{
if ( frameWidth() <= 0 )
return 0;
QFont fnt;
if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )