Fixed zoom panel position loading bug.
[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 <QSize>
23 #include <QDebug>
24 #include <QPixmap>
25 #include <QPainter>
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 there is a file name provided for icon image, use it as the icon for the button
37     if (!normalIconPictureFileName.isEmpty()) {
38         QPixmap iconPixmap(normalIconPictureFileName);
39         QSize buttonSize = iconPixmap.size(); // Get the button size from the normal state icon picture
40         QIcon icon(iconPixmap);
41
42         // If there is a picture for selected state, use it instead of a simple highlight change
43         if(!selectedIconPictureFileName.isEmpty())
44             icon.addFile(selectedIconPictureFileName, buttonSize, QIcon::Selected);
45
46         initButton(buttonSize, icon);
47     }
48 }
49
50 void ImageButton::setButtonIcon(const QPixmap &image)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     QSize buttonSize = image.size(); // Get the button size from the normal state icon picture
55     QIcon icon(image);
56
57     initButton(buttonSize, icon);
58 }
59
60 void ImageButton::mousePressEvent(QMouseEvent *event)
61 {
62     qDebug() << __PRETTY_FUNCTION__;
63
64     if(m_buttonMode != QIcon::Disabled) {
65         QPushButton::mousePressEvent(event);
66         setMode(QIcon::Selected);
67     } else {
68         setDown(true);
69         emit pressed();
70     }
71 }
72
73 void ImageButton::mouseReleaseEvent(QMouseEvent *event)
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76
77     if(m_buttonMode != QIcon::Disabled) {
78         QPushButton::mouseReleaseEvent(event);
79         setMode(QIcon::Normal);
80     } else {
81         setDown(false);
82         emit released();
83     }
84 }
85
86 void ImageButton::paintEvent(QPaintEvent *event)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     Q_UNUSED(event);
91
92     QPainter painter(this);
93     icon().paint(&painter, this->rect(), NULL, m_buttonMode);
94 }
95
96 void ImageButton::setMode(QIcon::Mode mode)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     m_buttonMode = mode;
101     update();
102 }
103
104 QIcon::Mode ImageButton::mode()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     return m_buttonMode;
109 }
110
111 void ImageButton::initButton(const QSize &size, const QIcon &icon)
112 {
113     setIcon(icon);
114     setIconSize(size);
115     setFixedSize(size);
116 }