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