Merge branch 'master' into new_panels_with_context_buttons
[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 <QPainter>
24
25 #include "imagebutton.h"
26
27 ImageButton::ImageButton(const QString &normalIconPictureFileName,
28                          const QString &selectedIconPictureFileName,
29                          const QString &disabledIconPictureFileName, QWidget *parent)
30     : QPushButton(parent),
31       m_buttonMode(QIcon::Normal)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34
35     // If there is a file name provided for icon image, use it as the icon for the button
36     if (!normalIconPictureFileName.isEmpty()) {
37         QPixmap iconPixmap(normalIconPictureFileName);
38         QSize 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.isEmpty())
43             icon.addFile(selectedIconPictureFileName, buttonSize, QIcon::Selected);
44
45         // If there is a picture for disabled state, use it instead of a simple color change
46         if(!disabledIconPictureFileName.isEmpty())
47             icon.addFile(disabledIconPictureFileName, buttonSize, QIcon::Disabled);
48
49         initButton(buttonSize, icon);
50     }
51 }
52
53 void ImageButton::setButtonIcon(const QPixmap &image)
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56
57     QSize buttonSize = image.size(); // Get the button size from the normal state icon picture
58     QIcon icon(image);
59
60     initButton(buttonSize, icon);
61 }
62
63 void ImageButton::mousePressEvent(QMouseEvent *event)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     if(m_buttonMode != QIcon::Disabled) {
68         QPushButton::mousePressEvent(event);
69         setMode(QIcon::Selected);
70     } else {
71         setDown(true);
72         emit pressed();
73     }
74 }
75
76 void ImageButton::mouseReleaseEvent(QMouseEvent *event)
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     if(m_buttonMode != QIcon::Disabled) {
81         QPushButton::mouseReleaseEvent(event);
82         setMode(QIcon::Normal);
83     } else {
84         setDown(false);
85         emit released();
86     }
87 }
88
89 void ImageButton::paintEvent(QPaintEvent *event)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     Q_UNUSED(event);
94
95     QPainter painter(this);
96     icon().paint(&painter, this->rect(), NULL, m_buttonMode);
97 }
98
99 void ImageButton::setMode(QIcon::Mode mode)
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     m_buttonMode = mode;
104     update();
105 }
106
107 QIcon::Mode ImageButton::mode()
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     return m_buttonMode;
112 }
113
114 void ImageButton::initButton(const QSize &size, const QIcon &icon)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     setIcon(icon);
119     setIconSize(size);
120     setFixedSize(size);
121 }