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.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file vclicklabel.cpp
00013 ** \version $Id: vclicklabel.cpp 2995 2008-08-19 02:12:31Z edmanm $
00014 ** \brief Custom widget to create a clickable label with both an image and text.
00015 */
00016 
00017 #include <QPainter>
00018 #include <vidalia.h>
00019 
00020 #include "vclicklabel.h"
00021 
00022 
00023 /** Default constructor. */
00024 VClickLabel::VClickLabel(QWidget *parent)
00025  : QWidget(parent)
00026 {
00027   setCursor(Qt::PointingHandCursor);
00028 }
00029 
00030 /** Returns the current size hint for this widget's current contents. */
00031 QSize
00032 VClickLabel::sizeHint() const
00033 {
00034   int height = qMax(_pixmap.height(), fontMetrics().height())+2;
00035   int width = _pixmap.width() + fontMetrics().width(_text)+2;
00036   return QSize(width, height);
00037 }
00038 
00039 /** Returns the minimum size hint for this widget's current contents. */
00040 QSize
00041 VClickLabel::minimumSizeHint() const
00042 {
00043   return sizeHint();
00044 }
00045 
00046 /** Overloaded paint event to draw a pixmap and a text label. */
00047 void
00048 VClickLabel::paintEvent(QPaintEvent *e)
00049 {
00050   QPainter p(this);
00051   QRect rect = this->rect();
00052 
00053   if (vApp->isLeftToRight()) {
00054     if (!_pixmap.isNull())
00055       p.drawPixmap(0, qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00056     if (!_text.isEmpty())
00057       p.drawText(_pixmap.width()+2, (rect.height()+fontInfo().pixelSize())/2, _text);
00058   } else {
00059     if (!_pixmap.isNull())
00060       p.drawPixmap(qMax(rect.right()-_pixmap.width(), 0),
00061                    qMax((rect.height()-_pixmap.height())/2, 0), _pixmap);
00062     if (!_text.isEmpty()) {
00063       int textWidth  = fontMetrics().width(_text);
00064       p.drawText(qMax(rect.right()-_pixmap.width()-textWidth-2, 0),
00065                  (rect.height()+fontInfo().pixelSize())/2, _text);
00066     }
00067   }
00068   e->accept();
00069 }
00070 
00071 /** Overloaded mouse event to catch left mouse button clicks. */
00072 void
00073 VClickLabel::mouseReleaseEvent(QMouseEvent *e)
00074 {
00075   if (e->button() == Qt::LeftButton) {
00076     emit clicked();
00077   }
00078   e->accept();
00079 }
00080 
00081 /** Sets the label text to <b>text</b>. */
00082 void
00083 VClickLabel::setText(const QString &text)
00084 {
00085   _text = text;
00086   update();
00087 }
00088 
00089 /** Sets the widget's image to <b>img</b>. */
00090 void
00091 VClickLabel::setPixmap(const QPixmap &pixmap)
00092 {
00093   _pixmap = pixmap;
00094   update();
00095 }
00096 

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1