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 :
23 : #pragma once
24 :
25 : #include <qqmlintegration.h>
26 :
27 : #include <QObject>
28 : #include <QString>
29 :
30 : #include "global/async/asyncable.h"
31 :
32 : #include "rcommand/commandtypes.h"
33 :
34 : #include "muse_framework_config.h"
35 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
36 : #include "actions/actiontypes.h"
37 : #include "ui/uiaction.h"
38 : #endif
39 :
40 : namespace muse::uicomponents {
41 : // This must be in sync with QAction::MenuRole
42 : enum class MenuItemRole {
43 : NoRole = 0,
44 : TextHeuristicRole,
45 : ApplicationSpecificRole,
46 : AboutQtRole,
47 : AboutRole,
48 : PreferencesRole,
49 : QuitRole
50 : };
51 : class MenuItem;
52 : using MenuItemList = QList<MenuItem*>;
53 :
54 : class MenuItem : public QObject, public async::Asyncable
55 : {
56 : Q_OBJECT
57 : QML_ELEMENT
58 :
59 : Q_PROPERTY(QString id READ id NOTIFY idChanged)
60 : Q_PROPERTY(QString code READ code_property NOTIFY itemChanged)
61 :
62 : Q_PROPERTY(QString title READ translatedTitle NOTIFY itemChanged)
63 : Q_PROPERTY(QString titleWithMnemonicUnderline READ titleWithMnemonicUnderline NOTIFY itemChanged)
64 : Q_PROPERTY(QString description READ translatedDescription NOTIFY itemChanged)
65 : Q_PROPERTY(QString shortcuts READ shortcutsTitle NOTIFY shortcutsChanged)
66 : Q_PROPERTY(QString portableShortcuts READ portableShortcuts NOTIFY shortcutsChanged)
67 :
68 : Q_PROPERTY(int icon READ icon_property NOTIFY itemChanged)
69 : Q_PROPERTY(QString iconColor READ iconColor_property NOTIFY itemChanged)
70 :
71 : Q_PROPERTY(bool enabled READ enabled NOTIFY stateChanged)
72 : Q_PROPERTY(bool checkable READ checkable NOTIFY itemChanged)
73 : Q_PROPERTY(bool checked READ checked NOTIFY stateChanged)
74 : Q_PROPERTY(bool selectable READ selectable NOTIFY selectableChanged)
75 : Q_PROPERTY(bool selected READ selected NOTIFY selectedChanged)
76 :
77 : Q_PROPERTY(QString section READ section NOTIFY sectionChanged)
78 : Q_PROPERTY(int role READ role_property NOTIFY roleChanged)
79 : Q_PROPERTY(muse::uicomponents::MenuItemList subitems READ subitems NOTIFY subitemsChanged)
80 :
81 : public:
82 : MenuItem(QObject* parent = nullptr);
83 :
84 : void setId(const QString& id);
85 : QString id() const;
86 : std::string intent() const;
87 : QString code_property() const;
88 :
89 : void setTitle(const MnemonicString& title);
90 : QString translatedTitle() const;
91 : QString titleWithMnemonicUnderline() const;
92 : QString titleWithMnemonicAmpersand() const;
93 : void setDescription(const TranslatableString& description);
94 : QString translatedDescription() const;
95 : void setShortcuts(const std::vector<std::string>& shortcuts);
96 : QString shortcutsTitle() const;
97 : QString portableShortcuts() const;
98 :
99 : void setIcon(ui::IconCode::Code icon);
100 : int icon_property() const;
101 : void setIconColor(const QString& iconColor);
102 : QString iconColor_property() const;
103 :
104 : void setEnabled(bool enabled);
105 : bool enabled() const;
106 : void setCheckable(bool checkable);
107 : bool checkable() const;
108 : void setChecked(bool checked);
109 : bool checked() const;
110 : void setSelectable(bool selectable);
111 : bool selectable() const;
112 : void setSelected(bool selected);
113 : bool selected() const;
114 :
115 : void setSection(const QString& section);
116 : QString section() const;
117 : void setRole(MenuItemRole role);
118 : MenuItemRole role() const;
119 : int role_property() const;
120 : void setSubitems(const MenuItemList& subitems);
121 : MenuItemList subitems() const;
122 :
123 : bool isValid() const;
124 :
125 : // command support
126 : MenuItem(const rcommand::CommandInfo& info, QObject* parent = nullptr);
127 : void setCommandInfo(const rcommand::CommandInfo& info);
128 : rcommand::CommandInfo commandInfo() const;
129 : void setCommand(const rcommand::Command& command);
130 : rcommand::Command command() const;
131 : void setParams(const rcommand::Params& params);
132 : rcommand::Params params() const;
133 : void setCommandQuery(const rcommand::CommandQuery& query);
134 : rcommand::CommandQuery commandQuery() const;
135 : void setCommandState(const rcommand::CommandState& state);
136 : rcommand::CommandState commandState() const;
137 :
138 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
139 : // action support
140 : MenuItem(const ui::UiAction& action, QObject* parent = nullptr);
141 : void setActionCode(const actions::ActionCode& code);
142 : actions::ActionCode actionCode() const;
143 : void setAction(const muse::ui::UiAction& action);
144 : ui::UiAction action() const;
145 : void setState(const muse::ui::UiActionState& state);
146 : ui::UiActionState state() const;
147 : void setArgs(const muse::actions::ActionData& args);
148 : muse::actions::ActionData args() const;
149 : void setQuery(const muse::actions::ActionQuery& query);
150 : muse::actions::ActionQuery query() const;
151 : #endif
152 :
153 : signals:
154 : void idChanged(QString id);
155 : void titleChanged(QString title);
156 : void sectionChanged(QString section);
157 : void stateChanged();
158 : void selectableChanged(bool selectable);
159 : void selectedChanged(bool selected);
160 : void roleChanged(int role);
161 : void subitemsChanged(uicomponents::MenuItemList subitems, const QString& menuId);
162 : void itemChanged();
163 : void shortcutsChanged();
164 :
165 : private:
166 :
167 : QString m_id;
168 : std::string m_intent;
169 : MnemonicString m_title;
170 : TranslatableString m_description;
171 : std::vector<std::string> m_shortcuts;
172 :
173 : ui::IconCode::Code m_icon = ui::IconCode::Code::NONE;
174 : QString m_iconColor;
175 :
176 : bool m_enabled = true;
177 : bool m_checkable = false;
178 : bool m_checked = false;
179 : bool m_selectable = false;
180 : bool m_selected = false;
181 :
182 : QString m_section;
183 : MenuItemRole m_role = MenuItemRole::NoRole;
184 : MenuItemList m_subitems;
185 :
186 : rcommand::Params m_params;
187 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
188 : muse::actions::ActionData m_args;
189 : #endif
190 : };
191 :
192 0 : inline QVariantList menuItemListToVariantList(const uicomponents::MenuItemList& list)
193 : {
194 0 : QVariantList result;
195 0 : for (MenuItem* item: list) {
196 0 : result << QVariant::fromValue(item);
197 : }
198 :
199 0 : return result;
200 0 : }
201 : }
|