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 : #include "menuitem.h"
23 :
24 : #include <QVariantMap>
25 :
26 : #include "types/color.h"
27 : #include "types/translatablestring.h"
28 : #include "muse_framework_config.h"
29 : #ifdef MUSE_MODULE_SHORTCUTS_V2
30 : #include "shortcuts_v2/shortcutstypes.h"
31 : #else
32 : #include "shortcuts/shortcutstypes.h"
33 : #endif
34 :
35 : using namespace muse::uicomponents;
36 : using namespace muse::ui;
37 :
38 0 : MenuItem::MenuItem(QObject* parent)
39 0 : : QObject(parent)
40 : {
41 0 : }
42 :
43 12 : void MenuItem::setId(const QString& id)
44 : {
45 12 : if (m_id == id) {
46 6 : return;
47 : }
48 :
49 6 : m_id = id;
50 6 : emit idChanged(m_id);
51 : }
52 :
53 0 : QString MenuItem::id() const
54 : {
55 0 : return m_id;
56 : }
57 :
58 0 : std::string MenuItem::intent() const
59 : {
60 0 : return m_intent;
61 : }
62 :
63 0 : QString MenuItem::code_property() const
64 : {
65 0 : return QString::fromStdString(m_intent);
66 : }
67 :
68 6 : void MenuItem::setTitle(const MnemonicString& title)
69 : {
70 6 : if (m_title == title) {
71 0 : return;
72 : }
73 :
74 6 : m_title = title;
75 6 : emit itemChanged();
76 : }
77 :
78 0 : QString MenuItem::translatedTitle() const
79 : {
80 0 : return m_title.qTranslatedWithoutMnemonic();
81 : }
82 :
83 0 : QString MenuItem::titleWithMnemonicUnderline() const
84 : {
85 0 : return m_title.qTranslatedWithMnemonicUnderline();
86 : }
87 :
88 0 : QString MenuItem::titleWithMnemonicAmpersand() const
89 : {
90 0 : return m_title.qTranslatedWithMnemonicAmpersand();
91 : }
92 :
93 0 : void MenuItem::setDescription(const TranslatableString& description)
94 : {
95 0 : if (m_description == description) {
96 0 : return;
97 : }
98 :
99 0 : m_description = description;
100 0 : emit itemChanged();
101 : }
102 :
103 0 : QString MenuItem::translatedDescription() const
104 : {
105 0 : return m_description.qTranslated();
106 : }
107 :
108 6 : void MenuItem::setShortcuts(const std::vector<std::string>& shortcuts)
109 : {
110 6 : if (m_shortcuts == shortcuts) {
111 6 : return;
112 : }
113 :
114 0 : m_shortcuts = shortcuts;
115 0 : emit shortcutsChanged();
116 : }
117 :
118 0 : QString MenuItem::shortcutsTitle() const
119 : {
120 0 : return shortcuts::sequencesToNativeText(m_shortcuts);
121 : }
122 :
123 0 : QString MenuItem::portableShortcuts() const
124 : {
125 0 : return QString::fromStdString(shortcuts::Shortcut::sequencesToString(m_shortcuts));
126 : }
127 :
128 0 : void MenuItem::setIcon(ui::IconCode::Code icon)
129 : {
130 0 : if (m_icon == icon) {
131 0 : return;
132 : }
133 :
134 0 : m_icon = icon;
135 0 : emit itemChanged();
136 : }
137 :
138 0 : int MenuItem::icon_property() const
139 : {
140 0 : return static_cast<int>(m_icon);
141 : }
142 :
143 0 : void MenuItem::setIconColor(const QString& iconColor)
144 : {
145 0 : if (m_iconColor == iconColor) {
146 0 : return;
147 : }
148 :
149 0 : m_iconColor = iconColor;
150 0 : emit itemChanged();
151 : }
152 :
153 0 : QString MenuItem::iconColor_property() const
154 : {
155 0 : return m_iconColor;
156 : }
157 :
158 0 : void MenuItem::setEnabled(bool enabled)
159 : {
160 0 : if (m_enabled == enabled) {
161 0 : return;
162 : }
163 :
164 0 : m_enabled = enabled;
165 0 : emit stateChanged();
166 : }
167 :
168 0 : bool MenuItem::enabled() const
169 : {
170 0 : return m_enabled;
171 : }
172 :
173 0 : void MenuItem::setCheckable(bool checkable)
174 : {
175 0 : if (m_checkable == checkable) {
176 0 : return;
177 : }
178 :
179 0 : m_checkable = checkable;
180 0 : emit itemChanged();
181 : }
182 :
183 0 : bool MenuItem::checkable() const
184 : {
185 0 : return m_checkable;
186 : }
187 :
188 0 : void MenuItem::setChecked(bool checked)
189 : {
190 0 : if (m_checked == checked) {
191 0 : return;
192 : }
193 :
194 0 : m_checked = checked;
195 0 : emit stateChanged();
196 : }
197 :
198 0 : bool MenuItem::checked() const
199 : {
200 0 : return m_checked;
201 : }
202 :
203 0 : void MenuItem::setSelectable(bool selectable)
204 : {
205 0 : if (m_selectable == selectable) {
206 0 : return;
207 : }
208 :
209 0 : m_selectable = selectable;
210 0 : emit selectableChanged(m_selectable);
211 : }
212 :
213 0 : bool MenuItem::selectable() const
214 : {
215 0 : return m_selectable;
216 : }
217 :
218 0 : void MenuItem::setSelected(bool selected)
219 : {
220 0 : if (m_selected == selected) {
221 0 : return;
222 : }
223 :
224 0 : m_selected = selected;
225 0 : emit selectedChanged(m_selected);
226 : }
227 :
228 0 : void MenuItem::setSection(const QString& section)
229 : {
230 0 : if (m_section == section) {
231 0 : return;
232 : }
233 :
234 0 : m_section = section;
235 0 : emit sectionChanged(m_section);
236 : }
237 :
238 0 : bool MenuItem::selected() const
239 : {
240 0 : return m_selected;
241 : }
242 :
243 0 : QString MenuItem::section() const
244 : {
245 0 : return m_section;
246 : }
247 :
248 0 : void MenuItem::setRole(MenuItemRole role)
249 : {
250 0 : if (m_role == role) {
251 0 : return;
252 : }
253 :
254 0 : m_role = role;
255 0 : emit roleChanged(role_property());
256 : }
257 :
258 0 : MenuItemRole MenuItem::role() const
259 : {
260 0 : return m_role;
261 : }
262 :
263 0 : int MenuItem::role_property() const
264 : {
265 0 : return static_cast<int>(m_role);
266 : }
267 :
268 0 : void MenuItem::setSubitems(const QList<MenuItem*>& subitems)
269 : {
270 0 : if (m_subitems == subitems) {
271 0 : return;
272 : }
273 :
274 0 : m_subitems = subitems;
275 0 : emit subitemsChanged(m_subitems, m_id);
276 : }
277 :
278 6 : MenuItemList MenuItem::subitems() const
279 : {
280 6 : return m_subitems;
281 : }
282 :
283 0 : bool MenuItem::isValid() const
284 : {
285 0 : return !m_id.isEmpty();
286 : }
287 :
288 : // command support
289 6 : MenuItem::MenuItem(const rcommand::CommandInfo& info, QObject* parent)
290 6 : : QObject(parent)
291 : {
292 6 : setCommandInfo(info);
293 6 : }
294 :
295 6 : void MenuItem::setCommandInfo(const rcommand::CommandInfo& info)
296 : {
297 6 : m_intent = info.command.toString();
298 6 : setId(QString::fromStdString(m_intent));
299 :
300 6 : m_title = info.title;
301 6 : m_description = info.description;
302 6 : m_icon = info.decoration.iconCode;
303 6 : m_iconColor = QString::fromStdString(info.decoration.iconColor.toString());
304 6 : m_checkable = info.decoration.checkable == rcommand::Checkable::Yes;
305 :
306 6 : emit itemChanged();
307 6 : }
308 :
309 0 : muse::rcommand::CommandInfo MenuItem::commandInfo() const
310 : {
311 0 : rcommand::CommandInfo info;
312 0 : info.command = rcommand::Command(m_intent);
313 0 : info.title = m_title;
314 0 : info.description = m_description;
315 0 : info.decoration.iconCode = m_icon;
316 0 : info.decoration.iconColor = Color::fromString(m_iconColor.toStdString());
317 0 : info.decoration.checkable = m_checkable ? rcommand::Checkable::Yes : rcommand::Checkable::No;
318 0 : return info;
319 0 : }
320 :
321 6 : void MenuItem::setCommand(const rcommand::Command& command)
322 : {
323 6 : m_intent = command.toString();
324 6 : setId(QString::fromStdString(m_intent));
325 6 : }
326 :
327 6 : muse::rcommand::Command MenuItem::command() const
328 : {
329 6 : return rcommand::Command(m_intent);
330 : }
331 :
332 12 : void MenuItem::setParams(const rcommand::Params& params)
333 : {
334 12 : m_params = params;
335 12 : }
336 :
337 0 : muse::rcommand::Params MenuItem::params() const
338 : {
339 0 : return m_params;
340 : }
341 :
342 0 : void MenuItem::setCommandQuery(const rcommand::CommandQuery& query)
343 : {
344 0 : m_intent = query.toString();
345 0 : setId(QString::fromStdString(m_intent));
346 0 : }
347 :
348 0 : muse::rcommand::CommandQuery MenuItem::commandQuery() const
349 : {
350 0 : return rcommand::CommandQuery(m_intent);
351 : }
352 :
353 6 : void MenuItem::setCommandState(const rcommand::CommandState& state)
354 : {
355 6 : if (m_enabled == state.enabled && m_checked == state.checked) {
356 6 : return;
357 : }
358 0 : m_enabled = state.enabled;
359 0 : m_checked = state.checked;
360 0 : emit stateChanged();
361 : }
362 :
363 0 : muse::rcommand::CommandState MenuItem::commandState() const
364 : {
365 0 : return rcommand::CommandState(m_enabled, m_checked);
366 : }
367 :
368 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
369 : // action support
370 0 : MenuItem::MenuItem(const UiAction& action, QObject* parent)
371 0 : : QObject(parent)
372 : {
373 0 : m_id = QString::fromStdString(action.code);
374 0 : setAction(action);
375 0 : }
376 :
377 0 : void MenuItem::setActionCode(const actions::ActionCode& code)
378 : {
379 0 : m_intent = code;
380 0 : }
381 :
382 0 : muse::actions::ActionCode MenuItem::actionCode() const
383 : {
384 0 : return m_intent;
385 : }
386 :
387 0 : void MenuItem::setAction(const UiAction& action)
388 : {
389 0 : m_intent = action.code;
390 0 : m_title = action.title;
391 0 : m_description = action.description;
392 0 : m_icon = action.iconCode;
393 0 : m_iconColor = action.iconColor;
394 0 : m_checkable = action.checkable == ui::Checkable::Yes;
395 :
396 0 : emit itemChanged();
397 0 : }
398 :
399 0 : UiAction MenuItem::action() const
400 : {
401 0 : UiAction action;
402 0 : action.code = m_intent;
403 0 : action.title = m_title;
404 0 : action.description = m_description;
405 0 : action.iconCode = m_icon;
406 0 : action.iconColor = m_iconColor;
407 0 : action.checkable = m_checkable ? ui::Checkable::Yes : ui::Checkable::No;
408 0 : return action;
409 0 : }
410 :
411 0 : void MenuItem::setState(const UiActionState& state)
412 : {
413 0 : if (m_enabled == state.enabled && m_checked == state.checked) {
414 0 : return;
415 : }
416 :
417 0 : m_enabled = state.enabled;
418 0 : m_checked = state.checked;
419 0 : emit stateChanged();
420 : }
421 :
422 0 : UiActionState MenuItem::state() const
423 : {
424 0 : return UiActionState{ m_enabled, m_checked };
425 : }
426 :
427 0 : void MenuItem::setArgs(const muse::actions::ActionData& args)
428 : {
429 0 : m_args = args;
430 0 : }
431 :
432 0 : muse::actions::ActionData MenuItem::args() const
433 : {
434 0 : return m_args;
435 : }
436 :
437 0 : void MenuItem::setQuery(const muse::actions::ActionQuery& query)
438 : {
439 0 : m_intent = query.toString();
440 0 : }
441 :
442 0 : muse::actions::ActionQuery MenuItem::query() const
443 : {
444 0 : return muse::actions::ActionQuery(m_intent);
445 : }
446 :
447 : #endif
|