Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "notationselection.h"
23 :
24 : #include <QMimeData>
25 :
26 : #include "engraving/dom/score.h"
27 : #include "engraving/dom/segment.h"
28 : #include "engraving/dom/measure.h"
29 : #include "engraving/dom/keysig.h"
30 : #include "engraving/dom/timesig.h"
31 : #include "engraving/dom/box.h"
32 : #include "engraving/dom/staff.h"
33 : #include "engraving/dom/part.h"
34 : #include "engraving/dom/stringdata.h"
35 : #include "engraving/dom/stafftype.h"
36 :
37 : #include "engraving/editing/edittie.h"
38 : #include "engraving/editing/navigation.h"
39 :
40 : #include "notationselectionrange.h"
41 : #include "notationerrors.h"
42 :
43 : #include "log.h"
44 : #include "thirdparty/kors_logger/src/log_base.h"
45 :
46 : using namespace muse;
47 : using namespace mu::notation;
48 : using namespace mu::engraving;
49 :
50 0 : NotationSelection::NotationSelection(IGetScore* getScore, IInteractionForSelection* interaction)
51 0 : : m_getScore(getScore), m_interaction(interaction)
52 : {
53 0 : m_range = std::make_shared<NotationSelectionRange>(getScore);
54 0 : }
55 :
56 0 : bool NotationSelection::isNone() const
57 : {
58 0 : return score()->selection().isNone();
59 : }
60 :
61 0 : bool NotationSelection::isSingle() const
62 : {
63 0 : return score()->selection().isSingle();
64 : }
65 :
66 0 : bool NotationSelection::isRange() const
67 : {
68 0 : return score()->selection().isRange();
69 : }
70 :
71 0 : SelectionState NotationSelection::state() const
72 : {
73 0 : return score()->selection().state();
74 : }
75 :
76 0 : Ret NotationSelection::canCopy() const
77 : {
78 0 : if (isNone()) {
79 0 : return make_ret(Err::EmptySelection);
80 : }
81 :
82 0 : if (!score()->selection().canCopy()) {
83 0 : return make_ret(Err::SelectCompleteTupletOrTremolo);
84 : }
85 :
86 0 : return muse::make_ok();
87 : }
88 :
89 0 : muse::ByteArray NotationSelection::mimeData() const
90 : {
91 0 : return score()->selection().mimeData();
92 : }
93 :
94 0 : QMimeData* NotationSelection::qMimeData() const
95 : {
96 0 : QString mimeType = score()->selection().mimeType();
97 0 : if (mimeType.isEmpty()) {
98 0 : return nullptr;
99 : }
100 :
101 0 : QMimeData* mimeData = new QMimeData();
102 0 : mimeData->setData(mimeType, score()->selection().mimeData().toQByteArray());
103 :
104 0 : return mimeData;
105 0 : }
106 :
107 0 : EngravingItem* NotationSelection::element() const
108 : {
109 0 : return score()->selection().element();
110 : }
111 :
112 0 : const std::vector<EngravingItem*>& NotationSelection::elements() const
113 : {
114 0 : return score()->selection().elements();
115 : }
116 :
117 0 : std::vector<Note*> NotationSelection::notes(NoteFilter filter) const
118 : {
119 0 : switch (filter) {
120 0 : case NoteFilter::All: return score()->selection().noteList();
121 0 : case NoteFilter::WithTie: return mu::engraving::EditTie::cmdTieNoteList(score()->selection(), false);
122 0 : case NoteFilter::WithSlur: {
123 0 : NOT_IMPLEMENTED;
124 0 : return {};
125 : }
126 : }
127 :
128 0 : return {};
129 : }
130 :
131 0 : muse::RectF NotationSelection::canvasBoundingRect() const
132 : {
133 0 : if (isNone()) {
134 0 : return RectF();
135 : }
136 :
137 0 : if (const mu::engraving::EngravingItem* element = score()->selection().element()) {
138 0 : return element->canvasBoundingRect();
139 : }
140 :
141 0 : RectF result;
142 :
143 0 : for (const RectF& rect : range()->boundingArea()) {
144 0 : result = result.united(rect);
145 0 : }
146 :
147 0 : return result;
148 : }
149 :
150 0 : INotationSelectionRangePtr NotationSelection::range() const
151 : {
152 0 : return m_range;
153 : }
154 :
155 0 : Score* NotationSelection::score() const
156 : {
157 0 : return m_getScore->score();
158 : }
159 :
160 0 : void NotationSelection::onElementHit(EngravingItem* el)
161 : {
162 0 : m_lastElementHit = el;
163 0 : }
164 :
165 0 : MeasureBase* NotationSelection::startMeasureBase() const
166 : {
167 0 : return score()->selection().startMeasureBase();
168 : }
169 :
170 0 : MeasureBase* NotationSelection::endMeasureBase() const
171 : {
172 0 : return score()->selection().endMeasureBase();
173 : }
174 :
175 0 : std::vector<MeasureBase*> NotationSelection::measureBasesInSelection() const
176 : {
177 0 : return score()->selection().measureBasesInSelection();
178 : }
179 :
180 0 : std::vector<System*> NotationSelection::selectedSystems() const
181 : {
182 0 : return score()->selection().selectedSystems();
183 : }
184 :
185 0 : std::vector<mu::engraving::Page*> NotationSelection::pagesContainingSelection() const
186 : {
187 0 : return score()->selection().pagesContainingSelection();
188 : }
189 :
190 0 : EngravingItem* NotationSelection::lastElementHit() const
191 : {
192 0 : return m_lastElementHit;
193 : }
194 :
195 0 : bool NotationSelection::elementsSelected(const mu::engraving::ElementTypeSet& types) const
196 : {
197 0 : return score()->selection().elementsSelected(types);
198 : }
199 :
200 0 : muse::Ret NotationSelection::select(SelectionTarget target)
201 : {
202 : //! TODO It's better to change the implementation,
203 : // instead of calling different methods, it's better to do this:
204 : // 1. get target elements
205 : // 2. call select method with the elements
206 0 : switch (target) {
207 0 : case SelectionTarget::Undefined:
208 0 : break;
209 0 : case SelectionTarget::FirstItem:
210 : case SelectionTarget::BeginScore:
211 0 : selectFirstElement();
212 0 : break;
213 0 : case SelectionTarget::LastItem:
214 : case SelectionTarget::EndScore:
215 0 : selectLastElement();
216 0 : break;
217 0 : case SelectionTarget::NextItem:
218 0 : moveSelection(MoveDirection::Right, MoveSelectionType::EngravingItem);
219 0 : break;
220 0 : case SelectionTarget::PrevItem:
221 0 : moveSelection(MoveDirection::Left, MoveSelectionType::EngravingItem);
222 0 : break;
223 0 : case SelectionTarget::NextSegment:
224 0 : moveSegmentSelection(MoveDirection::Right);
225 0 : break;
226 0 : case SelectionTarget::PrevSegment:
227 0 : moveSegmentSelection(MoveDirection::Left);
228 0 : break;
229 0 : case SelectionTarget::NextChord:
230 0 : moveSelection(MoveDirection::Right, MoveSelectionType::Chord);
231 0 : break;
232 0 : case SelectionTarget::PrevChord:
233 0 : moveSelection(MoveDirection::Left, MoveSelectionType::Chord);
234 0 : break;
235 0 : case SelectionTarget::NextMeasure:
236 0 : moveSelection(MoveDirection::Right, MoveSelectionType::Measure);
237 0 : break;
238 0 : case SelectionTarget::PrevMeasure:
239 0 : moveSelection(MoveDirection::Left, MoveSelectionType::Measure);
240 0 : break;
241 0 : case SelectionTarget::NextTrack:
242 0 : moveSelection(MoveDirection::Right, MoveSelectionType::Track);
243 0 : break;
244 0 : case SelectionTarget::PrevTrack:
245 0 : moveSelection(MoveDirection::Left, MoveSelectionType::Track);
246 0 : break;
247 0 : case SelectionTarget::NextFrame:
248 0 : moveSelection(MoveDirection::Right, MoveSelectionType::Frame);
249 0 : break;
250 0 : case SelectionTarget::PrevFrame:
251 0 : moveSelection(MoveDirection::Left, MoveSelectionType::Frame);
252 0 : break;
253 0 : case SelectionTarget::NextSystem:
254 0 : moveSelection(MoveDirection::Right, MoveSelectionType::System);
255 0 : break;
256 0 : case SelectionTarget::PrevSystem:
257 0 : moveSelection(MoveDirection::Left, MoveSelectionType::System);
258 0 : break;
259 0 : case SelectionTarget::BeginSystem:
260 0 : return make_ret(Ret::Code::NotImplemented);
261 : break;
262 0 : case SelectionTarget::EndSystem:
263 0 : return make_ret(Ret::Code::NotImplemented);
264 : break;
265 0 : case SelectionTarget::AboveStaff:
266 0 : moveSelection(MoveDirection::Up, MoveSelectionType::Track);
267 0 : break;
268 0 : case SelectionTarget::BelowStaff:
269 0 : moveSelection(MoveDirection::Down, MoveSelectionType::Track);
270 0 : break;
271 0 : case SelectionTarget::UpNoteInChord:
272 0 : moveChordNoteSelection(MoveDirection::Up);
273 0 : break;
274 0 : case SelectionTarget::DownNoteInChord:
275 0 : moveChordNoteSelection(MoveDirection::Down);
276 0 : break;
277 0 : case SelectionTarget::TopNoteInChord:
278 0 : selectTopOrBottomOfChord(MoveDirection::Up);
279 0 : break;
280 0 : case SelectionTarget::BottomNoteInChord:
281 0 : selectTopOrBottomOfChord(MoveDirection::Down);
282 0 : break;
283 0 : case SelectionTarget::TopStaff:
284 0 : selectTopStaff();
285 0 : break;
286 0 : case SelectionTarget::EmptyTrailingMeasure:
287 0 : selectEmptyTrailingMeasure();
288 0 : break;
289 0 : case SelectionTarget::Similar:
290 0 : selectAllSimilarElements();
291 0 : break;
292 0 : case SelectionTarget::SimilarInStaff:
293 0 : selectAllSimilarElementsInStaff();
294 0 : break;
295 0 : case SelectionTarget::SimilarInRange:
296 0 : selectAllSimilarElementsInRange();
297 0 : break;
298 0 : case SelectionTarget::NotesInChord:
299 0 : selectAllNotesInChord();
300 0 : break;
301 0 : case SelectionTarget::All:
302 0 : selectAll();
303 0 : break;
304 0 : case SelectionTarget::Section:
305 0 : selectSection();
306 0 : break;
307 : }
308 :
309 0 : return make_ret(Ret::Code::Ok);
310 : }
311 :
312 0 : muse::Ret NotationSelection::addToSelection(SelectionTarget target)
313 : {
314 0 : switch (target) {
315 0 : case SelectionTarget::NextChord:
316 0 : addToSelection(MoveDirection::Right, MoveSelectionType::Chord);
317 0 : break;
318 0 : case SelectionTarget::PrevChord:
319 0 : addToSelection(MoveDirection::Left, MoveSelectionType::Chord);
320 0 : break;
321 0 : case SelectionTarget::NextMeasure:
322 0 : addToSelection(MoveDirection::Right, MoveSelectionType::Measure);
323 0 : break;
324 0 : case SelectionTarget::PrevMeasure:
325 0 : addToSelection(MoveDirection::Left, MoveSelectionType::Measure);
326 0 : break;
327 0 : case SelectionTarget::AboveStaff:
328 0 : addToSelection(MoveDirection::Up, MoveSelectionType::Track);
329 0 : break;
330 0 : case SelectionTarget::BelowStaff:
331 0 : addToSelection(MoveDirection::Down, MoveSelectionType::Track);
332 0 : break;
333 0 : case SelectionTarget::BeginSystem:
334 0 : expandSelection(SelectionTarget::BeginSystem);
335 0 : break;
336 0 : case SelectionTarget::EndSystem:
337 0 : expandSelection(SelectionTarget::EndSystem);
338 0 : break;
339 0 : case SelectionTarget::BeginScore:
340 0 : expandSelection(SelectionTarget::BeginScore);
341 0 : break;
342 0 : case SelectionTarget::EndScore:
343 0 : expandSelection(SelectionTarget::EndScore);
344 0 : break;
345 0 : default:
346 0 : NOT_IMPLEMENTED << "SelectionTarget: " << (int)target;
347 0 : return make_ret(Ret::Code::NotImplemented);
348 : break;
349 : }
350 :
351 0 : return make_ret(Ret::Code::Ok);
352 : }
353 :
354 0 : void NotationSelection::expandSelection(SelectionTarget target)
355 : {
356 0 : ChordRest* cr = activeCr(score());
357 0 : if (!cr) {
358 0 : return;
359 : }
360 0 : ChordRest* el = 0;
361 0 : switch (target) {
362 0 : case SelectionTarget::BeginSystem: {
363 0 : Measure* measure = cr->segment()->measure()->system()->firstMeasure();
364 0 : if (measure) {
365 0 : el = measure->first()->nextChordRest(cr->track());
366 : }
367 0 : break;
368 : }
369 0 : case SelectionTarget::EndSystem: {
370 0 : Measure* measure = cr->segment()->measure()->system()->lastMeasure();
371 0 : if (measure) {
372 0 : el = measure->last()->nextChordRest(cr->track(), true);
373 : }
374 0 : break;
375 : }
376 0 : case SelectionTarget::BeginScore: {
377 0 : Measure* measure = score()->firstMeasureMM();
378 0 : if (measure) {
379 0 : el = measure->first()->nextChordRest(cr->track());
380 : }
381 0 : break;
382 : }
383 0 : case SelectionTarget::EndScore: {
384 0 : Measure* measure = score()->lastMeasureMM();
385 0 : if (measure) {
386 0 : el = measure->last()->nextChordRest(cr->track(), true);
387 : }
388 0 : break;
389 : }
390 0 : default:
391 0 : NOT_SUPPORTED << "SelectionTarget: " << (int)target;
392 0 : break;
393 : }
394 :
395 0 : if (el) {
396 0 : select({ el }, SelectType::RANGE, el->staffIdx());
397 : }
398 : }
399 :
400 0 : muse::Ret NotationSelection::select(const std::vector<EngravingItem*>& elements, SelectType type, staff_idx_t staffIndex)
401 : {
402 0 : TRACEFUNC;
403 :
404 0 : const mu::engraving::Selection& selection = score()->selection();
405 0 : const std::vector<EngravingItem*> oldSelectedElements = selection.elements();
406 0 : const mu::engraving::SelState oldSelectionState = selection.state();
407 :
408 0 : const Fraction oldStartTick = selection.tickStart();
409 0 : const Fraction oldEndTick = selection.tickEnd();
410 :
411 0 : const staff_idx_t oldStartStaff = selection.staffStart();
412 0 : const staff_idx_t oldEndStaff = selection.staffEnd();
413 :
414 0 : const Box* oldStartBox = selection.startBox();
415 0 : const Box* oldEndBox = selection.endBox();
416 :
417 0 : doSelect(elements, type, staffIndex);
418 :
419 0 : bool rangeChanged = false;
420 0 : if (selection.isRange()) {
421 0 : const bool ticksChanged = oldStartTick != selection.tickStart() || oldEndTick != selection.tickEnd();
422 0 : const bool stavesChanged = oldStartStaff != selection.staffStart() || oldEndStaff != selection.staffEnd();
423 0 : const bool boxesChanged = oldStartBox != selection.startBox() || oldEndBox != selection.endBox();
424 0 : rangeChanged = ticksChanged || stavesChanged || boxesChanged;
425 : }
426 :
427 0 : if (rangeChanged || oldSelectionState != selection.state() || oldSelectedElements != selection.elements()) {
428 0 : notifyAboutSelectionChangedIfNeed();
429 : } else {
430 0 : score()->setSelectionChanged(false);
431 : }
432 :
433 0 : return make_ret(Ret::Code::Ok);
434 0 : }
435 :
436 0 : void NotationSelection::doSelect(const std::vector<EngravingItem*>& elements, SelectType type, staff_idx_t staffIndex)
437 : {
438 0 : TRACEFUNC;
439 :
440 0 : if (elements.size() == 1 && type == SelectType::ADD && QGuiApplication::keyboardModifiers() == Qt::KeyboardModifier::ControlModifier) {
441 0 : if (score()->selection().isRange()) {
442 0 : score()->selection().setState(mu::engraving::SelState::LIST);
443 0 : score()->setUpdateAll();
444 : }
445 :
446 0 : if (elements.front()->selected()) {
447 0 : score()->deselect(elements.front());
448 0 : return;
449 : }
450 : }
451 :
452 0 : if (type == SelectType::REPLACE) {
453 0 : score()->deselectAll();
454 0 : type = SelectType::ADD;
455 : }
456 :
457 0 : if (type == SelectType::SINGLE && elements.size() == 1) {
458 0 : const mu::engraving::EngravingItem* element = elements.front();
459 0 : mu::engraving::Segment* segment = nullptr;
460 :
461 0 : if (element->isKeySig()) {
462 0 : segment = mu::engraving::toKeySig(element)->segment();
463 0 : } else if (element->isTimeSig()) {
464 0 : segment = mu::engraving::toTimeSig(element)->segment();
465 : }
466 :
467 0 : if (segment) {
468 0 : selectElementsWithSameTypeOnSegment(element->type(), segment);
469 0 : return;
470 : }
471 : }
472 :
473 0 : score()->select(elements, type, staffIndex);
474 0 : }
475 :
476 0 : void NotationSelection::clearSelection()
477 : {
478 0 : score()->deselectAll();
479 :
480 0 : notifyAboutSelectionChangedIfNeed();
481 0 : }
482 :
483 0 : void NotationSelection::notifyAboutSelectionChangedIfNeed()
484 : {
485 0 : if (!score()->selectionChanged()) {
486 0 : return;
487 : }
488 :
489 0 : TRACEFUNC;
490 :
491 0 : score()->setSelectionChanged(false);
492 :
493 0 : m_selectionChanged.notify();
494 0 : }
495 :
496 0 : muse::async::Notification NotationSelection::selectionChanged() const
497 : {
498 0 : return m_selectionChanged;
499 : }
500 :
501 0 : void NotationSelection::selectElementsWithSameTypeOnSegment(mu::engraving::ElementType elementType, mu::engraving::Segment* segment)
502 : {
503 0 : TRACEFUNC;
504 :
505 0 : IF_ASSERT_FAILED(segment) {
506 0 : return;
507 : }
508 :
509 0 : score()->deselectAll();
510 :
511 0 : std::vector<EngravingItem*> elementsToSelect;
512 :
513 0 : for (track_idx_t track = 0; track < score()->ntracks(); track += VOICES) {
514 0 : EngravingItem* element = segment->element(track);
515 0 : if (element && element->type() == elementType) {
516 0 : elementsToSelect.push_back(element);
517 : }
518 : }
519 :
520 0 : score()->select(elementsToSelect, SelectType::ADD);
521 0 : }
522 :
523 0 : void NotationSelection::selectFirstElement(bool frame)
524 : {
525 0 : if (EngravingItem* element = Navigation::firstElement(score(), frame)) {
526 0 : select({ element }, SelectType::SINGLE, element->staffIdx());
527 0 : m_interaction->showItem(element);
528 : }
529 0 : }
530 :
531 0 : void NotationSelection::selectLastElement()
532 : {
533 0 : if (EngravingItem* element = Navigation::lastElement(score())) {
534 0 : select({ element }, SelectType::SINGLE, element->staffIdx());
535 0 : m_interaction->showItem(element);
536 : }
537 0 : }
538 :
539 0 : void NotationSelection::moveSelection(MoveDirection d, MoveSelectionType type)
540 : {
541 0 : IF_ASSERT_FAILED(MoveSelectionType::Undefined != type) {
542 0 : return;
543 : }
544 :
545 0 : if (type != MoveSelectionType::String) {
546 0 : IF_ASSERT_FAILED(MoveDirection::Left == d || MoveDirection::Right == d) {
547 0 : return;
548 : }
549 : } else {
550 0 : IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
551 0 : return;
552 : }
553 : }
554 :
555 0 : if (MoveSelectionType::EngravingItem == type) {
556 0 : moveElementSelection(d);
557 0 : return;
558 : }
559 :
560 0 : if (MoveSelectionType::String == type) {
561 0 : moveStringSelection(d);
562 0 : return;
563 : }
564 :
565 : // TODO: rewrite, Score::move( ) logic needs to be here
566 :
567 0 : auto typeToString = [](MoveSelectionType type) {
568 0 : switch (type) {
569 0 : case MoveSelectionType::Undefined: return QString();
570 0 : case MoveSelectionType::EngravingItem: return QString();
571 0 : case MoveSelectionType::Chord: return QString("chord");
572 0 : case MoveSelectionType::Measure: return QString("measure");
573 0 : case MoveSelectionType::Track: return QString("track");
574 0 : case MoveSelectionType::Frame: return QString("frame");
575 0 : case MoveSelectionType::System: return QString("system");
576 0 : case MoveSelectionType::String: return QString();
577 : }
578 0 : return QString();
579 : };
580 :
581 0 : QString cmd;
582 0 : if (MoveDirection::Left == d) {
583 0 : cmd = "prev-";
584 0 : } else if (MoveDirection::Right == d) {
585 0 : cmd = "next-";
586 : }
587 :
588 0 : cmd += typeToString(type);
589 :
590 0 : mu::engraving::EngravingItem* item = Navigation::move(score(), cmd);
591 0 : m_interaction->resetHitElementContext();
592 :
593 0 : notifyAboutSelectionChangedIfNeed();
594 0 : m_interaction->showItem(item);
595 0 : }
596 :
597 0 : ChordRest* NotationSelection::activeCr(const mu::engraving::Score* score) const
598 : {
599 0 : ChordRest* cr = score->selection().activeCR();
600 0 : if (!cr) {
601 0 : cr = score->selection().lastChordRest();
602 0 : if (!cr && score->noteEntryMode()) {
603 0 : cr = score->inputState().cr();
604 : }
605 : }
606 0 : return cr;
607 : }
608 :
609 0 : void NotationSelection::addToSelection(MoveDirection d, MoveSelectionType type)
610 : {
611 0 : ChordRest* cr = activeCr(score());
612 0 : if (!cr) {
613 0 : return;
614 : }
615 0 : ChordRest* el = 0;
616 0 : switch (type) {
617 0 : case MoveSelectionType::Chord: {
618 0 : ChordRestNavigateOptions options;
619 0 : options.skipGrace = true;
620 0 : if (d == MoveDirection::Right) {
621 0 : el = mu::engraving::Navigation::nextChordRest(cr, options);
622 : } else {
623 0 : el = mu::engraving::Navigation::prevChordRest(cr, options);
624 : }
625 0 : break;
626 : }
627 0 : case MoveSelectionType::Measure:
628 0 : if (d == MoveDirection::Right) {
629 0 : el = mu::engraving::Navigation::nextMeasure(score(), cr, true, true);
630 : } else {
631 0 : el = mu::engraving::Navigation::prevMeasure(score(), cr, true);
632 : }
633 0 : break;
634 0 : case MoveSelectionType::Track:
635 0 : if (d == MoveDirection::Up) {
636 0 : el = mu::engraving::Navigation::upStaff(cr);
637 : } else {
638 0 : el = mu::engraving::Navigation::downStaff(score(), cr);
639 : }
640 : case MoveSelectionType::EngravingItem:
641 : case MoveSelectionType::Frame:
642 : case MoveSelectionType::System:
643 : case MoveSelectionType::String:
644 : case MoveSelectionType::Undefined:
645 0 : break;
646 : }
647 :
648 0 : if (el) {
649 0 : select({ el }, SelectType::RANGE, el->staffIdx());
650 0 : m_interaction->showItem(el);
651 0 : m_interaction->resetHitElementContext();
652 : }
653 : }
654 :
655 0 : void NotationSelection::moveElementSelection(MoveDirection d)
656 : {
657 0 : EngravingItem* el = score()->selection().element();
658 0 : if (!el && !score()->selection().elements().empty()) {
659 0 : el = score()->selection().elements().back();
660 : }
661 :
662 0 : if (m_interaction->isTextEditingStarted() && el && el->isTextBase()) {
663 0 : m_interaction->navigateToNearText(d);
664 0 : return;
665 : }
666 :
667 0 : const bool isLeftDirection = MoveDirection::Left == d;
668 0 : const bool isHorizontalLayout = score()->isLayoutMode(LayoutMode::LINE) || score()->isLayoutMode(LayoutMode::HORIZONTAL_FIXED);
669 :
670 : // VBoxes are not included in horizontal layouts - skip over them (and their contents) when moving selections...
671 0 : const auto nextNonVBox = [this, isLeftDirection](EngravingItem* currElem) -> EngravingItem* {
672 0 : IF_ASSERT_FAILED(currElem) {
673 0 : return nullptr;
674 : }
675 :
676 0 : while (const EngravingItem* vBox = currElem->findAncestor(ElementType::VBOX)) {
677 0 : currElem = isLeftDirection ? toVBox(vBox)->prevMM() : toVBox(vBox)->nextMM();
678 0 : if (currElem && currElem->isMeasure()) {
679 0 : const ChordRest* cr = score()->selection().currentCR();
680 0 : const staff_idx_t si = cr ? cr->staffIdx() : 0;
681 0 : Measure* mb = toMeasure(currElem);
682 0 : currElem = isLeftDirection ? mb->prevElementStaff(si, currElem) : mb->nextElementStaff(si, currElem);
683 : }
684 :
685 0 : if (!currElem) {
686 0 : break;
687 : }
688 0 : }
689 :
690 0 : return currElem;
691 0 : };
692 :
693 0 : EngravingItem* toEl = nullptr;
694 :
695 0 : if (el) {
696 0 : toEl = isLeftDirection ? score()->prevElement() : score()->nextElement();
697 0 : if (isHorizontalLayout) {
698 0 : toEl = nextNonVBox(toEl);
699 : }
700 : } else {
701 : // Nothing currently selected (e.g. because user pressed Esc or clicked on
702 : // an empty region of the page). Try to restore previous selection.
703 0 : if (ChordRest* cr = score()->selection().currentCR()) {
704 0 : el = cr->isChord() ? toChord(cr)->upNote() : toEngravingItem(cr);
705 : }
706 0 : if (el) {
707 0 : toEl = el; // Restoring previous selection.
708 : } else {
709 0 : toEl = isLeftDirection ? Navigation::lastElement(score()) : Navigation::firstElement(score());
710 0 : if (isHorizontalLayout) {
711 0 : toEl = nextNonVBox(toEl);
712 : }
713 : }
714 : }
715 :
716 0 : if (!toEl) {
717 0 : return;
718 : }
719 :
720 0 : if (m_interaction->isEditingElement()) {
721 0 : m_interaction->endEditElement();
722 : }
723 :
724 0 : select({ toEl }, SelectType::REPLACE);
725 0 : m_interaction->resetHitElementContext();
726 0 : m_interaction->showItem(toEl);
727 :
728 0 : if (toEl->isNote() || toEl->isHarmony()) {
729 0 : score()->setPlayNote(true);
730 : }
731 :
732 0 : if (toEl->needStartEditingAfterSelecting()) {
733 0 : m_interaction->startEditElement(toEl);
734 : }
735 : }
736 :
737 0 : void NotationSelection::moveStringSelection(MoveDirection d)
738 : {
739 0 : mu::engraving::InputState& is = score()->inputState();
740 0 : mu::engraving::Staff* staff = score()->staff(track2staff(is.track()));
741 0 : int instrStrgs = static_cast<int>(staff->part()->stringData(is.tick(), staff->idx())->strings());
742 0 : int delta = (staff->staffType(is.tick())->upsideDown() ? -1 : 1);
743 :
744 0 : if (MoveDirection::Up == d) {
745 0 : delta = -delta;
746 : }
747 :
748 0 : int strg = is.string() + delta;
749 0 : if (strg >= 0 && strg < instrStrgs && strg != is.string()) {
750 0 : is.setString(strg);
751 :
752 0 : const ChordRest* chordRest = is.cr();
753 0 : if (chordRest && chordRest->isChord()) {
754 0 : const Chord* chord = toChord(chordRest);
755 :
756 0 : for (Note* note : chord->notes()) {
757 0 : if (note->string() == strg) {
758 0 : select({ note }, SelectType::SINGLE);
759 : }
760 : }
761 : }
762 : }
763 0 : }
764 :
765 0 : void NotationSelection::moveSegmentSelection(MoveDirection d)
766 : {
767 0 : IF_ASSERT_FAILED(MoveDirection::Left == d || MoveDirection::Right == d) {
768 0 : return;
769 : }
770 :
771 0 : EngravingItem* e = this->element();
772 0 : if (!e && !this->elements().empty()) {
773 0 : e = d == MoveDirection::Left ? this->elements().front() : this->elements().back();
774 : }
775 :
776 0 : if (!e || (e = d == MoveDirection::Left ? e->prevSegmentElement() : e->nextSegmentElement()) == nullptr) {
777 0 : e = d == MoveDirection::Left ? Navigation::firstElement(score()) : Navigation::lastElement(score());
778 : }
779 :
780 0 : select({ e }, SelectType::SINGLE);
781 0 : m_interaction->showItem(e);
782 : }
783 :
784 0 : void NotationSelection::moveChordNoteSelection(MoveDirection d)
785 : {
786 0 : IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
787 0 : return;
788 : }
789 :
790 0 : EngravingItem* current = this->element();
791 0 : if (!current || !(current->isNote() || current->isRest())) {
792 0 : return;
793 : }
794 :
795 : EngravingItem* chordElem;
796 0 : if (d == MoveDirection::Up) {
797 0 : chordElem = Navigation::chordNoteAbove(score(), current);
798 : } else {
799 0 : chordElem = Navigation::chordNoteBelow(score(), current);
800 : }
801 :
802 0 : if (chordElem == current) {
803 0 : return;
804 : }
805 :
806 0 : select({ chordElem }, SelectType::SINGLE, chordElem->staffIdx());
807 0 : m_interaction->showItem(chordElem);
808 : }
809 :
810 0 : void NotationSelection::selectTopOrBottomOfChord(MoveDirection d)
811 : {
812 0 : IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
813 0 : return;
814 : }
815 :
816 0 : EngravingItem* current = this->element();
817 0 : if (!current || !current->isNote()) {
818 0 : return;
819 : }
820 :
821 : EngravingItem* target = d == MoveDirection::Up
822 0 : ? Navigation::topNoteInChord(toNote(current)) : Navigation::bottomNoteInChord(toNote(current));
823 :
824 0 : if (target == current) {
825 0 : return;
826 : }
827 :
828 0 : select({ target }, SelectType::SINGLE);
829 0 : m_interaction->showItem(target);
830 : }
831 :
832 0 : void NotationSelection::selectTopStaff()
833 : {
834 0 : EngravingItem* el = Navigation::topStaff(score(), activeCr(score()));
835 0 : if (score()->noteEntryMode()) {
836 0 : score()->inputState().moveInputPos(el);
837 : }
838 :
839 0 : if (el->isChord()) {
840 0 : el = mu::engraving::toChord(el)->upNote();
841 : }
842 :
843 0 : select({ el }, SelectType::SINGLE, 0);
844 0 : m_interaction->showItem(el);
845 0 : m_interaction->resetHitElementContext();
846 0 : }
847 :
848 0 : void NotationSelection::selectEmptyTrailingMeasure()
849 : {
850 0 : ChordRest* cr = activeCr(score());
851 0 : const Measure* ftm = score()->firstTrailingMeasure(cr ? &cr : nullptr);
852 0 : if (!ftm) {
853 0 : ftm = score()->lastMeasure();
854 : }
855 0 : if (ftm) {
856 0 : if (score()->style().styleB(mu::engraving::Sid::createMultiMeasureRests) && ftm->hasMMRest()) {
857 0 : ftm = ftm->coveringMMRestOrThis();
858 : }
859 : EngravingItem* el
860 0 : = !cr ? ftm->first()->nextChordRest(0, false) : ftm->first()->nextChordRest(mu::engraving::trackZeroVoice(cr->track()), false);
861 0 : score()->inputState().moveInputPos(el);
862 0 : select({ el }, SelectType::SINGLE);
863 0 : m_interaction->resetHitElementContext();
864 : }
865 0 : }
866 :
867 0 : FilterElementsOptions NotationSelection::elementsFilterOptions(const EngravingItem* element) const
868 : {
869 0 : TRACEFUNC;
870 0 : FilterElementsOptions options;
871 0 : options.elementType = element->type();
872 :
873 0 : if (element->isNote()) {
874 0 : const mu::engraving::Note* note = dynamic_cast<const mu::engraving::Note*>(element);
875 0 : if (note->chord()->isGrace()) {
876 0 : options.subtype = -1;
877 : } else {
878 0 : options.subtype = element->subtype();
879 : }
880 0 : } else if (element->isHairpinSegment() || element->isHarmony()) {
881 0 : options.subtype = element->subtype();
882 0 : options.bySubtype = true;
883 : }
884 :
885 0 : return options;
886 0 : }
887 :
888 0 : void NotationSelection::selectAllSimilarElements()
889 : {
890 0 : TRACEFUNC;
891 0 : auto notationElements = m_interaction->elements();
892 0 : if (!notationElements) {
893 0 : return;
894 : }
895 :
896 0 : EngravingItem* selectedElement = this->element();
897 0 : if (!selectedElement) {
898 0 : return;
899 : }
900 :
901 0 : FilterElementsOptions options = elementsFilterOptions(selectedElement);
902 0 : std::vector<EngravingItem*> elements = notationElements->elements(options);
903 0 : if (elements.empty()) {
904 0 : return;
905 : }
906 :
907 0 : clearSelection();
908 :
909 0 : select(elements, SelectType::ADD);
910 0 : }
911 :
912 0 : void NotationSelection::selectAllSimilarElementsInStaff()
913 : {
914 0 : TRACEFUNC;
915 0 : auto notationElements = m_interaction->elements();
916 0 : if (!notationElements) {
917 0 : return;
918 : }
919 :
920 0 : EngravingItem* selectedElement = this->element();
921 0 : if (!selectedElement) {
922 0 : return;
923 : }
924 :
925 0 : FilterElementsOptions options = elementsFilterOptions(selectedElement);
926 0 : options.staffStart = static_cast<int>(selectedElement->staffIdx());
927 0 : options.staffEnd = options.staffStart + 1;
928 :
929 0 : std::vector<EngravingItem*> elements = notationElements->elements(options);
930 0 : if (elements.empty()) {
931 0 : return;
932 : }
933 :
934 0 : clearSelection();
935 :
936 0 : select(elements, SelectType::ADD);
937 0 : }
938 :
939 0 : void NotationSelection::selectAllSimilarElementsInRange()
940 : {
941 0 : auto elements = m_interaction->elements();
942 0 : if (!elements) {
943 0 : return;
944 : }
945 :
946 0 : mu::engraving::EngravingItem* lastHit = this->lastElementHit();
947 0 : if (!lastHit) {
948 0 : return;
949 : }
950 :
951 0 : mu::engraving::Score* score = elements->msScore();
952 0 : score->selectSimilarInRange(lastHit);
953 0 : notifyAboutSelectionChangedIfNeed();
954 0 : }
955 :
956 0 : void NotationSelection::selectAllNotesInChord()
957 : {
958 0 : TRACEFUNC;
959 :
960 0 : const std::vector<EngravingItem*>& selectedElements = this->elements();
961 0 : if (selectedElements.empty()) {
962 0 : return;
963 : }
964 :
965 0 : std::set<const Chord*> chords;
966 0 : for (const EngravingItem* item : selectedElements) {
967 0 : if (item->isNote()) {
968 0 : chords.insert(toNote(item)->chord());
969 : }
970 : }
971 :
972 0 : if (chords.empty()) {
973 0 : return;
974 : }
975 :
976 0 : std::vector<EngravingItem*> allNotes;
977 0 : for (const Chord* chord : chords) {
978 0 : for (Note* note : chord->notes()) {
979 0 : allNotes.push_back(note);
980 : }
981 : }
982 :
983 0 : clearSelection();
984 0 : select(allNotes, SelectType::ADD);
985 0 : }
986 :
987 0 : void NotationSelection::selectAll()
988 : {
989 0 : if (m_interaction->isTextEditingStarted()) {
990 0 : m_interaction->selectAllText();
991 : } else {
992 0 : score()->cmdSelectAll();
993 : }
994 :
995 0 : notifyAboutSelectionChangedIfNeed();
996 0 : }
997 :
998 0 : void NotationSelection::selectSection()
999 : {
1000 0 : score()->cmdSelectSection();
1001 :
1002 0 : notifyAboutSelectionChangedIfNeed();
1003 0 : }
|