1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 Paul Lemire (paul.lemire350@gmail.com) |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qeventfilterservice_p.h" |
41 | |
42 | #include <QtCore/QObject> |
43 | |
44 | #include <Qt3DCore/private/qabstractserviceprovider_p.h> |
45 | |
46 | #include <algorithm> |
47 | #include <vector> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | namespace { |
52 | struct FilterPriorityPair |
53 | { |
54 | QObject *filter; |
55 | int priority; |
56 | }; |
57 | |
58 | const auto byPriority = [](const FilterPriorityPair &a, const FilterPriorityPair &b) noexcept |
59 | { |
60 | return a.priority < b.priority; |
61 | }; |
62 | } |
63 | |
64 | Q_DECLARE_TYPEINFO(FilterPriorityPair, Q_PRIMITIVE_TYPE); |
65 | |
66 | namespace Qt3DCore { |
67 | |
68 | namespace { |
69 | |
70 | class InternalEventListener : public QObject |
71 | { |
72 | Q_OBJECT |
73 | public: |
74 | explicit InternalEventListener(QEventFilterServicePrivate *filterService, QObject *parent = nullptr); |
75 | bool eventFilter(QObject *obj, QEvent *e) final; |
76 | QEventFilterServicePrivate* m_filterService; |
77 | }; |
78 | |
79 | } // anonymous |
80 | |
81 | |
82 | class QEventFilterServicePrivate : public QAbstractServiceProviderPrivate |
83 | { |
84 | public: |
85 | QEventFilterServicePrivate() |
86 | : QAbstractServiceProviderPrivate(QServiceLocator::EventFilterService, QStringLiteral("Default event filter service implementation" )) |
87 | {} |
88 | |
89 | Q_DECLARE_PUBLIC(QEventFilterService) |
90 | |
91 | void registerEventFilter(QObject *eventFilter, int priority) |
92 | { |
93 | FilterPriorityPair fpPair; |
94 | fpPair.filter = eventFilter; |
95 | fpPair.priority = priority; |
96 | const auto it = std::lower_bound(first: m_eventFilters.begin(), last: m_eventFilters.end(), val: fpPair, comp: byPriority); |
97 | if (it == m_eventFilters.end() || it->priority != priority) |
98 | m_eventFilters.insert(position: it, x: std::move(fpPair)); |
99 | } |
100 | |
101 | void unregisterEventFilter(QObject *eventFilter) |
102 | { |
103 | for (auto it = m_eventFilters.begin(), end = m_eventFilters.end(); it != end; ++it) { |
104 | if (it->filter == eventFilter) { |
105 | m_eventFilters.erase(position: it); |
106 | return; |
107 | } |
108 | } |
109 | } |
110 | |
111 | QScopedPointer<InternalEventListener> m_eventDispatcher; |
112 | std::vector<FilterPriorityPair> m_eventFilters; |
113 | }; |
114 | |
115 | /* !\internal |
116 | \class Qt3DCore::QEventFilterService |
117 | \inmodule Qt3DCore |
118 | |
119 | \brief Allows to register event filters with a priority. |
120 | |
121 | The QEventFilterService class allows registering prioritized event filters. |
122 | Events are dispatched to the event filter with the highest priority first, |
123 | and then propagates to lower priority event filters if the event wasn't |
124 | accepted. |
125 | */ |
126 | |
127 | QEventFilterService::QEventFilterService() |
128 | : QAbstractServiceProvider(*new QEventFilterServicePrivate()) |
129 | { |
130 | } |
131 | |
132 | QEventFilterService::~QEventFilterService() |
133 | { |
134 | } |
135 | |
136 | // Note: event filters can only be registered to QObject in the same thread |
137 | void QEventFilterService::initialize(QObject *eventSource) |
138 | { |
139 | Q_D(QEventFilterService); |
140 | if (eventSource == nullptr) { |
141 | d->m_eventDispatcher.reset(); |
142 | } else { |
143 | d->m_eventDispatcher.reset(other: new InternalEventListener(d)); |
144 | eventSource->installEventFilter(filterObj: d->m_eventDispatcher.data()); |
145 | } |
146 | } |
147 | |
148 | void QEventFilterService::shutdown(QObject *eventSource) |
149 | { |
150 | Q_D(QEventFilterService); |
151 | if (eventSource && d->m_eventDispatcher.data()) |
152 | eventSource->removeEventFilter(obj: d->m_eventDispatcher.data()); |
153 | } |
154 | |
155 | void QEventFilterService::registerEventFilter(QObject *eventFilter, int priority) |
156 | { |
157 | Q_D(QEventFilterService); |
158 | d->registerEventFilter(eventFilter, priority); |
159 | } |
160 | |
161 | void QEventFilterService::unregisterEventFilter(QObject *eventFilter) |
162 | { |
163 | Q_D(QEventFilterService); |
164 | d->unregisterEventFilter(eventFilter); |
165 | } |
166 | |
167 | namespace{ |
168 | |
169 | InternalEventListener::InternalEventListener(QEventFilterServicePrivate *filterService, QObject *parent) |
170 | : QObject(parent), |
171 | m_filterService(filterService) |
172 | { |
173 | } |
174 | |
175 | bool InternalEventListener::eventFilter(QObject *obj, QEvent *e) |
176 | { |
177 | for (auto i = m_filterService->m_eventFilters.size(); i > 0; --i) { |
178 | const FilterPriorityPair &fPPair = m_filterService->m_eventFilters[i - 1]; |
179 | if (fPPair.filter->eventFilter(watched: obj, event: e)) |
180 | return true; |
181 | } |
182 | return false; |
183 | } |
184 | } |
185 | |
186 | } // Qt3DCore |
187 | |
188 | QT_END_NAMESPACE |
189 | |
190 | #include "qeventfilterservice.moc" |
191 | |