#ifndef QXTLABEL_H
#define QXTLABEL_H
#include <QLabel>
#include <QTime>
class QxtLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode)
public:
QxtLabel(QWidget* parent = 0, Qt::WindowFlags flags = 0);
QxtLabel(const QString& text, QWidget* parent = 0, Qt::WindowFlags flags = 0);
virtual ~QxtLabel();
QString text() const;
Qt::Alignment alignment() const;
void setAlignment(Qt::Alignment alignment);
Qt::TextElideMode elideMode() const;
void setElideMode(Qt::TextElideMode mode);
virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const;
public Q_SLOTS:
void setText(const QString& text);
Q_SIGNALS:
void clicked();
void textChanged(const QString& text);
protected:
virtual void changeEvent(QEvent* event);
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);
virtual void paintEvent(QPaintEvent* event);
private:
QString textReal;
Qt::Alignment align;
Qt::TextElideMode elideModeReal;
QTime time;
};
#endif // QXTLABEL_H
#include "qxtlabel.h"
#include <QTime>
#include <QEvent>
#include <QPainter>
#include <QFontMetrics>
#include <QApplication>
/*!
\class QxtLabel
\inmodule QxtGui
\brief The QxtLabel widget is a label which is able to show elided and rotated plain text.
QxtLabel is a label which is able to show elided and rotated plain text.
In addition, QxtLabel provides a signal for clicking.
\image qxtlabel.png "QxtLabel in action."
Usage:
\code
QxtLabel* label = new QxtLabel(tr("Hello, World!"), this);
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
label->setElideMode(Qt::ElideRight);
label->setRotation(Qxt::CounterClockwise);
connect(label, SIGNAL(clicked()), this, SLOT(labelClicked()));
\endcode
\sa QLabel
*/
/*!
\fn QxtLabel::clicked()
This signal is emitted whenever the label has been clicked.
\bold {Note:} A combination of mouse button press and release in shorter
time than QApplication::doubleClickInterval is considered
as a click.
\sa QApplication::doubleClickInterval
*/
/*!
\fn QxtLabel::textChanged(const QString& text)
This signal is emitted whenever the \a text has changed.
*/
/*!
Constructs a new QxtLabel with \a parent and \a flags.
*/
QxtLabel::QxtLabel(QWidget* parent, Qt::WindowFlags flags) : QLabel(parent, flags)
{
elideModeReal = Qt::ElideMiddle;
}
/*!
Constructs a new QxtLabel with \a text, \a parent and \a flags.
*/
QxtLabel::QxtLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags) : QLabel(parent, flags)
{
textReal = text;
elideModeReal = Qt::ElideMiddle;
}
/*!
Destructs the label.
*/
QxtLabel::~QxtLabel()
{}
/*!
\property QxtLabel::text
\brief the text of the label
*/
QString QxtLabel::text() const
{
return textReal;
}
void QxtLabel::setText(const QString& text)
{
if (textReal != text)
{
textReal = text;
// qxt_d().updateLabel();
emit textChanged(text);
update();
}
}
/*!
\property QxtLabel::alignment
\brief the alignment of the text
The text is aligned according to this property.
The default value is Qt::AlignCenter.
\sa text, Qt::Alignment
*/
Qt::Alignment QxtLabel::alignment() const
{
return align;
}
void QxtLabel::setAlignment(Qt::Alignment alignment)
{
if (align != alignment)
{
align = alignment;
update(); // no geometry change, repaint is sufficient
}
}
/*!
\property QxtLabel::elideMode
\brief the elide mode of the text
The text is elided according to this property.
The default value is Qt::ElideMiddle.
\sa text, Qt::TextElideMode
*/
Qt::TextElideMode QxtLabel::elideMode() const
{
return elideModeReal;
}
void QxtLabel::setElideMode(Qt::TextElideMode mode)
{
if (elideModeReal != mode)
{
elideModeReal = mode;
//qxt_d().updateLabel();
}
}
/*!
\reimp
*/
QSize QxtLabel::sizeHint() const
{
const QFontMetrics& fm = fontMetrics();
QSize size(fm.width(textReal), fm.height());
return size;
}
/*!
\reimp
*/
QSize QxtLabel::minimumSizeHint() const
{
switch (elideModeReal)
{
case Qt::ElideNone:
return sizeHint();
default:
{
const QFontMetrics& fm = fontMetrics();
QSize size(fm.width("..."), fm.height());
return size;
}
}
}
/*!
\reimp
*/
void QxtLabel::paintEvent(QPaintEvent* event)
{
QLabel::paintEvent(event);
QPainter p(this);
QRect r = contentsRect();
const QString elidedText = fontMetrics().elidedText(textReal, elideModeReal, r.width());
p.drawText(r, elidedText);
}
/*!
\reimp
*/
void QxtLabel::changeEvent(QEvent* event)
{
QLabel::changeEvent(event);
switch (event->type())
{
case QEvent::FontChange:
case QEvent::ApplicationFontChange:
//qxt_d().updateLabel();
break;
default:
// nothing to do
break;
}
}
/*!
\reimp
*/
void QxtLabel::mousePressEvent(QMouseEvent* event)
{
QFrame::mousePressEvent(event);
time.start();
}
/*!
\reimp
*/
void QxtLabel::mouseReleaseEvent(QMouseEvent* event)
{
QFrame::mouseReleaseEvent(event);
if (time.elapsed() < qApp->doubleClickInterval())
emit clicked();
}