Fixed FacebookLoginBrowser::destroyed() signal handling
[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 <QMouseEvent>
24 #include <QPainter>
25
26 #include "imagebutton.h"
27
28 ImageButton::ImageButton(const QString &normalIconPictureFileName,
29                          const QString &selectedIconPictureFileName,
30                          const QString &disabledIconPictureFileName, QWidget *parent)
31     : QPushButton(parent)
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::paintEvent(QPaintEvent *event)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     Q_UNUSED(event);
68
69     QPainter painter(this);
70
71     if (isEnabled()) {
72         if (isChecked())
73             icon().paint(&painter, rect(), NULL, QIcon::Selected);
74         else if (isDown())
75             icon().paint(&painter, rect(), NULL, QIcon::Selected);
76         else
77             icon().paint(&painter, rect(), NULL, QIcon::Normal);
78     } else {
79         icon().paint(&painter, rect(), NULL, QIcon::Disabled);
80     }
81 }
82
83 void ImageButton::initButton(const QSize &size, const QIcon &icon)
84 {
85     qDebug() << __PRETTY_FUNCTION__;
86
87     setIcon(icon);
88     setIconSize(size);
89     setFixedSize(size);
90 }