From 3fbcf1aa8f91a804af50fad4e2b122f698259c94 Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: ćšć, 09 5æ 2024 23:45:28 +0800 Subject: [PATCH] Remove `isHostSizeFixed` in delegate --- src/widgets/widgetitemdelegate.cpp | 121 ++++++++++++++++++++++++++++++++------- 1 files changed, 98 insertions(+), 23 deletions(-) diff --git a/src/widgets/widgetitemdelegate.cpp b/src/widgets/widgetitemdelegate.cpp index 9d338c1..b1b7f6c 100644 --- a/src/widgets/widgetitemdelegate.cpp +++ b/src/widgets/widgetitemdelegate.cpp @@ -1,27 +1,56 @@ +// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware) +// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao) +// SPDX-License-Identifier: Apache-2.0 + #include "widgetitemdelegate_p.h" #include <QtGui/QMouseEvent> -#include <QtWidgets/QWidget> #include <QtWidgets/QApplication> +#include <QtWidgets/QWidget> -extern Q_WIDGETS_EXPORT QWidget *qt_button_down; +#include <QWKCore/private/abstractwindowcontext_p.h> + +extern Q_DECL_IMPORT QWidget *qt_button_down; namespace QWK { + + class WidgetWinIdChangeEventFilter : public WinIdChangeEventFilter { + public: + explicit WidgetWinIdChangeEventFilter(QObject *host, AbstractWindowContext *ctx) + : WinIdChangeEventFilter(host, ctx), widget(static_cast<QWidget *>(host)) { + widget->installEventFilter(this); + } + + WId winId() const override { + return widget->effectiveWinId(); + } + + protected: + bool eventFilter(QObject *obj, QEvent *event) override { + Q_UNUSED(obj) + if (event->type() == QEvent::WinIdChange) { + context->notifyWinIdChange(); + } + return false; + } + + QWidget *widget; + }; WidgetItemDelegate::WidgetItemDelegate() = default; WidgetItemDelegate::~WidgetItemDelegate() = default; - QWindow *WidgetItemDelegate::window(QObject *obj) const { - return static_cast<QWidget *>(obj)->windowHandle(); + QWindow *WidgetItemDelegate::window(const QObject *obj) const { + return static_cast<const QWidget *>(obj)->windowHandle(); } - bool WidgetItemDelegate::isEnabled(QObject *obj) const { - return static_cast<QWidget *>(obj)->isEnabled(); + bool WidgetItemDelegate::isEnabled(const QObject *obj) const { + return static_cast<const QWidget *>(obj)->isEnabled(); } - bool WidgetItemDelegate::isVisible(QObject *obj) const { - return static_cast<QWidget *>(obj)->isVisible(); + bool WidgetItemDelegate::isVisible(const QObject *obj) const { + return static_cast<const QWidget *>(obj)->isVisible(); } QRect WidgetItemDelegate::mapGeometryToScene(const QObject *obj) const { @@ -31,26 +60,72 @@ return {originPoint, size}; } - QWindow *WidgetItemDelegate::hostWindow(QObject *host) const { - return static_cast<QWidget *>(host)->windowHandle(); + QWindow *WidgetItemDelegate::hostWindow(const QObject *host) const { + return static_cast<const QWidget *>(host)->windowHandle(); } - bool WidgetItemDelegate::isHostSizeFixed(QObject *host) const { - return false; + bool WidgetItemDelegate::isWindowActive(const QObject *host) const { + return static_cast<const QWidget *>(host)->isActiveWindow(); } - bool WidgetItemDelegate::resetQtGrabbedControl() const { - if (qt_button_down) { - static constexpr const auto invalidPos = QPoint{-99999, -99999}; - const auto event = - new QMouseEvent(QEvent::MouseButtonRelease, invalidPos, invalidPos, invalidPos, - Qt::LeftButton, QGuiApplication::mouseButtons() ^ Qt::LeftButton, - QGuiApplication::keyboardModifiers()); - QApplication::postEvent(qt_button_down, event); - qt_button_down = nullptr; - return true; + void WidgetItemDelegate::resetQtGrabbedControl(QObject *host) const { + Q_UNUSED(host); + if (!qt_button_down) { + return; } - return false; + static constexpr const auto invalidPos = + QPoint{std::numeric_limits<int>::lowest(), std::numeric_limits<int>::lowest()}; + const auto event = new QMouseEvent( + QEvent::MouseButtonRelease, invalidPos, invalidPos, invalidPos, Qt::LeftButton, + QGuiApplication::mouseButtons() ^ Qt::LeftButton, QGuiApplication::keyboardModifiers()); + QCoreApplication::postEvent(qt_button_down, event); + qt_button_down = nullptr; + } + + Qt::WindowStates WidgetItemDelegate::getWindowState(const QObject *host) const { + return static_cast<const QWidget *>(host)->windowState(); + } + + void WidgetItemDelegate::setWindowState(QObject *host, Qt::WindowStates state) const { + static_cast<QWidget *>(host)->setWindowState(state); + } + + void WidgetItemDelegate::setCursorShape(QObject *host, Qt::CursorShape shape) const { + static_cast<QWidget *>(host)->setCursor(QCursor(shape)); + } + + void WidgetItemDelegate::restoreCursorShape(QObject *host) const { + static_cast<QWidget *>(host)->unsetCursor(); + } + + Qt::WindowFlags WidgetItemDelegate::getWindowFlags(const QObject *host) const { + return static_cast<const QWidget *>(host)->windowFlags(); + } + + QRect WidgetItemDelegate::getGeometry(const QObject *host) const { + return static_cast<const QWidget *>(host)->geometry(); + } + + void WidgetItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const { + static_cast<QWidget *>(host)->setWindowFlags(flags); + } + + void WidgetItemDelegate::setWindowVisible(QObject *host, bool visible) const { + static_cast<QWidget *>(host)->setVisible(visible); + } + + void WidgetItemDelegate::setGeometry(QObject *host, const QRect &rect) { + static_cast<QWidget *>(host)->setGeometry(rect); + } + + void WidgetItemDelegate::bringWindowToTop(QObject *host) const { + static_cast<QWidget *>(host)->raise(); + } + + WinIdChangeEventFilter * + WidgetItemDelegate::createWinIdEventFilter(QObject *host, + AbstractWindowContext *context) const { + return new WidgetWinIdChangeEventFilter(host, context); } } \ No newline at end of file -- Gitblit v1.9.1