From da342753a2fb6d6dc8481f952d7dab73b24e2ee4 Mon Sep 17 00:00:00 2001 From: Altair Wei Date: Sun, 19 Sep 2021 17:47:14 +0800 Subject: [PATCH] change cursor according mouse state --- framelesshelper.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ framelesshelper.h | 9 +++++++ 2 files changed, 74 insertions(+) diff --git a/framelesshelper.cpp b/framelesshelper.cpp index 17f5e62..34fa01a 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -157,6 +157,71 @@ Qt::WindowFrameSection FramelessHelper::mapPosToFrameSection(const QPoint& pos) return Qt::NoSection; } +bool FramelessHelper::isHoverResizeHandler() +{ + return m_hoveredFrameSection == Qt::LeftSection || + m_hoveredFrameSection == Qt::RightSection || + m_hoveredFrameSection == Qt::TopSection || + m_hoveredFrameSection == Qt::BottomSection || + m_hoveredFrameSection == Qt::TopLeftSection || + m_hoveredFrameSection == Qt::TopRightSection || + m_hoveredFrameSection == Qt::BottomLeftSection || + m_hoveredFrameSection == Qt::BottomRightSection; +} + +QCursor FramelessHelper::cursorForFrameSection(Qt::WindowFrameSection frameSection) +{ + Qt::CursorShape cursor = Qt::ArrowCursor; + + switch (frameSection) + { + case Qt::LeftSection: + case Qt::RightSection: + cursor = Qt::SizeHorCursor; + break; + case Qt::BottomSection: + case Qt::TopSection: + cursor = Qt::SizeVerCursor; + break; + case Qt::TopLeftSection: + case Qt::BottomRightSection: + cursor = Qt::SizeFDiagCursor; + break; + case Qt::TopRightSection: + case Qt::BottomLeftSection: + cursor = Qt::SizeBDiagCursor; + break; + case Qt::TitleBarArea: + cursor = Qt::ArrowCursor; + break; + default: + break; + } + + return QCursor(cursor); +} + +void FramelessHelper::setCursor(const QCursor& cursor) +{ + m_window->setCursor(cursor); + m_cursorChanged = true; +} + +void FramelessHelper::unsetCursor() +{ + if (!m_cursorChanged) + return; + + m_window->unsetCursor(); + m_cursorChanged = false; +} + +void FramelessHelper::updateCursor() +{ + if (isHoverResizeHandler()) + setCursor(cursorForFrameSection(m_hoveredFrameSection)); +} + bool FramelessHelper::eventFilter(QObject *object, QEvent *event) { diff --git a/framelesshelper.h b/framelesshelper.h index 62904fb..12d4bfd 100644 --- a/framelesshelper.h +++ b/framelesshelper.h @@ -70,6 +70,13 @@ public: bool isInTitlebarArea(const QPoint& pos); Qt::WindowFrameSection mapPosToFrameSection(const QPoint& pos); + bool isHoverResizeHandler(); + + QCursor cursorForFrameSection(Qt::WindowFrameSection frameSection); + void setCursor(const QCursor& cursor); + void unsetCursor(); + void updateCursor(); + protected: bool eventFilter(QObject *object, QEvent *event) override; @@ -80,6 +87,8 @@ private: int m_resizeBorderThickness; bool m_resizable; Qt::WindowFlags m_origWindowFlags; + bool m_cursorChanged; + Qt::WindowFrameSection m_hoveredFrameSection; }; FRAMELESSHELPER_END_NAMESPACE