Simplified ImageButton class implementation and now the button is drawed correctly in toggled and disabled state.
Made small changes to ZoomButton & ZoomButtonPanel to reflect changes in ImageButton.
Reviewed by: Jussi Laitinen
*/
#include <QDebug>
+#include <QMouseEvent>
#include <QPainter>
#include "imagebutton.h"
ImageButton::ImageButton(const QString &normalIconPictureFileName,
const QString &selectedIconPictureFileName,
const QString &disabledIconPictureFileName, QWidget *parent)
- : QPushButton(parent),
- m_buttonMode(QIcon::Normal)
+ : QPushButton(parent)
{
qDebug() << __PRETTY_FUNCTION__;
initButton(buttonSize, icon);
}
-void ImageButton::mousePressEvent(QMouseEvent *event)
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- if(m_buttonMode != QIcon::Disabled) {
- QPushButton::mousePressEvent(event);
- setMode(QIcon::Selected);
- } else {
- setDown(true);
- emit pressed();
- }
-}
-
-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)
{
qDebug() << __PRETTY_FUNCTION__;
Q_UNUSED(event);
QPainter painter(this);
- icon().paint(&painter, this->rect(), NULL, m_buttonMode);
-}
-
-void ImageButton::setMode(QIcon::Mode mode)
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- m_buttonMode = mode;
- update();
-}
-
-QIcon::Mode ImageButton::mode()
-{
- qDebug() << __PRETTY_FUNCTION__;
- return m_buttonMode;
+ if (isEnabled()) {
+ if (isChecked())
+ icon().paint(&painter, rect(), NULL, QIcon::Selected);
+ else if (isDown())
+ icon().paint(&painter, rect(), NULL, QIcon::Selected);
+ else
+ icon().paint(&painter, rect(), NULL, QIcon::Normal);
+ } else {
+ icon().paint(&painter, rect(), NULL, QIcon::Disabled);
+ }
}
void ImageButton::initButton(const QSize &size, const QIcon &icon)
******************************************************************************/
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
*/
void setButtonIcon(const QPixmap &image);
- /**
- * @brief Sets the button icon mode
- *
- * @param mode Button icon mode
- */
- void setMode(QIcon::Mode mode);
-
- /**
- * @brief Returns the current button icon mode
- *
- * @return Button icon mode
- */
- QIcon::Mode mode();
-
private:
/**
* @brief Initializes the button by setting graphics and alpha mask
* @param icon Image icon
*/
void initButton(const QSize &size, const QIcon &icon);
-
-/*******************************************************************************
- * DATA MEMBERS
- ******************************************************************************/
-protected:
- QIcon::Mode m_buttonMode; ///< Button mode (Normal, Selected etc...)
};
#endif // IMAGEBUTTON_H
return m_eventPosition;
}
-void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- QToolButton::mouseMoveEvent(event);
-
- event->ignore();
-}
-
void IndicatorButton::mousePressEvent(QMouseEvent *event)
{
qDebug() << __PRETTY_FUNCTION__;
******************************************************************************/
protected:
/**
- * @brief Event handler for mouse move events
- *
- * @param event Mouse event
- */
- void mouseMoveEvent(QMouseEvent *event);
-
- /**
* @brief Event handler for mouse press events
*
* @param event Mouse event
{
qDebug() << __PRETTY_FUNCTION__;
- ImageButton::mousePressEvent(event);
-
m_eventPosition = mapToParent(event->pos());
+
+ ImageButton::mousePressEvent(event);
}
const QPoint& ZoomButton::eventPosition()
}
}
-const ZoomButton* ZoomButtonPanel::zoomInButton()
+const ZoomButton* ZoomButtonPanel::zoomInButton() const
{
qDebug() << __PRETTY_FUNCTION__;
return m_zoomInButton;
}
-const ZoomButton* ZoomButtonPanel::zoomOutButton()
+const ZoomButton* ZoomButtonPanel::zoomOutButton() const
{
qDebug() << __PRETTY_FUNCTION__;
{
qDebug() << __PRETTY_FUNCTION__;
- m_zoomInButton->setMode(QIcon::Disabled);
+ m_zoomInButton->setEnabled(false);
}
void ZoomButtonPanel::disableZoomOutButton()
{
qDebug() << __PRETTY_FUNCTION__;
- m_zoomOutButton->setMode(QIcon::Disabled);
+ m_zoomOutButton->setEnabled(false);
}
void ZoomButtonPanel::resetButtons()
{
qDebug() << __PRETTY_FUNCTION__;
- m_zoomInButton->setMode(QIcon::Normal);
- m_zoomOutButton->setMode(QIcon::Normal);
+ m_zoomInButton->setEnabled(true);
+ m_zoomOutButton->setEnabled(true);
}
void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
if(mode) {
emit draggingModeTriggered();
- m_zoomInMode = m_zoomInButton->mode();
- m_zoomOutMode = m_zoomOutButton->mode();
- m_zoomInButton->setMode(QIcon::Disabled);
- m_zoomOutButton->setMode(QIcon::Disabled);
+ m_zoomInMode = m_zoomInButton->isEnabled();
+ m_zoomOutMode = m_zoomOutButton->isEnabled();
+
+ m_zoomInButton->setEnabled(false);
+ m_zoomOutButton->setEnabled(false);
grabMouse();
m_forceReleaseTimer->start();
m_dragPosition = eventPosition;
} else {
- if(m_zoomInMode == QIcon::Selected)
- m_zoomInButton->setMode(QIcon::Normal);
- else
- m_zoomInButton->setMode(m_zoomInMode);
-
- if(m_zoomOutMode == QIcon::Selected)
- m_zoomOutButton->setMode(QIcon::Normal);
- else
- m_zoomOutButton->setMode(m_zoomOutMode);
+ m_zoomInButton->setEnabled(m_zoomInMode);
+ m_zoomOutButton->setEnabled(m_zoomOutMode);
releaseMouse();
m_forceReleaseTimer->stop();
+
m_zoomInButton->setDown(false);
m_zoomOutButton->setDown(false);
}
*
* @return Pointer to the zoomInButton
*/
- const ZoomButton* zoomInButton();
+ const ZoomButton* zoomInButton() const;
/**
* @brief Getter for the zoom out button
*
* @return Pointer to the zoomOutButton
*/
- const ZoomButton* zoomOutButton();
+ const ZoomButton* zoomOutButton() const;
public slots:
/**
******************************************************************************/
private:
bool m_isDraggable; ///< Boolean for tracking the draggability state
+ bool m_zoomInMode; ///< Boolean for storing zoom in button mode before dragging
+ bool m_zoomOutMode; ///< Boolean for storing zoom out button mode before dragging
QGridLayout m_panelLayout; ///< Panel layout
QPoint m_dragPosition; ///< Location from where the widget is grabbed
- QIcon::Mode m_zoomInMode; ///< Store for zoom in button mode before dragging
- QIcon::Mode m_zoomOutMode; ///< Store for zoom out button mode before dragging
-
QSize m_screenSize; ///< Store for the screen size
QTimer *m_dragStartTimer; ///< Timer to init draggability of the zoom panel