Merge branch 'master' into userinfo
[situare] / src / ui / imagebutton.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Pekka Nissinen - pekka.nissinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QDebug>
23 #include <QPixmap>
24 #include <QPainter>
25 #include <QAbstractButton>
26
27 #include "imagebutton.h"
28
29 ImageButton::ImageButton(QWidget *parent, QString normalIconPictureFileName,
30                      QString selectedIconPictureFileName)
31     : QPushButton(parent),
32       m_buttonMode(QIcon::Normal)
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35
36     if (normalIconPictureFileName != "") {
37         QPixmap iconPixmap(normalIconPictureFileName);
38         m_buttonSize = iconPixmap.size(); // Get the button size from the normal state icon picture
39         QIcon icon(iconPixmap);
40
41         // If there is a picture for selected state, use it instead of a simple highlight change
42         if(selectedIconPictureFileName != "")
43             icon.addFile(selectedIconPictureFileName, m_buttonSize, QIcon::Selected);
44
45         setIcon(icon);
46         setIconSize(m_buttonSize);
47         setFixedSize(m_buttonSize);
48     }
49 }
50
51 void ImageButton::setButtonIcon(const QPixmap &image)
52 {
53     m_buttonSize = image.size(); // Get the button size from the normal state icon picture
54     QIcon icon(image);
55
56     setIcon(icon);
57     setIconSize(m_buttonSize);
58     setFixedSize(m_buttonSize);
59 }
60
61 void ImageButton::mousePressEvent(QMouseEvent *event)
62 {
63     qDebug() << __PRETTY_FUNCTION__;
64
65     if(m_buttonMode != QIcon::Disabled) {
66         QAbstractButton::mousePressEvent(event);
67         setMode(QIcon::Selected);
68     }
69 }
70
71 void ImageButton::mouseReleaseEvent(QMouseEvent *event)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     if(m_buttonMode != QIcon::Disabled) {
76         QAbstractButton::mouseReleaseEvent(event);
77         setMode(QIcon::Normal);
78     }
79 }
80
81 void ImageButton::paintEvent(QPaintEvent *event)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     Q_UNUSED(event);
86
87     QPainter painter(this);
88     icon().paint(&painter, this->rect(), NULL, m_buttonMode);
89 }
90
91 void ImageButton::setMode(QIcon::Mode mode)
92 {
93     qDebug() << __PRETTY_FUNCTION__;
94
95     m_buttonMode = mode;
96     update();
97 }
98
99 QIcon::Mode ImageButton::mode()
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     return m_buttonMode;
104 }