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::setButtonIcon(const QPixmap &image)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QSize buttonSize = image.size(); // Get the button size from the normal state icon picture
+ QIcon icon(image);
+
+ initButton(buttonSize, icon);
}
void ImageButton::mousePressEvent(QMouseEvent *event)
qDebug() << __PRETTY_FUNCTION__;
if(m_buttonMode != QIcon::Disabled) {
- QAbstractButton::mousePressEvent(event);
+ QPushButton::mousePressEvent(event);
setMode(QIcon::Selected);
+ } else {
+ setDown(true);
+ emit pressed();
}
}
qDebug() << __PRETTY_FUNCTION__;
if(m_buttonMode != QIcon::Disabled) {
- QAbstractButton::mouseReleaseEvent(event);
+ QPushButton::mouseReleaseEvent(event);
setMode(QIcon::Normal);
+ } else {
+ setDown(false);
+ emit released();
}
}
void ImageButton::setMode(QIcon::Mode mode)
{
- qDebug() << __PRETTY_FUNCTION__ << "Button icon mode:" << mode;
+ qDebug() << __PRETTY_FUNCTION__;
m_buttonMode = mode;
update();
}
+
+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);
+}