Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qxtlabel.h"
00026 #include <QTime>
00027 #include <QEvent>
00028 #include <QPainter>
00029 #include <QFontMetrics>
00030 #include <QApplication>
00031 #if QT_VERSION < 0x040200
00032 #include <QAbstractItemDelegate>
00033 #endif // QT_VERSION
00034
00035 static const int Vertical_Mask = 0x02;
00036
00037 class QxtLabelPrivate : public QxtPrivate<QxtLabel>
00038 {
00039 public:
00040 QXT_DECLARE_PUBLIC(QxtLabel);
00041
00042 void init(const QString& txt = QString());
00043 void updateLabel();
00044
00045 QTime time;
00046 QString text;
00047 Qt::Alignment align;
00048 Qt::TextElideMode mode;
00049 Qxt::Rotation rot;
00050 };
00051
00052 void QxtLabelPrivate::init(const QString& txt)
00053 {
00054 text = txt;
00055 align = Qt::AlignCenter;
00056 mode = Qt::ElideMiddle;
00057 rot = Qxt::NoRotation;
00058 }
00059
00060 void QxtLabelPrivate::updateLabel()
00061 {
00062 qxt_p().updateGeometry();
00063 qxt_p().update();
00064 }
00065
00092 QxtLabel::QxtLabel(QWidget* parent, Qt::WindowFlags flags) : QFrame(parent, flags)
00093 {
00094 QXT_INIT_PRIVATE(QxtLabel);
00095 qxt_d().init();
00096 }
00097
00101 QxtLabel::QxtLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags) : QFrame(parent, flags)
00102 {
00103 QXT_INIT_PRIVATE(QxtLabel);
00104 qxt_d().init(text);
00105 }
00106
00110 QxtLabel::~QxtLabel()
00111 {}
00112
00117 QString QxtLabel::text() const
00118 {
00119 return qxt_d().text;
00120 }
00121
00122 void QxtLabel::setText(const QString& text)
00123 {
00124 if (qxt_d().text != text)
00125 {
00126 qxt_d().text = text;
00127 qxt_d().updateLabel();
00128 emit textChanged(text);
00129 }
00130 }
00131
00141 Qt::Alignment QxtLabel::alignment() const
00142 {
00143 return qxt_d().align;
00144 }
00145
00146 void QxtLabel::setAlignment(Qt::Alignment alignment)
00147 {
00148 if (qxt_d().align != alignment)
00149 {
00150 qxt_d().align = alignment;
00151 update();
00152 }
00153 }
00154
00164 Qt::TextElideMode QxtLabel::elideMode() const
00165 {
00166 return qxt_d().mode;
00167 }
00168
00169 void QxtLabel::setElideMode(Qt::TextElideMode mode)
00170 {
00171 if (qxt_d().mode != mode)
00172 {
00173 qxt_d().mode = mode;
00174 qxt_d().updateLabel();
00175 }
00176 }
00177
00187 Qxt::Rotation QxtLabel::rotation() const
00188 {
00189 return qxt_d().rot;
00190 }
00191
00192 void QxtLabel::setRotation(Qxt::Rotation rotation)
00193 {
00194 if (qxt_d().rot != rotation)
00195 {
00196 Qxt::Rotation prev = qxt_d().rot;
00197 qxt_d().rot = rotation;
00198 switch (rotation)
00199 {
00200 case Qxt::NoRotation:
00201 case Qxt::UpsideDown:
00202 if (prev & Vertical_Mask)
00203 {
00204 updateGeometry();
00205 }
00206 break;
00207
00208 case Qxt::Clockwise:
00209 case Qxt::CounterClockwise:
00210 if ((prev & Vertical_Mask) == 0)
00211 {
00212 updateGeometry();
00213 }
00214 break;
00215 default:
00216
00217 break;
00218 }
00219 }
00220 update();
00221 }
00222
00226 QSize QxtLabel::sizeHint() const
00227 {
00228 const QFontMetrics& fm = fontMetrics();
00229 QSize size(fm.width(qxt_d().text), fm.height());
00230 if (qxt_d().rot & Vertical_Mask)
00231 size.transpose();
00232 return size;
00233 }
00234
00238 QSize QxtLabel::minimumSizeHint() const
00239 {
00240 switch (qxt_d().mode)
00241 {
00242 #if QT_VERSION >= 0x040200
00243 case Qt::ElideNone:
00244 return sizeHint();
00245 #endif // QT_VERSION
00246 default:
00247 {
00248 const QFontMetrics& fm = fontMetrics();
00249 QSize size(fm.width("..."), fm.height());
00250 if (qxt_d().rot & Vertical_Mask)
00251 size.transpose();
00252 return size;
00253 }
00254 }
00255 }
00256
00260 void QxtLabel::paintEvent(QPaintEvent* event)
00261 {
00262 QFrame::paintEvent(event);
00263 QPainter p(this);
00264 p.rotate(qxt_d().rot);
00265 QRect r = contentsRect();
00266 switch (qxt_d().rot)
00267 {
00268 case Qxt::UpsideDown:
00269 p.translate(-r.width(), -r.height());
00270 break;
00271
00272 case Qxt::Clockwise:
00273 p.translate(0, -r.width());
00274 break;
00275
00276 case Qxt::CounterClockwise:
00277 p.translate(-r.height(), 0);
00278 break;
00279
00280 default:
00281
00282 break;
00283 }
00284
00285 if (qxt_d().rot & Vertical_Mask)
00286 {
00287 QSize s = r.size();
00288 s.transpose();
00289 r = QRect(r.topLeft(), s);
00290 }
00291
00292 #if QT_VERSION < 0x040200
00293 const QString elidedText = QAbstractItemDelegate::elidedText(fontMetrics(), r.width(), qxt_d().mode, qxt_d().text);
00294 #else // QT_VERSION >= 0x040200
00295 const QString elidedText = fontMetrics().elidedText(qxt_d().text, qxt_d().mode, r.width());
00296 #endif // QT_VERSION
00297 p.drawText(r, qxt_d().align, elidedText);
00298 }
00299
00303 void QxtLabel::changeEvent(QEvent* event)
00304 {
00305 QFrame::changeEvent(event);
00306 switch (event->type())
00307 {
00308 case QEvent::FontChange:
00309 case QEvent::ApplicationFontChange:
00310 qxt_d().updateLabel();
00311 break;
00312 default:
00313
00314 break;
00315 }
00316 }
00317
00321 void QxtLabel::mousePressEvent(QMouseEvent* event)
00322 {
00323 QFrame::mousePressEvent(event);
00324 qxt_d().time.start();
00325 }
00326
00330 void QxtLabel::mouseReleaseEvent(QMouseEvent* event)
00331 {
00332 QFrame::mouseReleaseEvent(event);
00333 if (qxt_d().time.elapsed() < qApp->doubleClickInterval())
00334 emit clicked();
00335 }