pineapple-pictures/app/framelesshandler.cpp
2021-01-04 13:50:09 +08:00

150 lines
4.3 KiB
C++

#include "framelesshandler.h"
#include <QMouseEvent>
#include <QWidget>
#include <QWindow>
#include <QDebug>
#include <QApplication>
FramelessHandler::FramelessHandler(QWidget *parent)
: QObject(parent)
, m_parentWidget(parent)
{
m_parentWidget->setMouseTracking(true);
m_parentWidget->installEventFilter(this);
}
bool FramelessHandler::eventFilter(QObject *o, QEvent *evt)
{
if (evt->type() != QEvent::MouseButtonPress &&
evt->type() != QEvent::MouseButtonRelease &&
evt->type() != QEvent::MouseMove) {
return false;
}
Q_ASSERT(o == m_parentWidget);
if (QApplication::activePopupWidget()) {
return false;
}
switch (evt->type()) {
case QEvent::MouseButtonPress: {
if (m_parentWidget->isMaximized()) break;
QMouseEvent *e = static_cast<QMouseEvent *>(evt);
const QRect widgetRect = m_parentWidget->rect();
const QPoint cursorPoint = m_parentWidget->mapFromGlobal(e->globalPos());
if (!widgetRect.contains(cursorPoint)) return false;
if (e->button() & Qt::LeftButton) {
m_oldMousePos = e->pos();
m_clickedOnWindow = true;
mouseMoveEvent(e);
return true;
}
} break;
case QEvent::MouseButtonRelease: {
if (m_parentWidget->isMaximized()) break;
QMouseEvent *e = static_cast<QMouseEvent *>(evt);
if (e->button() & Qt::LeftButton) {
qDebug() << "released";
m_clickedOnWindow = false;
return true;
}
} break;
case QEvent::MouseMove: {
if (m_parentWidget->isMaximized()) break;
QMouseEvent *e = static_cast<QMouseEvent *>(evt);
mouseMoveEvent(e);
return true;
} break;
default:
break;
}
return false;
}
void FramelessHandler::mouseMoveEvent(QMouseEvent * evt)
{
QPoint pos = m_parentWidget->mapFromGlobal(evt->globalPos());
Qt::Edges mode = Qt::Edges();
if (pos.x() <= m_borderWidth) {
mode.setFlag(Qt::LeftEdge);
} else if (pos.x() >= m_parentWidget->width() - m_borderWidth) {
mode.setFlag(Qt::RightEdge);
}
if (pos.y() <= m_borderWidth) {
mode.setFlag(Qt::TopEdge);
} else if (pos.y() >= m_parentWidget->height() - m_borderWidth) {
mode.setFlag(Qt::BottomEdge);
}
setMouseCursor(mode);
if ((evt->button() & Qt::LeftButton) == Qt::NoButton && !m_clickedOnWindow) {
return;
}
qDebug() << evt->button() << m_clickedOnWindow;
if (mode) {
startSystemResize(mode);
} else {
if (!startSystemMove()) {
m_parentWidget->move(evt->globalPos() - m_oldMousePos);
}
}
return;
}
void FramelessHandler::setMouseCursor(Qt::Edges edge)
{
switch (edge) {
case Qt::TopEdge:
case Qt::BottomEdge:
m_parentWidget->setCursor(Qt::SizeVerCursor);
m_parentWidget->window()->setCursor(Qt::SizeVerCursor);
break;
case Qt::LeftEdge:
case Qt::RightEdge:
m_parentWidget->setCursor(Qt::SizeHorCursor);
m_parentWidget->window()->setCursor(Qt::SizeHorCursor);
break;
case Qt::TopEdge | Qt::LeftEdge:
case Qt::BottomEdge | Qt::RightEdge:
m_parentWidget->setCursor(Qt::SizeFDiagCursor);
m_parentWidget->window()->setCursor(Qt::SizeFDiagCursor);
break;
case Qt::BottomEdge | Qt::LeftEdge:
case Qt::TopEdge | Qt::RightEdge:
m_parentWidget->setCursor(Qt::SizeBDiagCursor);
m_parentWidget->window()->setCursor(Qt::SizeBDiagCursor);
break;
default:
m_parentWidget->setCursor(Qt::ArrowCursor);
m_parentWidget->window()->setCursor(Qt::ArrowCursor);
break;
}
// qDebug() << "setMouseCursor" << edge;
}
bool FramelessHandler::startSystemResize(Qt::Edges edges)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return m_parentWidget->window()->windowHandle()->startSystemResize(edges);
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return false;
}
bool FramelessHandler::startSystemMove()
{
#if not QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return m_parentWidget->window()->windowHandle()->startSystemMove();
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return false;
}