Sine Striker
2023-12-11 c3c6647e7888b7dbe9d9d22fb77bf08104a3653c
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
#include "nativeeventfilter.h"
 
#include <QtCore/QAbstractNativeEventFilter>
#include <QtGui/QGuiApplication>
 
namespace QWK {
 
    // Avoid adding multiple global native event filters to QGuiApplication
    // in this library.
    class MasterNativeEventFilter : public QAbstractNativeEventFilter {
    public:
        MasterNativeEventFilter() {
            qApp->installNativeEventFilter(this);
        }
 
        ~MasterNativeEventFilter() override {
            qApp->removeNativeEventFilter(this);
        }
 
        bool nativeEventFilter(const QByteArray &eventType, void *message,
                               QT_NATIVE_EVENT_RESULT_TYPE *result) override {
            for (const auto &child : qAsConst(m_children)) {
                if (child->nativeEventFilter(eventType, message, result)) {
                    return true;
                }
            }
            return false;
        }
 
        inline int count() const {
            return m_children.size();
        }
 
        inline void addChild(NativeEventFilter *child) {
            m_children.append(child);
        }
 
        inline void removeChild(NativeEventFilter *child) {
            m_children.removeOne(child);
        }
 
        static MasterNativeEventFilter *instance;
 
    protected:
        QVector<NativeEventFilter *> m_children;
    };
 
    MasterNativeEventFilter *MasterNativeEventFilter::instance = nullptr;
 
    NativeEventFilter::NativeEventFilter() {
        if (!MasterNativeEventFilter::instance) {
            MasterNativeEventFilter::instance = new MasterNativeEventFilter();
        }
        MasterNativeEventFilter::instance->addChild(this);
    }
 
    NativeEventFilter::~NativeEventFilter() {
        MasterNativeEventFilter::instance->removeChild(this);
        if (MasterNativeEventFilter::instance->count() == 0) {
            delete MasterNativeEventFilter::instance;
            MasterNativeEventFilter::instance = nullptr;
        }
    }
 
}