1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
28 #include <QFontMetrics>
29 #include <QApplication>
30 #if QT_VERSION < 0x040200
31 #include <QAbstractItemDelegate>
34 static const int Vertical_Mask = 0x02;
36 class QxtLabelPrivate : public QxtPrivate<QxtLabel>
39 QXT_DECLARE_PUBLIC(QxtLabel);
41 void init(const QString& txt = QString());
47 Qt::TextElideMode mode;
51 void QxtLabelPrivate::init(const QString& txt)
54 align = Qt::AlignCenter;
55 mode = Qt::ElideMiddle;
56 rot = Qxt::NoRotation;
59 void QxtLabelPrivate::updateLabel()
61 qxt_p().updateGeometry();
66 \class QxtLabel QxtLabel
68 \brief A label which is able to show elided and rotated plain text.
70 QxtLabel is a label which is able to show elided and rotated plain text.
71 In addition, QxtLabel provides a signal for clicking.
73 \image html qxtlabel.png "QxtLabel in action."
77 \fn QxtLabel::clicked()
79 This signal is emitted whenever the label has been clicked.
81 \note A combination of mouse button press and release in shorter
82 time than \b QApplication::doubleClickInterval is considered
85 \sa QApplication::doubleClickInterval
89 Constructs a new QxtLabel with \a parent and \a flags.
91 QxtLabel::QxtLabel(QWidget* parent, Qt::WindowFlags flags) : QFrame(parent, flags)
93 QXT_INIT_PRIVATE(QxtLabel);
98 Constructs a new QxtLabel with \a text, \a parent and \a flags.
100 QxtLabel::QxtLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags) : QFrame(parent, flags)
102 QXT_INIT_PRIVATE(QxtLabel);
109 QxtLabel::~QxtLabel()
113 \property QxtLabel::text
114 \brief This property holds the text of the label
116 QString QxtLabel::text() const
121 void QxtLabel::setText(const QString& text)
123 if (qxt_d().text != text)
126 qxt_d().updateLabel();
127 emit textChanged(text);
132 \property QxtLabel::alignment
133 \brief This property holds the alignment of the text
135 The text is aligned according to this property.
136 The default value is \b Qt::AlignCenter.
138 \sa text, Qt::Alignment
140 Qt::Alignment QxtLabel::alignment() const
142 return qxt_d().align;
145 void QxtLabel::setAlignment(Qt::Alignment alignment)
147 if (qxt_d().align != alignment)
149 qxt_d().align = alignment;
150 update(); // no geometry change, repaint is sufficient
155 \property QxtLabel::elideMode
156 \brief This property holds the elide mode of the text
158 The text is elided according to this property.
159 The default value is \b Qt::ElideMiddle.
161 \sa text, Qt::TextElideMode
163 Qt::TextElideMode QxtLabel::elideMode() const
168 void QxtLabel::setElideMode(Qt::TextElideMode mode)
170 if (qxt_d().mode != mode)
173 qxt_d().updateLabel();
178 \property QxtLabel::rotation
179 \brief This property holds the rotation of the label
181 The label is rotated according to this property.
182 The default value is \b Qxt::NoRotation.
186 Qxt::Rotation QxtLabel::rotation() const
191 void QxtLabel::setRotation(Qxt::Rotation rotation)
193 if (qxt_d().rot != rotation)
195 Qxt::Rotation prev = qxt_d().rot;
196 qxt_d().rot = rotation;
199 case Qxt::NoRotation:
200 case Qxt::UpsideDown:
201 if (prev & Vertical_Mask)
208 case Qxt::CounterClockwise:
209 if ((prev & Vertical_Mask) == 0)
222 QSize QxtLabel::sizeHint() const
224 const QFontMetrics& fm = fontMetrics();
225 QSize size(fm.width(qxt_d().text), fm.height());
226 if (qxt_d().rot & Vertical_Mask)
231 QSize QxtLabel::minimumSizeHint() const
233 switch (qxt_d().mode)
235 #if QT_VERSION >= 0x040200
241 const QFontMetrics& fm = fontMetrics();
242 QSize size(fm.width("..."), fm.height());
243 if (qxt_d().rot & Vertical_Mask)
250 void QxtLabel::paintEvent(QPaintEvent* event)
252 QFrame::paintEvent(event);
254 p.rotate(qxt_d().rot);
255 QRect r = contentsRect();
258 case Qxt::UpsideDown:
259 p.translate(-r.width(), -r.height());
263 p.translate(0, -r.width());
266 case Qxt::CounterClockwise:
267 p.translate(-r.height(), 0);
275 if (qxt_d().rot & Vertical_Mask)
279 r = QRect(r.topLeft(), s);
282 #if QT_VERSION < 0x040200
283 const QString elidedText = QAbstractItemDelegate::elidedText(fontMetrics(), r.width(), qxt_d().mode, qxt_d().text);
284 #else // QT_VERSION >= 0x040200
285 const QString elidedText = fontMetrics().elidedText(qxt_d().text, qxt_d().mode, r.width());
287 p.drawText(r, qxt_d().align, elidedText);
290 void QxtLabel::changeEvent(QEvent* event)
292 QFrame::changeEvent(event);
293 switch (event->type())
295 case QEvent::FontChange:
296 case QEvent::ApplicationFontChange:
297 qxt_d().updateLabel();
305 void QxtLabel::mousePressEvent(QMouseEvent* event)
307 QFrame::mousePressEvent(event);
308 qxt_d().time.start();
311 void QxtLabel::mouseReleaseEvent(QMouseEvent* event)
313 QFrame::mouseReleaseEvent(event);
314 if (qxt_d().time.elapsed() < qApp->doubleClickInterval())