revert: restore system default window instead of custom one.
custom window is not the goal of this project. we remove it and use system default window instead.
This commit is contained in:
@@ -11,32 +11,11 @@
|
|||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QWindow>
|
#include <QWindow>
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
#include "windows.h"
|
|
||||||
void fixWindowStyle(QWidget * that)
|
|
||||||
{
|
|
||||||
HWND hwnd = (HWND)that->winId();
|
|
||||||
|
|
||||||
LONG style = GetWindowLong(hwnd, GWL_STYLE);
|
|
||||||
style |= WS_THICKFRAME | WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU;
|
|
||||||
SetWindowLong(hwnd, GWL_STYLE, style);
|
|
||||||
|
|
||||||
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
|
|
||||||
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE |
|
|
||||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
FramelessWindow::FramelessWindow(QWidget *parent)
|
FramelessWindow::FramelessWindow(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, m_centralLayout(new QVBoxLayout(this))
|
, m_centralLayout(new QVBoxLayout(this))
|
||||||
, m_oldCursorShape(Qt::ArrowCursor)
|
|
||||||
, m_oldEdges()
|
|
||||||
{
|
{
|
||||||
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
|
this->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
||||||
this->setMouseTracking(true);
|
|
||||||
this->setAttribute(Qt::WA_Hover, true);
|
|
||||||
this->installEventFilter(this);
|
|
||||||
|
|
||||||
m_centralLayout->setContentsMargins(QMargins());
|
m_centralLayout->setContentsMargins(QMargins());
|
||||||
}
|
}
|
||||||
@@ -51,110 +30,3 @@ void FramelessWindow::setCentralWidget(QWidget *widget)
|
|||||||
m_centralLayout->addWidget(widget);
|
m_centralLayout->addWidget(widget);
|
||||||
m_centralWidget = widget;
|
m_centralWidget = widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FramelessWindow::installResizeCapture(QObject* widget)
|
|
||||||
{
|
|
||||||
widget->installEventFilter(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FramelessWindow::showEvent(QShowEvent *event)
|
|
||||||
{
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
fixWindowStyle(this);
|
|
||||||
#endif // Q_OS_WIN
|
|
||||||
return QWidget::showEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FramelessWindow::eventFilter(QObject* o, QEvent* e)
|
|
||||||
{
|
|
||||||
switch (e->type()) {
|
|
||||||
case QEvent::HoverMove:
|
|
||||||
{
|
|
||||||
QWidget* wg = qobject_cast<QWidget*>(o);
|
|
||||||
if (wg != nullptr)
|
|
||||||
return mouseHover(static_cast<QHoverEvent*>(e), wg);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QEvent::MouseButtonPress:
|
|
||||||
return mousePress(static_cast<QMouseEvent*>(e));
|
|
||||||
}
|
|
||||||
|
|
||||||
return QWidget::eventFilter(o, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FramelessWindow::mouseHover(QHoverEvent* event, QWidget* wg)
|
|
||||||
{
|
|
||||||
if (!isMaximized() && !isFullScreen()) {
|
|
||||||
QWindow* win = window()->windowHandle();
|
|
||||||
Qt::Edges edges = this->getEdgesByPos(wg->mapToGlobal(event->oldPos()), win->frameGeometry());
|
|
||||||
|
|
||||||
// backup & restore cursor shape
|
|
||||||
if (edges && !m_oldEdges)
|
|
||||||
// entering the edge. backup cursor shape
|
|
||||||
m_oldCursorShape = win->cursor().shape();
|
|
||||||
if (!edges && m_oldEdges)
|
|
||||||
// leaving the edge. restore cursor shape
|
|
||||||
win->setCursor(m_oldCursorShape);
|
|
||||||
|
|
||||||
// save the latest edges status
|
|
||||||
m_oldEdges = edges;
|
|
||||||
|
|
||||||
// show resize cursor shape if cursor is within border
|
|
||||||
if (edges) {
|
|
||||||
win->setCursor(this->getCursorByEdge(edges, Qt::ArrowCursor));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FramelessWindow::mousePress(QMouseEvent* event)
|
|
||||||
{
|
|
||||||
if (event->buttons() & Qt::LeftButton && !isMaximized() && !isFullScreen()) {
|
|
||||||
QWindow* win = window()->windowHandle();
|
|
||||||
Qt::Edges edges = this->getEdgesByPos(event->globalPosition().toPoint(), win->frameGeometry());
|
|
||||||
if (edges) {
|
|
||||||
win->startSystemResize(edges);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt::CursorShape FramelessWindow::getCursorByEdge(const Qt::Edges& edges, Qt::CursorShape default_cursor)
|
|
||||||
{
|
|
||||||
if ((edges == (Qt::TopEdge | Qt::LeftEdge)) || (edges == (Qt::RightEdge | Qt::BottomEdge)))
|
|
||||||
return Qt::SizeFDiagCursor;
|
|
||||||
else if ((edges == (Qt::TopEdge | Qt::RightEdge)) || (edges == (Qt::LeftEdge | Qt::BottomEdge)))
|
|
||||||
return Qt::SizeBDiagCursor;
|
|
||||||
else if (edges & (Qt::TopEdge | Qt::BottomEdge))
|
|
||||||
return Qt::SizeVerCursor;
|
|
||||||
else if (edges & (Qt::LeftEdge | Qt::RightEdge))
|
|
||||||
return Qt::SizeHorCursor;
|
|
||||||
else
|
|
||||||
return default_cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt::Edges FramelessWindow::getEdgesByPos(const QPoint gpos, const QRect& winrect)
|
|
||||||
{
|
|
||||||
const int borderWidth = 8;
|
|
||||||
Qt::Edges edges;
|
|
||||||
|
|
||||||
int x = gpos.x() - winrect.x();
|
|
||||||
int y = gpos.y() - winrect.y();
|
|
||||||
|
|
||||||
if (x < borderWidth)
|
|
||||||
edges |= Qt::LeftEdge;
|
|
||||||
if (x > (winrect.width() - borderWidth))
|
|
||||||
edges |= Qt::RightEdge;
|
|
||||||
if (y < borderWidth)
|
|
||||||
edges |= Qt::TopEdge;
|
|
||||||
if (y > (winrect.height() - borderWidth))
|
|
||||||
edges |= Qt::BottomEdge;
|
|
||||||
|
|
||||||
return edges;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,23 +18,8 @@ public:
|
|||||||
explicit FramelessWindow(QWidget *parent = nullptr);
|
explicit FramelessWindow(QWidget *parent = nullptr);
|
||||||
|
|
||||||
void setCentralWidget(QWidget * widget);
|
void setCentralWidget(QWidget * widget);
|
||||||
void installResizeCapture(QObject* widget);
|
|
||||||
|
|
||||||
protected slots:
|
|
||||||
void showEvent(QShowEvent *event) override;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
bool eventFilter(QObject *o, QEvent *e) override;
|
|
||||||
bool mouseHover(QHoverEvent* event, QWidget* wg);
|
|
||||||
bool mousePress(QMouseEvent* event);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Qt::Edges m_oldEdges;
|
|
||||||
Qt::CursorShape m_oldCursorShape;
|
|
||||||
|
|
||||||
Qt::CursorShape getCursorByEdge(const Qt::Edges& edges, Qt::CursorShape default_cursor);
|
|
||||||
Qt::Edges getEdgesByPos(const QPoint pos, const QRect& winrect);
|
|
||||||
|
|
||||||
QVBoxLayout * m_centralLayout = nullptr;
|
QVBoxLayout * m_centralLayout = nullptr;
|
||||||
QWidget * m_centralWidget = nullptr; // just a pointer, doesn't take the ownership.
|
QWidget * m_centralWidget = nullptr; // just a pointer, doesn't take the ownership.
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -163,12 +163,6 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
Settings::instance()->applyUserShortcuts(this);
|
Settings::instance()->applyUserShortcuts(this);
|
||||||
});
|
});
|
||||||
|
|
||||||
// allow some mouse events can go through these widgets for resizing window.
|
|
||||||
installResizeCapture(m_closeButton);
|
|
||||||
installResizeCapture(m_graphicsView);
|
|
||||||
installResizeCapture(m_graphicsView->viewport());
|
|
||||||
installResizeCapture(m_gv);
|
|
||||||
installResizeCapture(m_gv->viewport());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
|
|||||||
Reference in New Issue
Block a user