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 2362 2008-02-29 04:30:11Z 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   connect(&_anim, SIGNAL(frameChanged(int)), 
00029              this, SLOT(animationFrameChanged(int)));
00030 }
00031 
00032 /** Returns the current size hint for this widget's current contents. */
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 /** Returns the minimum size hint for this widget's current contents. */
00042 QSize
00043 VClickLabel::minimumSizeHint() const
00044 {
00045   return sizeHint();
00046 }
00047 
00048 /** Overloaded paint event to draw a pixmap and a text label. */
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 /** Sets the widget's image to the animated image file <b>animFile</b>. */
00074 void
00075 VClickLabel::setAnimation(const QPixmap &animPixmap)
00076 {
00077   _anim.setPixmap(animPixmap);
00078   _anim.start();
00079 }
00080 
00081 /** Responds to a frame change on the animation. */
00082 void
00083 VClickLabel::animationFrameChanged(int frameNumber)
00084 {
00085   Q_UNUSED(frameNumber);
00086   _pixmap = _anim.currentFrame();
00087   update();
00088 }
00089 
00090 /** Overloaded mouse event to catch left mouse button clicks. */
00091 void
00092 VClickLabel::mouseReleaseEvent(QMouseEvent *e)
00093 {
00094   if (e->button() == Qt::LeftButton) {
00095     emit clicked();
00096   }
00097   e->accept();
00098 }
00099 
00100 /** Sets the label text to <b>text</b>. */
00101 void
00102 VClickLabel::setText(const QString &text)
00103 {
00104   _text = text;
00105   update();
00106 }
00107 
00108 /** Sets the widget's image to <b>img</b>. */
00109 void
00110 VClickLabel::setPixmap(const QPixmap &pixmap)
00111 {
00112   _anim.stop();
00113   _pixmap = pixmap;
00114   update();
00115 }
00116 

Generated on Sat Aug 16 17:38:36 2008 for Vidalia by  doxygen 1.5.6