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 "containers.h"
24 :
25 : #include "iengravingconfiguration.h" // IWYU pragma: keep
26 :
27 : #include "types/typesconv.h"
28 :
29 : #include "barline.h"
30 : #include "clef.h"
31 : #include "cleflist.h"
32 : #include "excerpt.h"
33 : #include "factory.h"
34 : #include "instrtemplate.h"
35 : #include "linkedobjects.h"
36 : #include "masterscore.h"
37 : #include "measure.h"
38 : #include "mscore.h"
39 : #include "note.h"
40 : #include "ottava.h"
41 : #include "part.h"
42 : #include "score.h"
43 : #include "segment.h"
44 : #include "staff.h"
45 : #include "stafftype.h"
46 : #include "timesig.h"
47 : #include "utils.h"
48 :
49 : #include "../editing/editstaffbrackets.h"
50 : #include "../editing/editcapo.h"
51 : #include "../editing/transpose.h"
52 :
53 : // #define DEBUG_CLEFS
54 :
55 : #include "log.h"
56 :
57 : using namespace mu;
58 : using namespace mu::engraving;
59 :
60 : #ifdef DEBUG_CLEFS
61 : #define DUMP_CLEFS(s) dumpClefs(s)
62 : #else
63 : #define DUMP_CLEFS(s)
64 : #endif
65 :
66 : namespace mu::engraving {
67 : //---------------------------------------------------------
68 : // Staff
69 : //---------------------------------------------------------
70 :
71 4266 : Staff::Staff(Part* parent)
72 21330 : : EngravingItem(ElementType::STAFF, parent)
73 : {
74 4266 : m_color = configuration()->defaultColor();
75 4266 : initFromStaffType(0);
76 4266 : }
77 :
78 : //---------------------------------------------------------
79 : // copy ctor
80 : //---------------------------------------------------------
81 :
82 4 : Staff::Staff(const Staff& staff)
83 20 : : EngravingItem(staff)
84 : {
85 4 : init(&staff);
86 4 : m_part = staff.m_part;
87 4 : }
88 :
89 : //---------------------------------------------------------
90 : // clone
91 : //---------------------------------------------------------
92 :
93 4 : Staff* Staff::clone() const
94 : {
95 4 : return new Staff(*this);
96 : }
97 :
98 : //---------------------------------------------------------
99 : // idx
100 : //---------------------------------------------------------
101 :
102 433460 : staff_idx_t Staff::idx() const
103 : {
104 433460 : return muse::indexOf(score()->staves(), (Staff*)this);
105 : }
106 :
107 293382 : Part* Staff::originPart(const Fraction& tick) const
108 : {
109 : // Hides EngravingItem::originPart, whose track and tick are meaningless for a staff
110 : // Always returns part of voice 0
111 293382 : if (!m_part || !m_part->isSharedPart()) {
112 284994 : return m_part;
113 : }
114 :
115 8388 : return m_part->originPart(staff2track(idx(), 0), tick);
116 : }
117 :
118 : //---------------------------------------------------------
119 : // triggerLayout
120 : //---------------------------------------------------------
121 :
122 538 : void Staff::triggerLayout() const
123 : {
124 538 : score()->setLayoutAll(idx(), this);
125 538 : }
126 :
127 36 : void Staff::triggerLayout(const Fraction& tick)
128 : {
129 36 : score()->setLayout(tick, idx());
130 36 : }
131 :
132 162 : Staff* Staff::findLinkedInScore(const Score* score) const
133 : {
134 271 : for (Staff* staff : score->staves()) {
135 236 : if (staff->id() == id()) {
136 127 : return staff;
137 : }
138 : }
139 :
140 35 : return nullptr;
141 : }
142 :
143 6867 : track_idx_t Staff::getLinkedTrackInStaff(const Staff* linkedStaff, const track_idx_t originalTrack) const
144 : {
145 6867 : IF_ASSERT_FAILED(linkedStaff && originalTrack != muse::nidx) {
146 0 : return muse::nidx;
147 : }
148 :
149 6867 : Score* thisScore = score();
150 6867 : Score* linkedStaffScore = linkedStaff->score();
151 6867 : staff_idx_t linkedStaffIdx = linkedStaff->idx();
152 :
153 6867 : if (thisScore == linkedStaffScore) {
154 : // For staves linked within the same score, voices are always mapped 1 to 1
155 6737 : voice_idx_t voice = track2voice(originalTrack);
156 6737 : return staff2track(linkedStaffIdx, voice);
157 : }
158 :
159 : // NOTE 1: if linkedStaff has a different id than *this, it means that there isn't direct voice mapping between
160 : // the two. Example: if we have guitar+TAB in the score, with corresponding guitar+TAB in the part, *this
161 : // may be the notation-staff of the score and linkedStaff may be the TAB-staff of the part. In that case, the
162 : // correct voice mapping must be obtained by looking for the staff with same id in thisScore.
163 :
164 130 : track_idx_t refTrack = originalTrack;
165 130 : if (linkedStaff->id() != id()) {
166 2 : Staff* correspondingStaffInThisScore = linkedStaff->findLinkedInScore(thisScore);
167 2 : if (!correspondingStaffInThisScore) {
168 0 : return muse::nidx;
169 : }
170 2 : voice_idx_t originalVoice = track2voice(originalTrack);
171 2 : refTrack = staff2track(correspondingStaffInThisScore->idx(), originalVoice);
172 : }
173 :
174 : // NOTE 2: TracksMap is a map from a track in the *score* to track(s) in the *part*.
175 : // If we are in the master score, refTrack corresponds to one of the keys in the map, so we can retrieve
176 : // the linked tracks by simply querying the map by key. However, if we are in a part, we need to search for
177 : // refTrack among the *values* of the map, and the corresponding key gives us the linked track in the score.
178 : // If linkedStaff is *also* in a part, we need to do it in two steps: first find the linked track in the score,
179 : // then use it to find the linked track in linkeStaff's part.
180 :
181 130 : if (thisScore->isMaster()) {
182 120 : const TracksMap& tracksMap = linkedStaffScore->excerpt()->tracksMapping();
183 120 : std::vector<track_idx_t> linkedTracks = muse::values(tracksMap, refTrack);
184 120 : for (track_idx_t track : linkedTracks) {
185 118 : if (track2staff(track) == linkedStaffIdx) {
186 118 : return track;
187 : }
188 : }
189 :
190 2 : return muse::nidx;
191 120 : }
192 :
193 10 : const TracksMap& thisTracksMap = thisScore->excerpt()->tracksMapping();
194 10 : track_idx_t linkedTrackInScore = muse::nidx;
195 24 : for (auto pair : thisTracksMap) {
196 24 : track_idx_t trackInScore = pair.first;
197 24 : std::vector<track_idx_t> tracksInPart = muse::values(thisTracksMap, trackInScore);
198 38 : for (track_idx_t trackInPart : tracksInPart) {
199 24 : if (trackInPart == refTrack) {
200 10 : linkedTrackInScore = trackInScore;
201 10 : break;
202 : }
203 : }
204 24 : if (linkedTrackInScore != muse::nidx) {
205 10 : break;
206 : }
207 24 : }
208 :
209 10 : if (linkedStaffScore->isMaster() || linkedTrackInScore == muse::nidx) {
210 10 : return linkedTrackInScore;
211 : }
212 :
213 0 : const TracksMap& linkedTracksMap = linkedStaffScore->excerpt()->tracksMapping();
214 0 : std::vector<track_idx_t> linkedTracks = muse::values(linkedTracksMap, linkedTrackInScore);
215 0 : for (track_idx_t track : linkedTracks) {
216 0 : if (track2staff(track) == linkedStaffIdx) {
217 0 : return track;
218 : }
219 : }
220 :
221 0 : return muse::nidx;
222 0 : }
223 :
224 17 : bool Staff::trackHasLinksInVoiceZero(track_idx_t track)
225 : {
226 34 : for (Staff* linkedStaff : staffList()) {
227 17 : track_idx_t linkedTrack = getLinkedTrackInStaff(linkedStaff, track);
228 17 : if (linkedTrack != muse::nidx && track2voice(linkedTrack) == 0) {
229 0 : return true;
230 : }
231 17 : }
232 :
233 17 : return false;
234 : }
235 :
236 0 : void Staff::undoSetShowMeasureNumbers(bool show)
237 : {
238 0 : bool isTopStave = score()->staves().front() == this;
239 0 : if (show) {
240 0 : undoChangeProperty(Pid::SHOW_MEASURE_NUMBERS, isTopStave ? AutoOnOff::AUTO : AutoOnOff::ON);
241 : } else {
242 0 : undoChangeProperty(Pid::SHOW_MEASURE_NUMBERS, isTopStave ? AutoOnOff::OFF : AutoOnOff::AUTO);
243 : }
244 0 : }
245 :
246 20310 : bool Staff::shouldShowMeasureNumbers() const
247 : {
248 20310 : MeasureNumberPlacement placementMode = style().styleV(Sid::measureNumberPlacementMode).value<MeasureNumberPlacement>();
249 20310 : switch (placementMode) {
250 20306 : case MeasureNumberPlacement::ABOVE_SYSTEM:
251 20306 : return score()->staves().front() == this;
252 0 : case MeasureNumberPlacement::BELOW_SYSTEM:
253 0 : return score()->staves().back() == this;
254 4 : case MeasureNumberPlacement::ON_SYSTEM_OBJECT_STAVES:
255 : {
256 4 : bool isTopStave = score()->staves().front() == this;
257 4 : return (isTopStave && m_showMeasureNumbers != AutoOnOff::OFF) || (isSystemObjectStaff() && m_showMeasureNumbers == AutoOnOff::ON);
258 : }
259 0 : case MeasureNumberPlacement::ON_ALL_STAVES:
260 0 : return show();
261 : }
262 :
263 0 : return false;
264 : }
265 :
266 1442 : bool Staff::isLastOfScore() const
267 : {
268 1442 : return score()->staves().empty() ? false : score()->staves().back() == this;
269 : }
270 :
271 55169 : bool Staff::isSystemObjectStaff() const
272 : {
273 55169 : return score() && muse::contains(score()->systemObjectStaves(), const_cast<Staff*>(this));
274 : }
275 :
276 54785 : bool Staff::hasSystemObjectsBelowBottomStaff() const
277 : {
278 54785 : return isSystemObjectStaff() && isLastOfScore() && style().styleB(Sid::systemObjectsBelowBottomStaff);
279 : }
280 :
281 6155 : String Staff::individualStaffNameLong(const Fraction& tick) const
282 : {
283 6155 : IF_ASSERT_FAILED(m_part) {
284 0 : return String();
285 : }
286 :
287 6155 : return staffType(tick)->longName();
288 : }
289 :
290 4222 : String Staff::individualStaffNameShort(const Fraction& tick) const
291 : {
292 4222 : IF_ASSERT_FAILED(m_part) {
293 0 : return String();
294 : }
295 :
296 4222 : return staffType(tick)->shortName();
297 : }
298 :
299 7912 : bool Staff::playbackVoice(int voice) const
300 : {
301 7912 : return m_playbackVoice[voice];
302 : }
303 :
304 1 : void Staff::setPlaybackVoice(int voice, bool val)
305 : {
306 1 : m_playbackVoice[voice] = val;
307 1 : }
308 :
309 647 : const std::array<bool, VOICES>& Staff::visibilityVoices() const
310 : {
311 647 : return m_visibilityVoices;
312 : }
313 :
314 36 : bool Staff::isVoiceVisible(voice_idx_t voice) const
315 : {
316 36 : if (voice >= VOICES) {
317 0 : return false;
318 : }
319 :
320 36 : return m_visibilityVoices[voice];
321 : }
322 :
323 24 : void Staff::setVoiceVisible(voice_idx_t voice, bool visible)
324 : {
325 24 : if (voice >= VOICES) {
326 0 : return;
327 : }
328 :
329 24 : m_visibilityVoices[voice] = visible;
330 : }
331 :
332 4 : bool Staff::canDisableVoice() const
333 : {
334 4 : auto voices = visibilityVoices();
335 4 : int countOfVisibleVoices = 0;
336 40 : for (size_t i = 0; i < voices.size(); ++i) {
337 16 : if (voices[i]) {
338 10 : countOfVisibleVoices++;
339 : }
340 : }
341 :
342 4 : return countOfVisibleVoices > 1;
343 : }
344 :
345 249 : void Staff::updateVisibilityVoices(const Staff* masterStaff, const TracksMap& tracks)
346 : {
347 249 : if (tracks.empty()) {
348 1 : m_visibilityVoices = { true, true, true, true };
349 1 : return;
350 : }
351 :
352 248 : std::array<bool, VOICES> voices{ false, false, false, false };
353 :
354 248 : staff_idx_t masterStaffIdx = masterStaff->idx();
355 248 : staff_idx_t staffIdx = idx();
356 :
357 248 : voice_idx_t voiceIndex = 0;
358 1240 : for (voice_idx_t voice = 0; voice < VOICES; voice++) {
359 992 : std::vector<track_idx_t> masterStaffTracks = muse::values(tracks, masterStaffIdx * VOICES + voice % VOICES);
360 992 : bool isVoiceVisible = muse::contains(masterStaffTracks, staffIdx * VOICES + voiceIndex % VOICES);
361 992 : if (isVoiceVisible) {
362 969 : voices[voice] = true;
363 969 : voiceIndex++;
364 : }
365 992 : }
366 :
367 248 : m_visibilityVoices = voices;
368 : }
369 :
370 1156 : bool Staff::reflectTranspositionInLinkedTab() const
371 : {
372 1156 : return m_reflectTranspositionInLinkedTab;
373 : }
374 :
375 10 : void Staff::setReflectTranspositionInLinkedTab(bool reflect)
376 : {
377 10 : m_reflectTranspositionInLinkedTab = reflect;
378 10 : }
379 :
380 : //---------------------------------------------------------
381 : // partName
382 : //---------------------------------------------------------
383 :
384 161 : String Staff::partName() const
385 : {
386 161 : return m_part->partName();
387 : }
388 :
389 : //---------------------------------------------------------
390 : // Staff::clefType
391 : //---------------------------------------------------------
392 :
393 96354 : ClefTypeList Staff::clefType(const Fraction& tick) const
394 : {
395 96354 : ClefTypeList ct = m_clefs.clef(tick.ticks());
396 96354 : if (ct.concertClef == ClefType::INVALID) {
397 : // Clef compatibility based on instrument (override StaffGroup)
398 60334 : StaffGroup staffGroup = staffType(tick)->group();
399 60334 : if (staffGroup != StaffGroup::TAB) {
400 59664 : staffGroup = originPart(tick)->instrument(tick)->useDrumset() ? StaffGroup::PERCUSSION : StaffGroup::STANDARD;
401 : }
402 :
403 60334 : switch (staffGroup) {
404 670 : case StaffGroup::TAB:
405 : {
406 670 : ClefType sct = ClefType(style().styleI(Sid::tabClef));
407 670 : ct = staffType(tick)->lines() <= 4 ? ClefTypeList(sct == ClefType::TAB ? ClefType::TAB4 : ClefType::TAB4_SERIF) : ClefTypeList(
408 : sct == ClefType::TAB ? ClefType::TAB : ClefType::TAB_SERIF);
409 : }
410 670 : break;
411 58788 : case StaffGroup::STANDARD:
412 : case StaffGroup::JIANPU:
413 58788 : ct = defaultClefType();
414 58788 : break;
415 876 : case StaffGroup::PERCUSSION:
416 876 : ct = ClefTypeList(ClefType::PERC);
417 876 : break;
418 : }
419 : }
420 96354 : return ct;
421 : }
422 :
423 : //---------------------------------------------------------
424 : // Staff::clef
425 : //---------------------------------------------------------
426 :
427 75405 : ClefType Staff::clef(const Fraction& tick) const
428 : {
429 75405 : ClefTypeList c = clefType(tick);
430 150810 : return style().styleB(Sid::concertPitch) ? c.concertClef : c.transposingClef;
431 : }
432 :
433 : //---------------------------------------------------------
434 : // Staff::nextClefTick
435 : //
436 : // return the tick of next clef after tick
437 : // return last tick of score if not found
438 : //---------------------------------------------------------
439 :
440 57 : Fraction Staff::nextClefTick(const Fraction& tick) const
441 : {
442 57 : Fraction t = Fraction::fromTicks(m_clefs.nextClefTick(tick.ticks()));
443 57 : return t != Fraction(-1, 1) ? t : score()->endTick();
444 : }
445 :
446 : //---------------------------------------------------------
447 : // Staff::currentClefTick
448 : //
449 : // return the tick position of the clef currently
450 : // in effect at tick
451 : // return 0, if no such clef
452 : //---------------------------------------------------------
453 :
454 15242 : Fraction Staff::currentClefTick(const Fraction& tick) const
455 : {
456 15242 : return Fraction::fromTicks(m_clefs.currentClefTick(tick.ticks()));
457 : }
458 :
459 205 : String Staff::staffName() const
460 : {
461 205 : return TConv::translatedUserName(clefType(Fraction()).transposingClef);
462 : }
463 :
464 : #ifndef NDEBUG
465 : //---------------------------------------------------------
466 : // dumpClef
467 : //---------------------------------------------------------
468 :
469 0 : void Staff::dumpClefs(const char* title) const
470 : {
471 0 : LOGD("(%zd): %s", m_clefs.size(), title);
472 0 : for (auto& i : m_clefs) {
473 0 : LOGD(" %d: %d %d", i.first, int(i.second.concertClef), int(i.second.transposingClef));
474 : }
475 0 : }
476 :
477 : //---------------------------------------------------------
478 : // dumpKeys
479 : //---------------------------------------------------------
480 :
481 0 : void Staff::dumpKeys(const char* title) const
482 : {
483 0 : LOGD("(%zd): %s", m_keys.size(), title);
484 0 : for (auto& i : m_keys) {
485 0 : LOGD(" %d: %d", i.first, int(i.second.key()));
486 : }
487 0 : }
488 :
489 : //---------------------------------------------------------
490 : // dumpTimeSigs
491 : //---------------------------------------------------------
492 :
493 0 : void Staff::dumpTimeSigs(const char* title) const
494 : {
495 0 : LOGD("size (%zd) staffIdx %zu: %s", m_timesigs.size(), idx(), title);
496 0 : for (auto& i : m_timesigs) {
497 0 : LOGD(" %d: %d/%d", i.first, i.second->sig().numerator(), i.second->sig().denominator());
498 : }
499 0 : }
500 :
501 : #endif
502 :
503 : //---------------------------------------------------------
504 : // setClef
505 : //---------------------------------------------------------
506 :
507 1713 : void Staff::setClef(Clef* clef)
508 : {
509 1713 : if (clef->generated()) {
510 0 : return;
511 : }
512 1713 : Fraction tick = clef->segment()->tick();
513 3946 : for (Segment* s = clef->segment()->next1(); s && s->tick() == tick; s = s->next1()) {
514 4464 : if ((s->segmentType() == SegmentType::Clef || s->segmentType() == SegmentType::HeaderClef)
515 14 : && s->element(clef->track())
516 4466 : && !s->element(clef->track())->generated()) {
517 : // adding this clef has no effect on the clefs list
518 0 : return;
519 : }
520 : }
521 1713 : m_clefs.setClef(clef->segment()->tick().ticks(), clef->clefTypeList());
522 : DUMP_CLEFS("setClef");
523 : }
524 :
525 : //---------------------------------------------------------
526 : // removeClef
527 : //---------------------------------------------------------
528 :
529 74 : void Staff::removeClef(const Clef* clef)
530 : {
531 74 : if (clef->generated()) {
532 55 : return;
533 : }
534 19 : Fraction tick = clef->segment()->tick();
535 67 : for (Segment* s = clef->segment()->next1(); s && s->tick() == tick; s = s->next1()) {
536 96 : if ((s->segmentType() == SegmentType::Clef || s->segmentType() == SegmentType::HeaderClef)
537 0 : && s->element(clef->track())
538 96 : && !s->element(clef->track())->generated()) {
539 : // removal of this clef has no effect on the clefs list
540 0 : return;
541 : }
542 : }
543 19 : m_clefs.erase(clef->segment()->tick().ticks());
544 30 : for (Segment* s = clef->segment()->prev1(); s && s->tick() == tick; s = s->prev1()) {
545 22 : if ((s->segmentType() == SegmentType::Clef || s->segmentType() == SegmentType::HeaderClef)
546 1 : && s->element(clef->track())
547 22 : && !s->element(clef->track())->generated()) {
548 : // a previous clef at the same tick position gets valid
549 0 : m_clefs.setClef(tick.ticks(), toClef(s->element(clef->track()))->clefTypeList());
550 0 : break;
551 : }
552 : }
553 : DUMP_CLEFS("removeClef");
554 : }
555 :
556 : //---------------------------------------------------------
557 : // timeStretch
558 : //---------------------------------------------------------
559 :
560 1726171 : Fraction Staff::timeStretch(const Fraction& tick) const
561 : {
562 1726171 : TimeSig* timesig = timeSig(tick);
563 1726171 : return timesig ? timesig->stretch() : Fraction(1, 1);
564 : }
565 :
566 : //---------------------------------------------------------
567 : // timeSig
568 : // lookup time signature before or at tick
569 : //---------------------------------------------------------
570 :
571 2220348 : TimeSig* Staff::timeSig(const Fraction& tick) const
572 : {
573 2220348 : auto i = m_timesigs.upper_bound(tick.ticks());
574 2220348 : if (i != m_timesigs.begin()) {
575 2191973 : --i;
576 : }
577 2220348 : if (i == m_timesigs.end()) {
578 28082 : return 0;
579 2192266 : } else if (tick < Fraction::fromTicks(i->first)) {
580 293 : return 0;
581 : }
582 2191973 : return i->second;
583 : }
584 :
585 : //---------------------------------------------------------
586 : // nextTimeSig
587 : // lookup time signature at tick or after
588 : //---------------------------------------------------------
589 :
590 2 : TimeSig* Staff::nextTimeSig(const Fraction& tick) const
591 : {
592 2 : auto i = m_timesigs.lower_bound(tick.ticks());
593 2 : return (i == m_timesigs.end()) ? 0 : i->second;
594 : }
595 :
596 : //---------------------------------------------------------
597 : // currentTimeSigTick
598 : //
599 : // return the tick position of the time sig currently
600 : // in effect at tick
601 : //---------------------------------------------------------
602 :
603 0 : Fraction Staff::currentTimeSigTick(const Fraction& tick) const
604 : {
605 0 : if (m_timesigs.empty()) {
606 0 : return Fraction(0, 1);
607 : }
608 0 : auto i = m_timesigs.upper_bound(tick.ticks());
609 0 : if (i == m_timesigs.begin()) {
610 0 : return Fraction(0, 1);
611 : }
612 0 : --i;
613 0 : return Fraction::fromTicks(i->first);
614 : }
615 :
616 : //---------------------------------------------------------
617 : // group
618 : //---------------------------------------------------------
619 :
620 181444 : const Groups& Staff::group(const Fraction& tick) const
621 : {
622 181444 : TimeSig* ts = timeSig(tick);
623 181444 : if (ts) {
624 181414 : if (!ts->groups().empty()) {
625 0 : return ts->groups();
626 : }
627 181414 : return Groups::endings(ts->sig());
628 : }
629 30 : Measure* m = score()->tick2measure(tick);
630 30 : return Groups::endings(m ? m->timesig() : Fraction(4, 4));
631 : }
632 :
633 : //---------------------------------------------------------
634 : // addTimeSig
635 : //---------------------------------------------------------
636 :
637 8226 : void Staff::addTimeSig(TimeSig* timesig)
638 : {
639 8226 : if (timesig->segment()->segmentType() == SegmentType::TimeSig) {
640 7981 : m_timesigs[timesig->segment()->tick().ticks()] = timesig;
641 : }
642 : // dumpTimeSigs("after addTimeSig");
643 8226 : }
644 :
645 : //---------------------------------------------------------
646 : // removeTimeSig
647 : //---------------------------------------------------------
648 :
649 3753 : void Staff::removeTimeSig(TimeSig* timesig)
650 : {
651 3753 : if (timesig->segment()->segmentType() == SegmentType::TimeSig) {
652 3555 : if (m_timesigs[timesig->segment()->tick().ticks()] == timesig) {
653 3510 : m_timesigs.erase(timesig->segment()->tick().ticks());
654 : }
655 : }
656 : // dumpTimeSigs("after removeTimeSig");
657 3753 : }
658 :
659 : //---------------------------------------------------------
660 : // clearTimeSig
661 : //---------------------------------------------------------
662 :
663 4173 : void Staff::clearTimeSig()
664 : {
665 4173 : m_timesigs.clear();
666 4173 : }
667 :
668 : //---------------------------------------------------------
669 : // Staff::transpose
670 : //
671 : // actual staff transposioton at tick
672 : // (taking key into account)
673 : //---------------------------------------------------------
674 :
675 13980 : Interval Staff::transpose(const Fraction& tick) const
676 : {
677 : // get real transposition
678 :
679 13980 : const Part* p = originPart(tick);
680 13980 : Interval v = p->instrument(tick)->transpose();
681 13980 : if (v.isZero()) {
682 10303 : return v;
683 : }
684 3677 : Key cKey = concertKey(tick);
685 3677 : v.flip();
686 3677 : Key tKey = Transpose::transposeKey(cKey, v, p->preferSharpFlat());
687 3677 : v.flip();
688 :
689 3677 : int chromatic = (7 * (static_cast<int>(cKey) - static_cast<int>(tKey))) % 12;
690 3677 : if (chromatic < 0) {
691 722 : chromatic += 12;
692 : }
693 3677 : int diatonic = (4 * (static_cast<int>(cKey) - static_cast<int>(tKey))) % 7;
694 3677 : if (diatonic < 0) {
695 722 : diatonic += 7;
696 : }
697 :
698 3677 : if (v.chromatic < 0 || v.diatonic < 0) {
699 3667 : chromatic -= 12;
700 3667 : diatonic -= 7;
701 : }
702 :
703 3677 : v.chromatic = v.chromatic - (v.chromatic % 12) + (chromatic % 12);
704 3677 : v.diatonic = v.diatonic - (v.diatonic % 7) + (diatonic % 7);
705 :
706 3677 : return v;
707 : }
708 :
709 : //---------------------------------------------------------
710 : // Staff::keySigEvent
711 : //
712 : // locates the key sig currently in effect at tick
713 : //---------------------------------------------------------
714 :
715 1945998 : KeySigEvent Staff::keySigEvent(const Fraction& tick) const
716 : {
717 1945998 : return m_keys.key(tick.ticks());
718 : }
719 :
720 : //---------------------------------------------------------
721 : // setKey
722 : //---------------------------------------------------------
723 :
724 3787 : void Staff::setKey(const Fraction& tick, KeySigEvent k)
725 : {
726 3787 : m_keys.setKey(tick.ticks(), k);
727 3787 : }
728 :
729 : //---------------------------------------------------------
730 : // removeKey
731 : //---------------------------------------------------------
732 :
733 111 : void Staff::removeKey(const Fraction& tick)
734 : {
735 111 : m_keys.erase(tick.ticks());
736 111 : }
737 :
738 : //---------------------------------------------------------
739 : // prevkey
740 : //---------------------------------------------------------
741 :
742 0 : KeySigEvent Staff::prevKey(const Fraction& tick) const
743 : {
744 0 : return m_keys.prevKey(tick.ticks());
745 : }
746 :
747 : //---------------------------------------------------------
748 : // Staff::nextKeyTick
749 : //
750 : // return the tick at which the key sig after tick is located
751 : // return 0, if no such a key sig
752 : //---------------------------------------------------------
753 :
754 530 : Fraction Staff::nextKeyTick(const Fraction& tick) const
755 : {
756 530 : Fraction t = Fraction::fromTicks(m_keys.nextKeyTick(tick.ticks()));
757 530 : return t != Fraction(-1, 1) ? t : score()->endTick();
758 : }
759 :
760 : //---------------------------------------------------------
761 : // Staff::currentKeyTick
762 : //
763 : // return the tick position of the key currently
764 : // in effect at tick
765 : // return 0, if no such a key sig
766 : //---------------------------------------------------------
767 :
768 66467 : Fraction Staff::currentKeyTick(const Fraction& tick) const
769 : {
770 66467 : return Fraction::fromTicks(m_keys.currentKeyTick(tick.ticks()));
771 : }
772 :
773 : //---------------------------------------------------------
774 : // height
775 : //---------------------------------------------------------
776 :
777 36335 : double Staff::staffHeight() const
778 : {
779 36335 : Fraction tick = Fraction(0, 1);
780 36335 : return (lines(tick) - 1) * spatium(tick) * staffType(tick)->lineDistance().val();
781 : }
782 :
783 16804 : double Staff::staffHeight(const Fraction& tick) const
784 : {
785 16804 : return (lines(tick) - 1) * spatium(tick) * staffType(tick)->lineDistance().val();
786 : }
787 :
788 : //---------------------------------------------------------
789 : // spatium
790 : //---------------------------------------------------------
791 :
792 319780 : double Staff::spatium(const Fraction& tick) const
793 : {
794 319780 : return style().spatium() * staffMag(tick);
795 : }
796 :
797 4209937 : double Staff::spatium(const EngravingItem* e) const
798 : {
799 4209937 : return style().spatium() * staffMag(e);
800 : }
801 :
802 : //---------------------------------------------------------
803 : // mag
804 : //---------------------------------------------------------
805 :
806 12086750 : double Staff::staffMag(const StaffType* stt) const
807 : {
808 12086750 : return (stt->isSmall() ? style().styleD(Sid::smallStaffMag) : 1.0) * stt->userMag();
809 : }
810 :
811 668814 : double Staff::staffMag(const Fraction& tick) const
812 : {
813 668814 : return staffMag(staffType(tick));
814 : }
815 :
816 11417936 : double Staff::staffMag(const EngravingItem* element) const
817 : {
818 11417936 : return staffMag(staffTypeForElement(element));
819 : }
820 :
821 : //---------------------------------------------------------
822 : // swing
823 : //---------------------------------------------------------
824 :
825 1391 : SwingParameters Staff::swing(const Fraction& tick) const
826 : {
827 1391 : SwingParameters sp;
828 1391 : int swingUnit = 0;
829 1391 : muse::ByteArray ba = style().styleSt(Sid::swingUnit).toAscii();
830 1391 : DurationType unit = TConv::fromXml(ba.constChar(), DurationType::V_INVALID);
831 1391 : int swingRatio = style().styleI(Sid::swingRatio);
832 1391 : if (unit == DurationType::V_EIGHTH) {
833 0 : swingUnit = Constants::DIVISION / 2;
834 1391 : } else if (unit == DurationType::V_16TH) {
835 0 : swingUnit = Constants::DIVISION / 4;
836 1391 : } else if (unit == DurationType::V_ZERO) {
837 1391 : swingUnit = 0;
838 : }
839 1391 : sp.swingRatio = swingRatio;
840 1391 : sp.swingUnit = swingUnit;
841 1391 : if (m_swingMap.empty()) {
842 1391 : return sp;
843 : }
844 :
845 0 : auto it = muse::findLessOrEqual(m_swingMap, tick.ticks());
846 0 : return it == m_swingMap.cend() ? sp : it->second;
847 1391 : }
848 :
849 18424 : const CapoParams& Staff::capo(const Fraction& tick) const
850 : {
851 18424 : static const CapoParams dummy;
852 18424 : if (m_capoMap.empty()) {
853 17792 : return dummy;
854 : }
855 :
856 632 : auto it = muse::findLessOrEqual(m_capoMap, tick.ticks());
857 632 : return it == m_capoMap.cend() ? dummy : it->second;
858 : }
859 :
860 65 : void Staff::insertCapoParams(const Fraction& tick, const CapoParams& params, bool ignoreNotationUpdate)
861 : {
862 65 : if (ignoreNotationUpdate) {
863 13 : m_capoMap.insert_or_assign(tick.ticks(), params);
864 13 : return;
865 : }
866 26 : auto isNeedUpdate = [](const CapoParams& oldParams, const CapoParams& newParams) -> bool {
867 44 : return !(oldParams.active == newParams.active
868 18 : && oldParams.transposeMode == newParams.transposeMode
869 2 : && oldParams.fretPosition == newParams.fretPosition
870 28 : && oldParams.ignoredStrings == newParams.ignoredStrings);
871 : };
872 :
873 52 : int startTick = tick.ticks();
874 52 : int endTick = -1;
875 :
876 52 : if (auto it = m_capoMap.find(startTick); it == m_capoMap.end()) {
877 26 : auto result = m_capoMap.insert({ startTick, params });
878 26 : if (const auto nextIt = std::next(result.first); nextIt != m_capoMap.end()) {
879 0 : endTick = nextIt->first;
880 : }
881 26 : if (result.first != m_capoMap.begin()) {
882 6 : const auto prevIt = std::prev(result.first);
883 6 : CapoParams oldParams = prevIt->second;
884 : // We don't need to apply any changes if the previous capo is inactive
885 6 : if (oldParams.active) {
886 2 : EditCapo::updateNotationForCapoChange(oldParams, params, this, startTick, endTick);
887 : }
888 : // This is an undo action
889 26 : } else if (CapoParams::TransposeMode::PLAYBACK_ONLY != params.transposeMode
890 10 : && params.active) {
891 8 : CapoParams oldParams;
892 8 : oldParams.transposeMode = CapoParams::TransposeMode::PLAYBACK_ONLY;
893 8 : oldParams.fretPosition = params.fretPosition;
894 8 : oldParams.active = params.active;
895 8 : oldParams.ignoredStrings = params.ignoredStrings;
896 8 : EditCapo::updateNotationForCapoChange(oldParams, params, this, startTick, endTick);
897 8 : }
898 : } else {
899 26 : CapoParams oldParams = it->second;
900 26 : if (!isNeedUpdate(oldParams, params)) {
901 0 : return;
902 : }
903 26 : auto result = m_capoMap.insert_or_assign(startTick, params);
904 26 : if (const auto nextIt = std::next(result.first); nextIt != m_capoMap.end()) {
905 0 : endTick = nextIt->first;
906 : }
907 26 : EditCapo::updateNotationForCapoChange(oldParams, params, this, startTick, endTick);
908 26 : }
909 : }
910 :
911 10 : void Staff::removeCapoParams(const mu::engraving::Fraction& tick)
912 : {
913 10 : const int startTick = tick.ticks();
914 :
915 10 : const auto it = m_capoMap.find(startTick);
916 10 : IF_ASSERT_FAILED(it != m_capoMap.end()) {
917 0 : LOGE() << "Key must exist in capo map!";
918 0 : return;
919 : }
920 :
921 10 : const auto nextCapoIt = std::next(it);
922 10 : const int endTick = nextCapoIt == m_capoMap.end() ? -1 : nextCapoIt->first;
923 :
924 10 : CapoParams revertParams;
925 10 : revertParams.fretPosition = it->second.fretPosition;
926 :
927 10 : CapoParams oldParams = it->second;
928 : // If this capo is inactive, it is treated as PLAYBACK_ONLY mode capo
929 10 : if (!oldParams.active) {
930 2 : oldParams.transposeMode = CapoParams::TransposeMode::PLAYBACK_ONLY;
931 : }
932 :
933 10 : if (it != m_capoMap.begin()) {
934 6 : revertParams = std::prev(it)->second;
935 : // If not active, treat as PLAYBACK_ONLY
936 6 : if (!revertParams.active) {
937 4 : revertParams.transposeMode = CapoParams::TransposeMode::PLAYBACK_ONLY;
938 : }
939 : }
940 10 : EditCapo::updateNotationForCapoChange(oldParams, revertParams, this, startTick, endTick);
941 :
942 10 : m_capoMap.erase(startTick);
943 10 : }
944 :
945 2219 : bool Staff::shouldMergeMatchingRests() const
946 : {
947 2219 : return mergeMatchingRests() == AutoOnOff::ON
948 2219 : || (mergeMatchingRests() == AutoOnOff::AUTO && style().value(Sid::mergeMatchingRests).toBool());
949 : }
950 :
951 : //---------------------------------------------------------
952 : // channel
953 : //---------------------------------------------------------
954 :
955 21439 : int Staff::channel(const Fraction& tick, voice_idx_t voice) const
956 : {
957 21439 : const std::map<int, int>& map = m_channelList[voice];
958 21439 : if (map.empty()) {
959 19283 : return 0;
960 : }
961 :
962 2156 : auto it = muse::findLessOrEqual(map, tick.ticks());
963 2156 : return it == map.cend() ? 0 : it->second;
964 : }
965 :
966 : //---------------------------------------------------------
967 : // middleLine
968 : // returns logical line number of middle staff line
969 : //---------------------------------------------------------
970 :
971 30 : int Staff::middleLine(const Fraction& tick) const
972 : {
973 30 : return staffType(tick)->middleLine();
974 : }
975 :
976 : //---------------------------------------------------------
977 : // bottomLine
978 : // returns logical line number of bottom staff line
979 : //---------------------------------------------------------
980 :
981 0 : int Staff::bottomLine(const Fraction& tick) const
982 : {
983 0 : return staffType(tick)->bottomLine();
984 : }
985 :
986 : //---------------------------------------------------------
987 : // stemless
988 : //---------------------------------------------------------
989 :
990 3002120 : bool Staff::stemless(const Fraction& tick) const
991 : {
992 3002120 : return staffType(tick)->stemless();
993 : }
994 :
995 : //---------------------------------------------------------
996 : // setSlashStyle
997 : //---------------------------------------------------------
998 :
999 0 : void Staff::setSlashStyle(const Fraction& tick, bool val)
1000 : {
1001 0 : staffType(tick)->setStemless(val);
1002 0 : }
1003 :
1004 : //---------------------------------------------------------
1005 : // isPrimaryStaff
1006 : /// if there are linked staves, the primary staff is
1007 : /// the one who is played back and it's not a tab staff
1008 : /// because we don't have enough information to play
1009 : /// e.g ornaments. NOTE: it's not necessarily the top staff!
1010 : //---------------------------------------------------------
1011 :
1012 7065 : bool Staff::isPrimaryStaff() const
1013 : {
1014 7065 : if (!m_links) {
1015 4636 : return true;
1016 : }
1017 :
1018 2429 : std::vector<const Staff*> linkedStavesInThisScore;
1019 2429 : std::vector<const Staff*> linkedNonTabStavesInThisScore;
1020 :
1021 7287 : for (const EngravingObject* linked : *m_links) {
1022 4858 : const Staff* staff = toStaff(linked);
1023 :
1024 4858 : if (staff->score() == score()) {
1025 4856 : linkedStavesInThisScore.push_back(staff);
1026 :
1027 4856 : if (!staff->isTabStaff(Fraction(0, 1))) {
1028 2431 : linkedNonTabStavesInThisScore.push_back(staff);
1029 : }
1030 : }
1031 : }
1032 :
1033 2429 : IF_ASSERT_FAILED(!linkedStavesInThisScore.empty()) {
1034 0 : return true;
1035 : }
1036 :
1037 2429 : if (!linkedNonTabStavesInThisScore.empty()) {
1038 2429 : return linkedNonTabStavesInThisScore.front() == this;
1039 : }
1040 :
1041 0 : return linkedStavesInThisScore.front() == this;
1042 2429 : }
1043 :
1044 : //---------------------------------------------------------
1045 : // staffType
1046 : //---------------------------------------------------------
1047 :
1048 11070121 : const StaffType* Staff::staffType(const Fraction& tick) const
1049 : {
1050 11070121 : return &m_staffTypeList.staffType(tick);
1051 : }
1052 :
1053 21273 : const StaffType* Staff::constStaffType(const Fraction& tick) const
1054 : {
1055 21273 : return &m_staffTypeList.staffType(tick);
1056 : }
1057 :
1058 384798 : StaffType* Staff::staffType(const Fraction& tick)
1059 : {
1060 384798 : return &m_staffTypeList.staffType(tick);
1061 : }
1062 :
1063 19492950 : const StaffType* Staff::staffTypeForElement(const EngravingItem* e) const
1064 : {
1065 19492950 : if (m_staffTypeList.uniqueStaffType()) {
1066 : // if one staff type spans for the entire staff, optimize by omitting a call to `tick()`
1067 19348078 : return &m_staffTypeList.staffType({ 0, 1 });
1068 : }
1069 : // Handle items at the last tick of measures as StaffTypeList::staffType rounds up
1070 144872 : const Measure* measure = e->findMeasure();
1071 144872 : const Fraction tick = measure ? measure->tick() : e->tick();
1072 144872 : return &m_staffTypeList.staffType(tick);
1073 : }
1074 :
1075 64919 : bool Staff::isStaffTypeStartFrom(const Fraction& tick) const
1076 : {
1077 64919 : return m_staffTypeList.isStaffTypeStartFrom(tick);
1078 : }
1079 :
1080 0 : void Staff::moveStaffType(const Fraction& from, const Fraction& to)
1081 : {
1082 0 : m_staffTypeList.moveStaffType(from, to);
1083 0 : staffTypeListChanged(from);
1084 0 : }
1085 :
1086 1055 : std::pair<int, int> Staff::staffTypeRange(const Fraction& tick) const
1087 : {
1088 1055 : return m_staffTypeList.staffTypeRange(tick);
1089 : }
1090 :
1091 : //---------------------------------------------------------
1092 : // staffTypeListChanged
1093 : // Signal that the staffTypeList has changed at
1094 : // position tick. Update layout range.
1095 : //---------------------------------------------------------
1096 :
1097 18 : void Staff::staffTypeListChanged(const Fraction& tick)
1098 : {
1099 18 : std::pair<int, int> range = m_staffTypeList.staffTypeRange(tick);
1100 :
1101 18 : if (range.first < 0) {
1102 0 : triggerLayout(Fraction(0, 1));
1103 : } else {
1104 18 : triggerLayout(Fraction::fromTicks(range.first));
1105 : }
1106 :
1107 18 : if (range.second < 0) {
1108 : // When reading a score and there is a Staff Change on the first
1109 : // measure, there are no measures yet and nothing to layout.
1110 18 : if (score()->lastMeasure()) {
1111 18 : triggerLayout(score()->lastMeasure()->endTick());
1112 : }
1113 : } else {
1114 0 : triggerLayout(Fraction::fromTicks(range.second));
1115 : }
1116 18 : }
1117 :
1118 : //---------------------------------------------------------
1119 : // setStaffType
1120 : //---------------------------------------------------------
1121 :
1122 7250 : StaffType* Staff::setStaffType(const Fraction& tick, const StaffType& nst)
1123 : {
1124 7250 : StaffType* stt = m_staffTypeList.setStaffType(tick, nst);
1125 7250 : stt->setScore(score());
1126 7250 : return stt;
1127 : }
1128 :
1129 : //---------------------------------------------------------
1130 : // setStaffType
1131 : //---------------------------------------------------------
1132 :
1133 0 : void Staff::removeStaffType(const Fraction& tick)
1134 : {
1135 0 : double old = spatium(tick);
1136 0 : const bool removed = m_staffTypeList.removeStaffType(tick);
1137 0 : if (!removed) {
1138 0 : return;
1139 : }
1140 0 : setLocalSpatium(old, spatium(tick), tick);
1141 0 : staffTypeListChanged(tick);
1142 : }
1143 :
1144 : //---------------------------------------------------------
1145 : // init
1146 : //---------------------------------------------------------
1147 :
1148 126 : void Staff::init(const InstrumentTemplate* t, const StaffType* staffType, staff_idx_t templateStaffIdx)
1149 : {
1150 : // Initialises a staff from an InstrumentTemplate and StaffType. The staff MUST be added to the score already.
1151 126 : assert(idx() != muse::nidx);
1152 : // set staff-type-independent parameters
1153 126 : const StaffType* pst = staffType ? staffType : t->staffTypePreset;
1154 126 : if (!pst) {
1155 3 : pst = StaffType::getDefaultPreset(t->staffGroup);
1156 : }
1157 :
1158 126 : const staff_idx_t staffIdx = idx() != muse::nidx ? idx() : score()->nstaves();
1159 :
1160 126 : StaffType* stt = setStaffType(Fraction(0, 1), *pst);
1161 126 : if (templateStaffIdx >= MAX_STAVES) {
1162 0 : stt->setSmall(false);
1163 : } else {
1164 126 : stt->setSmall(t->smallStaff[templateStaffIdx]);
1165 126 : EditStaffBrackets::setBracketType(score(), staffIdx, 0, t->bracket[templateStaffIdx]);
1166 126 : EditStaffBrackets::setBracketSpan(score(), staffIdx, 0, t->bracketSpan[templateStaffIdx]);
1167 126 : setBarLineSpan(t->barlineSpan[templateStaffIdx]);
1168 : }
1169 126 : setDefaultClefType(t->clefType(templateStaffIdx));
1170 126 : }
1171 :
1172 : //---------------------------------------------------------
1173 : // init
1174 : //---------------------------------------------------------
1175 :
1176 108 : void Staff::init(const Staff* s)
1177 : {
1178 108 : m_id = s->m_id;
1179 :
1180 108 : setStaffType(Fraction(0, 1), s->m_staffTypeList.staffType(Fraction(0, 1)));
1181 108 : for (const auto& stPair : s->m_staffTypeList.staffTypeChanges()) {
1182 0 : const StaffType& st = stPair.second;
1183 0 : StaffType newStaffType(st);
1184 0 : setStaffType(Fraction::fromTicks(stPair.first), newStaffType);
1185 0 : }
1186 :
1187 108 : setDefaultClefType(s->defaultClefType());
1188 108 : m_barLineFrom = s->m_barLineFrom;
1189 108 : m_barLineTo = s->m_barLineTo;
1190 108 : m_hideWhenEmpty = s->m_hideWhenEmpty;
1191 108 : m_cutaway = s->m_cutaway;
1192 108 : m_showIfEntireSystemEmpty = s->m_showIfEntireSystemEmpty;
1193 108 : m_hideSystemBarLine = s->m_hideSystemBarLine;
1194 108 : m_mergeMatchingRests = s->m_mergeMatchingRests;
1195 108 : m_color = s->m_color;
1196 108 : m_userDist = s->m_userDist;
1197 108 : m_visibilityVoices = s->m_visibilityVoices;
1198 108 : m_capoMap = s->m_capoMap;
1199 108 : }
1200 :
1201 72096 : const ID& Staff::id() const
1202 : {
1203 72096 : return m_id;
1204 : }
1205 :
1206 4477 : void Staff::setId(const ID& id)
1207 : {
1208 4477 : m_id = id;
1209 4477 : }
1210 :
1211 : //---------------------------------------------------------
1212 : // initFromStaffType
1213 : //---------------------------------------------------------
1214 :
1215 4304 : void Staff::initFromStaffType(const StaffType* staffType)
1216 : {
1217 : // get staff type if given (if none, get default preset for default staff group)
1218 4304 : if (!staffType) {
1219 4300 : staffType = StaffType::getDefaultPreset(StaffGroup::STANDARD);
1220 : }
1221 :
1222 : // use selected staff type
1223 4304 : setStaffType(Fraction(0, 1), *staffType);
1224 4304 : }
1225 :
1226 : //---------------------------------------------------------
1227 : // show
1228 : //---------------------------------------------------------
1229 :
1230 3775254 : bool Staff::show() const
1231 : {
1232 3775254 : return m_part->show() && visible();
1233 : }
1234 :
1235 : //---------------------------------------------------------
1236 : // genKeySig
1237 : //---------------------------------------------------------
1238 :
1239 0 : bool Staff::genKeySig()
1240 : {
1241 0 : if (constStaffType(Fraction(0, 1))->group() == StaffGroup::TAB) {
1242 0 : return false;
1243 : } else {
1244 0 : return constStaffType(Fraction(0, 1))->genKeysig();
1245 : }
1246 : }
1247 :
1248 : //---------------------------------------------------------
1249 : // showLedgerLines
1250 : //---------------------------------------------------------
1251 :
1252 14071 : bool Staff::showLedgerLines(const Fraction& tick) const
1253 : {
1254 14071 : return staffType(tick)->showLedgerLines();
1255 : }
1256 :
1257 : //---------------------------------------------------------
1258 : // color
1259 : //---------------------------------------------------------
1260 :
1261 50805 : Color Staff::color(const Fraction& tick) const
1262 : {
1263 50805 : return staffType(tick)->color();
1264 : }
1265 :
1266 : //---------------------------------------------------------
1267 : // setColor
1268 : //---------------------------------------------------------
1269 :
1270 0 : void Staff::setColor(const Fraction& tick, const Color& val)
1271 : {
1272 0 : staffType(tick)->setColor(val);
1273 0 : }
1274 :
1275 : //---------------------------------------------------------
1276 : // updateOttava
1277 : //---------------------------------------------------------
1278 :
1279 3186 : void Staff::updateOttava()
1280 : {
1281 3186 : staff_idx_t staffIdx = idx();
1282 3186 : m_pitchOffsetMap.clear();
1283 5873 : for (auto i : score()->spanner()) {
1284 2687 : const Spanner* s = i.second;
1285 2687 : if (s->isOttava() && s->staffIdx() == staffIdx && s->playSpanner()) {
1286 223 : const Ottava* o = toOttava(s);
1287 223 : m_pitchOffsetMap.insert_or_assign(o->tick().ticks(), o->pitchShift());
1288 223 : m_pitchOffsetMap.insert_or_assign(o->tick2().ticks(), 0);
1289 : }
1290 : }
1291 3186 : }
1292 :
1293 57402 : int Staff::pitchOffset(const Fraction& tick) const
1294 : {
1295 57402 : if (m_pitchOffsetMap.empty()) {
1296 54822 : return 0;
1297 : }
1298 :
1299 2580 : const auto it = muse::findLessOrEqual(m_pitchOffsetMap, tick.ticks());
1300 2580 : return it == m_pitchOffsetMap.cend() ? 0 : it->second;
1301 : }
1302 :
1303 : //---------------------------------------------------------
1304 : // insertTime
1305 : //---------------------------------------------------------
1306 :
1307 1108 : void Staff::insertTime(const Fraction& tick, const Fraction& len)
1308 : {
1309 1108 : if (len.isZero()) {
1310 9 : return;
1311 : }
1312 :
1313 : // move all keys and clefs >= tick
1314 :
1315 1099 : if (len < Fraction(0, 1)) {
1316 : // remove entries between tickpos >= tick and tickpos < (tick+len)
1317 213 : m_keys.erase(m_keys.lower_bound(tick.ticks()), m_keys.lower_bound((tick - len).ticks()));
1318 213 : m_clefs.erase(m_clefs.lower_bound(tick.ticks()), m_clefs.lower_bound((tick - len).ticks()));
1319 : }
1320 :
1321 1099 : KeyList kl2;
1322 1173 : for (auto i = m_keys.lower_bound(tick.ticks()); i != m_keys.end();) {
1323 74 : KeySigEvent kse = i->second;
1324 74 : Fraction t = Fraction::fromTicks(i->first);
1325 74 : if (t + len < score()->endTick()) {
1326 34 : m_keys.erase(i++);
1327 34 : kl2[(t + len).ticks()] = kse;
1328 : } else {
1329 40 : ++i;
1330 : }
1331 74 : }
1332 1099 : m_keys.insert(kl2.begin(), kl2.end());
1333 :
1334 : // check if there is a clef at the end of measure
1335 : // before tick
1336 1099 : Clef* clef = 0;
1337 1099 : Measure* m = score()->tick2measure(tick);
1338 1099 : if (m && (m->tick() == tick) && (m->prevMeasure())) {
1339 729 : m = m->prevMeasure();
1340 729 : Segment* s = m->findSegment(SegmentType::Clef, tick);
1341 729 : if (s) {
1342 3 : track_idx_t track = idx() * VOICES;
1343 3 : clef = toClef(s->element(track));
1344 : }
1345 : }
1346 :
1347 1099 : ClefList cl2;
1348 1139 : for (auto i = m_clefs.lower_bound(tick.ticks()); i != m_clefs.end();) {
1349 40 : ClefTypeList ctl = i->second;
1350 40 : Fraction t = Fraction::fromTicks(i->first);
1351 40 : if (clef && tick == t) {
1352 2 : ++i;
1353 2 : continue;
1354 : }
1355 38 : m_clefs.erase(i++);
1356 38 : cl2.setClef((t + len).ticks(), ctl);
1357 : }
1358 1099 : m_clefs.insert(cl2.begin(), cl2.end());
1359 :
1360 : // check if there is a clef at the end of measure
1361 : // before tick: do not remove from clefs list
1362 :
1363 1099 : if (clef) {
1364 3 : setClef(clef);
1365 : }
1366 :
1367 1099 : updateOttava();
1368 : DUMP_CLEFS(" insertTime");
1369 1099 : }
1370 :
1371 : //---------------------------------------------------------
1372 : // staffList
1373 : // return list of linked staves
1374 : //---------------------------------------------------------
1375 :
1376 6818 : std::vector<Staff*> Staff::staffList() const
1377 : {
1378 6818 : std::vector<Staff*> staffList;
1379 6818 : if (m_links) {
1380 187 : staffList.reserve(m_links->size());
1381 565 : for (EngravingObject* e : *m_links) {
1382 378 : staffList.push_back(toStaff(e));
1383 : }
1384 : } else {
1385 6631 : staffList.push_back(const_cast<Staff*>(this));
1386 : }
1387 6818 : return staffList;
1388 0 : }
1389 :
1390 1491 : Staff* Staff::primaryStaff() const
1391 : {
1392 1491 : const LinkedObjects* linkedElements = links();
1393 1491 : if (!linkedElements) {
1394 363 : return nullptr;
1395 : }
1396 :
1397 1128 : for (auto linkedElement : *linkedElements) {
1398 1128 : Staff* staff = toStaff(linkedElement);
1399 1128 : if (staff && staff->isPrimaryStaff()) {
1400 1128 : return staff;
1401 : }
1402 : }
1403 :
1404 0 : return nullptr;
1405 : }
1406 :
1407 : //---------------------------------------------------------
1408 : // rstaff
1409 : //---------------------------------------------------------
1410 :
1411 397 : staff_idx_t Staff::rstaff() const
1412 : {
1413 397 : return muse::indexOf(m_part->staves(), this);
1414 : }
1415 :
1416 : //---------------------------------------------------------
1417 : // isTop
1418 : //---------------------------------------------------------
1419 :
1420 294 : bool Staff::isTop() const
1421 : {
1422 294 : if (m_part->staves().empty()) {
1423 0 : return false;
1424 : }
1425 :
1426 294 : return m_part->staves().front() == this;
1427 : }
1428 :
1429 : //---------------------------------------------------------
1430 : // getProperty
1431 : //---------------------------------------------------------
1432 :
1433 18457 : PropertyValue Staff::getProperty(Pid id) const
1434 : {
1435 18457 : switch (id) {
1436 0 : case Pid::SMALL:
1437 0 : return staffType(Fraction(0, 1))->isSmall();
1438 0 : case Pid::MAG:
1439 0 : return staffType(Fraction(0, 1))->userMag();
1440 0 : case Pid::LINE_DISTANCE:
1441 0 : return staffType(Fraction(0, 1))->lineDistance();
1442 0 : case Pid::STAFF_INVISIBLE:
1443 0 : return staffType(Fraction(0, 1))->invisible();
1444 0 : case Pid::HIDE_WHEN_EMPTY:
1445 0 : return m_hideWhenEmpty;
1446 0 : case Pid::STAFF_COLOR:
1447 0 : return PropertyValue::fromValue(staffType(Fraction(0, 1))->color());
1448 1978 : case Pid::PLAYBACK_VOICE1:
1449 1978 : return playbackVoice(0);
1450 1978 : case Pid::PLAYBACK_VOICE2:
1451 1978 : return playbackVoice(1);
1452 1978 : case Pid::PLAYBACK_VOICE3:
1453 1978 : return playbackVoice(2);
1454 1978 : case Pid::PLAYBACK_VOICE4:
1455 1978 : return playbackVoice(3);
1456 2424 : case Pid::STAFF_BARLINE_SPAN:
1457 2424 : return barLineSpan();
1458 1979 : case Pid::STAFF_BARLINE_SPAN_FROM:
1459 1979 : return barLineFrom();
1460 1979 : case Pid::STAFF_BARLINE_SPAN_TO:
1461 1979 : return barLineTo();
1462 1978 : case Pid::STAFF_USERDIST:
1463 1978 : return userDist();
1464 124 : case Pid::GENERATED:
1465 124 : return false;
1466 1992 : case Pid::SHOW_MEASURE_NUMBERS:
1467 1992 : return m_showMeasureNumbers;
1468 0 : case Pid::SHOW_IF_ENTIRE_SYSTEM_EMPTY:
1469 0 : return m_showIfEntireSystemEmpty;
1470 33 : case Pid::VISIBLE:
1471 33 : return visible();
1472 9 : case Pid::STAFF_CUTAWAY:
1473 9 : return cutaway();
1474 9 : case Pid::STAFF_HIDE_SYSTEM_BARLINE:
1475 9 : return hideSystemBarLine();
1476 9 : case Pid::STAFF_MERGE_MATCHING_RESTS:
1477 9 : return int(mergeMatchingRests());
1478 9 : case Pid::STAFF_REFLECT_TRANSPOSITION:
1479 9 : return reflectTranspositionInLinkedTab();
1480 0 : default:
1481 0 : LOGD("unhandled id <%s>", propertyName(id));
1482 0 : return PropertyValue();
1483 : }
1484 : }
1485 :
1486 : //---------------------------------------------------------
1487 : // setProperty
1488 : //---------------------------------------------------------
1489 :
1490 281 : bool Staff::setProperty(Pid id, const PropertyValue& v)
1491 : {
1492 281 : switch (id) {
1493 0 : case Pid::SMALL: {
1494 0 : double _spatium = spatium(Fraction(0, 1));
1495 0 : staffType(Fraction(0, 1))->setSmall(v.toBool());
1496 0 : setLocalSpatium(_spatium, spatium(Fraction(0, 1)), Fraction(0, 1));
1497 0 : break;
1498 : }
1499 9 : case Pid::MAG: {
1500 9 : double _spatium = spatium(Fraction(0, 1));
1501 9 : staffType(Fraction(0, 1))->setUserMag(v.toReal());
1502 9 : setLocalSpatium(_spatium, spatium(Fraction(0, 1)), Fraction(0, 1));
1503 9 : break;
1504 : }
1505 9 : case Pid::LINE_DISTANCE: {
1506 9 : staffType(Fraction(0, 1))->setLineDistance(v.value<Spatium>());
1507 9 : break;
1508 : }
1509 0 : case Pid::HIDE_WHEN_EMPTY:
1510 0 : setHideWhenEmpty(v.value<AutoOnOff>());
1511 0 : break;
1512 0 : case Pid::STAFF_COLOR:
1513 0 : setColor(Fraction(0, 1), v.value<Color>());
1514 0 : break;
1515 0 : case Pid::PLAYBACK_VOICE1:
1516 0 : setPlaybackVoice(0, v.toBool());
1517 0 : break;
1518 0 : case Pid::PLAYBACK_VOICE2:
1519 0 : setPlaybackVoice(1, v.toBool());
1520 0 : break;
1521 0 : case Pid::PLAYBACK_VOICE3:
1522 0 : setPlaybackVoice(2, v.toBool());
1523 0 : break;
1524 0 : case Pid::PLAYBACK_VOICE4:
1525 0 : setPlaybackVoice(3, v.toBool());
1526 0 : break;
1527 230 : case Pid::STAFF_BARLINE_SPAN: {
1528 230 : setBarLineSpan(v.toBool());
1529 : // update non-generated barlines
1530 230 : track_idx_t track = idx() * VOICES;
1531 230 : std::vector<EngravingItem*> blList;
1532 2095 : for (Measure* m = score()->firstMeasure(); m; m = m->nextMeasure()) {
1533 1865 : Segment* s = m->getSegmentR(SegmentType::EndBarLine, m->ticks());
1534 1865 : if (s && s->element(track)) {
1535 745 : blList.push_back(s->element(track));
1536 : }
1537 1865 : if (Measure* mm = m->mmRest()) {
1538 0 : Segment* ss = mm->getSegmentR(SegmentType::EndBarLine, mm->ticks());
1539 0 : if (ss && ss->element(track)) {
1540 0 : blList.push_back(ss->element(track));
1541 : }
1542 : }
1543 : }
1544 975 : for (EngravingItem* e : blList) {
1545 745 : if (e && e->isBarLine() && !e->generated()) {
1546 85 : toBarLine(e)->setSpanStaff(barLineSpan());
1547 : }
1548 : }
1549 230 : }
1550 230 : break;
1551 1 : case Pid::STAFF_BARLINE_SPAN_FROM:
1552 1 : setBarLineFrom(v.toInt());
1553 1 : break;
1554 1 : case Pid::STAFF_BARLINE_SPAN_TO:
1555 1 : setBarLineTo(v.toInt());
1556 1 : break;
1557 0 : case Pid::STAFF_USERDIST:
1558 0 : setUserDist(v.value<Spatium>());
1559 0 : break;
1560 3 : case Pid::SHOW_MEASURE_NUMBERS:
1561 3 : m_showMeasureNumbers = v.value<AutoOnOff>();
1562 3 : break;
1563 0 : case Pid::SHOW_IF_ENTIRE_SYSTEM_EMPTY:
1564 0 : m_showIfEntireSystemEmpty = v.toBool();
1565 0 : break;
1566 12 : case Pid::VISIBLE:
1567 12 : setVisible(v.toBool());
1568 12 : masterScore()->rebuildMidiMapping();
1569 12 : break;
1570 4 : case Pid::STAFF_CUTAWAY:
1571 4 : setCutaway(v.toBool());
1572 4 : break;
1573 4 : case Pid::STAFF_HIDE_SYSTEM_BARLINE:
1574 4 : setHideSystemBarLine(v.toBool());
1575 4 : break;
1576 4 : case Pid::STAFF_MERGE_MATCHING_RESTS:
1577 4 : setMergeMatchingRests(AutoOnOff(v.toInt()));
1578 4 : break;
1579 4 : case Pid::STAFF_REFLECT_TRANSPOSITION:
1580 4 : setReflectTranspositionInLinkedTab(v.toBool());
1581 4 : break;
1582 0 : default:
1583 0 : LOGD("unhandled id <%s>", propertyName(id));
1584 0 : break;
1585 : }
1586 281 : triggerLayout();
1587 281 : return true;
1588 : }
1589 :
1590 : //---------------------------------------------------------
1591 : // propertyDefault
1592 : //---------------------------------------------------------
1593 :
1594 17803 : PropertyValue Staff::propertyDefault(Pid id) const
1595 : {
1596 17803 : switch (id) {
1597 0 : case Pid::SMALL:
1598 0 : return false;
1599 0 : case Pid::MAG:
1600 0 : return 1.0;
1601 0 : case Pid::HIDE_WHEN_EMPTY:
1602 0 : return AutoOnOff::AUTO;
1603 0 : case Pid::STAFF_COLOR:
1604 0 : return PropertyValue::fromValue(configuration()->defaultColor());
1605 7912 : case Pid::PLAYBACK_VOICE1:
1606 : case Pid::PLAYBACK_VOICE2:
1607 : case Pid::PLAYBACK_VOICE3:
1608 : case Pid::PLAYBACK_VOICE4:
1609 7912 : return true;
1610 1978 : case Pid::STAFF_BARLINE_SPAN:
1611 1978 : return false;
1612 3956 : case Pid::STAFF_BARLINE_SPAN_FROM:
1613 : case Pid::STAFF_BARLINE_SPAN_TO:
1614 3956 : return 0;
1615 1978 : case Pid::STAFF_USERDIST:
1616 1978 : return 0.0_sp;
1617 1979 : case Pid::SHOW_MEASURE_NUMBERS:
1618 3958 : return AutoOnOff::AUTO;
1619 0 : case Pid::SHOW_IF_ENTIRE_SYSTEM_EMPTY:
1620 0 : return false;
1621 0 : case Pid::VISIBLE:
1622 0 : return true;
1623 0 : case Pid::STAFF_CUTAWAY:
1624 0 : return false;
1625 0 : case Pid::STAFF_HIDE_SYSTEM_BARLINE:
1626 0 : return false;
1627 0 : case Pid::STAFF_MERGE_MATCHING_RESTS:
1628 0 : return int(AutoOnOff::AUTO);
1629 0 : case Pid::STAFF_REFLECT_TRANSPOSITION:
1630 0 : return true;
1631 0 : default:
1632 0 : LOGD("unhandled id <%s>", propertyName(id));
1633 0 : return PropertyValue();
1634 : }
1635 : }
1636 :
1637 : //---------------------------------------------------------
1638 : // localSpatiumChanged
1639 : //---------------------------------------------------------
1640 :
1641 9 : void Staff::setLocalSpatium(double oldVal, double newVal, Fraction tick)
1642 : {
1643 9 : const int intEndTick = m_staffTypeList.staffTypeRange(tick).second;
1644 9 : const Fraction etick = (intEndTick == -1) ? score()->lastMeasure()->endTick() : Fraction::fromTicks(intEndTick);
1645 :
1646 9 : staff_idx_t staffIdx = idx();
1647 9 : track_idx_t startTrack = staffIdx * VOICES;
1648 9 : track_idx_t endTrack = startTrack + VOICES;
1649 10 : for (Segment* s = score()->tick2rightSegment(tick); s && s->tick() < etick; s = s->next1()) {
1650 1 : for (EngravingItem* e : s->annotations()) {
1651 0 : if (e->track() >= startTrack && e->track() < endTrack) {
1652 0 : e->localSpatiumChanged(oldVal, newVal);
1653 : }
1654 : }
1655 5 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
1656 4 : if (s->element(track)) {
1657 0 : s->element(track)->localSpatiumChanged(oldVal, newVal);
1658 : }
1659 : }
1660 : }
1661 9 : auto spanners = score()->spannerMap().findContained(tick.ticks(), etick.ticks());
1662 9 : for (auto interval : spanners) {
1663 0 : Spanner* spanner = interval.value;
1664 0 : if (spanner->staffIdx() == staffIdx) {
1665 0 : for (auto k : spanner->spannerSegments()) {
1666 0 : k->localSpatiumChanged(oldVal, newVal);
1667 : }
1668 : }
1669 : }
1670 9 : }
1671 :
1672 : //---------------------------------------------------------
1673 : // isPitchedStaff
1674 : //---------------------------------------------------------
1675 :
1676 73632 : bool Staff::isPitchedStaff(const Fraction& tick) const
1677 : {
1678 : //return staffType(tick)->group() == StaffGroup::STANDARD;
1679 73632 : return staffType(tick)->group() != StaffGroup::TAB && !originPart(tick)->instrument(tick)->useDrumset();
1680 : }
1681 :
1682 : //---------------------------------------------------------
1683 : // isTabStaff
1684 : //---------------------------------------------------------
1685 :
1686 922704 : bool Staff::isTabStaff(const Fraction& tick) const
1687 : {
1688 922704 : return staffType(tick)->group() == StaffGroup::TAB;
1689 : }
1690 :
1691 : //---------------------------------------------------------
1692 : // isDrumStaff
1693 : //---------------------------------------------------------
1694 :
1695 43723 : bool Staff::isDrumStaff(const Fraction& tick) const
1696 : {
1697 : //check for instrument instead of staffType (for pitched to unpitched instr. changes)
1698 43723 : return originPart(tick)->instrument(tick)->useDrumset();
1699 : }
1700 :
1701 : //---------------------------------------------------------
1702 : // isJianpuStaff
1703 : //---------------------------------------------------------
1704 :
1705 1210983 : bool Staff::isJianpuStaff(const Fraction& tick) const
1706 : {
1707 1210983 : return staffType(tick)->type() == StaffTypes::JIANPU;
1708 : }
1709 :
1710 : //---------------------------------------------------------
1711 : // lines
1712 : //---------------------------------------------------------
1713 :
1714 617674 : int Staff::lines(const Fraction& tick) const
1715 : {
1716 617674 : return staffType(tick)->lines();
1717 : }
1718 :
1719 : //---------------------------------------------------------
1720 : // setLines
1721 : //---------------------------------------------------------
1722 :
1723 763 : void Staff::setLines(const Fraction& tick, int val)
1724 : {
1725 763 : staffType(tick)->setLines(val);
1726 763 : }
1727 :
1728 : //---------------------------------------------------------
1729 : // lineDistance
1730 : // distance between staff lines
1731 : //---------------------------------------------------------
1732 :
1733 375102 : double Staff::lineDistance(const Fraction& tick) const
1734 : {
1735 375102 : return staffType(tick)->lineDistance().val();
1736 : }
1737 :
1738 : //---------------------------------------------------------
1739 : // invisible
1740 : //---------------------------------------------------------
1741 :
1742 296569 : bool Staff::isLinesInvisible(const Fraction& tick) const
1743 : {
1744 296569 : return staffType(tick)->invisible();
1745 : }
1746 :
1747 : //---------------------------------------------------------
1748 : // setInvisible
1749 : //---------------------------------------------------------
1750 :
1751 0 : void Staff::setIsLinesInvisible(const Fraction& tick, bool val)
1752 : {
1753 0 : staffType(tick)->setInvisible(val);
1754 0 : }
1755 : }
|