Vidalia 0.2.15
VClickLabel.cpp
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file VClickLabel.cpp
00013 ** \brief Custom widget to create a clickable label with both an image and text.
00014 */
00015 
00016 #include "VClickLabel.h"
00017 #include "Vidalia.h"
00018 
00019 #include <QPainter>
00020 
00021 
00022 /** Default constructor. */
00023 VClickLabel::VClickLabel(QWidget *parent)
00024  : QWidget(parent)
00025 {
00026   setCursor(Qt::PointingHandCursor);
00027 }
00028 
00029 /** Returns the current size hint for this widget's current contents. */
00030 QSize
00031 VClickLabel::sizeHint() const
00032 {
00033   int height = qMax(_pixmap.height(), fontMetrics().height())+2;
00034   int width = _pixmap.width() + fontMetrics().width(_text)+2;
00035   return QSize(width, height);
00036 }
00037 
00038 /** Returns the minimum size hint for this widget's current contents. */
00039 QSize
00040 VClickLabel::minimumSizeHint() const
00041 {
00042   return sizeHint();
00043 }
00044 
00045 /** Overloaded paint event to draw a pixmap and a text label. */
00046 void
00047 VClickLabel::paintEvent(QPaintEvent *e)
00048 {
00049   QPainter p(this);
00050   QRect rect = this->rect();
00051 
00052   if (vApp->isLeftToRight()) {
00053     if (!_pixmap.isNull())
00054       p.drawPixmap(0, qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00055     if (!_text.isEmpty())
00056       p.drawText(_pixmap.width()+2, (rect.height()+fontInfo().pixelSize())/2, _text);
00057   } else {
00058     if (!_pixmap.isNull())
00059       p.drawPixmap(qMax(rect.right()-_pixmap.width(), 0),
00060                    qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00061     if (!_text.isEmpty()) {
00062       int textWidth  = fontMetrics().width(_text);
00063       p.drawText(qMax(rect.right()-_pixmap.width()-textWidth-2, 0),
00064                  (rect.height()+fontInfo().pixelSize())/2, _text);
00065     }
00066   }
00067   e->accept();
00068 }
00069 
00070 /** Overloaded mouse event to catch left mouse button clicks. */
00071 void
00072 VClickLabel::mouseReleaseEvent(QMouseEvent *e)
00073 {
00074   if (e->button() == Qt::LeftButton) {
00075     emit clicked();
00076   }
00077   e->accept();
00078 }
00079 
00080 /** Sets the label text to <b>text</b>. */
00081 void
00082 VClickLabel::setText(const QString &text)
00083 {
00084   _text = text;
00085   update();
00086 }
00087 
00088 /** Sets the widget's image to <b>img</b>. */
00089 void
00090 VClickLabel::setPixmap(const QPixmap &pixmap)
00091 {
00092   _pixmap = pixmap;
00093   update();
00094 }
00095