Resized refresh and set location buttons to follow Fremantle Master Layout Quide
[situare] / src / ui / imagebutton.cpp
index 41f2c13..0dfd592 100644 (file)
    USA.
 */
 
+#include <QSize>
 #include <QDebug>
 #include <QPixmap>
 #include <QPainter>
-#include <QAbstractButton>
 
 #include "imagebutton.h"
 
 ImageButton::ImageButton(QWidget *parent, QString normalIconPictureFileName,
-                     QString selectedIconPictureFileName)
+                         QString selectedIconPictureFileName)
     : QPushButton(parent),
       m_buttonMode(QIcon::Normal)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QPixmap iconPixmap(normalIconPictureFileName);
-    m_buttonSize = iconPixmap.size(); // Get the button size from the normal state icon picture
-    QIcon icon(iconPixmap);
+    // If there is a file name provided for icon image, use it as the icon for the button
+    if (!normalIconPictureFileName.isEmpty()) {
+        QPixmap iconPixmap(normalIconPictureFileName);
+        QSize buttonSize = iconPixmap.size(); // Get the button size from the normal state icon picture
+        QIcon icon(iconPixmap);
 
-    // If there is a picture for selected state, use it instead of a simple highlight change
-    if(selectedIconPictureFileName != "")
-        icon.addFile(selectedIconPictureFileName, m_buttonSize, QIcon::Selected);
+        // If there is a picture for selected state, use it instead of a simple highlight change
+        if(!selectedIconPictureFileName.isEmpty())
+            icon.addFile(selectedIconPictureFileName, buttonSize, QIcon::Selected);
 
-    setIcon(icon);
-    setIconSize(m_buttonSize);
-    setFixedSize(m_buttonSize);
+        initButton(buttonSize, icon);
+    }
 }
 
-void ImageButton::mousePressEvent(QMouseEvent *event)
+void ImageButton::setButtonIcon(const QPixmap &image)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QAbstractButton::mousePressEvent(event);
+    QSize buttonSize = image.size(); // Get the button size from the normal state icon picture
+    QIcon icon(image);
 
-    m_buttonMode = QIcon::Selected;
-    update();
+    initButton(buttonSize, icon);
 }
 
-void ImageButton::mouseReleaseEvent(QMouseEvent *event)
+void ImageButton::mousePressEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QAbstractButton::mouseReleaseEvent(event);
+    if(m_buttonMode != QIcon::Disabled) {
+        QPushButton::mousePressEvent(event);
+        setMode(QIcon::Selected);
+    } else {
+        setDown(true);
+        emit pressed();
+    }
+}
 
-    m_buttonMode = QIcon::Normal;
-    update();
+void ImageButton::mouseReleaseEvent(QMouseEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_buttonMode != QIcon::Disabled) {
+        QPushButton::mouseReleaseEvent(event);
+        setMode(QIcon::Normal);
+    } else {
+        setDown(false);
+        emit released();
+    }
 }
 
 void ImageButton::paintEvent(QPaintEvent *event)
@@ -73,11 +90,29 @@ void ImageButton::paintEvent(QPaintEvent *event)
     Q_UNUSED(event);
 
     QPainter painter(this);
+    icon().paint(&painter, this->rect(), NULL, m_buttonMode);
+}
+
+void ImageButton::setMode(QIcon::Mode mode)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    // Temporary border
-    painter.setPen(Qt::white);
-    painter.drawRect(this->rect().x(), this->rect().y(),
-                     this->rect().width() - 1, this->rect().height() - 1);
+    m_buttonMode = mode;
+    update();
+}
 
-    icon().paint(&painter, this->rect(), NULL, m_buttonMode);
+QIcon::Mode ImageButton::mode()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_buttonMode;
+}
+
+void ImageButton::initButton(const QSize &size, const QIcon &icon)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setIcon(icon);
+    setIconSize(size);
+    setFixedSize(size);
 }