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