change cursor according mouse state

This commit is contained in:
Altair Wei 2021-09-19 17:47:14 +08:00
parent 3784ef0e7f
commit da342753a2
2 changed files with 74 additions and 0 deletions

View File

@ -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)
{

View File

@ -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