Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Fix hover event and system buttons (#262)
Browse files Browse the repository at this point in the history
* Fix hover event

* Fix system button
  • Loading branch information
SineStriker authored Aug 19, 2023
1 parent ca968b7 commit 8ac3d74
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject

Q_NODISCARD QSize getRecommendedButtonSize() const;

Q_NODISCARD bool isHovered() const;
Q_NODISCARD bool isPressed() const;
Q_NODISCARD QColor getHoverColor() const;
Q_NODISCARD QColor getPressColor() const;
Q_NODISCARD QColor getNormalColor() const;
Expand All @@ -67,8 +65,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
Q_NODISCARD bool isActive() const;
Q_NODISCARD int iconSize2() const;

void setHovered(const bool value);
void setPressed(const bool value);
void setHoverColor(const QColor &value);
void setPressColor(const QColor &value);
void setNormalColor(const QColor &value);
Expand All @@ -77,8 +73,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
void setActive(const bool value);
void setIconSize2(const int value);

void enterEventHandler(QT_ENTER_EVENT_TYPE *event);
void leaveEventHandler(QEvent *event);
void paintEventHandler(QPaintEvent *event);

private:
Expand All @@ -93,8 +87,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
QColor m_normalColor = {};
QColor m_activeForegroundColor = {};
QColor m_inactiveForegroundColor = {};
bool m_hovered = false;
bool m_pressed = false;
bool m_active = false;
std::optional<int> m_iconSize2 = std::nullopt;
};
Expand Down
14 changes: 2 additions & 12 deletions include/FramelessHelper/Widgets/standardsystembutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@
#pragma once

#include <FramelessHelper/Widgets/framelesshelperwidgets_global.h>
#include <QtWidgets/qabstractbutton.h>
#include <QtWidgets/qpushbutton.h>

FRAMELESSHELPER_BEGIN_NAMESPACE

class StandardSystemButtonPrivate;

class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QAbstractButton
class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QPushButton
{
Q_OBJECT
Q_DECLARE_PRIVATE(StandardSystemButton)
Q_DISABLE_COPY_MOVE(StandardSystemButton)
Q_PROPERTY(Global::SystemButtonType buttonType READ buttonType WRITE setButtonType NOTIFY buttonTypeChanged FINAL)
Q_PROPERTY(QString glyph READ glyph WRITE setGlyph NOTIFY glyphChanged FINAL)
Q_PROPERTY(bool hovered READ isHovered WRITE setHovered NOTIFY hoveredChanged FINAL)
Q_PROPERTY(bool pressed READ isPressed WRITE setPressed NOTIFY pressedChanged FINAL)
Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setHoverColor NOTIFY hoverColorChanged FINAL)
Q_PROPERTY(QColor pressColor READ pressColor WRITE setPressColor NOTIFY pressColorChanged FINAL)
Q_PROPERTY(QColor normalColor READ normalColor WRITE setNormalColor NOTIFY normalColorChanged FINAL)
Expand All @@ -56,8 +54,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QAbstractButton
Q_NODISCARD QSize sizeHint() const override;
Q_NODISCARD Global::SystemButtonType buttonType();
Q_NODISCARD QString glyph() const;
Q_NODISCARD bool isHovered() const;
Q_NODISCARD bool isPressed() const;
Q_NODISCARD QColor hoverColor() const;
Q_NODISCARD QColor pressColor() const;
Q_NODISCARD QColor normalColor() const;
Expand All @@ -69,8 +65,6 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QAbstractButton
public Q_SLOTS:
void setButtonType(const Global::SystemButtonType value);
void setGlyph(const QString &glyph);
void setHovered(const bool value);
void setPressed(const bool value);
void setHoverColor(const QColor &value);
void setPressColor(const QColor &value);
void setNormalColor(const QColor &value);
Expand All @@ -80,15 +74,11 @@ public Q_SLOTS:
void setIconSize2(const int value);

protected:
void enterEvent(QT_ENTER_EVENT_TYPE *event) override;
void leaveEvent(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;

Q_SIGNALS:
void buttonTypeChanged();
void glyphChanged();
void hoveredChanged();
void pressedChanged();
void hoverColorChanged();
void pressColorChanged();
void normalColorChanged();
Expand Down
22 changes: 20 additions & 2 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,31 @@ void Utils::emulateQtMouseEvent(const QObject *target, const QWindow *window, co
#endif
QCoreApplication::sendEvent(obj, &enterEvent);
if (hoverEnabled) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
QHoverEvent hoverEnterEvent(QEvent::HoverEnter, scenePos, globalPos, oldPos, modifiers);
#elif (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QHoverEvent hoverEnterEvent(QEvent::HoverEnter, localPos, globalPos, oldPos, modifiers);
#else
QHoverEvent hoverEnterEvent(QEvent::HoverEnter, localPos, oldPos, modifiers);
#endif
QCoreApplication::sendEvent(obj, &hoverEnterEvent);
}
};
const auto sendLeaveEvent = [&scenePos, &globalPos, &modifiers, hoverEnabled](QObject *obj) -> void {
const auto sendLeaveEvent = [&localPos, &scenePos, &globalPos, &modifiers, hoverEnabled](QObject *obj) -> void {
Q_ASSERT(obj);
if (!obj) {
return;
}
QEvent leaveEvent(QEvent::Leave);
QCoreApplication::sendEvent(obj, &leaveEvent);
if (hoverEnabled) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
QHoverEvent hoverLeaveEvent(QEvent::HoverLeave, scenePos, globalPos, oldPos, modifiers);
#elif (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QHoverEvent hoverLeaveEvent(QEvent::HoverLeave, localPos, globalPos, oldPos, modifiers);
#else
QHoverEvent hoverLeaveEvent(QEvent::HoverLeave, localPos, oldPos, modifiers);
#endif
QCoreApplication::sendEvent(obj, &hoverLeaveEvent);
}
};
Expand All @@ -689,15 +701,21 @@ void Utils::emulateQtMouseEvent(const QObject *target, const QWindow *window, co
QMouseEvent event(QEvent::MouseMove, localPos, scenePos, globalPos, actualButton, buttons, modifiers);
QCoreApplication::sendEvent(obj, &event);
};
const auto sendHoverMoveEvent = [&scenePos, &globalPos, &modifiers, hoverEnabled](QObject *obj) -> void {
const auto sendHoverMoveEvent = [&localPos, &scenePos, &globalPos, &modifiers, hoverEnabled](QObject *obj) -> void {
Q_ASSERT(obj);
if (!obj) {
return;
}
if (!hoverEnabled) {
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
QHoverEvent event(QEvent::HoverMove, scenePos, globalPos, oldPos, modifiers);
#elif (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
QHoverEvent event(QEvent::HoverMove, localPos, globalPos, oldPos, modifiers);
#else
QHoverEvent event(QEvent::HoverMove, localPos, oldPos, modifiers);
#endif
QCoreApplication::sendEvent(obj, &event);
};
const auto sendMousePressEvent = [&localPos, &scenePos, &globalPos, &buttons, &modifiers](QObject *obj) -> void {
Expand Down
131 changes: 6 additions & 125 deletions src/widgets/standardsystembutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,6 @@ QSize StandardSystemButtonPrivate::getRecommendedButtonSize() const
return kDefaultSystemButtonSize;
}

bool StandardSystemButtonPrivate::isHovered() const
{
return m_hovered;
}

bool StandardSystemButtonPrivate::isPressed() const
{
return m_pressed;
}

QColor StandardSystemButtonPrivate::getHoverColor() const
{
return m_hoverColor;
Expand Down Expand Up @@ -169,55 +159,6 @@ int StandardSystemButtonPrivate::iconSize2() const
return m_iconSize2.value_or(FramelessManagerPrivate::getIconFont().pointSize());
}

void StandardSystemButtonPrivate::setHovered(const bool value)
{
if (m_hovered == value) {
return;
}
m_hovered = value;
Q_Q(StandardSystemButton);
q->update();
#if 0
if (m_hovered) {
const QString toolTip = q->toolTip();
if (!toolTip.isEmpty() && !QToolTip::isVisible()) {
const auto yPos = [q]() -> int {
static const int h = kDefaultSystemButtonSize.height();
if (const QWidget * const window = q->window()) {
if (Utils::windowStatesToWindowState(window->windowState()) == Qt::WindowMaximized) {
return std::round(qreal(h) * qreal(0.5));
}
}
return -std::round(qreal(h) * qreal(1.3));
}();
QToolTip::showText(q->mapToGlobal(QPoint(-2, yPos)), toolTip, q, q->geometry());
}
} else {
if (QToolTip::isVisible()) {
QToolTip::hideText();
}
}
#endif
Q_EMIT q->hoveredChanged();
}

void StandardSystemButtonPrivate::setPressed(const bool value)
{
if (m_pressed == value) {
return;
}
m_pressed = value;
Q_Q(StandardSystemButton);
q->setDown(m_pressed);
q->update();
Q_EMIT q->pressedChanged();
if (m_pressed) {
Q_EMIT q->pressed();
} else {
Q_EMIT q->released();
}
}

void StandardSystemButtonPrivate::setHoverColor(const QColor &value)
{
Q_ASSERT(value.isValid());
Expand Down Expand Up @@ -319,26 +260,6 @@ void StandardSystemButtonPrivate::setIconSize2(const int value)
Q_EMIT q->iconSize2Changed();
}

void StandardSystemButtonPrivate::enterEventHandler(QT_ENTER_EVENT_TYPE *event)
{
Q_ASSERT(event);
if (!event) {
return;
}
setHovered(true);
event->accept();
}

void StandardSystemButtonPrivate::leaveEventHandler(QEvent *event)
{
Q_ASSERT(event);
if (!event) {
return;
}
setHovered(false);
event->accept();
}

void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
{
Q_ASSERT(event);
Expand All @@ -350,12 +271,12 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
painter.save();
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
| QPainter::SmoothPixmapTransform);
const auto backgroundColor = [this]() -> QColor {
const auto backgroundColor = [this, q]() -> QColor {
// The pressed state has higher priority than the hovered state.
if (m_pressed && m_pressColor.isValid()) {
if (q->isDown() && m_pressColor.isValid()) {
return m_pressColor;
}
if (m_hovered && m_hoverColor.isValid()) {
if (q->underMouse() && m_hoverColor.isValid()) {
return m_hoverColor;
}
if (m_normalColor.isValid()) {
Expand All @@ -368,8 +289,8 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
painter.fillRect(buttonRect, backgroundColor);
}
if (!m_glyph.isEmpty()) {
painter.setPen([this]() -> QColor {
if (!m_hovered && !m_active && m_inactiveForegroundColor.isValid()) {
painter.setPen([this, q]() -> QColor {
if (!q->underMouse() && !m_active && m_inactiveForegroundColor.isValid()) {
return m_inactiveForegroundColor;
}
if (m_activeForegroundColor.isValid()) {
Expand Down Expand Up @@ -397,12 +318,10 @@ void StandardSystemButtonPrivate::initialize()
q->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
q->setFixedSize(kDefaultSystemButtonSize);
q->setIconSize(kDefaultSystemButtonIconSize);
connect(q, &StandardSystemButton::pressed, this, [this](){ setPressed(true); });
connect(q, &StandardSystemButton::released, this, [this](){ setPressed(false); });
}

StandardSystemButton::StandardSystemButton(QWidget *parent)
: QAbstractButton(parent), d_ptr(new StandardSystemButtonPrivate(this))
: QPushButton(parent), d_ptr(new StandardSystemButtonPrivate(this))
{
}

Expand Down Expand Up @@ -444,30 +363,6 @@ void StandardSystemButton::setGlyph(const QString &glyph)
d->setGlyph(glyph);
}

bool StandardSystemButton::isHovered() const
{
Q_D(const StandardSystemButton);
return d->isHovered();
}

void StandardSystemButton::setHovered(const bool value)
{
Q_D(StandardSystemButton);
d->setHovered(value);
}

bool StandardSystemButton::isPressed() const
{
Q_D(const StandardSystemButton);
return d->isPressed();
}

void StandardSystemButton::setPressed(const bool value)
{
Q_D(StandardSystemButton);
d->setPressed(value);
}

QColor StandardSystemButton::hoverColor() const
{
Q_D(const StandardSystemButton);
Expand Down Expand Up @@ -552,20 +447,6 @@ void StandardSystemButton::setIconSize2(const int value)
d->setIconSize2(value);
}

void StandardSystemButton::enterEvent(QT_ENTER_EVENT_TYPE *event)
{
QAbstractButton::enterEvent(event);
Q_D(StandardSystemButton);
d->enterEventHandler(event);
}

void StandardSystemButton::leaveEvent(QEvent *event)
{
QAbstractButton::leaveEvent(event);
Q_D(StandardSystemButton);
d->leaveEventHandler(event);
}

void StandardSystemButton::paintEvent(QPaintEvent *event)
{
Q_D(StandardSystemButton);
Expand Down

0 comments on commit 8ac3d74

Please sign in to comment.