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 "slur.h"
23 :
24 : #include "../editing/editspanner.h"
25 : #include "../editing/mscoreview.h"
26 : #include "../editing/navigation.h"
27 : #include "../editing/transaction/transaction.h"
28 : #include "../rendering/iscorerenderer.h"
29 :
30 : #include "arpeggio.h"
31 : #include "beam.h"
32 : #include "chord.h"
33 : #include "dummyparent.h"
34 : #include "masterscore.h"
35 : #include "measure.h"
36 : #include "note.h"
37 : #include "part.h"
38 : #include "score.h"
39 : #include "staff.h"
40 : #include "stafftype.h"
41 : #include "stem.h"
42 : #include "system.h"
43 : #include "tremolotwochord.h"
44 :
45 : #include "log.h"
46 :
47 : using namespace mu;
48 : using namespace mu::engraving;
49 : using namespace muse::draw;
50 :
51 : namespace mu::engraving {
52 548 : SlurSegment::SlurSegment(Slur* sp, ElementType type)
53 548 : : SlurTieSegment(type, sp)
54 : {
55 548 : }
56 :
57 34 : SlurSegment::SlurSegment(const SlurSegment& ss)
58 34 : : SlurTieSegment(ss)
59 : {
60 34 : }
61 :
62 : //---------------------------------------------------------
63 : // searchCR
64 : //---------------------------------------------------------
65 :
66 0 : static ChordRest* searchCR(Segment* segment, track_idx_t startTrack, track_idx_t endTrack)
67 : {
68 0 : for (Segment* s = segment; s; s = s->next(SegmentType::ChordRest)) { // restrict search to measure
69 0 : if (startTrack > endTrack) {
70 0 : for (int t = static_cast<int>(startTrack) - 1; t >= static_cast<int>(endTrack); --t) {
71 0 : if (s->element(t)) {
72 0 : return toChordRest(s->element(t));
73 : }
74 : }
75 : } else {
76 0 : for (track_idx_t t = startTrack; t < endTrack; ++t) {
77 0 : if (s->element(t)) {
78 0 : return toChordRest(s->element(t));
79 : }
80 : }
81 : }
82 : }
83 0 : return 0;
84 : }
85 :
86 0 : bool SlurSegment::isEditAllowed(EditData& ed) const
87 : {
88 0 : if (SlurTieSegment::isEditAllowed(ed)) {
89 0 : return true;
90 : }
91 :
92 0 : const bool moveStart = ed.curGrip == Grip::START;
93 0 : const bool moveEnd = ed.curGrip == Grip::END || ed.curGrip == Grip::DRAG;
94 :
95 0 : if (!((ed.modifiers & ShiftModifier) && (isSingleType()
96 0 : || (isBeginType() && moveStart) || (isEndType() && moveEnd)))) {
97 0 : return false;
98 : }
99 :
100 : static const std::set<int> navigationKeys {
101 : Key_Left,
102 : Key_Up,
103 : Key_Down,
104 : Key_Right
105 0 : };
106 :
107 0 : return muse::contains(navigationKeys, ed.key);
108 : }
109 :
110 : //---------------------------------------------------------
111 : // edit
112 : // return true if event is accepted
113 : //---------------------------------------------------------
114 :
115 0 : bool SlurSegment::edit(EditData& ed)
116 : {
117 0 : if (!isEditAllowed(ed)) {
118 0 : return false;
119 : }
120 :
121 0 : if (SlurTieSegment::edit(ed)) {
122 0 : return true;
123 : }
124 :
125 0 : Slur* sl = slur();
126 :
127 0 : ChordRest* cr = nullptr;
128 : ChordRest* e;
129 : ChordRest* e1;
130 0 : const bool start = ed.curGrip == Grip::START;
131 0 : if (start) {
132 0 : e = sl->startCR();
133 0 : e1 = sl->endCR();
134 : } else {
135 0 : e = sl->endCR();
136 0 : e1 = sl->startCR();
137 : }
138 :
139 0 : const bool altMod = ed.modifiers & AltModifier;
140 0 : const bool shiftMod = ed.modifiers & ShiftModifier;
141 0 : const bool ctrlMod = ed.modifiers & ControlModifier;
142 0 : const bool extendToBarLine = shiftMod && altMod;
143 0 : const bool isPartialSlur = sl->isIncoming() || sl->isOutgoing();
144 :
145 0 : ChordRestNavigateOptions options;
146 0 : options.disableOverRepeats = true;
147 :
148 0 : if (ed.key == Key_Left) {
149 0 : if (extendToBarLine) {
150 0 : const Measure* measure = e->measure();
151 0 : if (start) {
152 0 : cr = measure->firstChordRest(e->track());
153 0 : if (!cr->hasPrecedingJumpItem()) {
154 0 : return false;
155 : }
156 0 : sl->undoSetIncoming(true);
157 0 : } else if (e->hasFollowingJumpItem()) {
158 0 : sl->undoSetOutgoing(false);
159 : }
160 : } else {
161 0 : if (!start && sl->isOutgoing()) {
162 0 : sl->undoSetOutgoing(false);
163 : } else {
164 0 : if (start && sl->isIncoming()) {
165 0 : sl->undoSetIncoming(false);
166 : }
167 0 : if (ctrlMod) {
168 0 : cr = Navigation::prevMeasure(score(), cr, true);
169 : } else {
170 0 : cr = Navigation::prevChordRest(e, options);
171 : }
172 : }
173 : }
174 0 : } else if (ed.key == Key_Right) {
175 0 : if (extendToBarLine) {
176 0 : const Measure* measure = e->measure();
177 0 : if (start && e->hasPrecedingJumpItem()) {
178 0 : sl->undoSetIncoming(false);
179 0 : } else if (!start) {
180 0 : cr = measure->lastChordRest(e->track());
181 0 : if (!cr->hasFollowingJumpItem()) {
182 0 : return false;
183 : }
184 0 : sl->undoSetOutgoing(true);
185 : }
186 : } else {
187 0 : if (start && sl->isIncoming()) {
188 0 : sl->undoSetIncoming(false);
189 : } else {
190 0 : if (!start && sl->isOutgoing()) {
191 0 : sl->undoSetOutgoing(false);
192 : }
193 0 : if (ctrlMod) {
194 0 : cr = Navigation::nextMeasure(score(), cr, false, true);
195 : } else {
196 0 : cr = Navigation::nextChordRest(e, options);
197 : }
198 : }
199 : }
200 0 : } else if (ed.key == Key_Up) {
201 0 : track_idx_t startTrack = e->part()->trackRange().startTrack;
202 0 : track_idx_t endTrack = e->track();
203 0 : cr = searchCR(e->segment(), endTrack, startTrack);
204 0 : } else if (ed.key == Key_Down) {
205 0 : track_idx_t startTrack = e->track() + 1;
206 0 : track_idx_t endTrack = e->part()->trackRange().endTrack;
207 0 : cr = searchCR(e->segment(), startTrack, endTrack);
208 : } else {
209 0 : return false;
210 : }
211 0 : if (cr && (cr != e1 || isPartialSlur)) {
212 0 : if (cr->staff() != e->staff() && (cr->staffType()->isTabStaff() || e->staffType()->isTabStaff())) {
213 0 : return false; // Cross-staff slurs don't make sense for TAB staves
214 : }
215 0 : if (cr->staff()->isLinked(e->staff())) {
216 0 : return false; // Don't allow slur to cross into staff that's linked to this
217 : }
218 0 : changeAnchor(ed, cr);
219 : }
220 0 : return true;
221 : }
222 :
223 : //---------------------------------------------------------
224 : // changeAnchor
225 : //---------------------------------------------------------
226 :
227 0 : void SlurSegment::changeAnchor(EditData& ed, EngravingItem* element)
228 : {
229 0 : Transaction& tx = masterScore()->transactionManager()->currentOrDummyTransaction();
230 :
231 0 : ChordRest* cr = element->isChordRest() ? toChordRest(element) : nullptr;
232 0 : ChordRest* scr = spanner()->startCR();
233 0 : ChordRest* ecr = spanner()->endCR();
234 0 : if (!cr || !scr || !ecr) {
235 0 : return;
236 : }
237 :
238 : // save current start/end elements
239 0 : for (EngravingObject* e : spanner()->linkList()) {
240 0 : Spanner* sp = toSpanner(e);
241 0 : tx.pushWithoutPerforming(new ChangeStartEndSpanner(sp, sp->startElement(), sp->endElement()));
242 0 : }
243 :
244 0 : if (ed.curGrip == Grip::START) {
245 0 : spanner()->undoChangeProperty(Pid::SPANNER_TICK, cr->tick());
246 0 : Fraction ticks = ecr->tick() - cr->tick();
247 0 : spanner()->undoChangeProperty(Pid::SPANNER_TICKS, ticks);
248 0 : int diff = static_cast<int>(cr->track() - spanner()->track());
249 0 : for (auto e : spanner()->linkList()) {
250 0 : Spanner* s = toSpanner(e);
251 0 : s->undoChangeProperty(Pid::TRACK, s->track() + diff);
252 0 : }
253 0 : scr = cr;
254 : } else {
255 0 : Fraction ticks = cr->tick() - scr->tick();
256 0 : spanner()->undoChangeProperty(Pid::SPANNER_TICKS, ticks);
257 0 : int diff = static_cast<int>(cr->track() - spanner()->track());
258 0 : for (auto e : spanner()->linkList()) {
259 0 : Spanner* s = toSpanner(e);
260 0 : s->undoChangeProperty(Pid::SPANNER_TRACK2, s->track() + diff);
261 0 : }
262 0 : ecr = cr;
263 : }
264 :
265 : // update start/end elements (which could be grace notes)
266 0 : slur()->undoChangeStartEndElements(scr, ecr);
267 :
268 0 : const size_t segments = spanner()->spannerSegments().size();
269 0 : ups(ed.curGrip).off = PointF();
270 0 : renderer()->layoutItem(spanner());
271 0 : if (spanner()->spannerSegments().size() != segments) {
272 0 : const std::vector<SpannerSegment*>& ss = spanner()->spannerSegments();
273 0 : const bool moveEnd = ed.curGrip == Grip::END || ed.curGrip == Grip::DRAG;
274 0 : SlurSegment* newSegment = toSlurSegment(moveEnd ? ss.back() : ss.front());
275 0 : ed.view()->changeEditElement(newSegment);
276 0 : triggerLayout();
277 : }
278 : }
279 :
280 0 : void SlurSegment::dragGrip(EditData& ed)
281 : {
282 0 : Grip g = ed.curGrip;
283 :
284 0 : switch (g) {
285 0 : case Grip::START:
286 : case Grip::END:
287 0 : ups(g).off += ed.delta;
288 : //
289 : // move anchor for slurs/ties
290 : //
291 0 : if ((g == Grip::START && isSingleBeginType()) || (g == Grip::END && isSingleEndType())) {
292 0 : Slur* slr = slur();
293 0 : KeyboardModifiers km = ed.modifiers;
294 0 : EngravingItem* e = ed.view()->elementNear(ed.pos);
295 0 : if (e && e->isNote()) {
296 0 : Note* note = toNote(e);
297 0 : Fraction tick = note->chord()->tick();
298 0 : if ((g == Grip::END && tick > slr->tick()) || (g == Grip::START && tick < slr->tick2())) {
299 0 : if (km != (ShiftModifier | ControlModifier)) {
300 0 : Chord* c = note->chord();
301 0 : ed.view()->setDropTarget(note);
302 0 : if (c->part() == slr->part() && c != slr->endCR()) {
303 0 : changeAnchor(ed, c);
304 : }
305 : }
306 : }
307 : } else {
308 0 : ed.view()->setDropTarget(0);
309 : }
310 : }
311 0 : renderer()->computeBezier(this);
312 0 : break;
313 0 : case Grip::BEZIER1:
314 : case Grip::BEZIER2:
315 0 : ups(g).off += ed.delta;
316 0 : renderer()->computeBezier(this);
317 0 : break;
318 0 : case Grip::SHOULDER:
319 0 : ups(g).off = PointF();
320 0 : renderer()->computeBezier(this, ed.delta);
321 0 : break;
322 0 : case Grip::DRAG:
323 0 : ups(Grip::DRAG).off = PointF();
324 0 : roffset() += ed.delta;
325 0 : break;
326 0 : default:
327 0 : UNREACHABLE;
328 : return;
329 : }
330 :
331 0 : triggerLayout();
332 0 : }
333 :
334 : //---------------------------------------------------------
335 : // isEdited
336 : //---------------------------------------------------------
337 :
338 0 : bool SlurSegment::isEdited() const
339 : {
340 0 : for (int i = 0; i < int(Grip::GRIPS); ++i) {
341 0 : if (!m_ups[i].off.isNull()) {
342 0 : return true;
343 : }
344 : }
345 0 : return false;
346 : }
347 :
348 2573 : bool SlurSegment::isEndPointsEdited() const
349 : {
350 2573 : return !(m_ups[int(Grip::START)].off.isNull() && m_ups[int(Grip::END)].off.isNull());
351 : }
352 :
353 940 : double SlurSegment::endWidth() const
354 : {
355 940 : return style().styleAbsolute(Sid::slurEndWidth);
356 : }
357 :
358 940 : double SlurSegment::midWidth() const
359 : {
360 940 : return style().styleAbsolute(Sid::slurMidWidth);
361 : }
362 :
363 29 : double SlurSegment::dottedWidth() const
364 : {
365 29 : return style().styleAbsolute(Sid::slurDottedWidth);
366 : }
367 :
368 0 : Color SlurSegment::curColor(const rendering::PaintOptions& opt) const
369 : {
370 0 : return EngravingItem::curColor(getProperty(Pid::VISIBLE).toBool(), getProperty(Pid::COLOR).value<Color>(), opt);
371 : }
372 :
373 42 : Slur::Slur(const Slur& s)
374 42 : : SlurTie(s)
375 : {
376 42 : _connectedElement = s._connectedElement;
377 42 : _partialSpannerDirection = s._partialSpannerDirection;
378 42 : }
379 :
380 : //---------------------------------------------------------
381 : // Slur
382 : //---------------------------------------------------------
383 :
384 484 : Slur::Slur(DummyParent* parent)
385 484 : : SlurTie(ElementType::SLUR, parent)
386 : {
387 484 : }
388 :
389 61 : Slur::Slur(const ElementType& type, EngravingObject* parent)
390 61 : : SlurTie(type, parent)
391 : {
392 61 : }
393 :
394 969 : double Slur::scalingFactor() const
395 : {
396 969 : Chord* startC = startElement() && startElement()->isChord() ? toChord(startElement()) : nullptr;
397 969 : Chord* endC = endElement() && endElement()->isChord() ? toChord(endElement()) : nullptr;
398 :
399 969 : if (!startC || !endC) {
400 13 : return 1.0;
401 : }
402 :
403 1003 : if ((startC->isGraceBefore() && startC->parent() == endC)
404 1003 : || (endC->isGraceAfter() && endC->parent() == startC)) {
405 34 : return style().styleD(Sid::graceNoteMag);
406 : }
407 :
408 922 : if (startC->isGrace()) {
409 29 : startC = toChord(startC->parent());
410 : }
411 922 : if (endC->isGrace()) {
412 28 : endC = toChord(endC->parent());
413 : }
414 :
415 922 : return 0.5 * (startC->intrinsicMag() + endC->intrinsicMag());
416 : }
417 :
418 0 : void Slur::undoSetIncoming(bool incoming)
419 : {
420 0 : if (incoming == isIncoming()) {
421 0 : return;
422 : }
423 :
424 0 : undoChangeProperty(Pid::PARTIAL_SPANNER_DIRECTION, calcIncomingDirection(incoming), PropertyFlags::UNSTYLED);
425 : }
426 :
427 2 : void Slur::undoSetOutgoing(bool outgoing)
428 : {
429 2 : if (outgoing == isOutgoing()) {
430 0 : return;
431 : }
432 :
433 2 : undoChangeProperty(Pid::PARTIAL_SPANNER_DIRECTION, calcOutgoingDirection(outgoing), PropertyFlags::UNSTYLED);
434 : }
435 :
436 0 : void Slur::setIncoming(bool incoming)
437 : {
438 0 : if (incoming == isIncoming()) {
439 0 : return;
440 : }
441 :
442 0 : _partialSpannerDirection = calcIncomingDirection(incoming);
443 : }
444 :
445 1 : void Slur::setOutgoing(bool outgoing)
446 : {
447 1 : if (outgoing == isOutgoing()) {
448 0 : return;
449 : }
450 :
451 1 : _partialSpannerDirection = calcOutgoingDirection(outgoing);
452 : }
453 :
454 0 : PartialSpannerDirection Slur::calcIncomingDirection(bool incoming)
455 : {
456 0 : PartialSpannerDirection dir = PartialSpannerDirection::INCOMING;
457 0 : if (incoming) {
458 0 : SlurSegment* firstSeg = nsegments() > 0 ? frontSegment() : nullptr;
459 0 : if (firstSeg) {
460 0 : firstSeg->setSlurOffset(Grip::START, PointF(0, 0));
461 : }
462 0 : dir = _partialSpannerDirection
463 0 : == PartialSpannerDirection::OUTGOING ? PartialSpannerDirection::BOTH : PartialSpannerDirection::INCOMING;
464 : } else {
465 0 : dir = _partialSpannerDirection == PartialSpannerDirection::BOTH ? PartialSpannerDirection::OUTGOING : PartialSpannerDirection::NONE;
466 : }
467 0 : return dir;
468 : }
469 :
470 3 : PartialSpannerDirection Slur::calcOutgoingDirection(bool outgoing)
471 : {
472 3 : PartialSpannerDirection dir = PartialSpannerDirection::OUTGOING;
473 3 : if (outgoing) {
474 2 : SlurSegment* lastSeg = nsegments() > 0 ? backSegment() : nullptr;
475 2 : if (lastSeg) {
476 0 : lastSeg->setSlurOffset(Grip::END, PointF(0, 0));
477 : }
478 2 : dir = _partialSpannerDirection
479 2 : == PartialSpannerDirection::INCOMING ? PartialSpannerDirection::BOTH : PartialSpannerDirection::OUTGOING;
480 : } else {
481 1 : dir = _partialSpannerDirection == PartialSpannerDirection::BOTH ? PartialSpannerDirection::INCOMING : PartialSpannerDirection::NONE;
482 : }
483 3 : return dir;
484 : }
485 :
486 1329 : bool Slur::isIncoming() const
487 : {
488 1329 : return _partialSpannerDirection == PartialSpannerDirection::BOTH || _partialSpannerDirection == PartialSpannerDirection::INCOMING;
489 : }
490 :
491 1312 : bool Slur::isOutgoing() const
492 : {
493 1312 : return _partialSpannerDirection == PartialSpannerDirection::BOTH || _partialSpannerDirection == PartialSpannerDirection::OUTGOING;
494 : }
495 :
496 0 : void Slur::undoChangeStartEndElements(ChordRest* scr, ChordRest* ecr)
497 : {
498 0 : for (EngravingObject* lsp : linkList()) {
499 0 : Spanner* sp = toSpanner(lsp);
500 0 : if (sp == this) {
501 0 : score()->undo(new ChangeSpannerElements(this, scr, ecr));
502 : } else {
503 0 : EngravingItem* se = 0;
504 0 : EngravingItem* ee = 0;
505 0 : if (scr) {
506 0 : std::list<EngravingObject*> sel = scr->linkList();
507 0 : for (EngravingObject* lcr : sel) {
508 0 : EngravingItem* le = toEngravingItem(lcr);
509 0 : if (le->score() == sp->score() && le->track() == sp->track()) {
510 0 : se = le;
511 0 : break;
512 : }
513 : }
514 0 : }
515 0 : if (ecr) {
516 0 : std::list<EngravingObject*> sel = ecr->linkList();
517 0 : for (EngravingObject* lcr : sel) {
518 0 : EngravingItem* le = toEngravingItem(lcr);
519 0 : if (le->score() == sp->score() && le->track() == sp->track2()) {
520 0 : ee = le;
521 0 : break;
522 : }
523 : }
524 0 : }
525 0 : if (se && ee) {
526 0 : score()->undo(new ChangeStartEndSpanner(sp, se, ee));
527 0 : renderer()->layoutItem(sp);
528 : }
529 : }
530 0 : }
531 0 : }
532 :
533 : //---------------------------------------------------------
534 : // setTrack
535 : //---------------------------------------------------------
536 :
537 986 : void Slur::setTrack(track_idx_t n)
538 : {
539 986 : EngravingItem::setTrack(n);
540 1114 : for (SpannerSegment* ss : spannerSegments()) {
541 128 : ss->setTrack(n);
542 : }
543 986 : }
544 :
545 6770 : bool Slur::isCrossStaff()
546 : {
547 13540 : return startCR() && endCR()
548 20146 : && (startCR()->staffMove() != 0 || endCR()->staffMove() != 0
549 13376 : || startCR()->vStaffIdx() != endCR()->vStaffIdx());
550 : }
551 :
552 1783 : bool Slur::hasCrossBeams()
553 : {
554 1783 : return (startCR() && startCR()->beam() && startCR()->beam()->cross()) || (endCR() && endCR()->beam() && endCR()->beam()->cross());
555 : }
556 :
557 5762 : PropertyValue Slur::getProperty(Pid propertyId) const
558 : {
559 5762 : switch (propertyId) {
560 313 : case Pid::PARTIAL_SPANNER_DIRECTION:
561 313 : return partialSpannerDirection();
562 5449 : default:
563 5449 : return SlurTie::getProperty(propertyId);
564 : }
565 : }
566 :
567 4658 : PropertyValue Slur::propertyDefault(Pid id) const
568 : {
569 4658 : switch (id) {
570 305 : case Pid::PARTIAL_SPANNER_DIRECTION:
571 610 : return PartialSpannerDirection::NONE;
572 4353 : default:
573 4353 : return SlurTie::propertyDefault(id);
574 : }
575 : }
576 :
577 117 : bool Slur::setProperty(Pid propertyId, const PropertyValue& v)
578 : {
579 117 : switch (propertyId) {
580 17 : case Pid::PARTIAL_SPANNER_DIRECTION:
581 17 : setPartialSpannerDirection(v.value<PartialSpannerDirection>());
582 17 : break;
583 100 : default:
584 100 : return SlurTie::setProperty(propertyId, v);
585 : }
586 17 : triggerLayout();
587 17 : return true;
588 : }
589 : }
|