Minor improvements.

Move the moveWindowToDesktopCenter function to FramelessHelper.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-05-09 11:33:20 +08:00
parent ed6267461c
commit ae62a8fb49
3 changed files with 60 additions and 34 deletions

View File

@ -27,6 +27,7 @@
#include <QDebug>
#include <QGuiApplication>
#include <QMargins>
#include <QScreen>
#ifdef QT_WIDGETS_LIB
#include <QWidget>
#endif
@ -54,14 +55,15 @@ QWindow *getWindowHandle(QObject *const val) {
#ifdef QT_WIDGETS_LIB
else if (val->isWidgetType()) {
const auto widget = qobject_cast<QWidget *>(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<QWindow *>(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<qreal>(sw - ww) / 2.0));
window->setY(qRound(static_cast<qreal>(sh - wh) / 2.0));
}
}
#ifdef QT_WIDGETS_LIB
else if (obj->isWidgetType()) {
const auto widget = qobject_cast<QWidget *>(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<qreal>(sw - ww) / 2.0),
qRound(static_cast<qreal>(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<QWidget *>(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<QWindow *>(window)->isTopLevel();
const auto win = qobject_cast<QWindow *>(window);
if (win) {
return win->isTopLevel();
}
}
#ifdef QT_WIDGETS_LIB
else if (window->isWidgetType()) {
return qobject_cast<QWidget *>(window)->isTopLevel();
const auto widget = qobject_cast<QWidget *>(window);
if (widget) {
return widget->isTopLevel();
}
}
#endif
}

View File

@ -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<QPointer<QObject>, QVector<QRect>>;
using Objects = QMap<QPointer<QObject>, QVector<QPointer<QObject>>>;
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<QPointer<QObject>, QVector<QRect>> m_ignoreAreas = {},
m_draggableAreas = {};
QMap<QPointer<QObject>, QVector<QPointer<QObject>>> m_ignoreObjects = {},
m_draggableObjects = {};
};

View File

@ -1,20 +1,7 @@
#include "framelesshelper.h"
#include <QApplication>
#include <QScreen>
#include <QWidget>
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<qreal>(sw - ww) / 2.0),
qRound(static_cast<qreal>(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();