summaryrefslogtreecommitdiffstats
path: root/src/core/services/qeventfilterservice.cpp
blob: 6cfb3dd3a843212b31d3afee25d8396b24d2c72d (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
/****************************************************************************
**
** Copyright (C) 2015 Paul Lemire (paul.lemire350@gmail.com)
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qeventfilterservice_p.h"
#include "qabstractserviceprovider_p.h"
#include <QMap>
#include <QObject>
#include <QVector>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

namespace {

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

bool operator <(const FilterPriorityPair &a, const FilterPriorityPair &b)
{
    return a.priority < b.priority;
}

class InternalEventListener : public QObject
{
    Q_OBJECT
public:
    explicit InternalEventListener(QObject *parent = Q_NULLPTR)
        : QObject(parent)
    {
    }

    bool eventFilter(QObject *obj, QEvent *e) Q_DECL_FINAL
    {
        for (int i = m_eventFilters.size() - 1; i >= 0; --i) {
            const FilterPriorityPair &fPPair = m_eventFilters.at(i);
            if (fPPair.filter->eventFilter(obj, e))
                return true;
        }
        return false;
    }

    void registerEventFilter(QObject *eventFilter, int priority)
    {
        for (int i = 0, m = m_eventFilters.size(); i < m; ++i)
            if (m_eventFilters.at(i).priority == priority)
                return;

        FilterPriorityPair fpPair;
        fpPair.filter = eventFilter;
        fpPair.priority = priority;
        m_eventFilters.push_back(fpPair);
        std::sort(m_eventFilters.begin(), m_eventFilters.end());
    }

    void unregisterEventFilter(QObject *eventFilter)
    {
        QVector<FilterPriorityPair>::iterator it = m_eventFilters.begin();
        const QVector<FilterPriorityPair>::iterator end = m_eventFilters.end();
        while (it != end) {
            if (it->filter == eventFilter) {
                m_eventFilters.erase(it);
                return;
            }
            ++it;
        }
    }

private:
    QVector<FilterPriorityPair> m_eventFilters;
};

} // anonymous

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

    QScopedPointer<InternalEventListener> m_eventDispatcher;
};


/*!
    \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 == Q_NULLPTR) {
        d->m_eventDispatcher.reset();
    } else {
        d->m_eventDispatcher.reset(new InternalEventListener());
        eventSource->installEventFilter(d->m_eventDispatcher.data());
    }
}

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

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

} // Qt3DCore

QT_END_NAMESPACE

#include "qeventfilterservice.moc"