Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-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 "noteinputbarmodel.h"
23 :
24 : #include "rcommand/commandtypes.h"
25 : #include "types/translatablestring.h"
26 :
27 : #include "notation/inotationelements.h" // IWYU pragma: keep
28 : #include "notation/inotationinteraction.h"
29 : #include "notation/inotationnoteinput.h"
30 : #include "notation/inotationselection.h"
31 : #include "notation/inotationundostack.h"
32 :
33 : #include "notationcommands.h"
34 :
35 : using namespace mu;
36 : using namespace mu::notation;
37 : using namespace muse;
38 : using namespace muse::ui;
39 : using namespace muse::uicomponents;
40 :
41 : static const QString TOOLBAR_NAME("noteInput");
42 :
43 : const std::string NoteInputBarModel::CROSS_STAFF_BEAMING_SUBITEMS("cross-staff-beaming-subitems");
44 : const std::string NoteInputBarModel::TUPLET_SUBITEMS("tuplets-subitems");
45 :
46 0 : NoteInputBarModel::NoteInputBarModel(QObject* parent)
47 0 : : AbstractMenuModel(parent)
48 : {
49 0 : }
50 :
51 0 : QVariant NoteInputBarModel::data(const QModelIndex& index, int role) const
52 : {
53 0 : int row = index.row();
54 0 : if (!isIndexValid(row)) {
55 0 : return QVariant();
56 : }
57 :
58 0 : const MenuItem* item = items().at(row);
59 0 : switch (role) {
60 0 : case OrderRole: return row;
61 0 : case SectionRole: return item->section();
62 : }
63 :
64 0 : return AbstractMenuModel::data(index, role);
65 : }
66 :
67 0 : QHash<int, QByteArray> NoteInputBarModel::roleNames() const
68 : {
69 0 : QHash<int, QByteArray> roles = AbstractMenuModel::roleNames();
70 0 : roles[OrderRole] = "order";
71 0 : roles[SectionRole] = "section";
72 :
73 0 : return roles;
74 0 : }
75 :
76 0 : void NoteInputBarModel::classBegin()
77 : {
78 0 : init();
79 0 : }
80 :
81 0 : void NoteInputBarModel::init()
82 : {
83 0 : subscribeOnChanges();
84 :
85 0 : uiState()->toolConfigChanged(TOOLBAR_NAME).onNotify(this, [this]() {
86 0 : load();
87 0 : });
88 :
89 0 : commandsController()->isNoteInputAllowedChanged().onReceive(this, [this](bool) {
90 0 : emit isInputAllowedChanged();
91 0 : });
92 :
93 0 : load();
94 0 : }
95 :
96 0 : const muse::ui::ToolConfig& NoteInputBarModel::defaultNoteInputConfig()
97 : {
98 0 : static ToolConfig config;
99 0 : if (!config.isValid()) {
100 0 : config.items = {
101 : { ToggleNoteInputByNameCommand::id, true },
102 : { ToggleNoteInputByDurationCommand::id, true },
103 : { ToggleNoteInputRhythmCommand::id, false },
104 : { ToggleNoteInputRepitchCommand::id, false },
105 : { ToggleNoteInputRealtimeAutoCommand::id, false },
106 : { ToggleNoteInputRealtimeManualCommand::id, false },
107 : { ToggleNoteInputTimewiseCommand::id, false },
108 : { ToolConfig::____________, true },
109 : { SetDuration1024thCommand::id, false },
110 : { SetDuration512thCommand::id, false },
111 : { SetDuration256thCommand::id, false },
112 : { SetDuration128thCommand::id, false },
113 : { SetDuration64thCommand::id, true },
114 : { SetDuration32ndCommand::id, true },
115 : { SetDuration16thCommand::id, true },
116 : { SetDurationEighthCommand::id, true },
117 : { SetDurationQuarterCommand::id, true },
118 : { SetDurationHalfCommand::id, true },
119 : { SetDurationWholeCommand::id, true },
120 : { SetDurationBreveCommand::id, false },
121 : { SetDurationLongaCommand::id, false },
122 : { ToolConfig::____________, true },
123 : { ToggleDotCommand::id, true },
124 : { ToggleDot2Command::id, false },
125 : { ToggleDot3Command::id, false },
126 : { ToggleDot4Command::id, false },
127 : { ToggleRestCommand::id, true },
128 : { ToolConfig::____________, true },
129 : { ToggleFlat2Command::id, true },
130 : { ToggleFlatCommand::id, true },
131 : { ToggleNaturalCommand::id, true },
132 : { ToggleSharpCommand::id, true },
133 : { ToggleSharp2Command::id, true },
134 : { ToolConfig::____________, true },
135 : { TOGGLE_TIE_COMMAND, true },
136 : { ADD_SLUR_COMMAND, true },
137 : { TOGGLE_LV_COMMAND, false },
138 : { ToolConfig::____________, true },
139 : { TOGGLE_MARCATO_COMMAND, true },
140 : { TOGGLE_SFORZATO_COMMAND, true },
141 : { TOGGLE_TENUTO_COMMAND, true },
142 : { TOGGLE_STACCATO_COMMAND, true },
143 : { ToolConfig::____________, true },
144 : { CROSS_STAFF_BEAMING_SUBITEMS, true }, // service
145 : { TUPLET_SUBITEMS, true }, // service
146 : { FlipCommand::id, true },
147 : { ToolConfig::____________, true },
148 : { UseVoice1Command::id, true },
149 : { UseVoice2Command::id, true },
150 : { UseVoice3Command::id, false },
151 : { UseVoice4Command::id, false }
152 0 : };
153 : }
154 0 : return config;
155 0 : }
156 :
157 0 : void NoteInputBarModel::load()
158 : {
159 0 : MenuItemList items;
160 :
161 0 : ToolConfig noteInputConfig = uiState()->toolConfig(TOOLBAR_NAME, NoteInputBarModel::defaultNoteInputConfig());
162 :
163 0 : int section = 0;
164 0 : for (const ToolConfig::Item& citem : noteInputConfig.items) {
165 0 : if (!citem.show) {
166 0 : continue;
167 : }
168 :
169 0 : if (citem.isSeparator()) {
170 0 : section++;
171 0 : continue;
172 : }
173 :
174 0 : if (citem.intent == CROSS_STAFF_BEAMING_SUBITEMS) {
175 0 : MenuItem* item = makeServiceItem(CROSS_STAFF_BEAMING_SUBITEMS, QString::number(section));
176 0 : item->setSubitems(makeCrossStaffBeamingItems());
177 0 : items << item;
178 0 : } else if (citem.intent == TUPLET_SUBITEMS) {
179 0 : MenuItem* item = makeServiceItem(TUPLET_SUBITEMS, QString::number(section));
180 0 : item->setSubitems(makeTupletItems());
181 0 : items << item;
182 : } else {
183 0 : MenuItem* item = makeCommandItem(rcommand::Command(citem.intent), QString::number(section));
184 0 : items << item;
185 : }
186 : }
187 :
188 0 : items << makeAddItem(QString::number(++section));
189 0 : setItems(items);
190 0 : }
191 :
192 0 : bool NoteInputBarModel::isInputAllowed() const
193 : {
194 0 : return commandsController()->isNoteInputAllowed();
195 : }
196 :
197 0 : NoteInputBarModel::ServiceItemInfo NoteInputBarModel::serviceItemInfo(const std::string& intent)
198 : {
199 0 : if (intent == CROSS_STAFF_BEAMING_SUBITEMS) {
200 0 : return { TranslatableString("action", "Cross-staff beaming"), IconCode::Code::CROSS_STAFF_BEAMING };
201 0 : } else if (intent == TUPLET_SUBITEMS) {
202 0 : return { TranslatableString("action", "Tuplet"), IconCode::Code::NOTE_TUPLET };
203 : }
204 0 : return { };
205 0 : }
206 :
207 0 : MenuItem* NoteInputBarModel::makeServiceItem(const std::string& intent, const QString& section)
208 : {
209 0 : const ServiceItemInfo info = serviceItemInfo(intent);
210 0 : MenuItem* item = new MenuItem(this);
211 0 : item->setId(QString::fromStdString(intent));
212 0 : item->setTitle(info.title);
213 0 : item->setIcon(info.icon);
214 0 : item->setSection(section);
215 0 : return item;
216 0 : }
217 :
218 0 : MenuItem* NoteInputBarModel::makeCommandItem(const muse::rcommand::Command& command, const QString& section)
219 : {
220 0 : const rcommand::CommandInfo info = commandsRegister()->commandInfo(command);
221 0 : MenuItem* item = new MenuItem(info, this);
222 0 : item->setSection(section);
223 0 : return item;
224 0 : }
225 :
226 0 : MenuItem* NoteInputBarModel::makeAddItem(const QString& section)
227 : {
228 0 : MenuItem* item = new MenuItem(this);
229 0 : item->setTitle(TranslatableString("global", "Add"));
230 0 : item->setIcon(IconCode::Code::PLUS);
231 0 : item->setSection(section);
232 0 : item->setSubitems(makeAddItems());
233 0 : return item;
234 : }
235 :
236 0 : MenuItemList NoteInputBarModel::makeCrossStaffBeamingItems()
237 : {
238 : MenuItemList items {
239 0 : makeMenuItem(MOVE_UP_COMMAND),
240 0 : makeMenuItem(MOVE_DOWN_COMMAND)
241 0 : };
242 :
243 0 : return items;
244 : }
245 :
246 0 : MenuItemList NoteInputBarModel::makeTupletItems()
247 : {
248 : MenuItemList items {
249 0 : makeMenuItem(AddDupletCommand::id),
250 0 : makeMenuItem(AddTripletCommand::id),
251 0 : makeMenuItem(AddQuadrupletCommand::id),
252 0 : makeMenuItem(AddQuintupletCommand::id),
253 0 : makeMenuItem(AddSextupletCommand::id),
254 0 : makeMenuItem(AddSeptupletCommand::id),
255 0 : makeMenuItem(AddOctupletCommand::id),
256 0 : makeMenuItem(AddNonupletCommand::id),
257 0 : makeMenuItem(OpenTupletConfigureCommand::id)
258 0 : };
259 :
260 0 : return items;
261 : }
262 :
263 0 : MenuItemList NoteInputBarModel::makeAddItems()
264 : {
265 : MenuItemList items {
266 0 : makeMenu(TranslatableString("notation", "Notes"), makeNotesItems()),
267 0 : makeMenu(TranslatableString("notation", "Intervals"), makeIntervalsItems()),
268 0 : makeMenu(TranslatableString("notation", "Measures"), makeMeasuresItems()),
269 0 : makeMenu(TranslatableString("notation", "Frames"), makeFramesItems()),
270 0 : makeMenu(TranslatableString("notation", "Text"), makeTextItems()),
271 0 : makeMenu(TranslatableString("notation", "Lines"), makeLinesItems()),
272 0 : makeMenu(TranslatableString("notation", "Chords and fretboard diagrams"), makeChordAndFretboardDiagramsItems()),
273 0 : };
274 :
275 0 : return items;
276 : }
277 :
278 0 : MenuItemList NoteInputBarModel::makeNotesItems()
279 : {
280 : MenuItemList items {
281 0 : makeMenuItem(AddNoteC_ToNextChordCommand::id),
282 0 : makeMenuItem(AddNoteD_ToNextChordCommand::id),
283 0 : makeMenuItem(AddNoteE_ToNextChordCommand::id),
284 0 : makeMenuItem(AddNoteF_ToNextChordCommand::id),
285 0 : makeMenuItem(AddNoteG_ToNextChordCommand::id),
286 0 : makeMenuItem(AddNoteA_ToNextChordCommand::id),
287 0 : makeMenuItem(AddNoteB_ToNextChordCommand::id),
288 0 : makeSeparator(),
289 0 : makeMenuItem(AddNoteC_ToCurrentChordCommand::id),
290 0 : makeMenuItem(AddNoteD_ToCurrentChordCommand::id),
291 0 : makeMenuItem(AddNoteE_ToCurrentChordCommand::id),
292 0 : makeMenuItem(AddNoteF_ToCurrentChordCommand::id),
293 0 : makeMenuItem(AddNoteG_ToCurrentChordCommand::id),
294 0 : makeMenuItem(AddNoteA_ToCurrentChordCommand::id),
295 0 : makeMenuItem(AddNoteB_ToCurrentChordCommand::id)
296 0 : };
297 :
298 0 : return items;
299 : }
300 :
301 0 : MenuItemList NoteInputBarModel::makeIntervalsItems()
302 : {
303 : MenuItemList items {
304 0 : makeMenuItem(AddInterval1Command::id),
305 0 : makeMenuItem(AddInterval2Command::id),
306 0 : makeMenuItem(AddInterval3Command::id),
307 0 : makeMenuItem(AddInterval4Command::id),
308 0 : makeMenuItem(AddInterval5Command::id),
309 0 : makeMenuItem(AddInterval6Command::id),
310 0 : makeMenuItem(AddInterval7Command::id),
311 0 : makeMenuItem(AddInterval8Command::id),
312 0 : makeMenuItem(AddInterval9Command::id),
313 0 : makeMenuItem(AddInterval10Command::id),
314 0 : makeSeparator(),
315 0 : makeMenuItem(AddIntervalMinus2Command::id),
316 0 : makeMenuItem(AddIntervalMinus3Command::id),
317 0 : makeMenuItem(AddIntervalMinus4Command::id),
318 0 : makeMenuItem(AddIntervalMinus5Command::id),
319 0 : makeMenuItem(AddIntervalMinus6Command::id),
320 0 : makeMenuItem(AddIntervalMinus7Command::id),
321 0 : makeMenuItem(AddIntervalMinus8Command::id),
322 0 : makeMenuItem(AddIntervalMinus9Command::id),
323 0 : makeMenuItem(AddIntervalMinus10Command::id)
324 0 : };
325 :
326 0 : return items;
327 : }
328 :
329 0 : MenuItemList NoteInputBarModel::makeMeasuresItems()
330 : {
331 : MenuItemList items {
332 0 : makeMenuItem(InsertMeasureCommand::id),
333 0 : makeMenuItem(AppendMeasureCommand::id),
334 0 : makeSeparator(),
335 0 : makeMenuItem(InsertMeasuresCommand::id),
336 0 : makeMenuItem(InsertMeasuresAfterSelectionCommand::id),
337 0 : makeSeparator(),
338 0 : makeMenuItem(InsertMeasuresAtStartOfScoreCommand::id),
339 0 : makeMenuItem(AppendMeasuresCommand::id)
340 0 : };
341 :
342 0 : return items;
343 : }
344 :
345 0 : MenuItemList NoteInputBarModel::makeFramesItems()
346 : {
347 : MenuItemList items {
348 0 : makeMenuItem(InsertHBoxCommand::id),
349 0 : makeMenuItem(InsertVBoxCommand::id),
350 0 : makeMenuItem(InsertTextFrameCommand::id),
351 0 : makeMenuItem(InsertFretFrameCommand::id),
352 0 : makeSeparator(),
353 0 : makeMenu(TranslatableString("notation", "Insert at end of score"), {
354 0 : makeMenuItem(AppendHBoxCommand::id),
355 0 : makeMenuItem(AppendVBoxCommand::id),
356 0 : makeMenuItem(AppendTextFrameCommand::id),
357 0 : makeMenuItem(AppendFretFrameCommand::id)
358 : })
359 0 : };
360 :
361 0 : return items;
362 : }
363 :
364 0 : MenuItemList NoteInputBarModel::makeTextItems()
365 : {
366 : MenuItemList items {
367 0 : makeMenuItem(AddTitleTextCommand::id),
368 0 : makeMenuItem(AddSubtitleTextCommand::id),
369 0 : makeMenuItem(AddComposerTextCommand::id),
370 0 : makeMenuItem(AddLyricistTextCommand::id),
371 0 : makeMenuItem(AddPartTextCommand::id),
372 0 : makeSeparator(),
373 0 : makeMenuItem(AddSystemTextCommand::id),
374 0 : makeMenuItem(AddStaffTextCommand::id),
375 0 : makeMenuItem(ADD_DYNAMIC_COMMAND),
376 0 : makeMenuItem(AddExpressionTextCommand::id),
377 0 : makeMenuItem(AddRehearsalMarkTextCommand::id),
378 0 : makeMenuItem(AddInstrumentChangeTextCommand::id),
379 0 : makeMenuItem(AddFingeringTextCommand::id),
380 0 : makeSeparator(),
381 0 : makeMenuItem(AddStickingTextCommand::id),
382 0 : makeMenuItem(AddChordTextCommand::id),
383 0 : makeMenuItem(AddRomanNumeralTextCommand::id),
384 0 : makeMenuItem(AddNashvilleNumberTextCommand::id),
385 0 : makeMenuItem(AddLyricsCommand::id),
386 0 : makeMenuItem(ADD_FIGURED_BASS_COMMAND),
387 0 : makeMenuItem(AddTempoCommand::id)
388 0 : };
389 :
390 0 : return items;
391 : }
392 :
393 0 : MenuItemList NoteInputBarModel::makeLinesItems()
394 : {
395 : MenuItemList items {
396 0 : makeMenuItem(ADD_SLUR_COMMAND),
397 0 : makeMenuItem(ADD_HAIRPIN_COMMAND),
398 0 : makeMenuItem(ADD_HAIRPIN_REVERSE_COMMAND),
399 0 : makeMenuItem(ADD_OTTAVA_8VA_COMMAND),
400 0 : makeMenuItem(ADD_OTTAVA_8VB_COMMAND),
401 0 : makeMenuItem(ADD_NOTELINE_COMMAND)
402 0 : };
403 :
404 0 : return items;
405 : }
406 :
407 0 : MenuItemList NoteInputBarModel::makeChordAndFretboardDiagramsItems()
408 : {
409 : MenuItemList items {
410 0 : makeMenuItem(AddChordTextCommand::id),
411 0 : makeMenuItem(ADD_FRETBOARD_DIAGRAM_COMMAND),
412 0 : makeSeparator(),
413 0 : makeMenuItem(InsertFretFrameCommand::id, TranslatableString("notation", "Fretboard diagram legend"))
414 0 : };
415 :
416 0 : return items;
417 : }
|