From ae62a8fb49cef5c148f1fb4151a0eab546167ba3 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sat, 9 May 2020 11:33:20 +0800 Subject: [PATCH] Minor improvements. Move the moveWindowToDesktopCenter function to FramelessHelper. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- framelesshelper.cpp | 64 +++++++++++++++++++++++++++++++++++---------- framelesshelper.h | 15 ++++++----- main_unix.cpp | 15 +---------- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/framelesshelper.cpp b/framelesshelper.cpp index b3939bf..204ee11 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef QT_WIDGETS_LIB #include #endif @@ -54,14 +55,15 @@ QWindow *getWindowHandle(QObject *const val) { #ifdef QT_WIDGETS_LIB else if (val->isWidgetType()) { const auto widget = qobject_cast(val); - if (widget) { + if (widget && widget->isTopLevel()) { return validWindow(widget->windowHandle()); } } #endif else { - qWarning().noquote() << "Can't acquire the window handle: only " - "QWidget and QWindow are accepted."; + qWarning().noquote() + << "Can't acquire the window handle: only " + "top level QWidget and QWindow are accepted."; } } return nullptr; @@ -69,14 +71,7 @@ QWindow *getWindowHandle(QObject *const val) { } // namespace -FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) { - // ### TODO: The default border width and height on Windows is 8 pixels if - // the scale factor is 1.0. Don't know how to acquire these values on UNIX - // platforms through native API. - m_borderWidth = 8; - m_borderHeight = 8; - m_titleBarHeight = 30; -} +FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {} void FramelessHelper::updateQtFrame(QWindow *const window, const int titleBarHeight) { @@ -98,6 +93,41 @@ void FramelessHelper::updateQtFrame(QWindow *const window, } } +void FramelessHelper::moveWindowToDesktopCenter(QObject *const obj) { + if (!obj) { + return; + } + if (obj->isWindowType()) { + const auto window = qobject_cast(obj); + if (window) { + const QRect sg = window->screen()->geometry(); + const int sw = sg.width(); + const int sh = sg.height(); + const int ww = window->width(); + const int wh = window->height(); + window->setX(qRound(static_cast(sw - ww) / 2.0)); + window->setY(qRound(static_cast(sh - wh) / 2.0)); + } + } +#ifdef QT_WIDGETS_LIB + else if (obj->isWidgetType()) { + const auto widget = qobject_cast(obj); + if (widget && widget->isTopLevel()) { + const QRect sg = widget->screen()->geometry(); + const int sw = sg.width(); + const int sh = sg.height(); + const int ww = widget->width(); + const int wh = widget->height(); + widget->move(qRound(static_cast(sw - ww) / 2.0), + qRound(static_cast(sh - wh) / 2.0)); + } + } +#endif + else { + qWarning().noquote() << "The given QObject is not a top level window."; + } +} + int FramelessHelper::getBorderWidth() const { return m_borderWidth; } void FramelessHelper::setBorderWidth(const int val) { m_borderWidth = val; } @@ -184,7 +214,7 @@ void FramelessHelper::removeWindowFrame(QObject *const obj) { #ifdef QT_WIDGETS_LIB else { const auto widget = qobject_cast(obj); - if (widget) { + if (widget && widget->isTopLevel()) { widget->setWindowFlags(flags); // We can't get MouseMove events if MouseTracking is // disabled. @@ -200,11 +230,17 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) { const auto isWindowTopLevel = [](QObject *const window) -> bool { if (window) { if (window->isWindowType()) { - return qobject_cast(window)->isTopLevel(); + const auto win = qobject_cast(window); + if (win) { + return win->isTopLevel(); + } } #ifdef QT_WIDGETS_LIB else if (window->isWidgetType()) { - return qobject_cast(window)->isTopLevel(); + const auto widget = qobject_cast(window); + if (widget) { + return widget->isTopLevel(); + } } #endif } diff --git a/framelesshelper.h b/framelesshelper.h index 17c8f3b..b9fbeb9 100644 --- a/framelesshelper.h +++ b/framelesshelper.h @@ -43,6 +43,7 @@ public: ~FramelessHelper() override = default; static void updateQtFrame(QWindow *const window, const int titleBarHeight); + static void moveWindowToDesktopCenter(QObject *const obj); int getBorderWidth() const; void setBorderWidth(const int val); @@ -73,10 +74,12 @@ protected: bool eventFilter(QObject *object, QEvent *event) override; private: - using Areas = QMap, QVector>; - using Objects = QMap, QVector>>; - - int m_borderWidth = -1, m_borderHeight = -1, m_titleBarHeight = -1; - Areas m_ignoreAreas = {}, m_draggableAreas = {}; - Objects m_ignoreObjects = {}, m_draggableObjects = {}; + // ### TODO: The default border width and height on Windows is 8 pixels if + // the scale factor is 1.0. Don't know how to acquire these values on UNIX + // platforms through native API. + int m_borderWidth = 8, m_borderHeight = 8, m_titleBarHeight = 30; + QMap, QVector> m_ignoreAreas = {}, + m_draggableAreas = {}; + QMap, QVector>> m_ignoreObjects = {}, + m_draggableObjects = {}; }; diff --git a/main_unix.cpp b/main_unix.cpp index 4136861..25fae73 100644 --- a/main_unix.cpp +++ b/main_unix.cpp @@ -1,20 +1,7 @@ #include "framelesshelper.h" #include -#include #include -static void moveWindowToDesktopCenter(QWidget *const widget) { - if (widget) { - const QRect sg = widget->screen()->geometry(); - const int sw = sg.width(); - const int sh = sg.height(); - const int ww = widget->width(); - const int wh = widget->height(); - widget->move(qRound(static_cast(sw - ww) / 2.0), - qRound(static_cast(sh - wh) / 2.0)); - } -} - int main(int argc, char *argv[]) { // High DPI scaling is enabled by default from Qt 6 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) @@ -41,7 +28,7 @@ int main(int argc, char *argv[]) { QWidget widget; helper.removeWindowFrame(&widget); widget.resize(800, 600); - moveWindowToDesktopCenter(&widget); + FramelessHelper::moveWindowToDesktopCenter(&widget); widget.show(); return QApplication::exec();