summaryrefslogtreecommitdiffstats
path: root/src/core/services/qeventfilterservice.cpp
blob: 0de86a425aac7c2ceb4c63514d4f199d66f4b991 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/****************************************************************************
**
** Copyright (C) 2015 Paul Lemire (paul.lemire350@gmail.com)
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qeventfilterservice_p.h"

#include <QtCore/QObject>

#include <Qt3DCore/private/qabstractserviceprovider_p.h>

#include <algorithm>
#include <vector>

QT_BEGIN_NAMESPACE

namespace {
    struct FilterPriorityPair
    {
        QObject *filter;
        int priority;
    };

    const auto byPriority = [](const FilterPriorityPair &a, const FilterPriorityPair &b) noexcept
    {
        return a.priority < b.priority;
    };
}

Q_DECLARE_TYPEINFO(FilterPriorityPair, Q_PRIMITIVE_TYPE);

namespace Qt3DCore {

namespace {

class InternalEventListener : public QObject
{
    Q_OBJECT
public:
    explicit InternalEventListener(QEventFilterServicePrivate *filterService, QObject *parent = nullptr);
    bool eventFilter(QObject *obj, QEvent *e) final;
    QEventFilterServicePrivate* m_filterService;
};

} // anonymous


class QEventFilterServicePrivate : public QAbstractServiceProviderPrivate
{
public:
    QEventFilterServicePrivate()
        : QAbstractServiceProviderPrivate(QServiceLocator::EventFilterService, QStringLiteral("Default event filter service implementation"))
    {}

    Q_DECLARE_PUBLIC(QEventFilterService)

    void registerEventFilter(QObject *eventFilter, int priority)
    {
        FilterPriorityPair fpPair;
        fpPair.filter = eventFilter;
        fpPair.priority = priority;
        const auto it = std::lower_bound(m_eventFilters.begin(), m_eventFilters.end(), fpPair, byPriority);
        if (it == m_eventFilters.end() || it->priority != priority)
            m_eventFilters.insert(it, std::move(fpPair));
    }

    void unregisterEventFilter(QObject *eventFilter)
    {
        for (auto it = m_eventFilters.begin(), end = m_eventFilters.end(); it != end; ++it) {
            if (it->filter == eventFilter) {
                m_eventFilters.erase(it);
                return;
            }
        }
    }

    QScopedPointer<InternalEventListener> m_eventDispatcher;
    std::vector<FilterPriorityPair> m_eventFilters;
};

/* !\internal
    \class Qt3DCore::QEventFilterService
    \inmodule Qt3DCore

    \brief Allows to register event filters with a priority.

    The QEventFilterService class allows registering prioritized event filters.
    Events are dispatched to the event filter with the highest priority first,
    and then propagates to lower priority event filters if the event wasn't
    accepted.
 */

QEventFilterService::QEventFilterService()
    : QAbstractServiceProvider(*new QEventFilterServicePrivate())
{
}

QEventFilterService::~QEventFilterService()
{
}

// Note: event filters can only be registered to QObject in the same thread
void QEventFilterService::initialize(QObject *eventSource)
{
    Q_D(QEventFilterService);
    if (eventSource == nullptr) {
        d->m_eventDispatcher.reset();
    } else {
        d->m_eventDispatcher.reset(new InternalEventListener(d));
        eventSource->installEventFilter(d->m_eventDispatcher.data());
    }
}

void QEventFilterService::shutdown(QObject *eventSource)
{
    Q_D(QEventFilterService);
    if (eventSource && d->m_eventDispatcher.data())
        eventSource->removeEventFilter(d->m_eventDispatcher.data());
}

void QEventFilterService::registerEventFilter(QObject *eventFilter, int priority)
{
    Q_D(QEventFilterService);
    d->registerEventFilter(eventFilter, priority);
}

void QEventFilterService::unregisterEventFilter(QObject *eventFilter)
{
    Q_D(QEventFilterService);
    d->unregisterEventFilter(eventFilter);
}

namespace{

InternalEventListener::InternalEventListener(QEventFilterServicePrivate *filterService, QObject *parent)
    : QObject(parent),
      m_filterService(filterService)
{
}

bool InternalEventListener::eventFilter(QObject *obj, QEvent *e)
{
    for (auto i = m_filterService->m_eventFilters.size(); i > 0; --i) {
        const FilterPriorityPair &fPPair = m_filterService->m_eventFilters[i - 1];
        if (fPPair.filter->eventFilter(obj, e))
            return true;
    }
    return false;
}
}

} // Qt3DCore

QT_END_NAMESPACE

#include "qeventfilterservice.moc"