vclicklabel.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file vclicklabel.cpp
00024  * \version $Id: vclicklabel.cpp 1700 2007-04-09 01:51:15Z edmanm $
00025  * \brief Custom widget to create a clickable label with both an image and text.
00026  */
00027 
00028 #include <QPainter>
00029 
00030 #include "vclicklabel.h"
00031 
00032 
00033 /** Default constructor. */
00034 VClickLabel::VClickLabel(QWidget *parent)
00035  : QWidget(parent)
00036 {
00037   setCursor(Qt::PointingHandCursor);
00038   connect(&_anim, SIGNAL(frameChanged(int)), 
00039              this, SLOT(animationFrameChanged(int)));
00040 }
00041 
00042 /** Returns the current size hint for this widget's current contents. */
00043 QSize
00044 VClickLabel::sizeHint() const
00045 {
00046   int height = qMax(_pixmap.height(), fontMetrics().height())+2;
00047   int width = _pixmap.width() + fontMetrics().width(_text)+2;
00048   return QSize(width, height);
00049 }
00050 
00051 /** Returns the minimum size hint for this widget's current contents. */
00052 QSize
00053 VClickLabel::minimumSizeHint() const
00054 {
00055   return sizeHint();
00056 }
00057 
00058 /** Overloaded paint event to draw a pixmap and a text label. */
00059 void
00060 VClickLabel::paintEvent(QPaintEvent *e)
00061 {
00062   QPainter p(this);
00063   QRect rect = this->rect();
00064   if (!_pixmap.isNull())
00065     p.drawPixmap(0, qMax((rect.height()-_pixmap.height())/2,0), _pixmap);
00066   if (!_text.isEmpty())
00067     p.drawText(_pixmap.width()+2, (rect.height()+fontInfo().pixelSize())/2, _text);
00068   e->accept();
00069 }
00070 
00071 /** Sets the widget's image to the animated image file <b>animFile</b>. */
00072 void
00073 VClickLabel::setAnimation(const QPixmap &animPixmap)
00074 {
00075   _anim.setPixmap(animPixmap);
00076   _anim.start();
00077 }
00078 
00079 /** Responds to a frame change on the animation. */
00080 void
00081 VClickLabel::animationFrameChanged(int frameNumber)
00082 {
00083   Q_UNUSED(frameNumber);
00084   _pixmap = _anim.currentFrame();
00085   update();
00086 }
00087 
00088 /** Overloaded mouse event to catch left mouse button clicks. */
00089 void
00090 VClickLabel::mouseReleaseEvent(QMouseEvent *e)
00091 {
00092   if (e->button() == Qt::LeftButton) {
00093     emit clicked();
00094   }
00095   e->accept();
00096 }
00097 
00098 /** Sets the label text to <b>text</b>. */
00099 void
00100 VClickLabel::setText(const QString &text)
00101 {
00102   _text = text;
00103   update();
00104 }
00105 
00106 /** Sets the widget's image to <b>img</b>. */
00107 void
00108 VClickLabel::setPixmap(const QPixmap &pixmap)
00109 {
00110   _anim.stop();
00111   _pixmap = pixmap;
00112   update();
00113 }
00114 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3