WWW, settings and info buttons changed to custom buttons.
[speedfreak] / Client / custombutton.cpp
1 /*
2  * Custom button class for customized button.
3  *
4  * @author      Jukka Kurttila <jktla@suomi24.fi>
5  * @copyright   (c) 2010 Speed Freak team
6  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "custombutton.h"
10
11 #include <QPainter>
12 #include <QIcon>
13
14 /**
15   *Constructor of this class.
16   */
17 CustomButton::CustomButton(QWidget *parent, QIcon *icon) : QWidget(parent)
18 {
19     bPressed = false;
20     //Get size of the icon
21     QList<QSize> list = icon->availableSizes(QIcon::Normal,QIcon::On);
22
23     //If icon is empty, do not create pixmaps and leave
24     if(list.isEmpty())
25         return;
26     QSize size = list.first();
27     if( icon )
28     {
29         pict1 = new QPixmap(icon->pixmap(size.width(),size.height(),QIcon::Normal,QIcon::On));
30         pict2 = new QPixmap(icon->pixmap(size.width(),size.height(),QIcon::Normal,QIcon::Off));
31     }
32 }
33 /**
34   *Destructor of this class.
35   */
36 CustomButton::~CustomButton()
37 {
38     if(!pict1)
39         delete pict1;
40     if(!pict2)
41         delete pict2;
42 }
43
44 void CustomButton::paintEvent(QPaintEvent *)
45 {
46     QPainter painter(this);
47
48     if(!bPressed)
49         painter.drawPixmap(0,0,*pict2);
50     else
51         painter.drawPixmap(0,0,*pict1);
52
53     //Debug print
54     //painter.drawText(50,50,"y: "+QString::number(mY));
55 }
56 void CustomButton::mousePressEvent(QMouseEvent* me)
57 {
58     bPressed = true;
59     repaint();
60 }
61 void CustomButton::mouseReleaseEvent(QMouseEvent* me)
62 {
63     mX = me->x();
64     mY = me->y();
65     //Emit open dialog signal if mouse is still over button
66     if( mY < this->height() && mY > 0 && mX < this->width() && mX > 0 )
67         emit OpenDialog();
68
69     bPressed = false;
70     repaint();
71 }
72 void CustomButton::mouseMoveEvent(QMouseEvent* me)
73 {
74     mX = me->x();
75     mY = me->y();
76     //Is mouse moved outside button?
77     if( mY > this->height() || mY < 0 || mX > this->width() || mX < 0 )
78         bPressed = false;
79     else
80         bPressed = true;
81     repaint();
82 }