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