1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
15
#include "qwt_painter.h"
16
#include "qwt_text_label.h"
18
class QwtTextLabel::PrivateData
33
Constructs an empty label.
34
\param parent Parent widget
36
QwtTextLabel::QwtTextLabel(QWidget *parent):
42
#if QT_VERSION < 0x040000
44
Constructs an empty label.
45
\param parent Parent widget
46
\param name Object name
48
QwtTextLabel::QwtTextLabel(QWidget *parent, const char *name):
56
Constructs a label that displays the text, text
57
\param parent Parent widget
60
QwtTextLabel::QwtTextLabel(const QwtText &text, QWidget *parent):
68
QwtTextLabel::~QwtTextLabel()
73
void QwtTextLabel::init()
75
d_data = new PrivateData();
76
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
80
Change the label's text, keeping all other QwtText attributes
82
\param textFormat Format of text
86
void QwtTextLabel::setText(const QString &text, QwtText::TextFormat textFormat)
88
d_data->text.setText(text, textFormat);
95
Change the label's text
98
void QwtTextLabel::setText(const QwtText &text)
107
const QwtText &QwtTextLabel::text() const
112
//! Clear the text and all QwtText attributes
113
void QwtTextLabel::clear()
115
d_data->text = QwtText();
121
//! Return label's text indent in pixels
122
int QwtTextLabel::indent() const
124
return d_data->indent;
128
Set label's text indent in pixels
129
\param indent Indentation in pixels
131
void QwtTextLabel::setIndent(int indent)
136
d_data->indent = indent;
142
//! Return label's text indent in pixels
143
int QwtTextLabel::margin() const
145
return d_data->margin;
149
Set label's margin in pixels
150
\param margin Margin in pixels
152
void QwtTextLabel::setMargin(int margin)
154
d_data->margin = margin;
160
//! Return label's margin in pixels
161
QSize QwtTextLabel::sizeHint() const
163
return minimumSizeHint();
166
//! Return a minimum size hint
167
QSize QwtTextLabel::minimumSizeHint() const
169
QSize sz = d_data->text.textSize(font());
171
int mw = 2 * (frameWidth() + d_data->margin);
174
int indent = d_data->indent;
176
indent = defaultIndent();
180
const int align = d_data->text.renderFlags();
181
if ( align & Qt::AlignLeft || align & Qt::AlignRight )
182
mw += d_data->indent;
183
else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
184
mh += d_data->indent;
193
Returns the preferred height for this widget, given the width.
196
int QwtTextLabel::heightForWidth(int width) const
198
const int renderFlags = d_data->text.renderFlags();
200
int indent = d_data->indent;
202
indent = defaultIndent();
204
width -= 2 * frameWidth();
205
if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
208
int height = d_data->text.heightForWidth(width, font());
209
if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
212
height += 2 * frameWidth();
218
void QwtTextLabel::paintEvent(QPaintEvent *event)
220
#if QT_VERSION >= 0x040000
221
QPainter painter(this);
223
if ( !contentsRect().contains( event->rect() ) )
226
painter.setClipRegion( event->region() & frameRect() );
227
drawFrame( &painter );
231
painter.setClipRegion(event->region() & contentsRect());
233
drawContents( &painter );
234
#else // QT_VERSION < 0x040000
235
QFrame::paintEvent(event);
240
//! Redraw the text and focus indicator
241
void QwtTextLabel::drawContents(QPainter *painter)
243
const QRect r = textRect();
247
painter->setFont(font());
248
#if QT_VERSION < 0x040000
249
painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
251
painter->setPen(palette().color(QPalette::Active, QPalette::Text));
254
drawText(painter, r);
258
const int margin = 2;
260
QRect focusRect = contentsRect();
261
focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
262
focusRect.width() - 2 * margin - 2,
263
focusRect.height() - 2 * margin - 2);
265
QwtPainter::drawFocusRect(painter, this, focusRect);
270
void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
272
d_data->text.draw(painter, textRect);
276
Calculate the rect for the text in widget coordinates
279
QRect QwtTextLabel::textRect() const
281
QRect r = contentsRect();
283
if ( !r.isEmpty() && d_data->margin > 0 )
285
r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
286
r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
291
int indent = d_data->indent;
293
indent = defaultIndent();
297
const int renderFlags = d_data->text.renderFlags();
299
if ( renderFlags & Qt::AlignLeft )
300
r.setX(r.x() + indent);
301
else if ( renderFlags & Qt::AlignRight )
302
r.setWidth(r.width() - indent);
303
else if ( renderFlags & Qt::AlignTop )
304
r.setY(r.y() + indent);
305
else if ( renderFlags & Qt::AlignBottom )
306
r.setHeight(r.height() - indent);
313
int QwtTextLabel::defaultIndent() const
315
if ( frameWidth() <= 0 )
319
if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
320
fnt = d_data->text.font();
324
return QFontMetrics(fnt).width('x') / 2;