Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2026 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "applicationeventcontroller.h"
23 :
24 : #ifndef NO_QT_SUPPORT
25 :
26 : #include <QCoreApplication>
27 :
28 : #include "containers.h"
29 : #include "runtime.h"
30 :
31 : #include "log.h"
32 :
33 : using namespace muse;
34 :
35 156 : ApplicationEventController::ApplicationEventController()
36 : {
37 52 : DO_ASSERT(std::this_thread::get_id() == runtime::mainThreadId());
38 52 : qApp->installEventFilter(this);
39 52 : }
40 :
41 58 : ApplicationEventController::~ApplicationEventController()
42 : {
43 52 : if (qApp) {
44 52 : qApp->removeEventFilter(this);
45 : }
46 58 : }
47 :
48 0 : async::Channel<IApplicationEventController::EventData> ApplicationEventController::eventReceived() const
49 : {
50 0 : return m_eventReceived;
51 : }
52 :
53 5 : void ApplicationEventController::setPendingEventTypes(const std::vector<QEvent::Type>& types)
54 : {
55 5 : m_pendingTypes = types;
56 5 : }
57 :
58 8 : std::vector< std::unique_ptr<QEvent> > ApplicationEventController::takePendingEvents()
59 : {
60 8 : DO_ASSERT(std::this_thread::get_id() == runtime::mainThreadId());
61 :
62 8 : m_pendingTypes.clear();
63 8 : std::vector<std::unique_ptr<QEvent> > events = std::move(m_pendingEvents);
64 8 : m_pendingEvents.clear();
65 :
66 8 : return events;
67 : }
68 :
69 3887 : bool ApplicationEventController::eventFilter(QObject* watched, QEvent* event)
70 : {
71 3887 : if (contains(m_pendingTypes, event->type())) {
72 4 : m_pendingEvents.emplace_back(event->clone());
73 : }
74 :
75 3887 : m_eventReceived.send(EventData { watched, event });
76 3887 : return false;
77 : }
78 :
79 : #endif // NO_QT_SUPPORT
|