Conflict solved: mainwindow.cpp and calculate.cpp
[speedfreak] / Client / custombutton.cpp
1 /*
2  * Custom button class for customized button.
3  *
4  * @author     Jukka Kurttila   <jktla@suomi24.fi>
5  * @author     Toni Jussila     <toni.jussila@fudeco.com>
6  * @copyright  (c) 2010 Speed Freak team
7  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "custombutton.h"
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 /**
35   * Destructor of this class.
36   */
37 CustomButton::~CustomButton()
38 {
39     if(!pict1)
40         delete pict1;
41     if(!pict2)
42         delete pict2;
43 }
44
45 /**
46   *
47   */
48 void CustomButton::paintEvent(QPaintEvent *)
49 {
50     QPainter painter(this);
51
52     if(!bPressed)
53         painter.drawPixmap(0,0,*pict2);
54     else
55         painter.drawPixmap(0,0,*pict1);
56
57     // Debug print
58     // painter.drawText(50,50,"y: "+QString::number(mY));
59 }
60
61 /**
62   * Mouse press event.
63   *
64   * @param QMouseEvent me
65   */
66 void CustomButton::mousePressEvent(QMouseEvent* me)
67 {
68     bPressed = true;
69     repaint();
70 }
71
72 /**
73   * Mouse release event.
74   *
75   * @param QMouseEvent me
76   */
77 void CustomButton::mouseReleaseEvent(QMouseEvent* me)
78 {
79     mX = me->x();
80     mY = me->y();
81     // Emit open dialog signal if mouse is still over button
82     if( mY < this->height() && mY > 0 && mX < this->width() && mX > 0 )
83         emit OpenDialog();
84
85     bPressed = false;
86     repaint();
87 }
88
89 /**
90   * Mouse move event.
91   *
92   * @param QMouseEvent me
93   */
94 void CustomButton::mouseMoveEvent(QMouseEvent* me)
95 {
96     mX = me->x();
97     mY = me->y();
98     // Is mouse moved outside button?
99     if( mY > this->height() || mY < 0 || mX > this->width() || mX < 0 )
100         bPressed = false;
101     else
102         bPressed = true;
103     repaint();
104 }