diff --git a/include/FramelessHelper/Widgets/private/standardsystembutton_p.h b/include/FramelessHelper/Widgets/private/standardsystembutton_p.h index a6ef6d9e..1765aeff 100644 --- a/include/FramelessHelper/Widgets/private/standardsystembutton_p.h +++ b/include/FramelessHelper/Widgets/private/standardsystembutton_p.h @@ -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; @@ -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); @@ -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: @@ -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 m_iconSize2 = std::nullopt; }; diff --git a/include/FramelessHelper/Widgets/standardsystembutton.h b/include/FramelessHelper/Widgets/standardsystembutton.h index de13d107..f8494b45 100644 --- a/include/FramelessHelper/Widgets/standardsystembutton.h +++ b/include/FramelessHelper/Widgets/standardsystembutton.h @@ -25,21 +25,19 @@ #pragma once #include -#include +#include 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) @@ -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; @@ -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); @@ -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(); diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 31318ff2..5dab0f9c 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -664,11 +664,17 @@ 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; @@ -676,7 +682,13 @@ void Utils::emulateQtMouseEvent(const QObject *target, const QWindow *window, co 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); } }; @@ -689,7 +701,7 @@ 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; @@ -697,7 +709,13 @@ void Utils::emulateQtMouseEvent(const QObject *target, const QWindow *window, co 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 { diff --git a/src/widgets/standardsystembutton.cpp b/src/widgets/standardsystembutton.cpp index 1ccb33d7..4735a78b 100644 --- a/src/widgets/standardsystembutton.cpp +++ b/src/widgets/standardsystembutton.cpp @@ -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; @@ -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()); @@ -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); @@ -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()) { @@ -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()) { @@ -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)) { } @@ -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); @@ -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);