Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore/Audacity CLA applies
4 : *
5 : * Copyright (C) 2026 MuseScore/Audacity and others
6 : *
7 : * This program is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License version 3 as
9 : * published by the Free Software Foundation.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "commandsregister.h"
21 :
22 : #include "log.h"
23 :
24 : using namespace muse;
25 : using namespace muse::rcommand;
26 :
27 3 : void CommandsRegister::reg(const IModuleCommandsRegisterPtr& module)
28 : {
29 3 : IF_ASSERT_FAILED(module) {
30 0 : return;
31 : }
32 :
33 3 : const std::string& moduleName = module->moduleName();
34 3 : IF_ASSERT_FAILED(!moduleName.empty()) {
35 0 : return;
36 : }
37 :
38 3 : IF_ASSERT_FAILED(m_modules.find(moduleName) == m_modules.end()) {
39 0 : LOGW() << "module already registered: " << moduleName;
40 0 : return;
41 : }
42 :
43 3 : m_modules[moduleName] = module;
44 :
45 3 : auto updateModuleNames = [this, module, moduleName]() {
46 15 : for (const auto& info : module->commandInfoList()) {
47 12 : m_commandModuleNames[info.command] = moduleName;
48 : }
49 6 : };
50 :
51 3 : module->commandListChanged().onNotify(this, [this, updateModuleNames]() {
52 0 : updateModuleNames();
53 0 : m_commandInfoListChanged.notify();
54 0 : });
55 :
56 3 : updateModuleNames();
57 3 : }
58 :
59 0 : void CommandsRegister::unreg(const IModuleCommandsRegisterPtr& module)
60 : {
61 0 : IF_ASSERT_FAILED(module) {
62 0 : return;
63 : }
64 :
65 0 : const std::string& moduleName = module->moduleName();
66 0 : IF_ASSERT_FAILED(!moduleName.empty()) {
67 0 : return;
68 : }
69 :
70 0 : m_modules.erase(moduleName);
71 :
72 0 : for (const auto& info : module->commandInfoList()) {
73 0 : m_commandModuleNames.erase(info.command);
74 : }
75 0 : }
76 :
77 3 : IModuleCommandsRegisterPtr CommandsRegister::moduleRegister(const std::string& moduleName) const
78 : {
79 3 : auto it = m_modules.find(moduleName);
80 3 : if (it != m_modules.end()) {
81 3 : return it->second;
82 : }
83 :
84 0 : return nullptr;
85 : }
86 :
87 0 : std::vector<CommandInfo> CommandsRegister::commandInfoList() const
88 : {
89 0 : std::vector<CommandInfo> commands;
90 0 : for (const auto& module : m_modules) {
91 0 : const auto& infos = module.second->commandInfoList();
92 0 : commands.insert(commands.end(), infos.begin(), infos.end());
93 : }
94 0 : return commands;
95 0 : }
96 :
97 6 : const CommandInfo& CommandsRegister::commandInfo(const Command& command) const
98 : {
99 6 : for (const auto& module : m_modules) {
100 6 : const auto& infos = module.second->commandInfoList();
101 12 : for (const auto& info : infos) {
102 12 : if (info.command == command) {
103 6 : return info;
104 : }
105 : }
106 : }
107 :
108 0 : static CommandInfo null;
109 0 : return null;
110 : }
111 :
112 0 : async::Notification CommandsRegister::commandInfoListChanged() const
113 : {
114 0 : return m_commandInfoListChanged;
115 : }
116 :
117 0 : const std::string& CommandsRegister::commandModuleName(const Command& command) const
118 : {
119 0 : auto it = m_commandModuleNames.find(command);
120 0 : if (it != m_commandModuleNames.end()) {
121 0 : return it->second;
122 : }
123 :
124 0 : static const std::string empty;
125 0 : return empty;
126 : }
|