From e96efe4610936f8d87eaa3cb1a11ddd6867f9324 Mon Sep 17 00:00:00 2001 From: Pekka Nissinen Date: Mon, 10 May 2010 11:20:52 +0300 Subject: [PATCH] Added a new all-purpose image button class --- src/ui/imagebutton.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++ src/ui/imagebutton.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/ui/imagebutton.cpp create mode 100644 src/ui/imagebutton.h diff --git a/src/ui/imagebutton.cpp b/src/ui/imagebutton.cpp new file mode 100644 index 0000000..7a9f003 --- /dev/null +++ b/src/ui/imagebutton.cpp @@ -0,0 +1,77 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Pekka Nissinen - pekka.nissinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#include +#include +#include +#include + +#include "imagebutton.h" + +ImageButton::ImageButton(QWidget *parent, QString normalIconPictureFileName, + 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 picture for selected state, use it instead of a simple highlight change + if(selectedIconPictureFileName != "") + icon.addFile(selectedIconPictureFileName, m_buttonSize, QIcon::Selected); + + setIcon(icon); + setIconSize(m_buttonSize); + setFixedSize(m_buttonSize); +} + +void ImageButton::mousePressEvent(QMouseEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; + + QAbstractButton::mousePressEvent(event); + + m_buttonMode = QIcon::Selected; + update(); +} + +void ImageButton::mouseReleaseEvent(QMouseEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; + + QAbstractButton::mouseReleaseEvent(event); + + m_buttonMode = QIcon::Normal; + update(); +} + +void ImageButton::paintEvent(QPaintEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; + + Q_UNUSED(event); + + QPainter painter(this); + icon().paint(&painter, this->rect(), NULL, m_buttonMode); +} diff --git a/src/ui/imagebutton.h b/src/ui/imagebutton.h new file mode 100644 index 0000000..b748d41 --- /dev/null +++ b/src/ui/imagebutton.h @@ -0,0 +1,86 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Pekka Nissinen - pekka.nissinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#ifndef IMAGEBUTTON_H +#define IMAGEBUTTON_H + +#include +#include +#include +#include +#include +#include + +/** +* @brief A simple class for icon button +* +* @author Pekka Nissinen - pekka.nissinen (at) ixonos.com +*/ +class ImageButton : public QPushButton +{ + Q_OBJECT + +public: + /** + * @brief Constructor + * + * @param parent Parent + * @param normalIconPictureFileName Normal state Icon image file name + * @param selectedIconPictureFileName Selected state Icon image file name (optional) + */ + ImageButton(QWidget *parent = 0, QString normalIconPictureFileName = "", + QString selectedIconPictureFileName = ""); + +/******************************************************************************* + * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS + ******************************************************************************/ +protected: + /** + * @brief Event handler for mouse press events + * + * @param event Mouse event + */ + void mousePressEvent(QMouseEvent *event); + + /** + * @brief Event handler for mouse release events + * + * @param event Mouse event + */ + void mouseReleaseEvent(QMouseEvent *event); + + /** + * @brief Event handler for paint events + * + * Paints the button and its icon + * @param event Paint event + */ + void paintEvent(QPaintEvent *event); + +/******************************************************************************* + * DATA MEMBERS + ******************************************************************************/ +private: + QIcon::Mode m_buttonMode; ///< Button mode (Normal, Selected etc...) + QSize m_buttonSize; ///< Button size +}; + +#endif // IMAGEBUTTON_H -- 1.7.9.5