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 "notationcontextmenumodel.h"
24 :
25 : #include "types/translatablestring.h"
26 :
27 : #include "ui/view/iconcodes.h"
28 :
29 : #include "engraving/dom/fret.h"
30 : #include "engraving/dom/gradualtempochange.h"
31 : #include "engraving/dom/harmony.h"
32 : #include "engraving/dom/part.h"
33 : #include "engraving/dom/staff.h"
34 :
35 : #include "notation/imasternotation.h"
36 : #include "notation/inotation.h"
37 : #include "notation/inotationselection.h"
38 :
39 : #include "notationscene/notationcommands.h"
40 : #include "palette/palettecommands.h"
41 : #include "instrumentsscene/instrumentscommands.h"
42 :
43 : #include "widgets/editstyleutils.h"
44 :
45 : using namespace mu::notation;
46 : using namespace mu::palette;
47 : using namespace mu::instrumentsscene;
48 : using namespace muse;
49 : using namespace muse::uicomponents;
50 :
51 0 : void NotationContextMenuModel::loadItems(int elementType)
52 : {
53 0 : AbstractMenuModel::load();
54 :
55 0 : MenuItemList items = makeItemsByElementType(static_cast<ElementType>(elementType));
56 :
57 0 : const INotationAutomationPtr automation = this->automation();
58 0 : if (automation && automation->isAutomationModeEnabled()) {
59 0 : items << makeSeparator()
60 0 : << makeMenu(TranslatableString("notation/automation", "Automation type"), makeAutomationLaneItems());
61 : }
62 :
63 0 : setItems(items);
64 0 : }
65 :
66 0 : MenuItemList NotationContextMenuModel::makeItemsByElementType(ElementType elementType)
67 : {
68 0 : switch (elementType) {
69 0 : case ElementType::MEASURE:
70 0 : return makeMeasureItems();
71 0 : case ElementType::PAGE:
72 0 : return makePageItems();
73 0 : case ElementType::STAFF_TEXT:
74 0 : return makeStaffTextItems();
75 0 : case ElementType::SYSTEM_TEXT:
76 : case ElementType::TRIPLET_FEEL:
77 0 : return makeSystemTextItems();
78 0 : case ElementType::TIMESIG:
79 0 : return makeTimeSignatureItems();
80 0 : case ElementType::INSTRUMENT_NAME:
81 0 : return makeInstrumentNameItems();
82 0 : case ElementType::HARMONY:
83 0 : return makeHarmonyItems();
84 0 : case ElementType::FRET_DIAGRAM:
85 0 : return makeFretboardDiagramItems();
86 0 : case ElementType::INSTRUMENT_CHANGE:
87 0 : return makeChangeInstrumentItems();
88 0 : case ElementType::VBOX:
89 0 : return makeVerticalBoxItems();
90 0 : case ElementType::HBOX:
91 0 : return makeHorizontalBoxItems();
92 0 : case ElementType::HAIRPIN_SEGMENT:
93 0 : return makeHairpinItems();
94 0 : case ElementType::GRADUAL_TEMPO_CHANGE_SEGMENT:
95 0 : return makeGradualTempoChangeItems();
96 0 : case ElementType::TEXT:
97 0 : return makeTextItems();
98 0 : default:
99 0 : break;
100 : }
101 :
102 0 : return makeElementItems();
103 : }
104 :
105 0 : MenuItemList NotationContextMenuModel::makePageItems()
106 : {
107 : MenuItemList items {
108 0 : makeMenuItem(OPEN_EDIT_STYLE_COMMAND),
109 0 : makeMenuItem(OPEN_PAGE_SETTINGS_COMMAND),
110 0 : makeMenuItem(LOAD_STYLE_COMMAND),
111 0 : };
112 :
113 0 : return items;
114 : }
115 :
116 0 : MenuItemList NotationContextMenuModel::makeDefaultCopyPasteItems()
117 : {
118 : MenuItemList items {
119 0 : makeMenuItem(CutCommand::id),
120 0 : makeMenuItem(CopyCommand::id),
121 0 : makeMenuItem(PasteDefaultCommand::id),
122 0 : makeMenuItem(CopyPasteSwapCommand::id),
123 0 : makeMenuItem(DeleteCommand::id),
124 0 : };
125 :
126 0 : return items;
127 : }
128 :
129 0 : MenuItemList NotationContextMenuModel::makeMeasureItems()
130 : {
131 : MenuItemList items = {
132 0 : makeMenuItem(CutCommand::id),
133 0 : makeMenuItem(CopyCommand::id),
134 0 : makeMenuItem(PasteDefaultCommand::id),
135 0 : makeMenuItem(CopyPasteSwapCommand::id),
136 0 : };
137 :
138 0 : items << makeSeparator();
139 :
140 0 : MenuItem* clearItem = makeMenuItem(DeleteCommand::id);
141 0 : clearItem->setTitle(TranslatableString("notation", "Clear measures"));
142 0 : MenuItem* deleteItem = makeMenuItem(REMOVE_SELECTED_RANGE_COMMAND);
143 0 : deleteItem->setTitle(TranslatableString("notation", "Delete measures"));
144 0 : items << clearItem;
145 0 : items << deleteItem;
146 :
147 0 : items << makeSeparator();
148 :
149 0 : if (isDrumsetStaff()) {
150 0 : items << makeMenuItem(OPEN_CUSTOMIZE_KIT_COMMAND);
151 : }
152 :
153 0 : items << makeMenuItem(OPEN_STAFF_PROPERTIES_COMMAND);
154 0 : items << makeSeparator();
155 0 : items << makeMenu(TranslatableString("notation", "Insert measures"), makeInsertMeasuresItems());
156 0 : if (projectSession()->currentNotation()->viewMode() == mu::notation::ViewMode::PAGE) {
157 0 : items << makeMenu(TranslatableString("notation", "Move measures"), makeMoveMeasureItems());
158 : }
159 0 : items << makeMenuItem(MAKE_INTO_SYSTEM_COMMAND);
160 0 : items << makeSeparator();
161 0 : items << makeMenuItem(OPEN_MEASURE_PROPERTIES_COMMAND);
162 :
163 0 : return items;
164 0 : }
165 :
166 0 : MenuItemList NotationContextMenuModel::makeStaffTextItems()
167 : {
168 0 : MenuItemList items = makeElementItems();
169 0 : items << makeSeparator();
170 0 : items << makeMenuItem(OPEN_STAFF_TEXT_PROPERTIES_COMMAND);
171 :
172 0 : return items;
173 0 : }
174 :
175 0 : MenuItemList NotationContextMenuModel::makeSystemTextItems()
176 : {
177 0 : MenuItemList items = makeElementItems();
178 0 : items << makeSeparator();
179 0 : items << makeMenuItem(OPEN_SYSTEM_TEXT_PROPERTIES_COMMAND);
180 :
181 0 : return items;
182 0 : }
183 :
184 0 : MenuItemList NotationContextMenuModel::makeTimeSignatureItems()
185 : {
186 0 : MenuItemList items = makeElementItems();
187 0 : items << makeSeparator();
188 0 : items << makeMenuItem(OPEN_TIME_SIGNATURE_PROPERTIES_COMMAND);
189 :
190 0 : return items;
191 0 : }
192 :
193 0 : MenuItemList NotationContextMenuModel::makeInstrumentNameItems()
194 : {
195 0 : MenuItemList items = makeElementItems();
196 0 : items << makeSeparator();
197 0 : items << makeMenuItem(OPEN_STAFF_PROPERTIES_COMMAND);
198 :
199 0 : return items;
200 0 : }
201 :
202 0 : MenuItemList NotationContextMenuModel::makeHarmonyItems()
203 : {
204 0 : const EngravingItem* element = currentElement();
205 0 : if (element && engraving::toHarmony(element)->isInFretBox()) {
206 0 : return makeElementInFretBoxItems();
207 : }
208 :
209 0 : MenuItemList items = makeElementItems();
210 0 : items << makeSeparator();
211 :
212 0 : if (element) {
213 0 : engraving::EngravingObject* parent = element->isHarmony() ? element->ownershipParent() : nullptr;
214 0 : bool hasLinkedFretboardDiagram = parent && parent->isFretDiagram();
215 0 : if (!hasLinkedFretboardDiagram) {
216 0 : items << makeMenuItem(ADD_FRETBOARD_DIAGRAM_COMMAND);
217 : }
218 : }
219 :
220 0 : items << makeMenuItem(OPEN_REALIZECHORDSYMBOLS_COMMAND);
221 :
222 0 : return items;
223 0 : }
224 :
225 0 : MenuItemList NotationContextMenuModel::makeFretboardDiagramItems()
226 : {
227 0 : const EngravingItem* element = currentElement();
228 0 : if (element && engraving::toFretDiagram(element)->isInFretBox()) {
229 0 : return makeElementInFretBoxItems();
230 : }
231 :
232 0 : MenuItemList items = makeElementItems();
233 :
234 0 : const engraving::FretDiagram* fretDiagram = engraving::toFretDiagram(element);
235 0 : if (!fretDiagram->harmony()) {
236 0 : items << makeSeparator();
237 0 : items << makeMenuItem(AddChordTextCommand::id, TranslatableString("notation", "Add c&hord symbol"));
238 : }
239 :
240 0 : return items;
241 0 : }
242 :
243 0 : MenuItemList NotationContextMenuModel::makeElementInFretBoxItems()
244 : {
245 : MenuItemList items {
246 0 : makeMenuItem(CopyCommand::id)
247 0 : };
248 :
249 0 : MenuItem* hideItem = makeMenuItem(DeleteCommand::id);
250 0 : hideItem->setTitle(TranslatableString("notation", "Hide"));
251 0 : hideItem->setIcon(ui::IconCode::Code::NONE);
252 :
253 0 : items << hideItem
254 0 : << makeSeparator();
255 :
256 0 : MenuItemList selectItems = makeSelectItems();
257 :
258 0 : if (!selectItems.isEmpty()) {
259 0 : items << makeMenu(TranslatableString("notation", "Select"), selectItems)
260 0 : << makeSeparator();
261 : }
262 :
263 0 : const EngravingItem* element = currentElement();
264 0 : items << makeEditStyle(element);
265 :
266 0 : return items;
267 0 : }
268 :
269 0 : MenuItemList NotationContextMenuModel::makeSelectItems()
270 : {
271 0 : if (isSingleSelection()) {
272 0 : return MenuItemList { makeMenuItem(SelectSimilarCommand {}),
273 0 : makeMenuItem(SelectSimilarInStaffCommand {}),
274 0 : makeMenuItem(OpenSelectionOptionsCommand {}) };
275 0 : } else if (canSelectSimilarInRange()) {
276 0 : return MenuItemList { makeMenuItem(SelectSimilarInRangeCommand {}),
277 0 : makeMenuItem(OpenSelectionOptionsCommand {}) };
278 0 : } else if (canSelectSimilar()) {
279 0 : return MenuItemList{ makeMenuItem(OpenSelectionOptionsCommand {}) };
280 : }
281 :
282 0 : return MenuItemList();
283 : }
284 :
285 0 : MenuItemList NotationContextMenuModel::makeElementItems()
286 : {
287 0 : MenuItemList items = makeDefaultCopyPasteItems();
288 :
289 0 : if (interaction()->isTextEditingStarted()) {
290 0 : return items;
291 : }
292 :
293 0 : MenuItemList selectItems = makeSelectItems();
294 :
295 0 : if (!selectItems.isEmpty()) {
296 0 : items << makeMenu(TranslatableString("notation", "Select"), selectItems);
297 : }
298 :
299 0 : const EngravingItem* element = currentElement();
300 :
301 0 : if (element && element->isEditable()) {
302 0 : items << makeSeparator();
303 0 : items << makeMenuItem(EDIT_ELEMENT_COMMAND);
304 : }
305 :
306 0 : items << makeSeparator()
307 0 : << makeEditStyle(element);
308 :
309 0 : return items;
310 0 : }
311 :
312 0 : MenuItemList NotationContextMenuModel::makeInsertMeasuresItems()
313 : {
314 : MenuItemList items {
315 0 : makeMenuItem(InsertMeasuresAfterSelectionCommand::id, TranslatableString("notation", "After selection…")),
316 0 : makeMenuItem(InsertMeasuresCommand::id, TranslatableString("notation", "Before selection…")),
317 0 : makeSeparator(),
318 0 : makeMenuItem(InsertMeasuresAtStartOfScoreCommand::id, TranslatableString("notation", "At start of score…")),
319 0 : makeMenuItem(AppendMeasuresCommand::id, TranslatableString("notation", "At end of score…"))
320 0 : };
321 :
322 0 : return items;
323 : }
324 :
325 0 : MenuItemList NotationContextMenuModel::makeMoveMeasureItems()
326 : {
327 : MenuItemList items {
328 0 : makeMenuItem(MOVE_MEASURE_TO_PREV_SYSTEM_COMMAND, TranslatableString("notation", "To previous system")),
329 0 : makeMenuItem(MOVE_MEASURE_TO_NEXT_SYSTEM_COMMAND, TranslatableString("notation", "To next system"))
330 0 : };
331 :
332 0 : return items;
333 : }
334 :
335 0 : MenuItemList NotationContextMenuModel::makeChangeInstrumentItems()
336 : {
337 0 : MenuItemList items = makeElementItems();
338 0 : items << makeSeparator();
339 0 : items << makeMenuItem(INSTRUMENTS_CHANGE_COMMAND);
340 :
341 0 : return items;
342 0 : }
343 :
344 0 : MenuItemList NotationContextMenuModel::makeVerticalBoxItems()
345 : {
346 0 : MenuItemList addMenuItems;
347 0 : addMenuItems << makeMenuItem(AddFrameTextCommand::id);
348 0 : addMenuItems << makeMenuItem(AddTitleTextCommand::id);
349 0 : addMenuItems << makeMenuItem(AddSubtitleTextCommand::id);
350 0 : addMenuItems << makeMenuItem(AddComposerTextCommand::id);
351 0 : addMenuItems << makeMenuItem(AddLyricistTextCommand::id);
352 0 : addMenuItems << makeMenuItem(AddPartTextCommand::id);
353 0 : addMenuItems << makeMenuItem(ADD_IMAGE_COMMAND);
354 :
355 0 : MenuItemList items = makeElementItems();
356 0 : items << makeSeparator();
357 0 : items << makeMenu(TranslatableString("notation", "Add"), addMenuItems);
358 :
359 0 : return items;
360 0 : }
361 :
362 0 : MenuItemList NotationContextMenuModel::makeHorizontalBoxItems()
363 : {
364 0 : MenuItemList addMenuItems;
365 0 : addMenuItems << makeMenuItem(AddFrameTextCommand::id);
366 0 : addMenuItems << makeMenuItem(ADD_IMAGE_COMMAND);
367 :
368 0 : MenuItemList items = makeElementItems();
369 0 : items << makeSeparator();
370 0 : items << makeMenu(TranslatableString("notation", "Add"), addMenuItems);
371 :
372 0 : return items;
373 0 : }
374 :
375 0 : MenuItemList NotationContextMenuModel::makeHairpinItems()
376 : {
377 0 : MenuItemList items = makeElementItems();
378 :
379 0 : const EngravingItem* element = currentElement();
380 0 : if (!element || !element->isHairpinSegment() || !isSingleSelection()) {
381 0 : return items;
382 : }
383 :
384 0 : items << makeSeparator();
385 :
386 0 : const engraving::Hairpin* h = toHairpinSegment(element)->hairpin();
387 :
388 0 : MenuItem* snapPrev = makeMenuItem(TOGGLE_SNAP_TO_PREV_COMMAND);
389 0 : snapPrev->setChecked(h->snapToItemBefore());
390 0 : items << snapPrev;
391 :
392 0 : MenuItem* snapNext = makeMenuItem(TOGGLE_SNAP_TO_NEXT_COMMAND);
393 0 : snapNext->setChecked(h->snapToItemAfter());
394 0 : items << snapNext;
395 :
396 0 : return items;
397 0 : }
398 :
399 0 : MenuItemList NotationContextMenuModel::makeGradualTempoChangeItems()
400 : {
401 0 : MenuItemList items = makeElementItems();
402 :
403 0 : const EngravingItem* element = currentElement();
404 0 : if (!element || !element->isGradualTempoChangeSegment() || !isSingleSelection()) {
405 0 : return items;
406 : }
407 :
408 0 : items << makeSeparator();
409 :
410 0 : const engraving::GradualTempoChange* gtc = toGradualTempoChangeSegment(element)->tempoChange();
411 :
412 0 : MenuItem* snapNext = makeMenuItem(TOGGLE_SNAP_TO_NEXT_COMMAND);
413 0 : snapNext->setChecked(gtc->snapToItemAfter());
414 0 : items << snapNext;
415 :
416 0 : return items;
417 0 : }
418 :
419 0 : MenuItemList NotationContextMenuModel::makeTextItems()
420 : {
421 0 : const EngravingItem* element = currentElement();
422 0 : if (!(element->ownershipParent() && element->ownershipParent()->isBarLine())) {
423 : // Regular text
424 0 : return makeElementItems();
425 : }
426 :
427 : // Play count text
428 0 : MenuItemList items;
429 :
430 0 : if (interaction()->isTextEditingStarted()) {
431 0 : return items;
432 : }
433 :
434 0 : MenuItemList selectItems = makeSelectItems();
435 :
436 0 : if (!selectItems.isEmpty()) {
437 0 : items << makeMenu(TranslatableString("notation", "Select"), selectItems);
438 : }
439 :
440 0 : items << makeSeparator()
441 0 : << makeEditStyle(element);
442 :
443 0 : return items;
444 0 : }
445 :
446 0 : MenuItem* NotationContextMenuModel::makeEditStyle(const EngravingItem* element)
447 : {
448 0 : MenuItem* item = makeMenuItem(OPEN_EDIT_STYLE_COMMAND);
449 :
450 0 : if (element) {
451 0 : std::string pageCode = EditStyleUtils::pageCodeForElement(element).toStdString();
452 0 : if (!pageCode.empty()) {
453 0 : rcommand::CommandQuery query(OPEN_EDIT_STYLE_COMMAND);
454 0 : query.addParam("page_code", Val(pageCode));
455 :
456 0 : std::string subPageCode = EditStyleUtils::subPageCodeForElement(element).toStdString();
457 0 : if (!subPageCode.empty()) {
458 0 : query.addParam("sub_page_code", Val(subPageCode));
459 : }
460 :
461 0 : item->setCommandQuery(query);
462 0 : }
463 0 : }
464 :
465 0 : return item;
466 : }
467 :
468 0 : bool NotationContextMenuModel::isSingleSelection() const
469 : {
470 0 : INotationSelectionPtr selection = this->selection();
471 0 : return selection ? selection->element() != nullptr : false;
472 0 : }
473 :
474 0 : bool NotationContextMenuModel::canSelectSimilar() const
475 : {
476 0 : return currentElement() != nullptr;
477 : }
478 :
479 0 : bool NotationContextMenuModel::canSelectSimilarInRange() const
480 : {
481 0 : return canSelectSimilar() && selection()->isRange();
482 : }
483 :
484 0 : bool NotationContextMenuModel::isDrumsetStaff() const
485 : {
486 0 : const INotationInteraction::HitElementContext& ctx = hitElementContext();
487 0 : if (!ctx.staff) {
488 0 : return false;
489 : }
490 :
491 0 : Fraction tick = ctx.element ? ctx.element->tick() : Fraction { -1, 1 };
492 0 : return ctx.staff->part()->instrument(tick)->drumset() != nullptr;
493 : }
494 :
495 0 : MenuItemList NotationContextMenuModel::makeAutomationLaneItems()
496 : {
497 : return {
498 0 : makeAutomationLaneItem(AutomationLane::Dynamics, TranslatableString("notation/automation", "Dynamics")),
499 0 : makeAutomationLaneItem(AutomationLane::Volume, TranslatableString("notation/automation", "Volume")),
500 0 : makeAutomationLaneItem(AutomationLane::Pan, TranslatableString("notation/automation", "Pan")),
501 0 : makeSeparator(),
502 0 : makeAutomationLaneItem(AutomationLane::MasterVolume, TranslatableString("notation/automation", "Master volume")),
503 0 : makeAutomationLaneItem(AutomationLane::MasterPan, TranslatableString("notation/automation", "Master pan")),
504 0 : makeSeparator(),
505 0 : makeAutomationLaneItem(AutomationLane::Tempo, TranslatableString("notation/automation", "Tempo")),
506 0 : };
507 : }
508 :
509 0 : MenuItem* NotationContextMenuModel::makeAutomationLaneItem(AutomationLane lane, const TranslatableString& title)
510 : {
511 0 : MenuItem* item = makeMenuItem(SELECT_AUTOMATION_TYPE_COMMAND, title);
512 0 : if (!item) {
513 0 : return item;
514 : }
515 :
516 0 : rcommand::CommandQuery query(SELECT_AUTOMATION_TYPE_COMMAND);
517 0 : query.addParam("type", Val(std::string(automationLaneToString(lane))));
518 0 : item->setCommandQuery(query);
519 :
520 0 : item->setChecked(notationConfiguration()->currentAutomationLane() == lane);
521 :
522 0 : return item;
523 0 : }
524 :
525 0 : INotationInteractionPtr NotationContextMenuModel::interaction() const
526 : {
527 0 : INotationPtr notation = projectSession()->currentNotation();
528 0 : return notation ? notation->interaction() : nullptr;
529 0 : }
530 :
531 0 : INotationSelectionPtr NotationContextMenuModel::selection() const
532 : {
533 0 : INotationPtr notation = projectSession()->currentNotation();
534 0 : return notation ? notation->interaction()->selection() : nullptr;
535 0 : }
536 :
537 0 : INotationAutomationPtr NotationContextMenuModel::automation() const
538 : {
539 0 : IMasterNotationPtr masterNotation = projectSession()->masterNotation();
540 0 : return masterNotation ? masterNotation->automation() : nullptr;
541 0 : }
542 :
543 0 : const EngravingItem* NotationContextMenuModel::currentElement() const
544 : {
545 0 : const EngravingItem* element = hitElementContext().element;
546 0 : if (element) {
547 0 : return element;
548 : }
549 :
550 0 : auto selection = this->selection();
551 0 : return selection && selection->element() ? selection->element() : nullptr;
552 0 : }
553 :
554 0 : const INotationInteraction::HitElementContext& NotationContextMenuModel::hitElementContext() const
555 : {
556 0 : if (INotationInteractionPtr interaction = this->interaction()) {
557 0 : return interaction->hitElementContext();
558 0 : }
559 :
560 : static INotationInteraction::HitElementContext dummy;
561 0 : return dummy;
562 : }
|