SineStriker
2024-05-09 bc6e2a4231d73d6e037340af9e73f2611649faa8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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/QApplication>
#include <QtWidgets/QWidget>
 
#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(const QObject *obj) const {
        return static_cast<const QWidget *>(obj)->windowHandle();
    }
 
    bool WidgetItemDelegate::isEnabled(const QObject *obj) const {
        return static_cast<const QWidget *>(obj)->isEnabled();
    }
 
    bool WidgetItemDelegate::isVisible(const QObject *obj) const {
        return static_cast<const QWidget *>(obj)->isVisible();
    }
 
    QRect WidgetItemDelegate::mapGeometryToScene(const QObject *obj) const {
        auto widget = static_cast<const QWidget *>(obj);
        const QPoint originPoint = widget->mapTo(widget->window(), QPoint(0, 0));
        const QSize size = widget->size();
        return {originPoint, size};
    }
 
    QWindow *WidgetItemDelegate::hostWindow(const QObject *host) const {
        return static_cast<const QWidget *>(host)->windowHandle();
    }
 
    bool WidgetItemDelegate::isHostSizeFixed(const QObject *host) const {
        const auto widget = static_cast<const QWidget *>(host);
        // "Qt::MSWindowsFixedSizeDialogHint" is used cross-platform actually.
        if (widget->windowFlags() & Qt::MSWindowsFixedSizeDialogHint) {
            return true;
        }
        // Caused by setFixedWidth/Height/Size().
        const QSize minSize = widget->minimumSize();
        const QSize maxSize = widget->maximumSize();
        if (!minSize.isEmpty() && !maxSize.isEmpty() && (minSize == maxSize)) {
            return true;
        }
        return false;
    }
 
    bool WidgetItemDelegate::isWindowActive(const QObject *host) const {
        return static_cast<const QWidget *>(host)->isActiveWindow();
    }
 
    void WidgetItemDelegate::resetQtGrabbedControl(QObject *host) const {
        Q_UNUSED(host);
        if (!qt_button_down) {
            return;
        }
        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);
    }
 
}