vclicklabel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <QPainter>
00018 #include <vidalia.h>
00019
00020 #include "vclicklabel.h"
00021
00022
00023
00024 VClickLabel::VClickLabel(QWidget *parent)
00025 : QWidget(parent)
00026 {
00027 setCursor(Qt::PointingHandCursor);
00028 connect(&_anim, SIGNAL(frameChanged(int)),
00029 this, SLOT(animationFrameChanged(int)));
00030 }
00031
00032
00033 QSize
00034 VClickLabel::sizeHint() const
00035 {
00036 int height = qMax(_pixmap.height(), fontMetrics().height())+2;
00037 int width = _pixmap.width() + fontMetrics().width(_text)+2;
00038 return QSize(width, height);
00039 }
00040
00041
00042 QSize
00043 VClickLabel::minimumSizeHint() const
00044 {
00045 return sizeHint();
00046 }
00047
00048
00049 void
00050 VClickLabel::paintEvent(QPaintEvent *e)
00051 {
00052 QPainter p(this);
00053 QRect rect = this->rect();
00054
00055 if (vApp->isLeftToRight()) {
00056 if (!_pixmap.isNull())
00057 p.drawPixmap(0, qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00058 if (!_text.isEmpty())
00059 p.drawText(_pixmap.width()+2, (rect.height()+fontInfo().pixelSize())/2, _text);
00060 } else {
00061 if (!_pixmap.isNull())
00062 p.drawPixmap(qMax(rect.right()-_pixmap.width(), 0),
00063 qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00064 if (!_text.isEmpty()) {
00065 int textWidth = fontMetrics().width(_text);
00066 p.drawText(qMax(rect.right()-_pixmap.width()-textWidth-2, 0),
00067 (rect.height()+fontInfo().pixelSize())/2, _text);
00068 }
00069 }
00070 e->accept();
00071 }
00072
00073
00074 void
00075 VClickLabel::setAnimation(const QPixmap &animPixmap)
00076 {
00077 _anim.setPixmap(animPixmap);
00078 _anim.start();
00079 }
00080
00081
00082 void
00083 VClickLabel::animationFrameChanged(int frameNumber)
00084 {
00085 Q_UNUSED(frameNumber);
00086 _pixmap = _anim.currentFrame();
00087 update();
00088 }
00089
00090
00091 void
00092 VClickLabel::mouseReleaseEvent(QMouseEvent *e)
00093 {
00094 if (e->button() == Qt::LeftButton) {
00095 emit clicked();
00096 }
00097 e->accept();
00098 }
00099
00100
00101 void
00102 VClickLabel::setText(const QString &text)
00103 {
00104 _text = text;
00105 update();
00106 }
00107
00108
00109 void
00110 VClickLabel::setPixmap(const QPixmap &pixmap)
00111 {
00112 _anim.stop();
00113 _pixmap = pixmap;
00114 update();
00115 }
00116