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) 2021 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 : #pragma once
23 :
24 : #include <qqmlintegration.h>
25 :
26 : #include <QAbstractListModel>
27 :
28 : #include "async/asyncable.h"
29 : #include "menuitem.h"
30 :
31 : #include "modularity/ioc.h"
32 : #include "rcommand/commandtypes.h"
33 : #include "types/uri.h"
34 : #include "rcommand/icommanddispatcher.h"
35 : #include "rcommand/icommandsregister.h"
36 : #include "rcommand/icommandsstate.h"
37 :
38 : #include "muse_framework_config.h"
39 :
40 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
41 : #include "ui/iuiactionsregister.h"
42 : #include "actions/iactionsdispatcher.h"
43 : #endif
44 :
45 : #ifdef MUSE_MODULE_SHORTCUTS_V2
46 : #include "shortcuts_v2/icommandshortcutsregister.h"
47 : #else
48 : #include "shortcuts/ishortcutsregister.h"
49 : #endif
50 :
51 : namespace muse::uicomponents {
52 : class AbstractMenuModel : public QAbstractListModel, public muse::Contextable, public async::Asyncable
53 : {
54 : Q_OBJECT
55 :
56 : Q_PROPERTY(int length READ rowCount NOTIFY itemsChanged)
57 : Q_PROPERTY(QVariantList items READ itemsProperty NOTIFY itemsChanged)
58 :
59 : QML_ELEMENT
60 :
61 : public:
62 : muse::GlobalInject<rcommand::ICommandsRegister> commandsRegister;
63 : muse::ContextInject<rcommand::ICommandsState> commandsState = { this };
64 : muse::ContextInject<rcommand::ICommandDispatcher> commandDispatcher = { this };
65 :
66 : #ifdef MUSE_MODULE_SHORTCUTS_V2
67 : muse::GlobalInject<shortcuts::ICommandShortcutsRegister> commandShortcutsRegister;
68 : #else
69 : muse::ContextInject<shortcuts::IShortcutsRegister> shortcutsRegister = { this };
70 : #endif
71 :
72 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
73 : muse::ContextInject<ui::IUiActionsRegister> uiActionsRegister = { this };
74 : muse::ContextInject<muse::actions::IActionsDispatcher> dispatcher = { this };
75 : #endif
76 :
77 : public:
78 : explicit AbstractMenuModel(QObject* parent = nullptr);
79 :
80 : int rowCount(const QModelIndex& parent = QModelIndex()) const override;
81 : QVariant data(const QModelIndex& index, int role) const override;
82 : QHash<int, QByteArray> roleNames() const override;
83 :
84 : virtual void load();
85 :
86 : QVariantList itemsProperty() const;
87 : const MenuItemList& items() const;
88 :
89 : Q_INVOKABLE virtual void handleMenuItem(const QString& itemId);
90 : Q_INVOKABLE QVariantMap get(int index);
91 :
92 : signals:
93 : void itemsChanged();
94 : void itemChanged(muse::uicomponents::MenuItem* item);
95 :
96 : protected:
97 : enum Roles {
98 : ItemRole,
99 :
100 : UserRole
101 : };
102 :
103 : virtual void subscribeOnChanges();
104 : virtual void onCommandStateChanged(const rcommand::Command& command, const rcommand::CommandState& state);
105 :
106 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
107 : virtual void onActionsStateChanges(const muse::actions::ActionCodeList& codes);
108 : #endif
109 :
110 : void setItem(int index, MenuItem* item);
111 : void setItems(const MenuItemList& items);
112 : void clear();
113 :
114 : static const int INVALID_ITEM_INDEX;
115 : int itemIndex(const QString& itemId) const;
116 :
117 : MenuItem& item(int index);
118 :
119 : MenuItem& findItem(const QString& itemId);
120 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
121 : MenuItem& findItem(const muse::actions::ActionCode& actionCode);
122 : MenuItemList findItems(const muse::actions::ActionCode& actionCode);
123 : #endif
124 : MenuItem& findItem(const muse::rcommand::Command& command);
125 : MenuItemList findItems(const muse::rcommand::Command& command);
126 : MenuItem& findMenu(const QString& menuId);
127 :
128 : MenuItem* makeMenu(const TranslatableString& title, const MenuItemList& items, const QString& menuId = "", bool enabled = true);
129 :
130 : MenuItem* makeMenuItem(const muse::rcommand::Command& command, const TranslatableString& title = {});
131 : MenuItem* makeMenuItem(const muse::rcommand::CommandQuery& query, const TranslatableString& title = {});
132 : MenuItem* makeMenuItem(const muse::rcommand::Command& command, const muse::rcommand::Params& params,
133 : const TranslatableString& title = {});
134 :
135 : template<muse::rcommand::TypedCommand C>
136 0 : MenuItem* makeMenuItem(const C& command, const TranslatableString& title = {})
137 : {
138 : if constexpr (muse::rcommand::HasInput<C>) {
139 : return makeMenuItem(command.id, muse::rcommand::toInputParams(command), title);
140 : } else {
141 0 : return makeMenuItem(command.id, title);
142 : }
143 : }
144 :
145 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
146 : MenuItem* makeMenuItem(const muse::actions::ActionCode& actionCode, const TranslatableString& title = {});
147 : #endif
148 :
149 : MenuItem* makeSeparator();
150 :
151 : bool isIndexValid(int index) const;
152 :
153 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
154 : void dispatchAction(const muse::actions::ActionCode& code, const muse::actions::ActionData& args = muse::actions::ActionData());
155 : void dispatchAction(const muse::UriQuery& query);
156 : #endif
157 : void dispatchCommand(const muse::rcommand::Command& command, const muse::rcommand::Params& params = muse::rcommand::Params());
158 : void dispatchCommand(const muse::rcommand::CommandQuery& query);
159 :
160 : private:
161 : MenuItem& item(MenuItemList& items, const QString& itemId);
162 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
163 : MenuItemList items(const MenuItemList& items, const muse::actions::ActionCode& actionCode) const;
164 : #endif
165 : MenuItemList items(const MenuItemList& items, const muse::rcommand::Command& command) const;
166 : MenuItem& menu(MenuItemList& items, const QString& menuId);
167 :
168 : void updateState(MenuItemList& items, const rcommand::Command& command, const rcommand::CommandState& state);
169 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
170 : void updateState(MenuItemList& items, const muse::actions::ActionCodeList& codes, std::map<muse::actions::ActionCode,
171 : muse::ui::UiActionState>& states);
172 : #endif
173 :
174 : void updateShortcutsAll();
175 : void updateShortcuts(MenuItem* item);
176 :
177 : bool m_subscribedOnChanges = false;
178 : MenuItemList m_items;
179 : };
180 : }
|