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 :
23 : #include "notationactioncontroller.h"
24 :
25 : #include "global/io/file.h"
26 : #include "global/translation.h"
27 : #include "global/types/ret.h"
28 :
29 : #include "engraving/dom/harmony.h"
30 : #include "engraving/dom/masterscore.h"
31 : #include "engraving/dom/note.h"
32 : #include "engraving/dom/chord.h"
33 : #include "engraving/dom/text.h"
34 : #include "engraving/dom/sig.h"
35 : #include "engraving/dom/tuplet.h"
36 : #include "engraving/editing/noteinput.h"
37 :
38 : #include "notation/imasternotation.h"
39 : #include "notation/inotation.h"
40 : #include "notation/inotationautomation.h" // IWYU pragma: keep
41 : #include "notation/inotationelements.h"
42 : #include "notation/inotationmidiinput.h"
43 : #include "notation/inotationnoteinput.h"
44 : #include "notation/inotationplayback.h" // IWYU pragma: keep
45 : #include "notation/inotationselection.h"
46 : #include "notation/inotationstyle.h"
47 : #include "notation/inotationundostack.h"
48 : #include "notation/inotationinteraction.h"
49 :
50 : #include "project/inotationproject.h"
51 :
52 : #include "qml/MuseScore/NotationScene/abstractelementpopupmodel.h"
53 : #include "qml/MuseScore/NotationScene/notationviewinputcontroller.h"
54 :
55 : #include "../notationcommands.h"
56 : #include "../notationcommandsenums.h"
57 : #include "playback/playbackcommands.h"
58 :
59 : #include "log.h"
60 :
61 : using namespace mu;
62 : using namespace muse;
63 : using namespace muse::io;
64 : using namespace muse::rcommand;
65 : using namespace mu::notation;
66 : using namespace mu::engraving;
67 :
68 : static constexpr qreal STRETCH_STEP = 0.1;
69 : static constexpr bool NEAR_NOTE_OR_REST = true;
70 :
71 : static constexpr bool DONT_PLAY_CHORD = false;
72 :
73 : static const muse::Uri NOTATION_REVIEW_PAGE_URI("musescore://notation/review");
74 :
75 : using EngravingDebuggingOptions = engraving::IEngravingConfiguration::DebuggingOptions;
76 : static const std::map<muse::rcommand::Command, bool EngravingDebuggingOptions::*> s_debuggingCommands = {
77 : { SHOW_ELEMENT_BOUNDING_RECTS_COMMAND, &EngravingDebuggingOptions::showElementBoundingRects },
78 : { COLOR_ELEMENT_SHAPES_COMMAND, &EngravingDebuggingOptions::colorElementShapes },
79 : { SHOW_SEGMENT_SHAPES_COMMAND, &EngravingDebuggingOptions::showSegmentShapes },
80 : { COLOR_SEGMENT_SHAPES_COMMAND, &EngravingDebuggingOptions::colorSegmentShapes },
81 : { SHOW_SKYLINES_COMMAND, &EngravingDebuggingOptions::showSkylines },
82 : { SHOW_SYSTEM_BOUNDING_RECTS_COMMAND, &EngravingDebuggingOptions::showSystemBoundingRects },
83 : { SHOW_ELEMENT_MASKS_COMMAND, &EngravingDebuggingOptions::showElementMasks },
84 : { SHOW_LINE_ATTACH_POINTS_COMMAND, &EngravingDebuggingOptions::showLineAttachPoints },
85 : { MARK_EMPTY_STAFF_COMMAND, &EngravingDebuggingOptions::markEmptyStaffVisibilityOverrides },
86 : { MARK_CORRUPTED_MEASURES_COMMAND, &EngravingDebuggingOptions::markCorruptedMeasures },
87 : { SHOW_GAP_RESTS_COMMAND, &EngravingDebuggingOptions::showGapRests },
88 : { SHOW_ORIGIN_AND_COMBINED_COMMAND, &EngravingDebuggingOptions::showOriginAndCombinedStaves },
89 : };
90 :
91 : //! NOTE Just for more readable
92 : using Controller = NotationActionController;
93 : using Interaction = INotationInteraction;
94 : using ViewController = INotationViewController;
95 :
96 0 : void NotationActionController::init()
97 : {
98 0 : TRACEFUNC;
99 :
100 : // global commands
101 0 : registerCommand<CancelCommand>(&Controller::resetState);
102 0 : registerCommand<UndoCommand>(&Controller::undo);
103 0 : registerCommand<RedoCommand>(&Controller::redo);
104 :
105 : // editing commands
106 0 : registerCommand<CopyCommand>(&Controller::copySelection);
107 0 : registerCommand<CopyPasteSwapCommand>(&Controller::copyPasteSwap);
108 0 : registerCommand<CopyLyricsCommand>(&Controller::copyLyrics);
109 0 : registerCommand<CutCommand>(&Controller::cutSelection);
110 0 : registerCommand<PasteCommand>(&Controller::pasteSelection);
111 0 : registerCommand<DeleteCommand>(&Controller::deleteSelection);
112 :
113 : // navigation and selection commands
114 :
115 0 : registerCommand<SelectCommand>(&Controller::select);
116 0 : registerCommand<AddToSelectionCommand>(&Controller::addToSelection);
117 0 : registerCommand<OpenSelectionOptionsCommand>(&Controller::openSelectionMoreOptions);
118 0 : registerCommand<ShowLocationCommand>(&Controller::showLocation);
119 :
120 : // note input commands
121 0 : registerCommand<ToggleNoteInputCommand>(&Controller::toggleNoteInput);
122 0 : registerCommand<ToggleInsertModeCommand>([this]() { return toggleNoteInputInsert(); });
123 :
124 0 : registerCommand<SetDurationCommand>(&Controller::setDuration);
125 0 : registerCommand<ChangeDurationCommand>(&Controller::changeDuration);
126 :
127 0 : registerCommand<ToggleDotsCommand>(&Controller::toggleDots);
128 0 : registerCommand<ToggleRestCommand>([this]() { return toggleRest(); });
129 :
130 0 : registerRequestCommand<RequestNoteInputStateCommand>(&Controller::requestNoteInputState);
131 :
132 0 : registerCommand<AddNoteCommand>(&Controller::addNote);
133 :
134 0 : registerCommand<ToggleAccidentalCommand>(&Controller::toggleAccidental);
135 :
136 0 : registerCommand<UseVoiceCommand>(&Controller::changeVoice);
137 0 : registerCommand<SwapVoicesCommand>(&Controller::swapVoices);
138 :
139 0 : registerCommand<AddTupletCommand>(&Controller::putTuplet);
140 0 : registerCommand<OpenTupletConfigureCommand>([this]() { return openTupletOtherDialog(); });
141 :
142 0 : registerCommand<AddBoxesCommand>(&Controller::addBoxes);
143 0 : registerCommand<AddGraceNoteCommand>(&Controller::addGraceNotes);
144 0 : registerCommand<AddIntervalCommand>(&Controller::addInterval);
145 0 : registerCommand<AddTextCommand>(&Controller::addText);
146 0 : registerCommand<AddBeamCommand>(&Controller::addBeam);
147 0 : registerCommand<AddOrnamentCommand>(&Controller::addOrnament);
148 0 : registerCommand<AddFretCommand>(&Controller::addFret);
149 :
150 0 : registerCommand<EditTextAdvanceCommand>(&Controller::editTextAdvance);
151 :
152 0 : registerCommand(ADD_BEAM_SELECTED_RANGE_COMMAND, &Interaction::beamSelectedRange);
153 :
154 : // text navigation commands
155 0 : registerCommand(EDITTEXT_NEXT_WORD_COMMAND, [this]() { nextWord(); });
156 0 : registerCommand(EDITTEXT_NEXT_ELEMENT_COMMAND, [this]() { nextTextElement(); });
157 0 : registerCommand(EDITTEXT_PREV_ELEMENT_COMMAND, [this]() { prevTextElement(); });
158 0 : registerCommand(EDITTEXT_NEXT_BEAT_COMMAND, &Controller::nextBeatTextElement, &Controller::textNavigationByBeatsAvailable);
159 0 : registerCommand(EDITTEXT_PREV_BEAT_COMMAND, &Controller::prevBeatTextElement, &Controller::textNavigationByBeatsAvailable);
160 :
161 : // lyrics editing commands
162 0 : registerCommand(EDITLYRIC_NEXT_VERSE_COMMAND, &Interaction::navigateToLyricsVerse, MoveDirection::Down);
163 0 : registerCommand(EDITLYRIC_PREV_VERSE_COMMAND, &Interaction::navigateToLyricsVerse, MoveDirection::Up);
164 0 : registerCommand(EDITLYRIC_NEXT_SYLLABLE_COMMAND, &Interaction::navigateToNextSyllable);
165 0 : registerCommand(EDITLYRIC_ADD_MELISMA_COMMAND, &Interaction::addMelisma);
166 0 : registerCommand(EDITLYRIC_ADD_VERSE_COMMAND, &Interaction::addLyricsVerse);
167 :
168 : // text editing commands
169 0 : registerCommand(EDITTEXT_TOGGLE_BOLD_COMMAND, &Interaction::toggleBold);
170 0 : registerCommand(EDITTEXT_TOGGLE_ITALIC_COMMAND, &Interaction::toggleItalic);
171 0 : registerCommand(EDITTEXT_TOGGLE_UNDERLINE_COMMAND, &Interaction::toggleUnderline);
172 0 : registerCommand(EDITTEXT_TOGGLE_STRIKE_COMMAND, &Interaction::toggleStrike);
173 0 : registerCommand(EDITTEXT_TOGGLE_SUBSCRIPT_COMMAND, &Interaction::toggleSubScript);
174 0 : registerCommand(EDITTEXT_TOGGLE_SUPERSCRIPT_COMMAND, &Interaction::toggleSuperScript);
175 :
176 0 : registerCommand(REALTIME_ADVANCE_COMMAND, &Controller::realtimeAdvance);
177 :
178 0 : registerCommand(EXTEND_TO_NEXT_NOTE_COMMAND, &Interaction::extendToNextNote);
179 :
180 0 : registerCommand(TOGGLE_TIE_COMMAND, &Controller::addTie);
181 0 : registerCommand(ADD_SLUR_COMMAND, &Controller::addSlur);
182 0 : registerCommand(TOGGLE_LV_COMMAND, &Controller::addLaissezVib);
183 0 : registerCommand(TOGGLE_MARCATO_COMMAND, [this]() { toggleArticulation(SymbolId::articMarcatoAbove); });
184 0 : registerCommand(TOGGLE_SFORZATO_COMMAND, [this]() { toggleArticulation(SymbolId::articAccentAbove); });
185 0 : registerCommand(TOGGLE_TENUTO_COMMAND, [this]() { toggleArticulation(SymbolId::articTenutoAbove); });
186 0 : registerCommand(TOGGLE_STACCATO_COMMAND, [this]() { toggleArticulation(SymbolId::articStaccatoAbove); });
187 :
188 0 : registerCommand<FlipCommand>(&Controller::flipSelection);
189 0 : registerCommand(FLIP_HORIZONTALLY_COMMAND, &Interaction::flipSelectionHorizontally);
190 :
191 0 : registerCommandWithParams(ADD_DRUM_NOTE_COMMAND, &Controller::addDrumNote);
192 :
193 0 : registerCommand(ENTER_REST_COMMAND, &Interaction::putRestToSelection);
194 :
195 0 : registerCommand(ADD_FRETBOARD_DIAGRAM_COMMAND, &Controller::addFretboardDiagram);
196 :
197 0 : registerCommand(ADD_OTTAVA_8VA_COMMAND, &Interaction::addOttavaToSelection, OttavaType::OTTAVA_8VA);
198 0 : registerCommand(ADD_OTTAVA_8VB_COMMAND, &Interaction::addOttavaToSelection, OttavaType::OTTAVA_8VB);
199 :
200 0 : registerCommand(ADD_DYNAMIC_COMMAND, &Interaction::toggleDynamicPopup);
201 0 : registerCommand(ADD_HAIRPIN_COMMAND, &Interaction::addHairpinsToSelection, HairpinType::CRESC_HAIRPIN);
202 0 : registerCommand(ADD_HAIRPIN_REVERSE_COMMAND, &Interaction::addHairpinsToSelection, HairpinType::DIM_HAIRPIN);
203 0 : registerCommand(INCREASE_DYNAMIC_COMMAND, &Interaction::increaseDecreaseSelectedDynamicsValues, /*delta*/ 1);
204 0 : registerCommand(DECREASE_DYNAMIC_COMMAND, &Interaction::increaseDecreaseSelectedDynamicsValues, /*delta*/ -1);
205 0 : registerCommand(ADD_NOTELINE_COMMAND, &Interaction::addAnchoredLineToSelectedNotes);
206 :
207 0 : registerCommand(ADD_IMAGE_COMMAND, [this]() { addImage(); });
208 :
209 0 : registerCommand(ADD_UP_BOW_COMMAND, &Interaction::toggleArticulation, mu::engraving::SymId::stringsUpBow);
210 0 : registerCommand(ADD_DOWN_BOW_COMMAND, &Interaction::toggleArticulation, mu::engraving::SymId::stringsDownBow);
211 :
212 0 : registerCommand(ADD_FIGURED_BASS_COMMAND, [this]() { addFiguredBass(); });
213 :
214 : // add brackets commands
215 0 : registerCommand(ADD_BRACKETS_COMMAND, &Interaction::addBracketsToSelection, BracketsType::Brackets);
216 0 : registerCommand(ADD_BRACES_COMMAND, &Interaction::addBracketsToSelection, BracketsType::Braces);
217 0 : registerCommand(ADD_PARENTHESES_COMMAND, &Interaction::addBracketsToSelection, BracketsType::Parentheses);
218 :
219 : // clef commands
220 0 : registerCommand(ADD_CLEF_VIOLIN_COMMAND, [this]() { insertClef(mu::engraving::ClefType::G); });
221 0 : registerCommand(ADD_CLEF_BASS_COMMAND, [this]() { insertClef(mu::engraving::ClefType::F); });
222 :
223 0 : registerCommand(ADD_FULL_MEASURE_REST_COMMAND, &Controller::addFullMeasureRest);
224 :
225 : // move commands
226 0 : registerCommand(MOVE_RIGHT_COMMAND, [this]() { move(MoveDirection::Right, false); });
227 0 : registerCommand(MOVE_LEFT_COMMAND, [this]() { move(MoveDirection::Left, false); });
228 0 : registerCommand(MOVE_RIGHT_QUICKLY_COMMAND, [this]() { move(MoveDirection::Right, true); });
229 0 : registerCommand(MOVE_LEFT_QUICKLY_COMMAND, [this]() { move(MoveDirection::Left, true); });
230 :
231 0 : registerCommand(MOVE_UP_COMMAND, &Interaction::moveChordRestToStaff, MoveDirection::Up);
232 0 : registerCommand(MOVE_DOWN_COMMAND, &Interaction::moveChordRestToStaff, MoveDirection::Down);
233 0 : registerCommand(SWAP_LEFT_COMMAND, &Interaction::swapChordRest, MoveDirection::Left);
234 0 : registerCommand(SWAP_RIGHT_COMMAND, &Interaction::swapChordRest, MoveDirection::Right);
235 :
236 0 : registerCommand(PITCH_UP_COMMAND, [this]() { move(MoveDirection::Up, false); });
237 0 : registerCommand(PITCH_DOWN_COMMAND, [this]() { move(MoveDirection::Down, false); });
238 0 : registerCommand(PITCH_UP_OCTAVE_COMMAND, [this]() { move(MoveDirection::Up, true); });
239 0 : registerCommand(PITCH_DOWN_OCTAVE_COMMAND, [this]() { move(MoveDirection::Down, true); });
240 0 : registerCommand(PITCH_UP_DIATONIC_COMMAND, [this]() { movePitchDiatonic(MoveDirection::Up, false); });
241 0 : registerCommand(PITCH_DOWN_DIATONIC_COMMAND, [this]() { movePitchDiatonic(MoveDirection::Down, false); });
242 :
243 0 : registerCommand(PITCH_UP_DIATONIC_ALTERATIONS_COMMAND, &Interaction::transposeDiatonicAlterations,
244 : mu::engraving::TransposeDirection::UP,
245 : PlayMode::PlayNote);
246 0 : registerCommand(PITCH_DOWN_DIATONIC_ALTERATIONS_COMMAND, &Interaction::transposeDiatonicAlterations,
247 : mu::engraving::TransposeDirection::DOWN,
248 : PlayMode::PlayNote);
249 :
250 : // properties commands
251 0 : registerCommand(TOGGLE_VISIBLE_COMMAND, &Interaction::toggleVisible);
252 :
253 : // snap commands
254 0 : registerCommand(TOGGLE_SNAP_TO_PREV_COMMAND, &Interaction::toggleSnapToPrevious);
255 0 : registerCommand(TOGGLE_SNAP_TO_NEXT_COMMAND, &Interaction::toggleSnapToNext);
256 :
257 : // layout commands
258 0 : registerCommand(TOGGLE_SYSTEM_BREAK_COMMAND, &Interaction::toggleLayoutBreak, LayoutBreakType::LINE);
259 0 : registerCommand(TOGGLE_PAGE_BREAK_COMMAND, &Interaction::toggleLayoutBreak, LayoutBreakType::PAGE);
260 0 : registerCommand(TOGGLE_SECTION_BREAK_COMMAND, &Interaction::toggleLayoutBreak, LayoutBreakType::SECTION);
261 :
262 0 : registerCommand(APPLY_SYSTEM_LOCK_COMMAND, &Interaction::applySystemLock);
263 0 : registerCommand(TOGGLE_SYSTEM_LOCK_COMMAND, &Interaction::toggleSystemLock);
264 0 : registerCommand(APPLY_PAGE_LOCK_COMMAND, &Interaction::applyPageLock);
265 0 : registerCommand(TOGGLE_PAGE_LOCK_COMMAND, &Interaction::togglePageLock);
266 0 : registerCommand(TOGGLE_SCORE_LOCK_COMMAND, &Interaction::toggleScoreLock);
267 :
268 0 : registerCommand(MAKE_INTO_SYSTEM_COMMAND, &Interaction::makeIntoSystem);
269 0 : registerCommand(MAKE_INTO_PAGE_COMMAND, &Interaction::makeIntoPage);
270 :
271 0 : registerCommand(MOVE_MEASURE_TO_PREV_SYSTEM_COMMAND, &Interaction::moveMeasureToPrevSystem);
272 0 : registerCommand(MOVE_MEASURE_TO_NEXT_SYSTEM_COMMAND, &Interaction::moveMeasureToNextSystem);
273 0 : registerCommand(MOVE_SYSTEM_TO_PREV_PAGE_COMMAND, &Interaction::moveSystemToPrevPage);
274 0 : registerCommand(MOVE_SYSTEM_TO_NEXT_PAGE_COMMAND, &Interaction::moveSystemToNextPage);
275 :
276 0 : registerCommand(SPLIT_MEASURE_COMMAND, &Interaction::splitSelectedMeasure);
277 0 : registerCommand(JOIN_MEASURES_COMMAND, &Interaction::joinSelectedMeasures);
278 0 : registerCommand(STRETCH_DECREASE_COMMAND, [this]() { addStretch(-STRETCH_STEP); });
279 0 : registerCommand(STRETCH_INCREASE_COMMAND, [this]() { addStretch(STRETCH_STEP); });
280 0 : registerCommand(STRETCH_RESET_COMMAND, &Controller::resetStretch);
281 :
282 : // open properties
283 0 : registerCommand(OPEN_PAGE_SETTINGS_COMMAND, &Controller::openPageSettingsDialog);
284 0 : registerCommand(OPEN_STAFF_PROPERTIES_COMMAND, &Controller::openStaffProperties);
285 0 : registerCommand(OPEN_EDIT_STRINGS_COMMAND, &Controller::openEditStringsDialog);
286 0 : registerCommand(OPEN_BREAKS_COMMAND, &Controller::openBreaksDialog);
287 0 : registerCommand(OPEN_STAFF_TEXT_PROPERTIES_COMMAND, &Controller::openStaffTextPropertiesDialog);
288 0 : registerCommand(OPEN_SYSTEM_TEXT_PROPERTIES_COMMAND, &Controller::openStaffTextPropertiesDialog);
289 0 : registerCommand(OPEN_MEASURE_PROPERTIES_COMMAND, &Controller::openMeasurePropertiesDialog);
290 0 : registerCommand(OPEN_TRANSPOSE_COMMAND, &Controller::openTransposeDialog);
291 0 : registerCommand(OPEN_PARTS_COMMAND, &Controller::openPartsDialog);
292 0 : registerCommand(OPEN_EDITGRIDSIZE_COMMAND, &Controller::openEditGridSizeDialog);
293 0 : registerCommand(OPEN_REALIZECHORDSYMBOLS_COMMAND, &Controller::openRealizeChordSymbolsDialog);
294 :
295 : // style commands
296 0 : registerCommand(LOAD_STYLE_COMMAND, &Controller::loadStyle);
297 0 : registerCommand(SAVE_STYLE_COMMAND, &Controller::saveStyle);
298 0 : registerCommandWithParams(OPEN_EDIT_STYLE_COMMAND, &Controller::openEditStyleDialog);
299 0 : registerCommand(TOGGLE_CONCERT_PITCH_COMMAND, &Controller::toggleConcertPitch);
300 :
301 : // reset commands
302 0 : registerCommand(RESET_TEXT_STYLE_OVERRIDES_COMMAND, &Interaction::resetTextStyleOverrides);
303 0 : registerCommand(RESET_BEAMS_COMMAND, &Controller::resetBeamMode);
304 0 : registerCommand(RESET_SHAPES_AND_POSITIONS_COMMAND, &Interaction::resetShapesAndPosition);
305 0 : registerCommand(RESET_TO_DEFAULT_LAYOUT_COMMAND, &Interaction::resetToDefaultLayout);
306 :
307 : // appearance commands
308 0 : registerCommand(FREEZE_CURRENT_PLACEMENT_COMMAND, &Interaction::freezeSelectionPosition);
309 :
310 : // show commands
311 0 : registerCommand(SHOW_INVISIBLE_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::ShowInvisibleElements); });
312 0 : registerCommand(SHOW_UNPRINTABLE_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::ShowUnprintableElements); });
313 0 : registerCommand(SHOW_FRAMES_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::ShowFrames); });
314 0 : registerCommand(SHOW_PAGEBORDERS_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::ShowPageMargins); });
315 0 : registerCommand(SHOW_SOUNDFLAGS_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::ShowSoundFlags); });
316 0 : registerCommand(SHOW_IRREGULAR_COMMAND, [this]() { toggleScoreConfig(ScoreConfigType::MarkIrregularMeasures); });
317 :
318 : // staff commands
319 0 : registerCommand(STAFF_EXPLODE_COMMAND, &Interaction::explodeSelectedStaff);
320 0 : registerCommand(STAFF_IMPLODE_COMMAND, &Interaction::implodeSelectedStaff);
321 :
322 : // remove commands
323 0 : registerCommand(REMOVE_SELECTED_RANGE_COMMAND, &Interaction::removeSelectedRange);
324 0 : registerCommand(REMOVE_EMPTY_TRAILING_MEASURES_COMMAND, &Interaction::removeEmptyTrailingMeasures);
325 :
326 : // slash commands
327 0 : registerCommand(SLASH_FILL_COMMAND, &Interaction::fillSelectionWithSlashes);
328 0 : registerCommand(SLASH_RHYTHM_COMMAND, &Interaction::replaceSelectedNotesWithSlashes);
329 :
330 : // spelling commands
331 0 : registerCommand(PITCH_SPELL_COMMAND, &Interaction::spellPitches);
332 0 : registerCommand(PITCH_SPELL_SHARPS_COMMAND, &Interaction::spellPitchesWithSharps);
333 0 : registerCommand(PITCH_SPELL_FLATS_COMMAND, &Interaction::spellPitchesWithFlats);
334 0 : registerCommand(ENHARMONIC_SPELL_BOTH_COMMAND, &Interaction::changeEnharmonicSpelling, true);
335 0 : registerCommand(ENHARMONIC_SPELL_CURRENT_COMMAND, &Interaction::changeEnharmonicSpelling, false);
336 :
337 : // screen commands
338 0 : registerCommandWithParams(SCREEN_PUT_NOTE_COMMAND, &Controller::putNote);
339 0 : registerCommandWithParams(SCREEN_REMOVE_NOTE_COMMAND, &Controller::removeNote);
340 0 : registerCommandWithParams(SCREEN_EDIT_TEXT_COMMAND, &Controller::startEditSelectedText);
341 0 : registerCommandWithParams(SCREEN_EDIT_ELEMENT_COMMAND, &Controller::startEditSelectedElement);
342 :
343 0 : registerCommandWithParams(EDIT_ELEMENT_COMMAND, &Controller::startEditSelectedElement); // same as screen edit element command, no params
344 :
345 : // others commands
346 0 : registerCommand(REGROUP_RHYTHMS_COMMAND, &Interaction::regroupNotesAndRests);
347 0 : registerCommand(RESEQUENCE_REHEARSAL_MARKS_COMMAND, &Interaction::resequenceRehearsalMarks);
348 0 : registerCommand(UNROLL_REPEATS_COMMAND, &Controller::unrollRepeats);
349 0 : registerCommand(REPEAT_SELECTION_COMMAND, &Controller::repeatSelection);
350 0 : registerCommand(TRANSPOSE_UP_COMMAND, &Interaction::transposeSemitone, 1, PlayMode::PlayNote);
351 0 : registerCommand(TRANSPOSE_DOWN_COMMAND, &Interaction::transposeSemitone, -1, PlayMode::PlayNote);
352 0 : registerCommand(TOGGLE_MMREST_COMMAND, &Controller::toggleMmrest);
353 0 : registerCommand(TOGGLE_HIDE_EMPTY_COMMAND, &Controller::toggleHideEmpty);
354 0 : registerCommand(MIRROR_NOTEHEAD_COMMAND, &Interaction::mirrorNotes);
355 0 : registerCommand(SET_VISIBLE_COMMAND, &Interaction::setSelectionVisible, true);
356 0 : registerCommand(UNSET_VISIBLE_COMMAND, &Interaction::setSelectionVisible, false);
357 0 : registerCommand(TOGGLE_AUTOPLACE_COMMAND, &Interaction::toggleAutoplace, false);
358 0 : registerCommand(AUTOPLACE_ENABLED_COMMAND, &Interaction::toggleAutoplace, true);
359 :
360 0 : registerCommand(VOICE_ASSIGNMENT_ALL_IN_INSTR_COMMAND, &Interaction::changeSelectedElementsVoiceAssignment,
361 : VoiceAssignment::ALL_VOICE_IN_INSTRUMENT);
362 0 : registerCommand(VOICE_ASSIGNMENT_ALL_IN_STAFF_COMMAND, &Interaction::changeSelectedElementsVoiceAssignment,
363 : VoiceAssignment::ALL_VOICE_IN_STAFF);
364 :
365 0 : registerCommand(TOGGLE_AUTOMATION_COMMAND, &Controller::toggleAutomation);
366 0 : registerCommandWithParams(SELECT_AUTOMATION_TYPE_COMMAND, &Controller::selectAutomationType);
367 :
368 : // TAB
369 0 : registerCommand(SET_DURATION_WHOLE_TAB_COMMAND, [this]() { setDuration(DurationType::V_WHOLE); });
370 0 : registerCommand(SET_DURATION_HALF_TAB_COMMAND, [this]() { setDuration(DurationType::V_HALF); });
371 0 : registerCommand(SET_DURATION_QUARTER_TAB_COMMAND, [this]() { setDuration(DurationType::V_QUARTER); });
372 0 : registerCommand(SET_DURATION_EIGHTH_TAB_COMMAND, [this]() { setDuration(DurationType::V_EIGHTH); });
373 0 : registerCommand(SET_DURATION_16TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_16TH); });
374 0 : registerCommand(SET_DURATION_32ND_TAB_COMMAND, [this]() { setDuration(DurationType::V_32ND); });
375 0 : registerCommand(SET_DURATION_64TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_64TH); });
376 0 : registerCommand(SET_DURATION_128TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_128TH); });
377 0 : registerCommand(SET_DURATION_256TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_256TH); });
378 0 : registerCommand(SET_DURATION_512TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_512TH); });
379 0 : registerCommand(SET_DURATION_1024TH_TAB_COMMAND, [this]() { setDuration(DurationType::V_1024TH); });
380 :
381 0 : registerCommand(ENTER_REST_TAB_COMMAND, &Interaction::putRestToSelection);
382 :
383 0 : registerCommand(ADD_STANDARD_BEND_COMMAND, [this]() { addGuitarBend(GuitarBendType::BEND); });
384 0 : registerCommand(ADD_PRE_BEND_COMMAND, [this]() { addGuitarBend(GuitarBendType::PRE_BEND); });
385 0 : registerCommand(ADD_GRACE_NOTE_BEND_COMMAND, [this]() { addGuitarBend(GuitarBendType::GRACE_NOTE_BEND); });
386 0 : registerCommand(ADD_SLIGHT_BEND_COMMAND, [this]() { addGuitarBend(GuitarBendType::SLIGHT_BEND); });
387 0 : registerCommand(ADD_DIVE_COMMAND, [this]() { addGuitarBend(GuitarBendType::DIVE); });
388 0 : registerCommand(ADD_PRE_DIVE_COMMAND, [this]() { addGuitarBend(GuitarBendType::PRE_DIVE); });
389 0 : registerCommand(ADD_DIP_COMMAND, [this]() { addGuitarBend(GuitarBendType::DIP); });
390 0 : registerCommand(ADD_SCOOP_COMMAND, [this]() { addGuitarBend(GuitarBendType::SCOOP); });
391 :
392 0 : registerCommand(ADD_HAMMER_ON_PULL_OFF_COMMAND, [this]() { addHammerOnPullOff(); });
393 :
394 0 : registerCommand(GOTO_STRING_ABOVE_COMMAND, [this]() { move(MoveDirection::Up, false); });
395 0 : registerCommand(GOTO_STRING_BELOW_COMMAND, [this]() { move(MoveDirection::Down, false); });
396 :
397 : // view commands
398 0 : registerViewCommand(ZOOM_IN_COMMAND, &ViewController::zoomIn);
399 0 : registerViewCommand(ZOOM_OUT_COMMAND, &ViewController::zoomOut);
400 0 : registerViewCommand(ZOOM_TO_PAGE_WIDTH_COMMAND, &ViewController::zoomToPageWidth);
401 0 : registerViewCommand(ZOOM_TO_WHOLE_PAGE_COMMAND, &ViewController::zoomToWholePage);
402 0 : registerViewCommand(ZOOM_TO_TWO_PAGES_COMMAND, &ViewController::zoomToTwoPages);
403 0 : registerViewCommand(ZOOM_TO_100_COMMAND, &ViewController::setZoom, 100);
404 0 : registerCommandWithParams(ZOOM_TO_PERCENT_COMMAND, &Controller::zoomToPercent);
405 :
406 0 : registerViewCommand(VIEW_MODE_PAGE_COMMAND, &ViewController::setViewMode, ViewMode::PAGE);
407 0 : registerViewCommand(VIEW_MODE_FLOAT_COMMAND, &ViewController::setViewMode, ViewMode::FLOAT);
408 0 : registerViewCommand(VIEW_MODE_CONTINUOUS_COMMAND, &ViewController::setViewMode, ViewMode::LINE);
409 0 : registerViewCommand(VIEW_MODE_SINGLE_COMMAND, &ViewController::setViewMode, ViewMode::SYSTEM);
410 :
411 0 : registerViewCommand(NEXT_SCREEN_COMMAND, &ViewController::nextScreen);
412 0 : registerViewCommand(PREV_SCREEN_COMMAND, &ViewController::previousScreen);
413 0 : registerViewCommand(NEXT_PAGE_COMMAND, &ViewController::nextPage);
414 0 : registerViewCommand(PREV_PAGE_COMMAND, &ViewController::previousPage);
415 0 : registerViewCommand(TOP_OF_FIRST_PAGE_COMMAND, &ViewController::startOfScore);
416 0 : registerViewCommand(BOTTOM_OF_LAST_PAGE_COMMAND, &ViewController::endOfScore);
417 :
418 0 : registerViewCommand(CONTEXT_MENU_OF_SELECTION_COMMAND, &ViewController::openContextMenuOfSelection);
419 :
420 0 : registerViewCommand(SHOW_SEARCH_COMMAND, &ViewController::showSearch);
421 :
422 0 : registerCommandWithParams(PIANO_KEYBOARD_SET_NUMBER_OF_KEYS_COMMAND, &Controller::setPianoKeyboardNumberOfKeys);
423 :
424 : // diagnostic commands
425 0 : registerViewCommand(DIAGNOSTIC_VIEW_REDRAW_COMMAND, &ViewController::redrawView);
426 :
427 0 : registerCommand(CHECK_FOR_SCORE_CORRUPTIONS_COMMAND, &Controller::checkForScoreCorruptions);
428 :
429 : // debugging commands
430 0 : for (auto& [command, member] : s_debuggingCommands) {
431 0 : registerCommand(command, [this, member = member]() {
432 0 : EngravingDebuggingOptions options = engravingConfiguration()->debuggingOptions();
433 0 : options.*member = !(options.*member);
434 0 : engravingConfiguration()->setDebuggingOptions(options);
435 0 : });
436 : }
437 :
438 : // listen on state changes
439 0 : projectSession()->currentNotationChanged().onNotify(this, [this]() {
440 0 : auto notation = projectSession()->currentNotation();
441 0 : if (notation) {
442 0 : auto interaction = notation->interaction();
443 :
444 0 : interaction->selectionChanged().onNotify(this, [this]() {
445 0 : m_selectionChanged.notify();
446 0 : }, Asyncable::Mode::SetReplace);
447 :
448 0 : interaction->noteInput()->stateChanged().onNotify(this, [this]() {
449 0 : m_noteInputStateChanged.notify();
450 0 : }, Asyncable::Mode::SetReplace);
451 :
452 0 : interaction->textEditingStarted().onNotify(this, [this]() {
453 0 : m_textEditingChanged.send(true);
454 0 : }, Asyncable::Mode::SetReplace);
455 0 : interaction->textEditingEnded().onReceive(this, [this](TextBase*) {
456 0 : m_textEditingChanged.send(false);
457 0 : }, Asyncable::Mode::SetReplace);
458 :
459 0 : interaction->scoreConfigChanged().onReceive(this, [this](ScoreConfigType configType) {
460 0 : m_scoreConfigChanged.send(configType);
461 0 : }, Asyncable::Mode::SetReplace);
462 :
463 0 : auto undoStack = notation->undoStack();
464 0 : undoStack->stackChanged().onNotify(this, [this]() {
465 0 : m_stackChanged.notify();
466 0 : }, Asyncable::Mode::SetReplace);
467 :
468 0 : notation->style()->styleChanged().onNotify(this, [this]() {
469 0 : m_currentNotationStyleChanged.notify();
470 0 : }, Asyncable::Mode::SetReplace);
471 :
472 0 : if (const IMasterNotationPtr masterNotation = notation->masterNotation()) {
473 0 : masterNotation->automation()->automationModeEnabledChanged().onNotify(this, [this]() {
474 0 : m_automationModeEnabledChanged.notify();
475 0 : }, Asyncable::Mode::SetReplace);
476 0 : }
477 0 : }
478 :
479 0 : m_textEditingChanged.send(isTextEditing());
480 0 : m_noteInputStateChanged.notify();
481 0 : m_currentNotationStyleChanged.notify();
482 0 : });
483 :
484 0 : playbackController()->isPlayingChanged().onReceive(this, [this](bool) {
485 0 : m_isNoteInputAllowedChanged.send(isNoteInputAllowed());
486 0 : }, Asyncable::Mode::SetReplace);
487 0 : }
488 :
489 0 : void NotationActionController::setViewController(INotationViewController* controller)
490 : {
491 0 : m_viewController = controller;
492 0 : LOGD() << "view controller changed";
493 0 : }
494 :
495 0 : INotationViewController* NotationActionController::viewController() const
496 : {
497 0 : return m_viewController;
498 : }
499 :
500 0 : INotationPtr NotationActionController::currentNotation() const
501 : {
502 0 : return projectSession()->currentNotation();
503 : }
504 :
505 0 : IMasterNotationPtr NotationActionController::currentMasterNotation() const
506 : {
507 0 : return projectSession()->masterNotation();
508 : }
509 :
510 0 : INotationInteractionPtr NotationActionController::currentNotationInteraction() const
511 : {
512 0 : return currentNotation() ? currentNotation()->interaction() : nullptr;
513 : }
514 :
515 0 : INotationSelectionPtr NotationActionController::currentNotationSelection() const
516 : {
517 0 : return currentNotationInteraction() ? currentNotationInteraction()->selection() : nullptr;
518 : }
519 :
520 0 : INotationElementsPtr NotationActionController::currentNotationElements() const
521 : {
522 0 : return currentNotation() ? currentNotation()->elements() : nullptr;
523 : }
524 :
525 0 : muse::async::Notification NotationActionController::currentNotationChanged() const
526 : {
527 0 : return projectSession()->currentNotationChanged();
528 : }
529 :
530 0 : muse::async::Notification NotationActionController::currentMasterNotationChanged() const
531 : {
532 0 : return projectSession()->masterNotationChanged();
533 : }
534 :
535 0 : INotationNoteInputPtr NotationActionController::currentNotationNoteInput() const
536 : {
537 0 : auto interaction = currentNotationInteraction();
538 0 : if (!interaction) {
539 0 : return nullptr;
540 : }
541 :
542 0 : return interaction->noteInput();
543 0 : }
544 :
545 0 : INotationUndoStackPtr NotationActionController::currentNotationUndoStack() const
546 : {
547 0 : auto notation = currentNotation();
548 0 : if (!notation) {
549 0 : return nullptr;
550 : }
551 :
552 0 : return notation->undoStack();
553 0 : }
554 :
555 0 : INotationMidiInputPtr NotationActionController::currentNotationMidiInput() const
556 : {
557 0 : auto notation = currentNotation();
558 0 : if (!notation) {
559 0 : return nullptr;
560 : }
561 :
562 0 : return notation->midiInput();
563 0 : }
564 :
565 0 : mu::engraving::Score* NotationActionController::currentNotationScore() const
566 : {
567 0 : return currentNotationElements() ? currentNotationElements()->msScore() : nullptr;
568 : }
569 :
570 0 : INotationStylePtr NotationActionController::notationStyle() const
571 : {
572 0 : auto notation = currentNotation();
573 0 : if (!notation) {
574 0 : return nullptr;
575 : }
576 :
577 0 : return notation->style();
578 0 : }
579 :
580 0 : muse::async::Notification NotationActionController::notationStyleChanged() const
581 : {
582 0 : return m_currentNotationStyleChanged;
583 : }
584 :
585 0 : muse::Ret NotationActionController::resetState()
586 : {
587 0 : TRACEFUNC;
588 :
589 0 : if (playbackController()->isPlaying()) {
590 0 : commandDispatcher()->dispatch(playback::STOP_COMMAND);
591 : }
592 :
593 0 : auto noteInput = currentNotationNoteInput();
594 0 : if (!noteInput) {
595 0 : return muse::make_ret(muse::Ret::Code::InternalError);
596 : }
597 :
598 0 : if (noteInput->isNoteInputMode()) {
599 0 : noteInput->endNoteInput();
600 0 : return muse::make_ok();
601 : }
602 :
603 0 : auto interaction = currentNotationInteraction();
604 0 : if (!interaction) {
605 0 : return muse::make_ret(muse::Ret::Code::InternalError);
606 : }
607 :
608 0 : if (interaction->isGripEditStarted()) {
609 : // Exit grip edit but leave element selected
610 0 : interaction->endEditElement();
611 0 : return muse::make_ok();
612 : }
613 :
614 0 : if (interaction->isTextEditingStarted()) {
615 0 : interaction->endEditElement();
616 0 : return muse::make_ok();
617 0 : } else if (interaction->isEditingElement()) {
618 0 : interaction->endEditElement();
619 : }
620 :
621 0 : if (!interaction->selection()->isNone()) {
622 0 : interaction->clearSelection();
623 : }
624 :
625 0 : if (automation()) {
626 0 : automation()->resetEditStateRequested().notify();
627 : }
628 :
629 0 : return muse::make_ok();
630 0 : }
631 :
632 0 : muse::Ret NotationActionController::undo()
633 : {
634 0 : auto interaction = currentNotationInteraction();
635 0 : if (!interaction) {
636 0 : return muse::make_ret(muse::Ret::Code::InternalError);
637 : }
638 :
639 0 : interaction->undo();
640 :
641 0 : seekSelectedElement();
642 :
643 0 : return muse::make_ok();
644 0 : }
645 :
646 0 : muse::Ret NotationActionController::redo()
647 : {
648 0 : auto interaction = currentNotationInteraction();
649 0 : if (!interaction) {
650 0 : return muse::make_ret(muse::Ret::Code::InternalError);
651 : }
652 :
653 0 : interaction->redo();
654 :
655 0 : seekSelectedElement();
656 :
657 0 : return muse::make_ok();
658 0 : }
659 :
660 0 : muse::Ret NotationActionController::copySelection()
661 : {
662 0 : auto interaction = currentNotationInteraction();
663 0 : if (!interaction) {
664 0 : return muse::make_ret(Ret::Code::InternalError);
665 : }
666 :
667 0 : interaction->copySelection();
668 0 : return muse::make_ok();
669 0 : }
670 :
671 0 : muse::Ret NotationActionController::select(const SelectCommand& command)
672 : {
673 0 : return select(command.target, command.playMode);
674 : }
675 :
676 0 : muse::Ret NotationActionController::select(SelectionTarget target, PlayMode playMode)
677 : {
678 0 : auto interaction = currentNotationInteraction();
679 0 : if (!interaction) {
680 0 : return muse::make_ret(Ret::Code::InternalError);
681 : }
682 :
683 0 : Ret ret = interaction->select(target);
684 0 : if (!ret) {
685 0 : return ret;
686 : }
687 :
688 0 : seekSelectedElement();
689 0 : if (playMode != PlayMode::NoPlay) {
690 0 : playSelectedElement(playMode == PlayMode::PlayChord);
691 : }
692 :
693 0 : return muse::make_ok();
694 0 : }
695 :
696 0 : muse::Ret NotationActionController::addToSelection(const AddToSelectionCommand& command)
697 : {
698 0 : return addToSelection(command.target);
699 : }
700 :
701 0 : muse::Ret NotationActionController::addToSelection(SelectionTarget target)
702 : {
703 0 : auto interaction = currentNotationInteraction();
704 0 : if (!interaction) {
705 0 : return muse::make_ret(Ret::Code::InternalError);
706 : }
707 :
708 0 : return interaction->addToSelection(target);
709 0 : }
710 :
711 0 : bool NotationActionController::isNoteInputAllowed() const
712 : {
713 0 : return !playbackController()->isPlaying();
714 : }
715 :
716 0 : muse::async::Channel<bool> NotationActionController::isNoteInputAllowedChanged() const
717 : {
718 0 : return m_isNoteInputAllowedChanged;
719 : }
720 :
721 0 : muse::async::Notification NotationActionController::noteInputStateChanged() const
722 : {
723 0 : return m_noteInputStateChanged;
724 : }
725 :
726 0 : bool NotationActionController::isNoteInputMode() const
727 : {
728 0 : auto noteInput = currentNotationNoteInput();
729 0 : if (!noteInput) {
730 0 : return false;
731 : }
732 :
733 0 : return noteInput->isNoteInputMode();
734 0 : }
735 :
736 0 : NoteInputMethod NotationActionController::noteInputMethod() const
737 : {
738 0 : auto noteInput = currentNotationNoteInput();
739 0 : if (!noteInput) {
740 0 : return NoteInputMethod::UNKNOWN;
741 : }
742 :
743 0 : return noteInput->state().noteEntryMethod();
744 0 : }
745 :
746 0 : muse::Ret NotationActionController::toggleNoteInput(const ToggleNoteInputCommand& command)
747 : {
748 0 : return toggleNoteInput(command.method);
749 : }
750 :
751 0 : muse::Ret NotationActionController::toggleNoteInput(NoteInputMethod method)
752 : {
753 0 : TRACEFUNC;
754 :
755 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
756 0 : if (!noteInput) {
757 0 : return muse::make_ret(Ret::Code::InternalError);
758 : }
759 :
760 0 : if (method == NoteInputMethod::DEFAULT) {
761 0 : method = configuration()->defaultNoteInputMethod();
762 : }
763 :
764 0 : if (!noteInput->isNoteInputMode()) {
765 0 : noteInput->startNoteInput(method);
766 0 : } else if (noteInput->usingNoteInputMethod(method)) {
767 0 : noteInput->endNoteInput();
768 : } else {
769 0 : noteInput->setNoteInputMethod(method);
770 : }
771 :
772 0 : return muse::make_ok();
773 0 : }
774 :
775 0 : muse::Ret NotationActionController::toggleNoteInputInsert()
776 : {
777 0 : if (!currentNotationNoteInput()->usingNoteInputMethod(NoteInputMethod::TIMEWISE)) {
778 0 : toggleNoteInput(NoteInputMethod::TIMEWISE);
779 : } else {
780 0 : toggleNoteInput(NoteInputMethod::BY_NOTE_NAME);
781 : }
782 0 : return muse::make_ok();
783 : }
784 :
785 0 : muse::RetVal<RequestNoteInputStateCommand::Output> NotationActionController::requestNoteInputState() const
786 : {
787 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
788 0 : if (!noteInput) {
789 0 : return muse::RetVal<RequestNoteInputStateCommand::Output>::make_ret(Ret::Code::InternalError);
790 : }
791 :
792 0 : RequestNoteInputStateCommand::Output output;
793 0 : output.enabled = noteInput->isNoteInputMode();
794 0 : if (output.enabled) {
795 0 : const NoteInputState& state = noteInput->state();
796 0 : output.method = state.noteEntryMethod();
797 : }
798 :
799 0 : return muse::RetVal<RequestNoteInputStateCommand::Output>::make_ok(output);
800 0 : }
801 :
802 0 : muse::Ret NotationActionController::flipSelection()
803 : {
804 0 : INotationInteractionPtr interaction = currentNotationInteraction();
805 0 : if (!interaction) {
806 0 : return muse::make_ret(Ret::Code::InternalError);
807 : }
808 :
809 0 : interaction->flipSelection();
810 0 : return muse::make_ok();
811 0 : }
812 :
813 0 : muse::Ret NotationActionController::addNote(const AddNoteCommand& command)
814 : {
815 0 : return addNote(command.note, command.mode);
816 : }
817 :
818 0 : muse::Ret NotationActionController::addNote(NoteName note, NoteAddingMode mode)
819 : {
820 0 : NoteInputParams params;
821 0 : const bool addFlag = mode == NoteAddingMode::CurrentChord;
822 0 : bool ok = mu::engraving::NoteInput::resolveNoteInputParams(currentNotationScore(), static_cast<int>(note), addFlag, params);
823 0 : if (!ok) {
824 0 : LOGE() << "Could not resolve note input params, note: " << (int)note << ", addFlag: " << addFlag;
825 0 : return muse::make_ret(Ret::Code::InternalError);
826 : }
827 :
828 0 : doAddNote(params, mode);
829 0 : return muse::make_ok();
830 : }
831 :
832 0 : muse::Ret NotationActionController::addDrumNote(const muse::rcommand::Params& params)
833 : {
834 0 : TRACEFUNC;
835 :
836 0 : int pitch = params.at("pitch").toInt();
837 0 : NoteAddingMode mode = EnumContract<NoteAddingMode>::enum_val(params.at("mode").toString()).value();
838 :
839 0 : NoteInputParams noteInputParams;
840 0 : noteInputParams.drumPitch = pitch;
841 :
842 0 : doAddNote(noteInputParams, mode);
843 :
844 0 : return muse::make_ok();
845 0 : }
846 :
847 0 : void NotationActionController::doAddNote(const NoteInputParams& params, const NoteAddingMode& addingMode)
848 : {
849 0 : TRACEFUNC;
850 :
851 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
852 0 : if (!noteInput) {
853 0 : return;
854 : }
855 :
856 0 : startNoteInput();
857 :
858 0 : if (addingMode == NoteAddingMode::NextChord) {
859 0 : if (noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)) {
860 0 : noteInput->setRestMode(false);
861 0 : noteInput->setInputNote(params);
862 :
863 0 : if (configuration()->isPlayPreviewNotesInInputByDuration()) {
864 0 : const NoteInputState& state = noteInput->state();
865 0 : playbackController()->playNotes(state.notes(), state.staffIdx(), state.segment());
866 : }
867 0 : return;
868 : }
869 : }
870 :
871 0 : auto addNote = [this, noteInput, params, addingMode]() {
872 0 : noteInput->addNote(params, addingMode);
873 0 : seekAndPlaySelectedElement();
874 0 : };
875 :
876 0 : Ret ret = noteInput->canAddNote(addingMode);
877 0 : if (!ret) {
878 0 : interactive()->askToContinue(ret).onResolve(this, [addNote](bool proceed) {
879 0 : if (proceed) {
880 0 : addNote();
881 : }
882 0 : });
883 0 : return;
884 : }
885 :
886 0 : addNote();
887 0 : }
888 :
889 0 : muse::Ret NotationActionController::setDuration(const SetDurationCommand& command)
890 : {
891 0 : return setDuration(command.duration);
892 : }
893 :
894 0 : muse::Ret NotationActionController::setDuration(DurationType duration)
895 : {
896 0 : TRACEFUNC;
897 :
898 0 : INotationInteractionPtr interaction = currentNotationInteraction();
899 0 : if (!interaction) {
900 0 : return muse::make_ret(Ret::Code::InternalError);
901 : }
902 :
903 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
904 0 : if (interaction->selection()->isNone()) {
905 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
906 0 : return muse::make_ret(Ret::Code::NotAllowed);
907 : }
908 :
909 0 : startNoteInput();
910 : }
911 0 : if (noteInput->isNoteInputMode() && !noteInput->state().isValid()) {
912 0 : return muse::make_ret(Ret::Code::InternalError);
913 : }
914 :
915 0 : noteInput->setDuration(duration);
916 :
917 0 : if (noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)
918 0 : || noteInput->usingNoteInputMethod(NoteInputMethod::RHYTHM)) {
919 0 : seekAndPlaySelectedElement();
920 : }
921 :
922 0 : return muse::make_ok();
923 0 : }
924 :
925 0 : muse::Ret NotationActionController::toggleRest()
926 : {
927 0 : TRACEFUNC;
928 :
929 0 : INotationInteractionPtr interaction = currentNotationInteraction();
930 0 : if (!interaction) {
931 0 : return muse::make_ret(Ret::Code::InternalError);
932 : }
933 :
934 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
935 0 : if (interaction->selection()->isNone()) {
936 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
937 0 : return muse::make_ret(Ret::Code::NotAllowed);
938 : }
939 :
940 0 : startNoteInput();
941 : }
942 :
943 0 : noteInput->toggleRest();
944 :
945 0 : if (noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)
946 0 : || noteInput->usingNoteInputMethod(NoteInputMethod::RHYTHM)) {
947 0 : seekSelectedElement();
948 : }
949 :
950 0 : return muse::make_ok();
951 0 : }
952 :
953 0 : muse::Ret NotationActionController::toggleDots(const ToggleDotsCommand& command)
954 : {
955 0 : return toggleDots(command.dots);
956 : }
957 :
958 0 : muse::Ret NotationActionController::toggleDots(uint dots)
959 : {
960 0 : TRACEFUNC;
961 :
962 0 : if (dots > engraving::MAX_DOTS) {
963 0 : return muse::make_ret(Ret::Code::BadArgs);
964 : }
965 :
966 0 : INotationInteractionPtr interaction = currentNotationInteraction();
967 0 : if (!interaction) {
968 0 : return muse::make_ret(Ret::Code::InternalError);
969 : }
970 :
971 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
972 0 : if (interaction->selection()->isNone()) {
973 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
974 0 : return muse::make_ret(Ret::Code::NotAllowed);
975 : }
976 :
977 0 : startNoteInput();
978 : }
979 :
980 0 : if (!noteInput->isNoteInputMode() || !configuration()->addAccidentalDotsArticulationsToNextNoteEntered()) {
981 0 : interaction->toggleDotsForSelection(dots);
982 0 : return muse::make_ok();
983 : }
984 :
985 0 : noteInput->toggleDots(dots);
986 :
987 0 : if (noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)
988 0 : || noteInput->usingNoteInputMethod(NoteInputMethod::RHYTHM)) {
989 0 : seekAndPlaySelectedElement();
990 : }
991 :
992 0 : return muse::make_ok();
993 0 : }
994 :
995 0 : DurationType NotationActionController::currentDurationType() const
996 : {
997 0 : constexpr DurationType INVALID_DURATION_TYPE = DurationType::V_INVALID;
998 :
999 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1000 0 : if (!interaction) {
1001 0 : return INVALID_DURATION_TYPE;
1002 : }
1003 :
1004 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1005 0 : if (!noteInput) {
1006 0 : return INVALID_DURATION_TYPE;
1007 : }
1008 :
1009 0 : INotationSelectionPtr selection = interaction->selection();
1010 0 : if (!selection) {
1011 0 : return INVALID_DURATION_TYPE;
1012 : }
1013 :
1014 0 : if (noteInput->isNoteInputMode()) {
1015 0 : return noteInput->state().duration().type();
1016 : }
1017 :
1018 0 : if (selection->isNone() || selection->isRange()) {
1019 0 : return INVALID_DURATION_TYPE;
1020 : }
1021 :
1022 0 : const std::vector<EngravingItem*>& selectedElements = selection->elements();
1023 0 : if (selectedElements.empty()) {
1024 0 : return INVALID_DURATION_TYPE;
1025 : }
1026 :
1027 0 : auto elementToChordRest = [](const EngravingItem* element) -> const ChordRest* {
1028 0 : if (!element) {
1029 0 : return nullptr;
1030 : }
1031 0 : if (element->isChordRest()) {
1032 0 : return toChordRest(element);
1033 : }
1034 0 : if (element->isNote()) {
1035 0 : return toNote(element)->chord();
1036 : }
1037 0 : if (element->isStem()) {
1038 0 : return toStem(element)->chord();
1039 : }
1040 0 : if (element->isHook()) {
1041 0 : return toHook(element)->chord();
1042 : }
1043 0 : return nullptr;
1044 : };
1045 :
1046 0 : DurationType result = INVALID_DURATION_TYPE;
1047 0 : bool isFirstElement = true;
1048 0 : for (const EngravingItem* element: selectedElements) {
1049 0 : const ChordRest* chordRest = elementToChordRest(element);
1050 0 : if (!chordRest) {
1051 0 : continue;
1052 : }
1053 :
1054 0 : if (isFirstElement) {
1055 0 : result = chordRest->durationType().type();
1056 0 : isFirstElement = false;
1057 0 : } else if (result != chordRest->durationType().type()) {
1058 0 : return INVALID_DURATION_TYPE;
1059 : }
1060 : }
1061 :
1062 0 : return result;
1063 0 : }
1064 :
1065 0 : int NotationActionController::currentDotCount() const
1066 : {
1067 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1068 0 : if (!interaction) {
1069 0 : return 0;
1070 : }
1071 :
1072 0 : return interaction->noteInput()->state().duration().dots();
1073 0 : }
1074 :
1075 0 : bool NotationActionController::currentIsRest() const
1076 : {
1077 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1078 0 : if (!interaction) {
1079 0 : return false;
1080 : }
1081 :
1082 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1083 0 : if (!noteInput) {
1084 0 : return false;
1085 : }
1086 :
1087 0 : if (noteInput->isNoteInputMode()) {
1088 0 : return noteInput->state().rest();
1089 : }
1090 :
1091 0 : INotationSelectionPtr selection = interaction->selection();
1092 0 : if (!selection) {
1093 0 : return false;
1094 : }
1095 :
1096 0 : if (selection->isNone() || selection->isRange()) {
1097 0 : return false;
1098 : }
1099 :
1100 0 : for (const EngravingItem* element: selection->elements()) {
1101 0 : if (!element->isRest()) {
1102 0 : return false;
1103 : }
1104 : }
1105 :
1106 0 : return true;
1107 0 : }
1108 :
1109 0 : AccidentalType NotationActionController::currentAccidentalType() const
1110 : {
1111 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1112 0 : if (!interaction) {
1113 0 : return AccidentalType::NONE;
1114 : }
1115 :
1116 0 : return interaction->noteInput()->state().accidentalType();
1117 0 : }
1118 :
1119 0 : std::set<SymbolId> NotationActionController::currentArticulations() const
1120 : {
1121 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1122 0 : if (!interaction) {
1123 0 : return {};
1124 : }
1125 :
1126 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1127 0 : if (!noteInput) {
1128 0 : return {};
1129 : }
1130 :
1131 0 : if (noteInput->isNoteInputMode()) {
1132 0 : return mu::engraving::splitArticulations(noteInput->state().articulationIds());
1133 : }
1134 :
1135 0 : INotationSelectionPtr selection = interaction->selection();
1136 0 : if (!selection) {
1137 0 : return {};
1138 : }
1139 :
1140 0 : if (selection->isNone()) {
1141 0 : return {};
1142 : }
1143 :
1144 0 : auto chordArticulations = [](const Chord* chord) {
1145 0 : std::set<SymbolId> result;
1146 0 : for (Articulation* articulation: chord->articulations()) {
1147 0 : result.insert(articulation->symId());
1148 : }
1149 :
1150 0 : result = mu::engraving::flipArticulations(result, mu::engraving::PlacementV::ABOVE);
1151 0 : return mu::engraving::splitArticulations(result);
1152 0 : };
1153 :
1154 0 : std::set<SymbolId> result;
1155 0 : bool isFirstNote = true;
1156 0 : for (const EngravingItem* element: selection->elements()) {
1157 0 : if (!element->isNote()) {
1158 0 : continue;
1159 : }
1160 :
1161 0 : const Note* note = toNote(element);
1162 0 : if (isFirstNote) {
1163 0 : result = chordArticulations(note->chord());
1164 0 : isFirstNote = false;
1165 : } else {
1166 0 : std::set<SymbolId> currentNoteArticulations = chordArticulations(note->chord());
1167 0 : for (auto it = result.begin(); it != result.end();) {
1168 0 : if (std::find(currentNoteArticulations.begin(), currentNoteArticulations.end(),
1169 0 : *it) == currentNoteArticulations.end()) {
1170 0 : it = result.erase(it);
1171 : } else {
1172 0 : ++it;
1173 : }
1174 : }
1175 0 : }
1176 : }
1177 :
1178 0 : return result;
1179 0 : }
1180 :
1181 0 : voice_idx_t NotationActionController::currentVoice() const
1182 : {
1183 0 : constexpr voice_idx_t INVALID_VOICE = muse::nidx;
1184 :
1185 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1186 0 : if (!interaction) {
1187 0 : return INVALID_VOICE;
1188 : }
1189 :
1190 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1191 0 : if (!noteInput) {
1192 0 : return INVALID_VOICE;
1193 : }
1194 :
1195 0 : if (noteInput->isNoteInputMode()) {
1196 0 : return noteInput->state().voice();
1197 : }
1198 :
1199 0 : INotationSelectionPtr selection = interaction->selection();
1200 0 : if (!selection) {
1201 0 : return INVALID_VOICE;
1202 : }
1203 :
1204 0 : if (selection->isNone()) {
1205 0 : return INVALID_VOICE;
1206 : }
1207 :
1208 0 : const std::vector<EngravingItem*>& selectedElements = selection->elements();
1209 0 : if (selectedElements.empty()) {
1210 0 : return INVALID_VOICE;
1211 : }
1212 :
1213 0 : voice_idx_t voice = INVALID_VOICE;
1214 0 : for (const EngravingItem* element : selectedElements) {
1215 0 : if (element->hasVoiceAssignmentProperties()) {
1216 0 : VoiceAssignment voiceAssignment = element->getProperty(Pid::VOICE_ASSIGNMENT).value<VoiceAssignment>();
1217 0 : if (voiceAssignment == VoiceAssignment::ALL_VOICE_IN_INSTRUMENT || voiceAssignment == VoiceAssignment::ALL_VOICE_IN_STAFF) {
1218 0 : return INVALID_VOICE;
1219 : }
1220 : }
1221 0 : voice_idx_t elementVoice = element->voice();
1222 0 : if (elementVoice != voice && voice != INVALID_VOICE) {
1223 0 : return INVALID_VOICE;
1224 : }
1225 :
1226 0 : voice = elementVoice;
1227 : }
1228 :
1229 0 : return voice;
1230 0 : }
1231 :
1232 0 : muse::async::Notification NotationActionController::selectionChanged() const
1233 : {
1234 0 : return m_selectionChanged;
1235 : }
1236 :
1237 0 : bool NotationActionController::selectionHasTie() const
1238 : {
1239 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1240 0 : if (!interaction) {
1241 0 : return false;
1242 : }
1243 :
1244 0 : std::vector<Note*> tiedNotes = interaction->selection()->notes(NoteFilter::WithTie);
1245 0 : if (tiedNotes.empty()) {
1246 0 : return false;
1247 : }
1248 :
1249 0 : bool hasTie = true;
1250 0 : for (const Note* note: tiedNotes) {
1251 0 : if (!note->tieFor()) {
1252 0 : hasTie = false;
1253 0 : break;
1254 : }
1255 0 : if (note->laissezVib()) {
1256 0 : hasTie = false;
1257 0 : break;
1258 : }
1259 : }
1260 :
1261 0 : return hasTie;
1262 0 : }
1263 :
1264 0 : bool NotationActionController::selectionHasLaissezVib() const
1265 : {
1266 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1267 0 : if (!interaction) {
1268 0 : return false;
1269 : }
1270 :
1271 0 : std::vector<Note*> tiedNotes = interaction->selection()->notes(NoteFilter::WithTie);
1272 0 : if (tiedNotes.empty()) {
1273 0 : return false;
1274 : }
1275 :
1276 0 : bool hasLaissezVib = true;
1277 0 : for (const Note* note: tiedNotes) {
1278 0 : if (!note->laissezVib()) {
1279 0 : hasLaissezVib = false;
1280 0 : break;
1281 : }
1282 : }
1283 0 : return hasLaissezVib;
1284 0 : }
1285 :
1286 0 : bool NotationActionController::selectionHasSlur() const
1287 : {
1288 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1289 0 : if (!interaction) {
1290 0 : return false;
1291 : }
1292 :
1293 0 : bool hasSlur = interaction->noteInput()->state().slur() != nullptr;
1294 0 : return hasSlur;
1295 0 : }
1296 :
1297 0 : muse::Ret NotationActionController::putNote(const muse::rcommand::Params& params)
1298 : {
1299 0 : TRACEFUNC;
1300 :
1301 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
1302 0 : if (!noteInput) {
1303 0 : return muse::make_ret(Ret::Code::NotSupported);
1304 : }
1305 :
1306 0 : IF_ASSERT_FAILED(params.contains("pos_x") && params.contains("pos_y")) {
1307 0 : return muse::make_ret(Ret::Code::BadArgs);
1308 : }
1309 :
1310 0 : float posX = params.at("pos_x").toFloat();
1311 0 : float posY = params.at("pos_y").toFloat();
1312 0 : bool replace = params.at("replace", Val(false)).toBool();
1313 0 : bool insert = params.at("insert", Val(false)).toBool();
1314 :
1315 0 : PointF pos = PointF(posX, posY);
1316 :
1317 0 : auto putNote = [this, noteInput, pos, replace, insert]() {
1318 0 : if (noteInput->putNote(pos, replace, insert)) {
1319 0 : seekAndPlaySelectedElement();
1320 : }
1321 0 : };
1322 :
1323 0 : Ret ret = noteInput->canPutNote(pos);
1324 0 : if (!ret) {
1325 0 : interactive()->askToContinue(ret).onResolve(this, [putNote](bool proceed) {
1326 0 : if (proceed) {
1327 0 : putNote();
1328 : }
1329 0 : });
1330 0 : return muse::make_ok();
1331 : }
1332 :
1333 0 : putNote();
1334 :
1335 0 : return muse::make_ok();
1336 0 : }
1337 :
1338 0 : muse::Ret NotationActionController::removeNote(const muse::rcommand::Params& params)
1339 : {
1340 0 : TRACEFUNC;
1341 :
1342 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
1343 0 : if (!noteInput) {
1344 0 : return muse::make_ret(Ret::Code::NotSupported);
1345 : }
1346 :
1347 0 : IF_ASSERT_FAILED(params.contains("pos_x") && params.contains("pos_y")) {
1348 0 : return muse::make_ret(Ret::Code::BadArgs);
1349 : }
1350 :
1351 0 : float posX = params.at("pos_x").toFloat();
1352 0 : float posY = params.at("pos_y").toFloat();
1353 :
1354 0 : PointF pos = PointF(posX, posY);
1355 :
1356 0 : auto removeNote = [this, noteInput, pos]() {
1357 0 : noteInput->removeNote(pos);
1358 0 : seekSelectedElement();
1359 0 : };
1360 :
1361 0 : Ret ret = noteInput->canPutNote(pos);
1362 0 : if (!ret) {
1363 0 : interactive()->askToContinue(ret).onResolve(this, [removeNote](bool proceed) {
1364 0 : if (proceed) {
1365 0 : removeNote();
1366 : }
1367 0 : });
1368 0 : return muse::make_ok();
1369 : }
1370 :
1371 0 : removeNote();
1372 :
1373 0 : return muse::make_ok();
1374 0 : }
1375 :
1376 0 : muse::Ret NotationActionController::toggleAccidental(const ToggleAccidentalCommand& command)
1377 : {
1378 : //! TODO The methods `toggleAccidental` and `changeAccidental` have been combined as-is. Refactoring is required.
1379 :
1380 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1381 0 : if (!interaction) {
1382 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1383 : }
1384 :
1385 0 : const AccidentalType type = command.type;
1386 :
1387 0 : if (command.affectNoteInput) {
1388 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1389 0 : if (!noteInput) {
1390 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1391 : }
1392 :
1393 0 : if (interaction->selection()->isNone()) {
1394 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
1395 0 : return muse::make_ret(muse::Ret::Code::NotSupported);
1396 : }
1397 :
1398 0 : startNoteInput();
1399 : }
1400 :
1401 0 : if (noteInput->isNoteInputMode() && configuration()->addAccidentalDotsArticulationsToNextNoteEntered()) {
1402 0 : noteInput->setAccidental(type);
1403 : } else {
1404 0 : interaction->toggleAccidentalForSelection(type);
1405 0 : seekAndPlaySelectedElement();
1406 : }
1407 0 : } else {
1408 0 : interaction->changeAccidental(type);
1409 :
1410 0 : seekSelectedElement();
1411 0 : if (command.playMode != PlayMode::NoPlay) {
1412 0 : playSelectedElement(command.playMode == PlayMode::PlayChord);
1413 : }
1414 : }
1415 :
1416 0 : return muse::make_ok();
1417 0 : }
1418 :
1419 0 : void NotationActionController::toggleArticulation(SymbolId articulationSymbolId)
1420 : {
1421 0 : TRACEFUNC;
1422 :
1423 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1424 0 : if (!interaction) {
1425 0 : return;
1426 : }
1427 :
1428 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1429 0 : if (!noteInput) {
1430 0 : return;
1431 : }
1432 :
1433 0 : if (interaction->selection()->isNone()) {
1434 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
1435 0 : return;
1436 : }
1437 :
1438 0 : startNoteInput();
1439 : }
1440 :
1441 0 : if (noteInput->isNoteInputMode() && configuration()->addAccidentalDotsArticulationsToNextNoteEntered()) {
1442 0 : noteInput->setArticulation(articulationSymbolId);
1443 : } else {
1444 0 : interaction->toggleArticulationForSelection(articulationSymbolId);
1445 : }
1446 0 : }
1447 :
1448 0 : muse::Ret NotationActionController::putTuplet(const AddTupletCommand& command)
1449 : {
1450 0 : auto interaction = currentNotationInteraction();
1451 0 : if (!interaction) {
1452 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1453 : }
1454 :
1455 0 : auto noteInput = interaction->noteInput();
1456 0 : if (!noteInput) {
1457 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1458 : }
1459 :
1460 0 : TupletOptions options;
1461 0 : options.ratio = Fraction::fromString(command.ratio);
1462 0 : options.numberType = command.numberType;
1463 0 : options.bracketType = command.bracketType;
1464 0 : options.autoBaseLen = command.autoBaseLen;
1465 0 : options.selectedDurationAs = command.selectedDurationAs;
1466 :
1467 : // resolve defaults
1468 0 : INotationStylePtr style = notationStyle();
1469 0 : if (!style) {
1470 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1471 : }
1472 :
1473 0 : if (options.bracketType == TupletBracketType::DEFAULT) {
1474 0 : int bracketType = style->styleValue(StyleId::tupletBracketType).toInt();
1475 0 : options.bracketType = static_cast<TupletBracketType>(bracketType);
1476 : }
1477 0 : if (options.numberType == TupletNumberType::DEFAULT) {
1478 0 : int numberType = style->styleValue(StyleId::tupletNumberType).toInt();
1479 0 : options.numberType = static_cast<TupletNumberType>(numberType);
1480 : }
1481 :
1482 0 : if (options.autoBaseLen && options.selectedDurationAs == TupletSelectedDuration::DEFAULT) {
1483 0 : options.selectedDurationAs = configuration()->tupletCommandsSelectedDurationAs();
1484 : }
1485 :
1486 : // e.g. a triplet of the selected duration
1487 0 : if (options.selectedDurationAs == TupletSelectedDuration::SUBDIVISION) {
1488 0 : const int numerator = options.ratio.numerator();
1489 0 : options.ratio.setDenominator(Tuplet::conventionalTupletDenominator(numerator));
1490 : }
1491 :
1492 0 : if (noteInput->isNoteInputMode()) {
1493 0 : noteInput->addTuplet(options);
1494 : } else {
1495 0 : interaction->addTupletToSelectedChordRests(options);
1496 : }
1497 0 : return muse::make_ok();
1498 0 : }
1499 :
1500 0 : muse::Ret NotationActionController::changeDuration(const ChangeDurationCommand& command)
1501 : {
1502 0 : return changeDuration(command.mode);
1503 : }
1504 :
1505 0 : muse::Ret NotationActionController::changeDuration(ChangeDurationMode mode)
1506 : {
1507 0 : TRACEFUNC;
1508 :
1509 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1510 0 : if (!interaction) {
1511 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1512 : }
1513 :
1514 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1515 :
1516 0 : if (noteInput->isNoteInputMode()) {
1517 0 : switch (mode) {
1518 0 : case ChangeDurationMode::Double:
1519 : case ChangeDurationMode::DoubleDotted:
1520 0 : noteInput->doubleNoteInputDuration();
1521 0 : break;
1522 0 : case ChangeDurationMode::Halve:
1523 : case ChangeDurationMode::HalveDotted:
1524 0 : noteInput->halveNoteInputDuration();
1525 0 : break;
1526 : }
1527 : } else {
1528 0 : switch (mode) {
1529 0 : case ChangeDurationMode::Double:
1530 0 : interaction->increaseDecreaseDuration(-1, false);
1531 0 : break;
1532 0 : case ChangeDurationMode::Halve:
1533 0 : interaction->increaseDecreaseDuration(1, false);
1534 0 : break;
1535 0 : case ChangeDurationMode::DoubleDotted:
1536 0 : interaction->increaseDecreaseDuration(-1, true);
1537 0 : break;
1538 0 : case ChangeDurationMode::HalveDotted:
1539 0 : interaction->increaseDecreaseDuration(1, true);
1540 0 : break;
1541 : }
1542 : }
1543 :
1544 0 : return muse::make_ok();
1545 0 : }
1546 :
1547 0 : void NotationActionController::realtimeAdvance()
1548 : {
1549 0 : TRACEFUNC;
1550 :
1551 0 : INotationMidiInputPtr midiInput = currentNotationMidiInput();
1552 0 : if (!midiInput) {
1553 0 : return;
1554 : }
1555 :
1556 0 : midiInput->onRealtimeAdvance();
1557 0 : }
1558 :
1559 0 : bool NotationActionController::isMoveSelectionAvailable(MoveSelectionType type) const
1560 : {
1561 0 : auto interaction = currentNotationInteraction();
1562 0 : return interaction && interaction->moveSelectionAvailable(type);
1563 0 : }
1564 :
1565 0 : bool NotationActionController::isToggleLayoutBreakAvailable() const
1566 : {
1567 0 : auto interaction = currentNotationInteraction();
1568 0 : return interaction && interaction->toggleLayoutBreakAvailable();
1569 0 : }
1570 :
1571 0 : ScoreConfig NotationActionController::scoreConfig() const
1572 : {
1573 0 : auto interaction = currentNotationInteraction();
1574 0 : if (!interaction) {
1575 0 : return ScoreConfig();
1576 : }
1577 0 : return interaction->scoreConfig();
1578 0 : }
1579 :
1580 0 : muse::async::Channel<ScoreConfigType> NotationActionController::scoreConfigChanged() const
1581 : {
1582 0 : return m_scoreConfigChanged;
1583 : }
1584 :
1585 0 : muse::Ret NotationActionController::moveWithRet(MoveDirection direction, bool quickly)
1586 : {
1587 0 : TRACEFUNC;
1588 0 : auto interaction = currentNotationInteraction();
1589 0 : if (!interaction) {
1590 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1591 : }
1592 :
1593 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1594 0 : const NoteInputState& state = noteInput->state();
1595 0 : const bool previousSelectionExists = currentNotationScore() && currentNotationScore()->selection().currentCR();
1596 0 : if (interaction->selection()->isNone() && previousSelectionExists && !state.beyondScore()) {
1597 : // Try to restore the previous selection...
1598 0 : interaction->moveSelectionDeprecated(direction, MoveSelectionType::EngravingItem);
1599 0 : seekAndPlaySelectedElement(true);
1600 0 : return muse::make_ok();
1601 : }
1602 :
1603 0 : bool playChord = false;
1604 :
1605 0 : switch (direction) {
1606 0 : case MoveDirection::Up:
1607 : case MoveDirection::Down:
1608 0 : if (state.isValid() && noteInput->isNoteInputMode() && noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)) {
1609 0 : moveInputNotes(direction == MoveDirection::Up, quickly ? PitchMode::OCTAVE : PitchMode::DIATONIC);
1610 0 : return muse::make_ok();
1611 0 : } else if (noteInput->isNoteInputMode() && state.staffGroup() == mu::engraving::StaffGroup::TAB) {
1612 0 : if (quickly) {
1613 0 : interaction->movePitch(direction, PitchMode::OCTAVE);
1614 : }
1615 0 : interaction->moveSelectionDeprecated(direction, MoveSelectionType::String);
1616 0 : return muse::make_ok();
1617 0 : } else if (interaction->selection()->isNone() && !state.beyondScore()) {
1618 0 : interaction->select(SelectionTarget::FirstItem);
1619 : } else {
1620 0 : interaction->moveSelectedItems(direction, quickly);
1621 : }
1622 0 : break;
1623 0 : case MoveDirection::Right:
1624 : case MoveDirection::Left:
1625 0 : if (playbackController()->isPlaying()) {
1626 0 : engraving::MeasureBeat beat = playbackController()->currentBeat();
1627 0 : int targetBeatIdx = static_cast<int>(beat.beat);
1628 0 : int targetMeasureIdx = beat.measureIndex;
1629 0 : int increment = (direction == MoveDirection::Right ? 1 : -1);
1630 :
1631 0 : if (quickly) {
1632 0 : targetBeatIdx = 0;
1633 0 : targetMeasureIdx += increment;
1634 0 : if (targetMeasureIdx > beat.maxMeasureIndex) {
1635 0 : targetMeasureIdx = beat.maxMeasureIndex;
1636 0 : } else if (targetMeasureIdx < 0) {
1637 0 : targetMeasureIdx = 0;
1638 : }
1639 : } else {
1640 0 : targetBeatIdx += increment;
1641 0 : if (targetBeatIdx > beat.maxBeatIndex) {
1642 0 : targetBeatIdx = 0;
1643 0 : targetMeasureIdx += 1;
1644 0 : } else if (targetBeatIdx < 0) {
1645 0 : targetMeasureIdx -= 1;
1646 :
1647 : // Set target beat to max beat of previous bar
1648 0 : engraving::TimeSigMap* timeSigMap = currentMasterNotation()->masterScore()->sigmap();
1649 0 : int targetBarStartTick = timeSigMap->bar2tick(targetMeasureIdx, 0);
1650 0 : targetBeatIdx = timeSigMap->timesig(Fraction::fromTicks(targetBarStartTick)).timesig().numerator() - 1;
1651 : }
1652 : }
1653 :
1654 0 : playbackController()->seekBeat(targetMeasureIdx, targetBeatIdx);
1655 0 : return muse::make_ok();
1656 : }
1657 :
1658 0 : if (interaction->isTextEditingStarted() && textNavigationAvailable()) {
1659 0 : navigateToTextElementInNearMeasure(direction);
1660 0 : return muse::make_ok();
1661 : }
1662 :
1663 0 : if (!interaction->moveSelectedItems(direction, quickly)) {
1664 0 : if (interaction->selection()->isNone() && !state.beyondScore()) {
1665 0 : interaction->select(SelectionTarget::FirstItem);
1666 : }
1667 0 : interaction->moveSelectionDeprecated(direction, quickly ? MoveSelectionType::Measure : MoveSelectionType::Chord);
1668 0 : playChord = true;
1669 : }
1670 0 : break;
1671 0 : case MoveDirection::Undefined:
1672 0 : break;
1673 : }
1674 :
1675 0 : seekAndPlaySelectedElement(playChord);
1676 :
1677 0 : return muse::make_ok();
1678 0 : }
1679 :
1680 0 : void NotationActionController::move(MoveDirection direction, bool quickly)
1681 : {
1682 0 : moveWithRet(direction, quickly);
1683 0 : }
1684 :
1685 0 : void NotationActionController::moveInputNotes(bool up, PitchMode mode)
1686 : {
1687 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1688 0 : if (!interaction) {
1689 0 : return;
1690 : }
1691 :
1692 0 : interaction->noteInput()->setRestMode(false);
1693 0 : interaction->noteInput()->moveInputNotes(up, mode);
1694 :
1695 0 : if (configuration()->isPlayPreviewNotesInInputByDuration()) {
1696 0 : const NoteInputState& state = interaction->noteInput()->state();
1697 0 : playbackController()->playNotes(state.notes(), state.staffIdx(), state.segment());
1698 : }
1699 0 : }
1700 :
1701 0 : void NotationActionController::movePitchDiatonic(MoveDirection direction, bool)
1702 : {
1703 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1704 0 : if (!interaction) {
1705 0 : return;
1706 : }
1707 :
1708 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1709 0 : if (noteInput->state().isValid() && noteInput->isNoteInputMode() && noteInput->usingNoteInputMethod(NoteInputMethod::BY_DURATION)) {
1710 0 : moveInputNotes(direction == MoveDirection::Up, PitchMode::DIATONIC);
1711 0 : return;
1712 : }
1713 :
1714 0 : interaction->movePitch(direction, PitchMode::DIATONIC);
1715 0 : seekAndPlaySelectedElement(true);
1716 0 : }
1717 :
1718 0 : muse::Ret NotationActionController::changeVoice(const UseVoiceCommand& command)
1719 : {
1720 0 : TRACEFUNC;
1721 :
1722 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1723 0 : if (!interaction) {
1724 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1725 : }
1726 :
1727 0 : INotationNoteInputPtr noteInput = interaction->noteInput();
1728 0 : if (!noteInput) {
1729 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1730 : }
1731 :
1732 0 : if (interaction->selection()->isNone()) {
1733 0 : if (!noteInput->isNoteInputMode() && !toggleNoteInputAllowed()) {
1734 0 : return muse::make_ret(muse::Ret::Code::NotAllowed);
1735 : }
1736 :
1737 0 : startNoteInput();
1738 : }
1739 :
1740 0 : const voice_idx_t voiceIndex = command.voice - 1; // number to index conversion
1741 0 : IF_ASSERT_FAILED(voiceIndex < MAX_VOICES) {
1742 0 : return muse::make_ret(muse::Ret::Code::BadArgs);
1743 : }
1744 :
1745 0 : noteInput->setCurrentVoice(voiceIndex);
1746 :
1747 0 : if (!noteInput->isNoteInputMode()) {
1748 0 : interaction->changeSelectedElementsVoice(voiceIndex);
1749 : }
1750 :
1751 0 : return muse::make_ok();
1752 0 : }
1753 :
1754 0 : muse::Ret NotationActionController::swapVoices(const SwapVoicesCommand& command)
1755 : {
1756 0 : TRACEFUNC;
1757 0 : auto interaction = currentNotationInteraction();
1758 0 : if (!interaction) {
1759 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1760 : }
1761 :
1762 0 : const voice_idx_t voiceAIndex = command.voiceA - 1; // number to index conversion
1763 0 : const voice_idx_t voiceBIndex = command.voiceB - 1; // number to index conversion
1764 :
1765 0 : IF_ASSERT_FAILED(voiceAIndex < MAX_VOICES) {
1766 0 : return muse::make_ret(muse::Ret::Code::BadArgs);
1767 : }
1768 :
1769 0 : IF_ASSERT_FAILED(voiceBIndex < MAX_VOICES) {
1770 0 : return muse::make_ret(muse::Ret::Code::BadArgs);
1771 : }
1772 :
1773 0 : interaction->swapVoices(voiceAIndex, voiceBIndex);
1774 0 : return muse::make_ok();
1775 0 : }
1776 :
1777 0 : muse::Ret NotationActionController::cutSelection()
1778 : {
1779 0 : TRACEFUNC;
1780 0 : auto interaction = currentNotationInteraction();
1781 0 : if (!interaction) {
1782 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1783 : }
1784 0 : interaction->copySelection();
1785 0 : interaction->deleteSelection();
1786 :
1787 0 : seekSelectedElement();
1788 :
1789 0 : return muse::make_ok();
1790 0 : }
1791 :
1792 0 : void NotationActionController::repeatSelection()
1793 : {
1794 0 : auto interaction = currentNotationInteraction();
1795 0 : if (!interaction) {
1796 0 : return;
1797 : }
1798 0 : interaction->repeatSelection();
1799 0 : seekAndPlaySelectedElement(true);
1800 0 : }
1801 :
1802 0 : muse::Ret NotationActionController::pasteSelection(const PasteCommand& command)
1803 : {
1804 0 : TRACEFUNC;
1805 0 : auto interaction = currentNotationInteraction();
1806 0 : if (!interaction) {
1807 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1808 : }
1809 :
1810 0 : Fraction scale = resolvePastingScale(interaction, command.type);
1811 0 : interaction->pasteSelection(scale);
1812 :
1813 0 : seekAndPlaySelectedElement(DONT_PLAY_CHORD);
1814 :
1815 0 : return muse::make_ok();
1816 0 : }
1817 :
1818 0 : muse::Ret NotationActionController::copyPasteSwap()
1819 : {
1820 0 : TRACEFUNC;
1821 0 : auto interaction = currentNotationInteraction();
1822 0 : if (!interaction) {
1823 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1824 : }
1825 :
1826 0 : interaction->swapSelection();
1827 :
1828 0 : seekSelectedElement();
1829 :
1830 0 : return muse::make_ok();
1831 0 : }
1832 :
1833 0 : muse::Ret NotationActionController::copyLyrics()
1834 : {
1835 0 : TRACEFUNC;
1836 0 : auto interaction = currentNotationInteraction();
1837 0 : if (!interaction) {
1838 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1839 : }
1840 :
1841 0 : interaction->copyLyrics();
1842 :
1843 0 : return muse::make_ok();
1844 0 : }
1845 :
1846 0 : muse::Ret NotationActionController::deleteSelection()
1847 : {
1848 0 : TRACEFUNC;
1849 0 : auto interaction = currentNotationInteraction();
1850 0 : if (!interaction) {
1851 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1852 : }
1853 :
1854 0 : interaction->deleteSelection();
1855 :
1856 0 : seekSelectedElement();
1857 :
1858 0 : return muse::make_ok();
1859 0 : }
1860 :
1861 0 : Fraction NotationActionController::resolvePastingScale(const INotationInteractionPtr& interaction, PastingType type) const
1862 : {
1863 0 : TRACEFUNC;
1864 0 : const Fraction DEFAULT_SCALE(1, 1);
1865 :
1866 0 : switch (type) {
1867 0 : case PastingType::Default: return DEFAULT_SCALE;
1868 0 : case PastingType::Half: return Fraction(1, 2);
1869 0 : case PastingType::Double: return Fraction(2, 1);
1870 0 : case PastingType::Special:
1871 0 : Fraction scale = DEFAULT_SCALE;
1872 0 : Fraction duration = interaction->noteInput()->state().duration().fraction();
1873 :
1874 0 : if (duration.isValid() && !duration.isZero()) {
1875 0 : scale = duration * 4;
1876 0 : scale.reduce();
1877 : }
1878 :
1879 0 : return scale;
1880 : }
1881 :
1882 0 : return DEFAULT_SCALE;
1883 0 : }
1884 :
1885 0 : void NotationActionController::addTie()
1886 : {
1887 0 : TRACEFUNC;
1888 0 : auto interaction = currentNotationInteraction();
1889 0 : if (!interaction) {
1890 0 : return;
1891 : }
1892 :
1893 0 : auto noteInput = interaction->noteInput();
1894 0 : if (!noteInput) {
1895 0 : return;
1896 : }
1897 :
1898 0 : if (noteInput->isNoteInputMode()) {
1899 0 : noteInput->addTie();
1900 0 : seekAndPlaySelectedElement(true);
1901 : } else {
1902 0 : interaction->toggleTieForSelection();
1903 : }
1904 0 : }
1905 :
1906 0 : void NotationActionController::addLaissezVib()
1907 : {
1908 0 : TRACEFUNC;
1909 0 : auto interaction = currentNotationInteraction();
1910 0 : if (!interaction) {
1911 0 : return;
1912 : }
1913 :
1914 0 : auto noteInput = interaction->noteInput();
1915 0 : if (!noteInput) {
1916 0 : return;
1917 : }
1918 :
1919 0 : if (noteInput->isNoteInputMode()) {
1920 0 : noteInput->addLaissezVib();
1921 0 : seekAndPlaySelectedElement(true);
1922 : } else {
1923 0 : interaction->addLaissezVibToSelection();
1924 : }
1925 0 : }
1926 :
1927 0 : void NotationActionController::addSlur()
1928 : {
1929 0 : TRACEFUNC;
1930 0 : auto interaction = currentNotationInteraction();
1931 0 : if (!interaction) {
1932 0 : return;
1933 : }
1934 :
1935 0 : auto noteInput = interaction->noteInput();
1936 0 : if (!noteInput) {
1937 0 : return;
1938 : }
1939 :
1940 0 : if (noteInput->isNoteInputMode() && noteInput->state().slur()) {
1941 0 : noteInput->resetSlur();
1942 : } else {
1943 0 : interaction->addSlurToSelection();
1944 : }
1945 0 : }
1946 :
1947 0 : void NotationActionController::addHammerOnPullOff()
1948 : {
1949 0 : auto interaction = currentNotationInteraction();
1950 0 : if (!interaction) {
1951 0 : return;
1952 : }
1953 :
1954 0 : interaction->addHammerOnPullOffToSelection();
1955 0 : }
1956 :
1957 0 : muse::Ret NotationActionController::addFret(const AddFretCommand& command)
1958 : {
1959 0 : auto interaction = currentNotationInteraction();
1960 0 : if (!interaction) {
1961 0 : return muse::make_ret(muse::Ret::Code::InternalError);
1962 : }
1963 :
1964 0 : auto addFret = [this, interaction, command]() {
1965 0 : interaction->addFret(command.num);
1966 0 : seekAndPlaySelectedElement(currentNotationScore()->playChord());
1967 0 : };
1968 :
1969 0 : Ret ret = interaction->canAddFret();
1970 0 : if (!ret) {
1971 0 : interactive()->askToContinue(ret).onResolve(this, [addFret](bool proceed) {
1972 0 : if (proceed) {
1973 0 : addFret();
1974 : }
1975 0 : });
1976 0 : return muse::make_ok();
1977 : }
1978 :
1979 0 : addFret();
1980 :
1981 0 : return muse::make_ok();
1982 0 : }
1983 :
1984 0 : void NotationActionController::insertClef(mu::engraving::ClefType type)
1985 : {
1986 0 : INotationInteractionPtr interaction = currentNotationInteraction();
1987 0 : if (!interaction || !interaction->canInsertClef(type)) {
1988 0 : return;
1989 : }
1990 :
1991 0 : interaction->insertClef(type);
1992 0 : }
1993 :
1994 0 : void NotationActionController::addImage()
1995 : {
1996 0 : auto interaction = currentNotationInteraction();
1997 0 : if (!interaction) {
1998 0 : return;
1999 : }
2000 :
2001 0 : EngravingItem* item = interaction->contextItem();
2002 0 : if (!interaction->canAddImageToItem(item)) {
2003 0 : return;
2004 : }
2005 :
2006 : std::vector<std::string> filter
2007 0 : = { muse::trc("notation", "All Supported Files") + " (*.svg *.svgz *.jpg *.jpeg *.png *.bmp *.tif *.tiff)",
2008 0 : muse::trc("notation", "Scalable Vector Graphics") + " (*.svg *.svgz)",
2009 0 : muse::trc("notation", "JPEG") + " (*.jpg *.jpeg)",
2010 0 : muse::trc("notation", "PNG Bitmap Graphic") + " (*.png)",
2011 0 : muse::trc("notation", "Bitmap") + " (*.bmp)",
2012 0 : muse::trc("notation", "TIFF") + " (*.tif *.tiff)",
2013 0 : muse::trc("notation", "All") + " (*)" };
2014 :
2015 0 : muse::io::path_t path = interactive()->selectOpeningFileSync(muse::trc("notation", "Insert Image"), "", filter);
2016 0 : interaction->addImageToItem(path, item);
2017 0 : }
2018 :
2019 0 : void NotationActionController::addFiguredBass()
2020 : {
2021 0 : TRACEFUNC;
2022 :
2023 0 : auto interaction = currentNotationInteraction();
2024 0 : if (!interaction) {
2025 0 : return;
2026 : }
2027 :
2028 0 : interaction->addFiguredBass();
2029 0 : }
2030 :
2031 0 : void NotationActionController::addGuitarBend(GuitarBendType bendType)
2032 : {
2033 0 : TRACEFUNC;
2034 :
2035 0 : auto interaction = currentNotationInteraction();
2036 0 : if (!interaction) {
2037 0 : return;
2038 : }
2039 :
2040 0 : interaction->addGuitarBend(bendType);
2041 0 : }
2042 :
2043 0 : void NotationActionController::addFretboardDiagram()
2044 : {
2045 0 : TRACEFUNC;
2046 :
2047 0 : auto interaction = currentNotationInteraction();
2048 0 : if (!interaction) {
2049 0 : return;
2050 : }
2051 :
2052 0 : interaction->addFretboardDiagram();
2053 0 : }
2054 :
2055 0 : muse::Ret NotationActionController::openSelectionMoreOptions()
2056 : {
2057 0 : auto interaction = currentNotationInteraction();
2058 0 : if (!interaction) {
2059 0 : return muse::make_ret(Ret::Code::InternalError);
2060 : }
2061 :
2062 0 : auto item = interaction->contextItem();
2063 0 : if (!item) {
2064 0 : return muse::make_ret(Ret::Code::InternalError);
2065 : }
2066 :
2067 0 : bool noteSelected = item->isNote();
2068 :
2069 0 : if (noteSelected) {
2070 0 : interactive()->open("musescore://notation/selectnote");
2071 : } else {
2072 0 : interactive()->open("musescore://notation/selectelement");
2073 : }
2074 :
2075 0 : return muse::make_ok();
2076 0 : }
2077 :
2078 0 : muse::Ret NotationActionController::showLocation()
2079 : {
2080 0 : auto interaction = currentNotationInteraction();
2081 0 : if (!interaction) {
2082 0 : return muse::make_ret(Ret::Code::InternalError);
2083 : }
2084 :
2085 0 : interaction->showLocation();
2086 0 : seekSelectedElement();
2087 0 : return muse::make_ok();
2088 0 : }
2089 :
2090 0 : muse::Ret NotationActionController::startEditSelectedElement(const muse::rcommand::Params& params)
2091 : {
2092 0 : auto interaction = currentNotationInteraction();
2093 0 : if (!interaction) {
2094 0 : return muse::make_ret(Ret::Code::NotSupported);
2095 : }
2096 :
2097 0 : auto selection = interaction->selection();
2098 0 : if (!selection) {
2099 0 : return muse::make_ret(Ret::Code::BadArgs);
2100 : }
2101 :
2102 0 : mu::engraving::EngravingItem* element = selection->element();
2103 0 : if (!element) {
2104 0 : return muse::make_ret(Ret::Code::BadArgs);
2105 : }
2106 :
2107 0 : if (element->isInstrumentName()) {
2108 0 : openStaffProperties();
2109 0 : return muse::make_ok();
2110 : }
2111 :
2112 0 : if (elementHasPopup(element) && !interaction->textEditingAllowed(element)) {
2113 0 : IF_ASSERT_FAILED(m_viewController) {
2114 0 : return muse::make_ret(Ret::Code::NotSupported);
2115 : }
2116 0 : m_viewController->togglePopupForItemIfSupports(element);
2117 0 : return muse::make_ok();
2118 : }
2119 :
2120 0 : if (element->isText()) {
2121 0 : TextStyleType styleType = mu::engraving::toText(element)->textStyleType();
2122 :
2123 0 : if (styleType == mu::engraving::TextStyleType::HEADER || styleType == mu::engraving::TextStyleType::FOOTER) {
2124 0 : openEditStyleDialog({ { "page_code", Val("header-and-footer") } });
2125 0 : return muse::make_ok();
2126 : }
2127 : }
2128 :
2129 0 : if (interaction->textEditingAllowed(element)) {
2130 0 : float posX = params.at("pos_x").toFloat();
2131 0 : float posY = params.at("pos_y").toFloat();
2132 0 : PointF cursorPos = PointF(posX, posY);
2133 0 : interaction->startEditText(element, cursorPos);
2134 : } else {
2135 0 : interaction->startEditElement(element);
2136 : }
2137 :
2138 0 : return muse::make_ok();
2139 0 : }
2140 :
2141 0 : muse::Ret NotationActionController::startEditSelectedText(const muse::rcommand::Params& params)
2142 : {
2143 0 : auto interaction = currentNotationInteraction();
2144 0 : if (!interaction) {
2145 0 : return muse::make_ret(Ret::Code::NotSupported);
2146 : }
2147 :
2148 0 : auto selection = interaction->selection();
2149 0 : if (!selection) {
2150 0 : return muse::make_ret(Ret::Code::BadArgs);
2151 : }
2152 :
2153 0 : mu::engraving::EngravingItem* element = selection->element();
2154 :
2155 0 : if (interaction->textEditingAllowed(element)) {
2156 0 : float posX = params.at("pos_x").toFloat();
2157 0 : float posY = params.at("pos_y").toFloat();
2158 0 : PointF cursorPos = PointF(posX, posY);
2159 0 : interaction->startEditText(element, cursorPos);
2160 : }
2161 :
2162 0 : return muse::make_ok();
2163 0 : }
2164 :
2165 0 : muse::Ret NotationActionController::addBoxes(const AddBoxesCommand& command)
2166 : {
2167 0 : TRACEFUNC;
2168 0 : auto interaction = currentNotationInteraction();
2169 0 : if (!interaction) {
2170 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2171 : }
2172 :
2173 0 : if (command.needInputCount) {
2174 0 : interactive()->open("musescore://notation/selectmeasurescount")
2175 0 : .onResolve(this, [interaction, command](const Val& v) {
2176 0 : int count = v.toInt();
2177 0 : interaction->addBoxes(command.boxType, count, command.target);
2178 0 : });
2179 : } else {
2180 0 : interaction->addBoxes(command.boxType, command.count, command.target);
2181 : }
2182 0 : return muse::make_ok();
2183 0 : }
2184 :
2185 0 : muse::Ret NotationActionController::addGraceNotes(const AddGraceNoteCommand& command)
2186 : {
2187 0 : TRACEFUNC;
2188 0 : auto interaction = currentNotationInteraction();
2189 0 : if (!interaction) {
2190 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2191 : }
2192 :
2193 0 : interaction->addGraceNotesToSelectedNotes(command.type);
2194 :
2195 0 : seekSelectedElement();
2196 :
2197 0 : return muse::make_ok();
2198 0 : }
2199 :
2200 0 : muse::Ret NotationActionController::addInterval(const AddIntervalCommand& command)
2201 : {
2202 0 : TRACEFUNC;
2203 0 : auto interaction = currentNotationInteraction();
2204 0 : if (!interaction) {
2205 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2206 : }
2207 :
2208 0 : if (!isNotesIntervalValid(command.interval)) {
2209 0 : return muse::make_ret(muse::Ret::Code::BadArgs);
2210 : }
2211 :
2212 0 : interaction->addIntervalToSelectedNotes(command.interval);
2213 :
2214 0 : seekSelectedElement();
2215 :
2216 0 : return muse::make_ok();
2217 0 : }
2218 :
2219 0 : muse::Ret NotationActionController::addText(const AddTextCommand& command)
2220 : {
2221 0 : TRACEFUNC;
2222 :
2223 0 : auto interaction = currentNotationInteraction();
2224 0 : if (!interaction) {
2225 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2226 : }
2227 :
2228 0 : interaction->addText(command.type);
2229 0 : return muse::make_ok();
2230 0 : }
2231 :
2232 0 : muse::Ret NotationActionController::addBeam(const AddBeamCommand& command)
2233 : {
2234 0 : auto interaction = currentNotationInteraction();
2235 0 : if (!interaction) {
2236 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2237 : }
2238 :
2239 0 : interaction->addBeamToSelectedChordRests(command.mode);
2240 0 : return muse::make_ok();
2241 0 : }
2242 :
2243 0 : muse::Ret NotationActionController::addOrnament(const AddOrnamentCommand& command)
2244 : {
2245 0 : auto interaction = currentNotationInteraction();
2246 0 : if (!interaction) {
2247 0 : return muse::make_ret(muse::Ret::Code::InternalError);
2248 : }
2249 :
2250 0 : interaction->toggleOrnament(command.type);
2251 0 : return muse::make_ok();
2252 0 : }
2253 :
2254 0 : void NotationActionController::unrollRepeats()
2255 : {
2256 0 : NOT_IMPLEMENTED;
2257 : // TODO: https://github.com/musescore/MuseScore/issues/9670
2258 0 : }
2259 :
2260 0 : void NotationActionController::toggleMmrest()
2261 : {
2262 0 : auto interaction = currentNotationInteraction();
2263 0 : if (!interaction) {
2264 0 : return;
2265 : }
2266 :
2267 0 : interaction->execute(&mu::engraving::Score::cmdToggleMmrest, TranslatableString("undoableAction", "Toggle multimeasure rests"));
2268 0 : }
2269 :
2270 0 : void NotationActionController::toggleHideEmpty()
2271 : {
2272 0 : auto interaction = currentNotationInteraction();
2273 0 : if (!interaction) {
2274 0 : return;
2275 : }
2276 :
2277 0 : interaction->execute(&mu::engraving::Score::cmdToggleHideEmpty, TranslatableString("undoableAction", "Toggle empty staves"));
2278 0 : }
2279 :
2280 0 : void NotationActionController::addFullMeasureRest()
2281 : {
2282 0 : auto interaction = currentNotationInteraction();
2283 0 : if (!interaction) {
2284 0 : return;
2285 : }
2286 :
2287 0 : interaction->execute(&mu::engraving::Score::cmdFullMeasureRest, TranslatableString("undoableAction", "Enter full measure rest"));
2288 0 : }
2289 :
2290 0 : void NotationActionController::addStretch(qreal value)
2291 : {
2292 0 : TRACEFUNC;
2293 0 : auto interaction = currentNotationInteraction();
2294 0 : if (!interaction) {
2295 0 : return;
2296 : }
2297 :
2298 0 : auto selection = currentNotationSelection();
2299 0 : if (!selection) {
2300 0 : return;
2301 : }
2302 :
2303 0 : if (!selection->isRange()) {
2304 0 : return;
2305 : }
2306 :
2307 0 : interaction->addStretch(value);
2308 0 : }
2309 :
2310 0 : void NotationActionController::resetStretch()
2311 : {
2312 0 : TRACEFUNC;
2313 0 : auto interaction = currentNotationInteraction();
2314 0 : if (!interaction) {
2315 0 : return;
2316 : }
2317 :
2318 0 : auto selection = currentNotationSelection();
2319 0 : if (!selection) {
2320 0 : return;
2321 : }
2322 :
2323 0 : if (!selection->isRange()) {
2324 0 : return;
2325 : }
2326 :
2327 0 : interaction->resetStretch();
2328 0 : }
2329 :
2330 0 : void NotationActionController::resetBeamMode()
2331 : {
2332 0 : TRACEFUNC;
2333 0 : auto interaction = currentNotationInteraction();
2334 0 : if (!interaction) {
2335 0 : return;
2336 : }
2337 :
2338 0 : auto selection = currentNotationSelection();
2339 0 : if (!selection) {
2340 0 : return;
2341 : }
2342 :
2343 0 : if (selection->isNone() || selection->isRange()) {
2344 0 : interaction->resetBeamMode();
2345 : }
2346 0 : }
2347 :
2348 0 : muse::Ret NotationActionController::openEditStyleDialog(const muse::rcommand::Params& params)
2349 : {
2350 0 : UriQuery uri("musescore://notation/style");
2351 :
2352 0 : if (params.contains("page_code")) {
2353 0 : uri.addParam("currentPageCode", Val(params.at("page_code").toString()));
2354 :
2355 0 : if (params.contains("sub_page_code")) {
2356 0 : uri.addParam("currentSubPageCode", Val(params.at("sub_page_code").toString()));
2357 : }
2358 : }
2359 :
2360 0 : interactive()->open(uri);
2361 0 : return muse::make_ok();
2362 0 : }
2363 :
2364 0 : void NotationActionController::openPageSettingsDialog()
2365 : {
2366 0 : interactive()->open("musescore://notation/pagesettings");
2367 0 : }
2368 :
2369 0 : void NotationActionController::openStaffProperties()
2370 : {
2371 0 : interactive()->open("musescore://notation/staffproperties");
2372 0 : }
2373 :
2374 0 : void NotationActionController::openEditStringsDialog()
2375 : {
2376 0 : interactive()->open("musescore://notation/editstrings");
2377 0 : }
2378 :
2379 0 : void NotationActionController::openBreaksDialog()
2380 : {
2381 0 : interactive()->open("musescore://notation/breaks");
2382 0 : }
2383 :
2384 0 : void NotationActionController::openTransposeDialog()
2385 : {
2386 0 : interactive()->open("musescore://notation/transpose").onResolve(this, [this](const Val&) {
2387 0 : currentNotationInteraction()->checkAndShowError();
2388 0 : });
2389 0 : }
2390 :
2391 0 : void NotationActionController::openPartsDialog()
2392 : {
2393 0 : interactive()->open("musescore://notation/parts");
2394 0 : }
2395 :
2396 0 : muse::io::path_t NotationActionController::selectStyleFile(bool forLoad)
2397 : {
2398 0 : muse::io::path_t dir = configuration()->userStylesPath();
2399 : std::string filterName = forLoad
2400 0 : ? muse::trc("notation", "MuseScore style files")
2401 0 : : muse::trc("notation", "MuseScore style file");
2402 0 : std::vector<std::string> filter = { filterName + " (*.mss)" };
2403 : return forLoad
2404 0 : ? interactive()->selectOpeningFileSync(muse::trc("notation", "Load style"), dir, filter)
2405 0 : : interactive()->selectSavingFileSync(muse::trc("notation", "Save style"), dir, filter);
2406 0 : }
2407 :
2408 0 : void NotationActionController::loadStyle()
2409 : {
2410 0 : TRACEFUNC;
2411 0 : auto path = selectStyleFile(true);
2412 0 : if (!path.empty()) {
2413 0 : File f(path.toQString());
2414 0 : if (!f.open(IODevice::ReadOnly) || !mu::engraving::MStyle::isValid(&f)) {
2415 0 : interactive()->error(muse::trc("notation", "The style file could not be loaded."),
2416 : f.errorString());
2417 0 : return;
2418 : }
2419 0 : if (!notationStyle()->loadStyle(path.toQString(), false)) {
2420 0 : auto promise = interactive()->warning(
2421 0 : muse::trc("notation",
2422 : "Since this style file is from a different version of MuseScore Studio, your score is not guaranteed to display correctly."),
2423 0 : muse::trc("notation", "Click OK to load anyway."), { IInteractive::Button::Ok, IInteractive::Button::Cancel },
2424 0 : IInteractive::Button::Ok);
2425 :
2426 0 : promise.onResolve(this, [this, path](const IInteractive::Result& res) {
2427 0 : if (res.isButton(IInteractive::Button::Ok)) {
2428 0 : notationStyle()->loadStyle(path.toQString(), true);
2429 : }
2430 0 : });
2431 0 : }
2432 0 : }
2433 0 : }
2434 :
2435 0 : void NotationActionController::saveStyle()
2436 : {
2437 0 : TRACEFUNC;
2438 0 : auto path = selectStyleFile(false);
2439 0 : if (!path.empty()) {
2440 0 : if (!notationStyle()->saveStyle(path)) {
2441 0 : interactive()->error(muse::trc("notation", "The style file could not be saved."),
2442 0 : muse::trc("notation", "An error occurred."));
2443 : }
2444 : }
2445 0 : }
2446 :
2447 0 : bool NotationActionController::isTextEditing() const
2448 : {
2449 0 : auto interaction = currentNotationInteraction();
2450 0 : if (!interaction) {
2451 0 : return false;
2452 : }
2453 :
2454 0 : return interaction->isTextEditingStarted();
2455 0 : }
2456 :
2457 0 : bool NotationActionController::isLyricsEditing() const
2458 : {
2459 0 : auto interaction = currentNotationInteraction();
2460 0 : if (!interaction) {
2461 0 : return false;
2462 : }
2463 :
2464 0 : return interaction->isTextEditingStarted()
2465 0 : && interaction->selection()->element()
2466 0 : && interaction->selection()->element()->isLyrics();
2467 0 : }
2468 :
2469 0 : muse::async::Channel<bool> NotationActionController::textEditingChanged() const
2470 : {
2471 0 : return m_textEditingChanged;
2472 : }
2473 :
2474 0 : bool NotationActionController::textNavigationAvailable() const
2475 : {
2476 0 : return resolveTextNavigationAvailable(TextNavigationType::NearNoteOrRest);
2477 : }
2478 :
2479 0 : bool NotationActionController::textNavigationByBeatsAvailable() const
2480 : {
2481 0 : return resolveTextNavigationAvailable(TextNavigationType::NearBeat);
2482 : }
2483 :
2484 0 : bool NotationActionController::textNavigationByFractionAvailable() const
2485 : {
2486 0 : return resolveTextNavigationAvailable(TextNavigationType::Fraction);
2487 : }
2488 :
2489 0 : bool NotationActionController::resolveTextNavigationAvailable(TextNavigationType type) const
2490 : {
2491 0 : if (!isEditingText()) {
2492 0 : return false;
2493 : }
2494 :
2495 : static const QList<mu::engraving::ElementType> allowedElementsForTextNavigation {
2496 : mu::engraving::ElementType::LYRICS,
2497 : mu::engraving::ElementType::HARMONY,
2498 : mu::engraving::ElementType::FIGURED_BASS,
2499 : mu::engraving::ElementType::STICKING,
2500 : mu::engraving::ElementType::FINGERING
2501 0 : };
2502 :
2503 : static const QList<mu::engraving::ElementType> allowedElementsForBeatsNavigation {
2504 : mu::engraving::ElementType::HARMONY
2505 0 : };
2506 :
2507 : static const QList<mu::engraving::ElementType> allowedElementsForFractionNavigation {
2508 : mu::engraving::ElementType::HARMONY,
2509 : mu::engraving::ElementType::FIGURED_BASS
2510 0 : };
2511 :
2512 0 : const mu::engraving::EngravingItem* element = selectedElement();
2513 0 : mu::engraving::ElementType elementType = element ? element->type() : mu::engraving::ElementType::INVALID;
2514 :
2515 0 : switch (type) {
2516 0 : case TextNavigationType::NearNoteOrRest:
2517 0 : return allowedElementsForTextNavigation.contains(elementType);
2518 0 : case TextNavigationType::NearBeat:
2519 0 : return allowedElementsForBeatsNavigation.contains(elementType);
2520 0 : case TextNavigationType::Fraction:
2521 0 : return allowedElementsForFractionNavigation.contains(elementType);
2522 : }
2523 :
2524 0 : return false;
2525 : }
2526 :
2527 0 : muse::Ret NotationActionController::nextTextElement()
2528 : {
2529 0 : navigateToTextElement(MoveDirection::Right, NEAR_NOTE_OR_REST);
2530 0 : return muse::make_ok();
2531 : }
2532 :
2533 0 : muse::Ret NotationActionController::prevTextElement()
2534 : {
2535 0 : navigateToTextElement(MoveDirection::Left, NEAR_NOTE_OR_REST);
2536 0 : return muse::make_ok();
2537 : }
2538 :
2539 0 : muse::Ret NotationActionController::nextWord()
2540 : {
2541 0 : if (!textNavigationAvailable()) {
2542 0 : return muse::make_ret(Ret::Code::NotSupported);
2543 : }
2544 :
2545 0 : navigateToTextElement(MoveDirection::Right, NEAR_NOTE_OR_REST, false);
2546 0 : return muse::make_ok();
2547 : }
2548 :
2549 0 : void NotationActionController::nextBeatTextElement()
2550 : {
2551 0 : navigateToTextElement(MoveDirection::Right);
2552 0 : }
2553 :
2554 0 : void NotationActionController::prevBeatTextElement()
2555 : {
2556 0 : navigateToTextElement(MoveDirection::Left);
2557 0 : }
2558 :
2559 0 : void NotationActionController::navigateToTextElement(MoveDirection direction, bool nearNoteOrRest, bool moveOnly)
2560 : {
2561 0 : const mu::engraving::EngravingItem* element = selectedElement();
2562 0 : if (!element) {
2563 0 : return;
2564 : }
2565 :
2566 0 : if (element->isLyrics()) {
2567 0 : currentNotationInteraction()->navigateToLyrics(direction, moveOnly);
2568 0 : } else if (element->isHarmony()) {
2569 0 : const engraving::Harmony* chordSymbol = editedChordSymbol();
2570 :
2571 : // otherwise, chord symbol will be deleted when navigating away from it
2572 0 : const bool canPlay = chordSymbol && !chordSymbol->harmonyName().empty();
2573 :
2574 0 : currentNotationInteraction()->navigateToNearHarmony(direction, nearNoteOrRest);
2575 :
2576 0 : if (canPlay) {
2577 0 : playbackController()->playElements({ chordSymbol });
2578 : }
2579 0 : } else if (element->isFiguredBass()) {
2580 0 : currentNotationInteraction()->navigateToNearFiguredBass(direction);
2581 : } else {
2582 0 : currentNotationInteraction()->navigateToNearText(direction);
2583 : }
2584 : }
2585 :
2586 0 : muse::Ret NotationActionController::editTextAdvance(const EditTextAdvanceCommand& command)
2587 : {
2588 0 : TRACEFUNC;
2589 :
2590 0 : if (!textNavigationByFractionAvailable()) {
2591 0 : return muse::make_ret(muse::Ret::Code::NotAllowed);
2592 : }
2593 :
2594 0 : const Fraction fraction = TDuration(command.duration).fraction();
2595 0 : navigateToTextItemByFraction(fraction);
2596 :
2597 0 : return muse::make_ok();
2598 0 : }
2599 :
2600 0 : void NotationActionController::navigateToTextItemByFraction(const Fraction& fraction)
2601 : {
2602 0 : const mu::engraving::EngravingItem* element = selectedElement();
2603 0 : if (!element) {
2604 0 : return;
2605 : }
2606 :
2607 0 : if (element->isHarmony()) {
2608 0 : const engraving::Harmony* chordSymbol = editedChordSymbol();
2609 :
2610 : // otherwise, chord symbol will be deleted when navigating away from it
2611 0 : const bool canPlay = chordSymbol && !chordSymbol->harmonyName().empty();
2612 :
2613 0 : currentNotationInteraction()->navigateToHarmony(fraction);
2614 :
2615 0 : if (canPlay) {
2616 0 : playbackController()->playElements({ chordSymbol });
2617 : }
2618 0 : } else if (element->isFiguredBass()) {
2619 0 : currentNotationInteraction()->navigateToFiguredBass(fraction);
2620 : }
2621 : }
2622 :
2623 0 : void NotationActionController::navigateToTextElementInNearMeasure(MoveDirection direction)
2624 : {
2625 0 : const mu::engraving::EngravingItem* element = selectedElement();
2626 0 : if (!element) {
2627 0 : return;
2628 : }
2629 :
2630 0 : if (element->isHarmony()) {
2631 0 : const engraving::Harmony* chordSymbol = editedChordSymbol();
2632 :
2633 : // otherwise, chord symbol will be deleted when navigating away from it
2634 0 : const bool canPlay = chordSymbol && !chordSymbol->harmonyName().empty();
2635 :
2636 0 : currentNotationInteraction()->navigateToHarmonyInNearMeasure(direction);
2637 :
2638 0 : if (canPlay) {
2639 0 : playbackController()->playElements({ chordSymbol });
2640 : }
2641 0 : } else if (element->isFiguredBass()) {
2642 0 : currentNotationInteraction()->navigateToFiguredBassInNearMeasure(direction);
2643 : }
2644 : }
2645 :
2646 0 : bool NotationActionController::isEditingText() const
2647 : {
2648 0 : auto interaction = currentNotationInteraction();
2649 0 : if (!interaction) {
2650 0 : return false;
2651 : }
2652 :
2653 0 : return interaction->isTextEditingStarted();
2654 0 : }
2655 :
2656 0 : muse::Ret NotationActionController::openTupletOtherDialog()
2657 : {
2658 0 : interactive()->open("musescore://notation/othertupletdialog");
2659 0 : return muse::make_ok();
2660 : }
2661 :
2662 0 : void NotationActionController::openStaffTextPropertiesDialog()
2663 : {
2664 0 : interactive()->open("musescore://notation/stafftextproperties");
2665 0 : }
2666 :
2667 0 : void NotationActionController::openMeasurePropertiesDialog()
2668 : {
2669 0 : if (currentNotationInteraction()->selectedMeasure() != nullptr) {
2670 0 : interactive()->open("musescore://notation/measureproperties");
2671 : }
2672 0 : }
2673 :
2674 0 : void NotationActionController::openEditGridSizeDialog()
2675 : {
2676 0 : interactive()->open("musescore://notation/editgridsize");
2677 0 : }
2678 :
2679 0 : void NotationActionController::openRealizeChordSymbolsDialog()
2680 : {
2681 0 : interactive()->open("musescore://notation/realizechordsymbols");
2682 0 : }
2683 :
2684 0 : void NotationActionController::toggleScoreConfig(ScoreConfigType configType)
2685 : {
2686 0 : TRACEFUNC;
2687 0 : auto interaction = currentNotationInteraction();
2688 0 : if (!interaction) {
2689 0 : return;
2690 : }
2691 :
2692 0 : ScoreConfig config = interaction->scoreConfig();
2693 :
2694 0 : bool isShow = config.isShown(configType);
2695 0 : config.setShown(configType, !isShow);
2696 :
2697 0 : interaction->setScoreConfig(config);
2698 0 : }
2699 :
2700 0 : void NotationActionController::toggleConcertPitch()
2701 : {
2702 0 : TRACEFUNC;
2703 0 : INotationStylePtr style = notationStyle();
2704 0 : if (!style) {
2705 0 : return;
2706 : }
2707 :
2708 0 : bool toggle = !style->styleValue(StyleId::concertPitch).toBool();
2709 :
2710 : const TranslatableString actionName = toggle
2711 0 : ? TranslatableString("undoableAction", "Display concert pitch")
2712 0 : : TranslatableString("undoableAction", "Display transposed");
2713 :
2714 0 : currentNotationUndoStack()->prepareChanges(actionName);
2715 0 : style->setStyleValue(StyleId::concertPitch, toggle);
2716 0 : currentNotationUndoStack()->commitChanges();
2717 0 : }
2718 :
2719 0 : void NotationActionController::seekAndPlaySelectedElement(bool playChord)
2720 : {
2721 0 : seekSelectedElement();
2722 0 : playSelectedElement(playChord);
2723 0 : }
2724 :
2725 0 : void NotationActionController::seekSelectedElement()
2726 : {
2727 0 : const IMasterNotationPtr master = currentMasterNotation();
2728 0 : if (!master || master->playback()->isLoopEnabled()) {
2729 0 : return;
2730 : }
2731 :
2732 0 : const EngravingItem* element = selectedElement();
2733 0 : if (!element) {
2734 0 : return;
2735 : }
2736 :
2737 0 : playbackController()->seekElement(element);
2738 0 : }
2739 :
2740 0 : void NotationActionController::playSelectedElement(bool playChord)
2741 : {
2742 0 : TRACEFUNC;
2743 :
2744 0 : const EngravingItem* element = selectedElement();
2745 0 : if (!element) {
2746 0 : return;
2747 : }
2748 :
2749 0 : if (playChord) {
2750 0 : element = element->elementBase();
2751 : }
2752 :
2753 0 : playbackController()->playElements({ element });
2754 :
2755 0 : currentNotationScore()->setPlayChord(false);
2756 0 : currentNotationScore()->setPlayNote(false);
2757 0 : }
2758 :
2759 0 : bool NotationActionController::toggleNoteInputAllowed() const
2760 : {
2761 0 : if (!isNoteInputAllowed()) {
2762 0 : return false;
2763 : }
2764 :
2765 : //! NOTE: We're more strict about starting note input mode than exiting it.
2766 0 : if (!isNoteInputMode() && isEditingElement()) {
2767 0 : return false;
2768 : }
2769 :
2770 0 : return true;
2771 : }
2772 :
2773 0 : void NotationActionController::startNoteInput()
2774 : {
2775 0 : INotationNoteInputPtr noteInput = currentNotationNoteInput();
2776 0 : if (noteInput) {
2777 0 : noteInput->startNoteInput(configuration()->defaultNoteInputMethod());
2778 : }
2779 0 : }
2780 :
2781 0 : bool NotationActionController::hasSelection() const
2782 : {
2783 0 : return currentNotationSelection() ? !currentNotationSelection()->isNone() : false;
2784 : }
2785 :
2786 0 : bool NotationActionController::isSingleSelection() const
2787 : {
2788 0 : return currentNotationSelection() ? currentNotationSelection()->isSingle() : false;
2789 : }
2790 :
2791 0 : bool NotationActionController::isRangeSelection() const
2792 : {
2793 0 : return currentNotationSelection() ? currentNotationSelection()->isRange() : false;
2794 : }
2795 :
2796 0 : mu::engraving::EngravingItem* NotationActionController::selectedElement() const
2797 : {
2798 0 : auto selection = currentNotationSelection();
2799 0 : return selection ? selection->element() : nullptr;
2800 0 : }
2801 :
2802 0 : bool NotationActionController::isNoteOrRestSelected() const
2803 : {
2804 0 : if (isNoteInputMode()) {
2805 0 : return true;
2806 : }
2807 :
2808 0 : INotationSelectionPtr selection = currentNotationInteraction() ? currentNotationInteraction()->selection() : nullptr;
2809 0 : return selection && selection->elementsSelected(NOTE_REST_TYPES);
2810 0 : }
2811 :
2812 0 : bool NotationActionController::isHairpinSelected() const
2813 : {
2814 0 : INotationSelectionPtr selection = currentNotationInteraction() ? currentNotationInteraction()->selection() : nullptr;
2815 0 : return selection && selection->element() && selection->element()->isHairpinSegment();
2816 0 : }
2817 :
2818 0 : const mu::engraving::Harmony* NotationActionController::editedChordSymbol() const
2819 : {
2820 0 : INotationInteractionPtr interaction = currentNotationInteraction();
2821 0 : if (!interaction) {
2822 0 : return nullptr;
2823 : }
2824 :
2825 0 : const TextBase* text = interaction->editedText();
2826 0 : if (!text || !text->isHarmony()) {
2827 0 : return nullptr;
2828 : }
2829 :
2830 0 : return toHarmony(text);
2831 0 : }
2832 :
2833 0 : bool NotationActionController::elementHasPopup(const EngravingItem* e) const
2834 : {
2835 0 : return AbstractElementPopupModel::hasElementEditPopup(e);
2836 : }
2837 :
2838 0 : bool NotationActionController::canUndo() const
2839 : {
2840 0 : return currentNotationUndoStack() ? currentNotationUndoStack()->canUndo() : false;
2841 : }
2842 :
2843 0 : bool NotationActionController::canRedo() const
2844 : {
2845 0 : return currentNotationUndoStack() ? currentNotationUndoStack()->canRedo() : false;
2846 : }
2847 :
2848 0 : muse::async::Notification NotationActionController::stackChanged() const
2849 : {
2850 0 : return m_stackChanged;
2851 : }
2852 :
2853 0 : bool NotationActionController::isTablatureStaff() const
2854 : {
2855 0 : return isNotEditingElement() && currentNotationScore()->inputState().staffGroup() == mu::engraving::StaffGroup::TAB;
2856 : }
2857 :
2858 0 : INotationAutomationPtr NotationActionController::automation() const
2859 : {
2860 0 : return currentMasterNotation() ? currentMasterNotation()->automation() : nullptr;
2861 : }
2862 :
2863 0 : bool NotationActionController::isAutomationModeEnabled() const
2864 : {
2865 0 : return automation() ? automation()->isAutomationModeEnabled() : false;
2866 : }
2867 :
2868 0 : muse::async::Notification NotationActionController::automationModeEnabledChanged() const
2869 : {
2870 0 : return m_automationModeEnabledChanged;
2871 : }
2872 :
2873 0 : bool NotationActionController::isDebuggingCommandEnabled(const muse::rcommand::Command& command) const
2874 : {
2875 0 : auto it = s_debuggingCommands.find(command);
2876 0 : if (it != s_debuggingCommands.cend()) {
2877 0 : return engravingConfiguration()->debuggingOptions().*(it->second);
2878 : }
2879 :
2880 0 : return false;
2881 : }
2882 :
2883 0 : muse::async::Notification NotationActionController::debuggingOptionsChanged() const
2884 : {
2885 0 : return engravingConfiguration()->debuggingOptionsChanged();
2886 : }
2887 :
2888 0 : bool NotationActionController::isEditingElement() const
2889 : {
2890 0 : auto interaction = currentNotationInteraction();
2891 0 : if (interaction) {
2892 0 : return interaction->isEditingElement() || interaction->isDragStarted();
2893 : }
2894 0 : return false;
2895 0 : }
2896 :
2897 0 : bool NotationActionController::isNotEditingElement() const
2898 : {
2899 0 : return !isEditingElement();
2900 : }
2901 :
2902 0 : void NotationActionController::checkForScoreCorruptions()
2903 : {
2904 0 : project::INotationProjectPtr project = projectSession()->project();
2905 0 : if (!project) {
2906 0 : return;
2907 : }
2908 :
2909 0 : String fileName = io::filename(project->path()).toString();
2910 :
2911 0 : Ret ret = project->masterNotation()->masterScore()->sanityCheck();
2912 0 : if (ret) {
2913 0 : std::string title = muse::mtrc("project", "File “%1” seems not corrupted").arg(fileName).toStdString();
2914 0 : std::string body = muse::trc("project", "This file does not seem to contain errors.");
2915 0 : interactive()->info(title, body);
2916 0 : } else {
2917 0 : std::string title = muse::mtrc("project", "File “%1” is corrupted").arg(fileName).toStdString();
2918 0 : IInteractive::Text text;
2919 0 : text.text = muse::trc("project", "This file contains errors that could cause MuseScore Studio to malfunction. "
2920 0 : "Please fix those at the earliest, to prevent crashes and further corruptions.");
2921 0 : text.detailedText = ret.text();
2922 :
2923 0 : interactive()->warning(title, text);
2924 0 : }
2925 0 : }
2926 :
2927 0 : void NotationActionController::toggleAutomation()
2928 : {
2929 0 : TRACEFUNC;
2930 :
2931 0 : IMasterNotationPtr masterNotation = currentMasterNotation();
2932 0 : if (!masterNotation) {
2933 0 : return;
2934 : }
2935 :
2936 0 : const bool isEnabled = masterNotation->automation()->isAutomationModeEnabled();
2937 0 : masterNotation->automation()->setAutomationModeEnabled(!isEnabled);
2938 0 : }
2939 :
2940 0 : muse::Ret NotationActionController::selectAutomationType(const muse::rcommand::Params& params)
2941 : {
2942 0 : const std::optional<AutomationLane> lane = automationLaneFromString(params.at("type").toString());
2943 0 : if (!lane) {
2944 0 : return muse::make_ret(Ret::Code::BadArgs);
2945 : }
2946 :
2947 0 : configuration()->setCurrentAutomationLane(*lane);
2948 :
2949 0 : return muse::make_ok();
2950 : }
2951 :
2952 0 : bool NotationActionController::isNoteInputActionAllowed() const
2953 : {
2954 0 : if (!isNoteInputMode() && !toggleNoteInputAllowed()) {
2955 0 : return false;
2956 : }
2957 :
2958 0 : return !isTablatureStaff();
2959 : }
2960 :
2961 : // COMMANDS
2962 : template<muse::rcommand::TypedCommand C>
2963 0 : void NotationActionController::registerCommand(std::function<muse::Ret()> handler)
2964 : {
2965 0 : commandDispatcher()->onRequest<C>(this, [this, handler](const C&) {
2966 0 : if (!commandsState()->commandState(C::id).enabled) {
2967 0 : return muse::make_ret(Ret::Code::NotSupported);
2968 : }
2969 0 : return handler();
2970 : });
2971 0 : }
2972 :
2973 : template<muse::rcommand::TypedCommand C>
2974 0 : void NotationActionController::registerCommand(muse::Ret (NotationActionController::* handler)())
2975 : {
2976 0 : commandDispatcher()->onRequest<C>(this, [this, handler](const C&) {
2977 0 : if (!commandsState()->commandState(C::id).enabled) {
2978 0 : return muse::make_ret(Ret::Code::NotSupported);
2979 : }
2980 0 : return (this->*handler)();
2981 : });
2982 0 : }
2983 :
2984 : template<muse::rcommand::TypedCommand C>
2985 0 : void NotationActionController::registerCommand(muse::Ret (NotationActionController::* handler)(const C&))
2986 : {
2987 0 : commandDispatcher()->onRequest<C>(this, [this, handler](const C& command) {
2988 0 : if (!commandsState()->commandState(C::id).enabled) {
2989 0 : LOGD() << "command: " << C::id.toString() << " is disabled";
2990 0 : return muse::make_ret(Ret::Code::NotAllowed);
2991 : }
2992 :
2993 0 : muse::Ret ret = (this->*handler)(command);
2994 0 : return ret;
2995 0 : });
2996 0 : }
2997 :
2998 : template<muse::rcommand::HasOutput C>
2999 0 : void NotationActionController::registerRequestCommand(muse::RetVal<typename C::Output>(NotationActionController::* handler)() const)
3000 : {
3001 0 : commandDispatcher()->onRequest<C>(this, [this, handler](const C&) {
3002 0 : if (!commandsState()->commandState(C::id).enabled) {
3003 0 : return muse::RetVal<typename C::Output>::make_ret(Ret::Code::NotAllowed);
3004 : }
3005 :
3006 0 : muse::RetVal<typename C::Output> ret = (this->*handler)();
3007 0 : return ret;
3008 0 : });
3009 0 : }
3010 :
3011 : template<muse::rcommand::HasOutput C>
3012 : void NotationActionController::registerRequestCommand(muse::RetVal<typename C::Output>(NotationActionController::* handler)(const C&) const)
3013 : {
3014 : commandDispatcher()->onRequest<C>(this, [this, handler](const C& command) {
3015 : if (!commandsState()->commandState(C::id).enabled) {
3016 : return muse::RetVal<typename C::Output>::make_ret(Ret::Code::NotAllowed);
3017 : }
3018 :
3019 : muse::RetVal<typename C::Output> ret = (this->*handler)(command);
3020 : return ret;
3021 : });
3022 : }
3023 :
3024 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command, std::function<void()> handler)
3025 : {
3026 0 : registerCommand(command, handler, nullptr);
3027 0 : }
3028 :
3029 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command,
3030 : std::function<void()> handler,
3031 : bool (NotationActionController::* enabler)() const)
3032 : {
3033 0 : commandDispatcher()->onRequest(this, command, [this, command, handler, enabler]() {
3034 0 : if (!commandsState()->commandState(command).enabled) {
3035 0 : return muse::make_ret(Ret::Code::NotAllowed);
3036 : }
3037 :
3038 0 : if (enabler && !(this->*enabler)()) {
3039 0 : return muse::make_ret(Ret::Code::NotAllowed);
3040 : }
3041 :
3042 0 : handler();
3043 0 : return muse::make_ok();
3044 : });
3045 0 : }
3046 :
3047 0 : void NotationActionController::registerCommandWithParams(const muse::rcommand::Command& command,
3048 : std::function<muse::Ret(const muse::rcommand::Params&)> handler)
3049 : {
3050 0 : commandDispatcher()->onRequest(this, command, [this, command, handler](const rcommand::Request& request) {
3051 0 : if (!commandsState()->commandState(command).enabled) {
3052 0 : return rcommand::make_response(request, muse::make_ret(Ret::Code::NotSupported));
3053 : }
3054 :
3055 0 : muse::Ret ret = handler(request.params);
3056 0 : return rcommand::make_response(request, ret);
3057 0 : });
3058 0 : }
3059 :
3060 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command, void (NotationActionController::* handler)())
3061 : {
3062 0 : registerCommand(command, handler, nullptr);
3063 0 : }
3064 :
3065 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command,
3066 : void (NotationActionController::* handler)(),
3067 : bool (NotationActionController::* enabler)() const)
3068 : {
3069 0 : commandDispatcher()->onRequest(this, command, [this, command, handler, enabler]() {
3070 0 : if (!commandsState()->commandState(command).enabled) {
3071 0 : return muse::make_ret(Ret::Code::NotSupported);
3072 : }
3073 :
3074 0 : if (enabler && !(this->*enabler)()) {
3075 0 : return muse::make_ret(Ret::Code::NotSupported);
3076 : }
3077 :
3078 0 : (this->*handler)();
3079 0 : return muse::make_ok();
3080 : });
3081 0 : }
3082 :
3083 0 : void NotationActionController::registerCommandWithParams(const muse::rcommand::Command& command,
3084 : muse::Ret (NotationActionController::* handler)(const muse::rcommand::Params&))
3085 : {
3086 0 : commandDispatcher()->onRequest(this, command, [this, command, handler](const rcommand::Request& request) {
3087 0 : if (!commandsState()->commandState(command).enabled) {
3088 0 : return rcommand::make_response(request, muse::make_ret(Ret::Code::NotSupported));
3089 : }
3090 :
3091 0 : muse::Ret ret = (this->*handler)(request.params);
3092 0 : return rcommand::make_response(request, ret);
3093 0 : });
3094 0 : }
3095 :
3096 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command,
3097 : void (INotationInteraction::* handler)(),
3098 : PlayMode playMode,
3099 : bool (NotationActionController::* enabler)() const)
3100 : {
3101 0 : registerCommand(command, [this, handler, playMode, enabler]()
3102 : {
3103 0 : if (enabler && !(this->*enabler)()) {
3104 0 : return muse::make_ret(Ret::Code::NotSupported);
3105 : }
3106 :
3107 0 : INotationPtr notation = currentNotation();
3108 0 : if (notation) {
3109 0 : (notation->interaction().get()->*handler)();
3110 :
3111 0 : seekSelectedElement();
3112 :
3113 0 : if (playMode != PlayMode::NoPlay) {
3114 0 : playSelectedElement(playMode == PlayMode::PlayChord);
3115 : }
3116 : }
3117 :
3118 0 : return muse::make_ok();
3119 0 : });
3120 0 : }
3121 :
3122 : template<class P1>
3123 0 : void NotationActionController::registerCommand(const muse::rcommand::Command& command,
3124 : void (INotationInteraction::* handler)(P1), P1 param1,
3125 : PlayMode playMode,
3126 : bool (NotationActionController::* enabler)() const)
3127 : {
3128 0 : registerCommand(command, [this, handler, param1, playMode]()
3129 : {
3130 0 : INotationPtr notation = currentNotation();
3131 0 : if (notation) {
3132 0 : (notation->interaction().get()->*handler)(param1);
3133 :
3134 0 : seekSelectedElement();
3135 :
3136 0 : if (playMode != PlayMode::NoPlay) {
3137 0 : playSelectedElement(playMode == PlayMode::PlayChord);
3138 : }
3139 : }
3140 0 : }, enabler);
3141 0 : }
3142 :
3143 0 : void NotationActionController::registerGetCommand(const muse::rcommand::Command& command,
3144 : std::function<muse::RetVal<muse::rcommand::Params>()> handler)
3145 : {
3146 0 : commandDispatcher()->onRequest(this, command, [this, command, handler](const rcommand::Request& request) {
3147 0 : if (!commandsState()->commandState(command).enabled) {
3148 0 : return rcommand::make_response(request, muse::make_ret(Ret::Code::NotSupported));
3149 : }
3150 :
3151 0 : muse::RetVal<muse::rcommand::Params> rv = handler();
3152 0 : return rcommand::make_response(request, rv.ret, rv.val);
3153 0 : });
3154 0 : }
3155 :
3156 0 : void NotationActionController::registerGetCommand(const muse::rcommand::Command& command,
3157 : muse::RetVal<muse::rcommand::Params>(NotationActionController::* handler)() const)
3158 : {
3159 0 : commandDispatcher()->onRequest(this, command, [this, command, handler](const rcommand::Request& request) {
3160 0 : if (!commandsState()->commandState(command).enabled) {
3161 0 : return rcommand::make_response(request, muse::make_ret(Ret::Code::NotSupported));
3162 : }
3163 :
3164 0 : muse::RetVal<muse::rcommand::Params> rv = (this->*handler)();
3165 0 : return rcommand::make_response(request, rv.ret, rv.val);
3166 0 : });
3167 0 : }
3168 :
3169 0 : void NotationActionController::registerViewCommand(const muse::rcommand::Command& command, void (INotationViewController::* handler)())
3170 : {
3171 0 : commandDispatcher()->onRequest(this, command, [this, command, handler]() {
3172 0 : if (!commandsState()->commandState(command).enabled) {
3173 0 : return muse::make_ret(Ret::Code::NotSupported);
3174 : }
3175 :
3176 0 : if (!m_viewController) {
3177 0 : LOGE() << "command: " << command.isValid() << " NotSupported, viewController is null";
3178 0 : return muse::make_ret(Ret::Code::NotSupported);
3179 : }
3180 :
3181 0 : (m_viewController->*handler)();
3182 0 : return muse::make_ok();
3183 : });
3184 0 : }
3185 :
3186 : template<typename P1>
3187 0 : void NotationActionController::registerViewCommand(const muse::rcommand::Command& command,
3188 : void (INotationViewController::* handler)(P1),
3189 : P1 p1)
3190 : {
3191 0 : commandDispatcher()->onRequest(this, command, [this, command, handler, p1]() {
3192 0 : if (!commandsState()->commandState(command).enabled) {
3193 0 : return muse::make_ret(Ret::Code::NotSupported);
3194 : }
3195 :
3196 0 : if (!m_viewController) {
3197 0 : LOGE() << "command: " << command.isValid() << " NotSupported, viewController is null";
3198 0 : return muse::make_ret(Ret::Code::NotSupported);
3199 : }
3200 :
3201 0 : (m_viewController->*handler)(p1);
3202 0 : return muse::make_ok();
3203 : });
3204 0 : }
3205 :
3206 0 : muse::Ret NotationActionController::zoomToPercent(const muse::rcommand::Params& params)
3207 : {
3208 0 : if (!params.contains("percent")) {
3209 0 : return muse::make_ret(Ret::Code::BadArgs);
3210 : }
3211 :
3212 0 : if (!m_viewController) {
3213 0 : LOGE() << "command: " << ZOOM_TO_PERCENT_COMMAND.toString() << " NotSupported, viewController is null";
3214 0 : return muse::make_ret(Ret::Code::NotSupported);
3215 : }
3216 :
3217 0 : int zoomPercentage = params.at("percent").toInt();
3218 0 : m_viewController->setZoom(zoomPercentage);
3219 0 : return muse::make_ok();
3220 : }
3221 :
3222 0 : muse::Ret NotationActionController::setPianoKeyboardNumberOfKeys(const muse::rcommand::Params& params)
3223 : {
3224 0 : if (!params.contains("keys")) {
3225 0 : return muse::make_ret(Ret::Code::BadArgs);
3226 : }
3227 :
3228 0 : int numberOfKeys = params.at("keys").toInt();
3229 0 : sceneConfiguration()->setPianoKeyboardNumberOfKeys(numberOfKeys);
3230 0 : return muse::make_ok();
3231 : }
|