forked from github_mirror/framelesshelper
Minor improvements.
Move the moveWindowToDesktopCenter function to FramelessHelper. Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
parent
ed6267461c
commit
ae62a8fb49
|
@ -27,6 +27,7 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
#include <QMargins>
|
#include <QMargins>
|
||||||
|
#include <QScreen>
|
||||||
#ifdef QT_WIDGETS_LIB
|
#ifdef QT_WIDGETS_LIB
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#endif
|
#endif
|
||||||
|
@ -54,14 +55,15 @@ QWindow *getWindowHandle(QObject *const val) {
|
||||||
#ifdef QT_WIDGETS_LIB
|
#ifdef QT_WIDGETS_LIB
|
||||||
else if (val->isWidgetType()) {
|
else if (val->isWidgetType()) {
|
||||||
const auto widget = qobject_cast<QWidget *>(val);
|
const auto widget = qobject_cast<QWidget *>(val);
|
||||||
if (widget) {
|
if (widget && widget->isTopLevel()) {
|
||||||
return validWindow(widget->windowHandle());
|
return validWindow(widget->windowHandle());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
qWarning().noquote() << "Can't acquire the window handle: only "
|
qWarning().noquote()
|
||||||
"QWidget and QWindow are accepted.";
|
<< "Can't acquire the window handle: only "
|
||||||
|
"top level QWidget and QWindow are accepted.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -69,14 +71,7 @@ QWindow *getWindowHandle(QObject *const val) {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FramelessHelper::updateQtFrame(QWindow *const window,
|
void FramelessHelper::updateQtFrame(QWindow *const window,
|
||||||
const int titleBarHeight) {
|
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; }
|
int FramelessHelper::getBorderWidth() const { return m_borderWidth; }
|
||||||
|
|
||||||
void FramelessHelper::setBorderWidth(const int val) { m_borderWidth = val; }
|
void FramelessHelper::setBorderWidth(const int val) { m_borderWidth = val; }
|
||||||
|
@ -184,7 +214,7 @@ void FramelessHelper::removeWindowFrame(QObject *const obj) {
|
||||||
#ifdef QT_WIDGETS_LIB
|
#ifdef QT_WIDGETS_LIB
|
||||||
else {
|
else {
|
||||||
const auto widget = qobject_cast<QWidget *>(obj);
|
const auto widget = qobject_cast<QWidget *>(obj);
|
||||||
if (widget) {
|
if (widget && widget->isTopLevel()) {
|
||||||
widget->setWindowFlags(flags);
|
widget->setWindowFlags(flags);
|
||||||
// We can't get MouseMove events if MouseTracking is
|
// We can't get MouseMove events if MouseTracking is
|
||||||
// disabled.
|
// disabled.
|
||||||
|
@ -200,11 +230,17 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) {
|
||||||
const auto isWindowTopLevel = [](QObject *const window) -> bool {
|
const auto isWindowTopLevel = [](QObject *const window) -> bool {
|
||||||
if (window) {
|
if (window) {
|
||||||
if (window->isWindowType()) {
|
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
|
#ifdef QT_WIDGETS_LIB
|
||||||
else if (window->isWidgetType()) {
|
else if (window->isWidgetType()) {
|
||||||
return qobject_cast<QWidget *>(window)->isTopLevel();
|
const auto widget = qobject_cast<QWidget *>(window);
|
||||||
|
if (widget) {
|
||||||
|
return widget->isTopLevel();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ public:
|
||||||
~FramelessHelper() override = default;
|
~FramelessHelper() override = default;
|
||||||
|
|
||||||
static void updateQtFrame(QWindow *const window, const int titleBarHeight);
|
static void updateQtFrame(QWindow *const window, const int titleBarHeight);
|
||||||
|
static void moveWindowToDesktopCenter(QObject *const obj);
|
||||||
|
|
||||||
int getBorderWidth() const;
|
int getBorderWidth() const;
|
||||||
void setBorderWidth(const int val);
|
void setBorderWidth(const int val);
|
||||||
|
@ -73,10 +74,12 @@ protected:
|
||||||
bool eventFilter(QObject *object, QEvent *event) override;
|
bool eventFilter(QObject *object, QEvent *event) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using Areas = QMap<QPointer<QObject>, QVector<QRect>>;
|
// ### TODO: The default border width and height on Windows is 8 pixels if
|
||||||
using Objects = QMap<QPointer<QObject>, QVector<QPointer<QObject>>>;
|
// the scale factor is 1.0. Don't know how to acquire these values on UNIX
|
||||||
|
// platforms through native API.
|
||||||
int m_borderWidth = -1, m_borderHeight = -1, m_titleBarHeight = -1;
|
int m_borderWidth = 8, m_borderHeight = 8, m_titleBarHeight = 30;
|
||||||
Areas m_ignoreAreas = {}, m_draggableAreas = {};
|
QMap<QPointer<QObject>, QVector<QRect>> m_ignoreAreas = {},
|
||||||
Objects m_ignoreObjects = {}, m_draggableObjects = {};
|
m_draggableAreas = {};
|
||||||
|
QMap<QPointer<QObject>, QVector<QPointer<QObject>>> m_ignoreObjects = {},
|
||||||
|
m_draggableObjects = {};
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,20 +1,7 @@
|
||||||
#include "framelesshelper.h"
|
#include "framelesshelper.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QScreen>
|
|
||||||
#include <QWidget>
|
#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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// High DPI scaling is enabled by default from Qt 6
|
// High DPI scaling is enabled by default from Qt 6
|
||||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
@ -41,7 +28,7 @@ int main(int argc, char *argv[]) {
|
||||||
QWidget widget;
|
QWidget widget;
|
||||||
helper.removeWindowFrame(&widget);
|
helper.removeWindowFrame(&widget);
|
||||||
widget.resize(800, 600);
|
widget.resize(800, 600);
|
||||||
moveWindowToDesktopCenter(&widget);
|
FramelessHelper::moveWindowToDesktopCenter(&widget);
|
||||||
widget.show();
|
widget.show();
|
||||||
|
|
||||||
return QApplication::exec();
|
return QApplication::exec();
|
||||||
|
|
Loading…
Reference in New Issue