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 <map>
27 :
28 : #include <QObject>
29 : #include <QString>
30 :
31 : #include "global/async/asyncable.h"
32 :
33 : #include "uicomponents/qml/Muse/UiComponents/menuitem.h"
34 :
35 : #include "abstracttoolbarmodel.h"
36 :
37 : #include "modularity/ioc.h"
38 :
39 : #include "global/iapplication.h"
40 :
41 : #include "rcommand/commandtypes.h"
42 : #include "rcommand/icommanddispatcher.h"
43 :
44 : #include "muse_framework_config.h"
45 :
46 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
47 : #include "ui/uiaction.h"
48 : #include "actions/iactionsdispatcher.h"
49 : #endif
50 :
51 : namespace muse::uicomponents {
52 : class ToolBarItem : public QObject, public Contextable, public async::Asyncable
53 : {
54 : Q_OBJECT
55 : QML_ELEMENT;
56 :
57 : Q_PROPERTY(QString id READ id NOTIFY idChanged)
58 :
59 : Q_PROPERTY(QString code READ code_property NOTIFY itemChanged)
60 : Q_PROPERTY(QString shortcuts READ shortcutsTitle NOTIFY shortcutsChanged)
61 :
62 : Q_PROPERTY(QString title READ translatedTitle NOTIFY itemChanged)
63 : Q_PROPERTY(bool showTitle READ showTitle WRITE setShowTitle NOTIFY showTitleChanged)
64 : Q_PROPERTY(bool isTitleBold READ isTitleBold WRITE setIsTitleBold NOTIFY isTitleBoldChanged)
65 :
66 : Q_PROPERTY(QString description READ description_property NOTIFY itemChanged)
67 :
68 : Q_PROPERTY(int icon READ icon_property NOTIFY itemChanged)
69 :
70 : Q_PROPERTY(bool enabled READ enabled NOTIFY stateChanged)
71 :
72 : Q_PROPERTY(bool checkable READ checkable_property NOTIFY itemChanged)
73 : Q_PROPERTY(bool checked READ checked_property NOTIFY stateChanged)
74 :
75 : Q_PROPERTY(bool selected READ selected_property NOTIFY selectedChanged)
76 :
77 : Q_PROPERTY(bool isTransparent READ isTransparent WRITE setIsTransparent NOTIFY isTransparentChanged)
78 :
79 : Q_PROPERTY(int type READ type_property NOTIFY typeChanged)
80 :
81 : Q_PROPERTY(QList<MenuItem*> menuItems READ menuItems NOTIFY menuItemsChanged)
82 : Q_PROPERTY(bool isMenuSecondary READ isMenuSecondary NOTIFY isMenuSecondaryChanged)
83 :
84 : GlobalInject<IApplication> application;
85 :
86 : ContextInject<muse::rcommand::ICommandDispatcher> commandDispatcher = { this };
87 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
88 : ContextInject<muse::actions::IActionsDispatcher> dispatcher = { this };
89 : #endif
90 :
91 : public:
92 : ToolBarItem(QObject* parent = nullptr);
93 : ToolBarItem(ToolBarItemType::Type type, QObject* parent = nullptr);
94 :
95 : ToolBarItemType::Type type() const;
96 : bool isValid() const;
97 :
98 : QString id() const;
99 : QString translatedTitle() const;
100 : bool showTitle() const;
101 : void setShowTitle(bool show);
102 : bool isTitleBold() const;
103 : void setIsTitleBold(bool newIsTitleBold);
104 :
105 : void setEnabled(bool enabled);
106 : bool enabled() const;
107 :
108 : bool selected() const;
109 :
110 : const QList<MenuItem*>& menuItems() const;
111 :
112 : bool isMenuSecondary() const;
113 : void setIsMenuSecondary(bool secondary);
114 :
115 : QString shortcutsTitle() const;
116 :
117 : bool isTransparent() const;
118 : void setIsTransparent(bool isTransparent);
119 :
120 : Q_INVOKABLE void activate();
121 : Q_INVOKABLE void handleMenuItem(const QString& menuId);
122 :
123 : // command support
124 : ToolBarItem(const rcommand::CommandInfo& info, ToolBarItemType::Type type, QObject* parent = nullptr);
125 : void setCommandInfo(const rcommand::CommandInfo& info);
126 : rcommand::CommandInfo commandInfo() const;
127 0 : void setCommand(const rcommand::Command& command, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
128 : rcommand::Command command() const;
129 : void setParams(const rcommand::Params& params);
130 : rcommand::Params params() const;
131 : void setCommandState(const rcommand::CommandState& state);
132 : rcommand::CommandState commandState() const;
133 :
134 : // action support
135 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
136 : ToolBarItem(const ui::UiAction& action, ToolBarItemType::Type type, QObject* parent = nullptr);
137 : void setAction(const ui::UiAction& action);
138 : ui::UiAction action() const;
139 : actions::ActionCode actionCode() const;
140 : void setState(const ui::UiActionState& state);
141 : ui::UiActionState state() const;
142 : void setArgs(const muse::actions::ActionData& args);
143 : muse::actions::ActionData args() const;
144 : #endif
145 :
146 : public slots:
147 : void setId(const QString& id);
148 : void setTitle(const TranslatableString& title);
149 : void setDescription(const TranslatableString& description);
150 : void setSelected(bool selected);
151 : void setType(muse::uicomponents::ToolBarItemType::Type type);
152 : void setMenuItems(const QList<uicomponents::MenuItem*>& menuItems);
153 : void setShortcuts(const std::vector<std::string>& shortcuts);
154 :
155 : signals:
156 : void idChanged(QString id);
157 : void titleChanged(QString title);
158 : void stateChanged();
159 : void selectedChanged(bool selected);
160 : void typeChanged(int type);
161 : void menuItemsChanged(QList<uicomponents::MenuItem*> menuItems, const QString& menuId);
162 : void isMenuSecondaryChanged(bool secondary);
163 : void itemChanged();
164 : void shortcutsChanged();
165 :
166 : void showTitleChanged();
167 : void isTitleBoldChanged();
168 : void isTransparentChanged();
169 :
170 : private:
171 : QString code_property() const;
172 : QString description_property() const;
173 : int icon_property() const;
174 : bool checkable_property() const;
175 : bool checked_property() const;
176 : bool selected_property() const;
177 : int type_property() const;
178 :
179 : std::string intent() const;
180 :
181 : ToolBarItemType::Type m_type = ToolBarItemType::ACTION;
182 :
183 : QString m_id;
184 : std::map<Qt::KeyboardModifiers, std::string> m_intents;
185 : MnemonicString m_title;
186 : TranslatableString m_description;
187 : bool m_showTitle = false;
188 : bool m_isTitleBold = false;
189 :
190 : ui::IconCode::Code m_icon = ui::IconCode::Code::NONE;
191 :
192 : bool m_enabled = true;
193 : bool m_checkable = false;
194 : bool m_checked = false;
195 : bool m_selected = false;
196 :
197 : QList<MenuItem*> m_menuItems;
198 : bool m_isMenuSecondary = false;
199 :
200 : std::vector<std::string> m_shortcuts;
201 : bool m_isTransparent = true;
202 :
203 : rcommand::Params m_params;
204 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
205 : muse::actions::ActionData m_args;
206 : #endif
207 : };
208 : using ToolBarItemList = QList<ToolBarItem*>;
209 : }
|