/
system.cpp
1407 lines (1240 loc) · 48.6 KB
/
system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2011 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
/**
\file
Implementation of classes SysStaff and System.
*/
#include "system.h"
#include "measure.h"
#include "segment.h"
#include "score.h"
#include "sig.h"
#include "key.h"
#include "xml.h"
#include "clef.h"
#include "text.h"
#include "navigate.h"
#include "select.h"
#include "staff.h"
#include "part.h"
#include "page.h"
#include "style.h"
#include "bracket.h"
#include "mscore.h"
#include "barline.h"
#include "system.h"
#include "box.h"
#include "chordrest.h"
#include "iname.h"
#include "spanner.h"
#include "sym.h"
#include "spacer.h"
#include "systemdivider.h"
#include "textframe.h"
#include "stafflines.h"
#include "bracketItem.h"
namespace Ms {
//---------------------------------------------------------
// ~SysStaff
//---------------------------------------------------------
SysStaff::~SysStaff()
{
qDeleteAll(instrumentNames);
}
//---------------------------------------------------------
// System
//---------------------------------------------------------
System::System(Score* s)
: Element(s)
{
}
//---------------------------------------------------------
// ~System
//---------------------------------------------------------
System::~System()
{
for (SpannerSegment* ss : spannerSegments()) {
if (ss->system() == this)
ss->setParent(nullptr);
}
for (MeasureBase* mb : measures()) {
if (mb->system() == this)
mb->setSystem(nullptr);
}
qDeleteAll(_staves);
qDeleteAll(_brackets);
delete _systemDividerLeft;
delete _systemDividerRight;
}
//---------------------------------------------------------
/// clear
/// Clear layout of System
//---------------------------------------------------------
void System::clear()
{
for (MeasureBase* mb : measures()) {
if (mb->system() == this)
mb->setSystem(nullptr);
}
ml.clear();
for (SpannerSegment* ss : _spannerSegments) {
if (ss->system() == this)
ss->setParent(0); // assume parent() is System
}
_spannerSegments.clear();
// _systemDividers are reused
}
//---------------------------------------------------------
// appendMeasure
//---------------------------------------------------------
void System::appendMeasure(MeasureBase* mb)
{
Q_ASSERT(!mb->isMeasure() || !(score()->styleB(Sid::createMultiMeasureRests) && toMeasure(mb)->hasMMRest()));
mb->setSystem(this);
ml.push_back(mb);
}
//---------------------------------------------------------
// removeMeasure
//---------------------------------------------------------
void System::removeMeasure(MeasureBase* mb)
{
ml.erase(std::remove(ml.begin(), ml.end(), mb), ml.end());
if (mb->system() == this)
mb->setSystem(nullptr);
}
//---------------------------------------------------------
// removeLastMeasure
//---------------------------------------------------------
void System::removeLastMeasure()
{
if (ml.empty())
return;
MeasureBase* mb = ml.back();
ml.pop_back();
if (mb->system() == this)
mb->setSystem(nullptr);
}
//---------------------------------------------------------
// vbox
// a system can only contain one vertical frame
//---------------------------------------------------------
Box* System::vbox() const
{
if (!ml.empty()) {
if (ml[0]->isVBox() || ml[0]->isTBox())
return toBox(ml[0]);
}
return 0;
}
//---------------------------------------------------------
// insertStaff
//---------------------------------------------------------
SysStaff* System::insertStaff(int idx)
{
SysStaff* staff = new SysStaff;
if (idx) {
// HACK: guess position
staff->bbox().setY(_staves[idx-1]->y() + 6 * spatium());
}
_staves.insert(idx, staff);
return staff;
}
//---------------------------------------------------------
// removeStaff
//---------------------------------------------------------
void System::removeStaff(int idx)
{
_staves.takeAt(idx);
}
//---------------------------------------------------------
// adjustStavesNumber
//---------------------------------------------------------
void System::adjustStavesNumber(int nstaves)
{
for (int i = _staves.size(); i < nstaves; ++i)
insertStaff(i);
const int dn = _staves.size() - nstaves;
for (int i = 0; i < dn; ++i)
removeStaff(_staves.size() - 1);
}
//---------------------------------------------------------
// layoutSystem
/// Layout the System
//---------------------------------------------------------
void System::layoutSystem(qreal xo1)
{
if (_staves.empty()) // ignore vbox
return;
static const Spatium instrumentNameOffset(1.0); // TODO: make style value
int nstaves = _staves.size();
//---------------------------------------------------
// find x position of staves
// create brackets
//---------------------------------------------------
qreal xoff2 = 0.0; // x offset for instrument name
int columns = getBracketsColumnsCount();
#if (!defined (_MSCVER) && !defined (_MSC_VER))
qreal bracketWidth[columns];
#else
// MSVC does not support VLA. Replace with std::vector. If profiling determines that the
// heap allocation is slow, an optimization might be used.
std::vector<qreal> bracketWidth(columns);
#endif
for (int i = 0; i < columns; ++i)
bracketWidth[i] = 0.0;
QList<Bracket*> bl;
bl.swap(_brackets);
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
Staff* s = score()->staff(staffIdx);
for (int i = 0; i < columns; ++i) {
for (auto bi : s->brackets()) {
if (bi->column() != i || bi->bracketType() == BracketType::NO_BRACKET)
continue;
Bracket* b = createBracket(bi, i, staffIdx, bl, this->firstMeasure());
if (b != nullptr) bracketWidth[i] = qMax(bracketWidth[i], b->width());
}
}
if (!staff(staffIdx)->show())
continue;
for (InstrumentName* t : _staves[staffIdx]->instrumentNames) {
t->layout();
qreal w = t->width() + point(instrumentNameOffset);
if (w > xoff2)
xoff2 = w;
}
}
for (Bracket* b : bl)
delete b;
//---------------------------------------------------
// layout SysStaff and StaffLines
//---------------------------------------------------
_leftMargin = xoff2;
qreal bd = score()->styleP(Sid::bracketDistance);
if (!_brackets.empty()) {
for (int w : bracketWidth)
_leftMargin += w + bd;
}
int nVisible = 0;
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
SysStaff* s = _staves[staffIdx];
Staff* staff = score()->staff(staffIdx);
if (!staff->show() || !s->show()) {
s->setbbox(QRectF());
continue;
}
++nVisible;
qreal staffMag = staff->mag(Fraction(0,1)); // ??? TODO
int staffLines = staff->lines(Fraction(0,1));
if (staffLines == 1) {
qreal h = staff->lineDistance(Fraction(0,1)) * staffMag * spatium();
s->bbox().setRect(_leftMargin + xo1, -h, 0.0, 2 * h);
}
else {
qreal h = (staffLines - 1) * staff->lineDistance(Fraction(0,1));
h = h * staffMag * spatium();
s->bbox().setRect(_leftMargin + xo1, 0.0, 0.0, h);
}
}
//---------------------------------------------------
// layout brackets
//---------------------------------------------------
setBracketsXPosition(xo1 + _leftMargin);
//---------------------------------------------------
// layout instrument names x position
//---------------------------------------------------
int idx = 0;
for (const Part* p : score()->parts()) {
SysStaff* s = staff(idx);
if (s->show() && p->show()) {
for (InstrumentName* t : s->instrumentNames) {
switch (int(t->align()) & int(Align::HMASK)) {
case int(Align::LEFT):
t->rxpos() = 0;
break;
case int(Align::HCENTER):
t->rxpos() = (xoff2 - point(instrumentNameOffset) + xo1) * .5;
break;
case int(Align::RIGHT):
default:
t->rxpos() = xoff2 - point(instrumentNameOffset) + xo1;
break;
}
// t->rxpos() += t->offset(t->spatium()).x();
}
}
idx += p->nstaves();
}
}
//---------------------------------------------------------
// addBrackets
// Add brackets in front of this measure, typically behind a HBox
//---------------------------------------------------------
void System::addBrackets(Measure* measure)
{
if (_staves.empty()) // ignore vbox
return;
int nstaves = _staves.size();
//---------------------------------------------------
// find x position of staves
// create brackets
//---------------------------------------------------
int columns = getBracketsColumnsCount();
QList<Bracket*> bl;
bl.swap(_brackets);
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
Staff* s = score()->staff(staffIdx);
for (int i = 0; i < columns; ++i) {
for (auto bi : s->brackets()) {
if (bi->column() != i || bi->bracketType() == BracketType::NO_BRACKET)
continue;
createBracket(bi, i, staffIdx, bl, measure);
}
}
if (!staff(staffIdx)->show())
continue;
}
//---------------------------------------------------
// layout brackets
//---------------------------------------------------
setBracketsXPosition(measure->x());
_brackets.append(bl);
}
//---------------------------------------------------------
// createBracket
// Create a bracket if it spans more then one visible system
// If measure is NULL adds the bracket in front of the system, else in front of the measure.
// Returns the bracket if it got created, else NULL
//---------------------------------------------------------
Bracket* System::createBracket(Ms::BracketItem* bi, int column, int staffIdx, QList<Ms::Bracket *>& bl, Measure* measure)
{
int nstaves = _staves.size();
int firstStaff = staffIdx;
int lastStaff = staffIdx + bi->bracketSpan() - 1;
if (lastStaff >= nstaves)
lastStaff = nstaves - 1;
for (; firstStaff <= lastStaff; ++firstStaff) {
if (score()->staff(firstStaff)->show())
break;
}
for (; lastStaff >= firstStaff; --lastStaff) {
if (score()->staff(lastStaff)->show())
break;
}
int span = lastStaff - firstStaff + 1;
//
// do not show bracket if it only spans one
// system due to some invisible staves
//
if ((span > 1) || (bi->bracketSpan() == span)) {
//
// this bracket is visible
//
Bracket* b = 0;
int track = staffIdx * VOICES;
for (int k = 0; k < bl.size(); ++k) {
if (bl[k]->track() == track && bl[k]->column() == column && bl[k]->bracketType() == bi->bracketType() && bl[k]->measure() == measure) {
b = bl.takeAt(k);
break;
}
}
if (b == 0) {
b = new Bracket(score());
b->setBracketItem(bi);
b->setGenerated(true);
b->setTrack(track);
b->setMeasure(measure);
}
add(b);
b->setStaffSpan(firstStaff, lastStaff);
return b;
}
return nullptr;
}
int System::getBracketsColumnsCount()
{
int columns = 0;
int nstaves = _staves.size();
for (int idx = 0; idx < nstaves; ++idx) {
for (auto bi : score()->staff(idx)->brackets())
columns = qMax(columns, bi->column() + 1);
}
return columns;
}
void System::setBracketsXPosition(const qreal xPosition)
{
qreal bracketDistance = score()->styleP(Sid::bracketDistance);
for (Bracket* b1 : _brackets) {
qreal xOffset = 0;
for (const Bracket* b2 : _brackets) {
bool b1FirstStaffInB2 = (b1->firstStaff() >= b2->firstStaff() && b1->firstStaff() <= b2->lastStaff());
bool b1LastStaffInB2 = (b1->lastStaff() >= b2->firstStaff() && b1->lastStaff() <= b2->lastStaff());
if (b1->column() > b2->column() &&
(b1FirstStaffInB2 || b1LastStaffInB2))
xOffset += b2->width() + bracketDistance;
}
b1->rxpos() = xPosition - xOffset - b1->width();
}
}
//---------------------------------------------------------
// nextVisibleStaff
//---------------------------------------------------------
int System::nextVisibleStaff(int staffIdx) const
{
int i;
for (i = staffIdx + 1; i < _staves.size(); ++i) {
Staff* s = score()->staff(i);
SysStaff* ss = _staves[i];
if (s->show() && ss->show())
break;
}
return i;
}
//---------------------------------------------------------
// firstVisibleStaff
//---------------------------------------------------------
int System::firstVisibleStaff() const
{
return nextVisibleStaff(-1);
}
//---------------------------------------------------------
// layout2
// called after measure layout
// adjusts staff distance
//---------------------------------------------------------
void System::layout2()
{
Box* vb = vbox();
if (vb) {
vb->layout();
setbbox(vb->bbox());
return;
}
setPos(0.0, 0.0);
QList<std::pair<int,SysStaff*>> visibleStaves;
for (int i = 0; i < _staves.size(); ++i) {
Staff* s = score()->staff(i);
SysStaff* ss = _staves[i];
if (s->show() && ss->show())
visibleStaves.append(std::pair<int,SysStaff*>(i, ss));
else
ss->setbbox(QRectF()); // already done in layout() ?
}
qreal _spatium = spatium();
qreal y = 0.0;
qreal minVerticalDistance = score()->styleP(Sid::minVerticalDistance);
qreal staffDistance = score()->styleP(Sid::staffDistance);
qreal akkoladeDistance = score()->styleP(Sid::akkoladeDistance);
if (visibleStaves.empty()) {
qDebug("====no visible staves, staves %d, score staves %d", _staves.size(), score()->nstaves());
return;
}
for (auto i = visibleStaves.begin();; ++i) {
SysStaff* ss = i->second;
int si1 = i->first;
Staff* staff = score()->staff(si1);
auto ni = i + 1;
qreal h = staff->height();
if (ni == visibleStaves.end()) {
// ss->setYOff(staff->lines(0) == 1 ? _spatium * staff->mag(0) : 0.0);
ss->setYOff(0.0);
ss->bbox().setRect(_leftMargin, y, width() - _leftMargin, h);
break;
}
int si2 = ni->first;
Staff* staff2 = score()->staff(si2);
qreal dist = h;
#if 1
if (staff->part() == staff2->part()) {
Measure* m = firstMeasure();
qreal mag = m ? staff->mag(m->tick()) : 1.0;
dist += akkoladeDistance * mag;
}
else {
dist += staffDistance;
}
#else
// TODO: provide style setting or brace property to allow braces to also define a grand staff
switch (staff2->innerBracket()) {
case BracketType::BRACE:
dist += akkoladeDistance;
break;
case BracketType::NORMAL:
case BracketType::SQUARE:
case BracketType::LINE:
case BracketType::NO_BRACKET:
dist += staffDistance;
break;
}
#endif
dist += staff2->userDist();
#if 0
for (MeasureBase* mb : ml) {
if (!mb->isMeasure())
continue;
Measure* m = toMeasure(mb);
Shape& s1 = m->staffShape(si1);
Shape& s2 = m->staffShape(si2);
qreal d = score()->lineMode() ? 0.0 : s1.minVerticalDistance(s2);
dist = qMax(dist, d + minVerticalDistance);
Spacer* sp = m->vspacerDown(si1);
if (sp) {
if (sp->spacerType() == SpacerType::FIXED) {
dist = staff->height() + sp->gap();
break;
}
else
dist = qMax(dist, staff->height() + sp->gap());
}
sp = m->vspacerUp(si2);
if (sp)
dist = qMax(dist, sp->gap());
}
#else
bool fixedSpace = false;
for (MeasureBase* mb : ml) {
if (!mb->isMeasure())
continue;
Measure* m = toMeasure(mb);
Spacer* sp = m->vspacerDown(si1);
if (sp) {
if (sp->spacerType() == SpacerType::FIXED) {
dist = staff->height() + sp->gap();
fixedSpace = true;
break;
}
else
dist = qMax(dist, staff->height() + sp->gap());
}
sp = m->vspacerUp(si2);
if (sp)
dist = qMax(dist, sp->gap() + h);
}
if (!fixedSpace) {
qreal d = score()->lineMode() ? 0.0 : ss->skyline().minDistance(System::staff(si2)->skyline());
dist = qMax(dist, d + minVerticalDistance);
}
#endif
// ss->setYOff(staff->lines(0) == 1 ? _spatium * staff->mag(0) : 0.0);
ss->setYOff(0.0);
ss->bbox().setRect(_leftMargin, y, width() - _leftMargin, h);
y += dist;
}
qreal systemHeight = staff(visibleStaves.back().first)->bbox().bottom();
setHeight(systemHeight);
for (MeasureBase* m : ml) {
if (m->isMeasure()) {
// note that the factor 2 * _spatium must be corrected for when exporting
// system distance in MusicXML (issue #24733)
m->bbox().setRect(0.0, -_spatium, m->width(), systemHeight + 2.0 * _spatium);
}
else if (m->isHBox()) {
m->bbox().setRect(0.0, 0.0, m->width(), systemHeight);
toHBox(m)->layout2();
}
else if (m->isTBox()) {
// m->bbox().setRect(0.0, 0.0, m->width(), systemHeight);
toTBox(m)->layout();
}
else
qDebug("unhandled measure type %s", m->name());
}
//---------------------------------------------------
// layout brackets vertical position
//---------------------------------------------------
for (Bracket* b : _brackets) {
int staffIdx1 = b->firstStaff();
int staffIdx2 = b->lastStaff();
qreal sy = 0; // assume bracket not visible
qreal ey = 0;
// if start staff not visible, try next staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx1]->show())
++staffIdx1;
// if end staff not visible, try prev staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx2]->show())
--staffIdx2;
// the bracket will be shown IF:
// it spans at least 2 visible staves (staffIdx1 < staffIdx2) OR
// it spans just one visible staff (staffIdx1 == staffIdx2) but it is required to do so
// (the second case happens at least when the bracket is initially dropped)
bool notHidden = (staffIdx1 < staffIdx2) || (b->span() == 1 && staffIdx1 == staffIdx2);
if (notHidden) { // set vert. pos. and height to visible spanned staves
sy = _staves[staffIdx1]->bbox().top();
ey = _staves[staffIdx2]->bbox().bottom();
}
b->rypos() = sy;
b->setHeight(ey - sy);
b->layout();
}
//---------------------------------------------------
// layout instrument names
//---------------------------------------------------
int staffIdx = 0;
for (Part* p : score()->parts()) {
SysStaff* s = staff(staffIdx);
SysStaff* s2;
int nstaves = p->nstaves();
if (s->show()) {
for (InstrumentName* t : s->instrumentNames) {
//
// override Text->layout()
//
qreal y1, y2;
switch (t->layoutPos()) {
default:
case 0: // center at part
y1 = s->bbox().top();
s2 = staff(staffIdx);
for (int i = staffIdx + nstaves - 1; i > 0; --i) {
SysStaff* s3 = staff(i);
if (s3->show()) {
s2 = s3;
break;
}
}
y2 = s2->bbox().bottom();
break;
case 1: // center at first staff
y1 = s->bbox().top();
y2 = s->bbox().bottom();
break;
// TODO:
// sort out invisible staves
case 2: // center between first and second staff
y1 = s->bbox().top();
y2 = staff(staffIdx + 1)->bbox().bottom();
break;
case 3: // center at second staff
y1 = staff(staffIdx + 1)->bbox().top();
y2 = staff(staffIdx + 1)->bbox().bottom();
break;
case 4: // center between first and second staff
y1 = staff(staffIdx + 1)->bbox().top();
y2 = staff(staffIdx + 2)->bbox().bottom();
break;
case 5: // center at third staff
y1 = staff(staffIdx + 2)->bbox().top();
y2 = staff(staffIdx + 2)->bbox().bottom();
break;
}
t->rypos() = y1 + (y2 - y1) * .5 + t->offset().y();
}
}
staffIdx += nstaves;
}
//---------------------------------------------------
// layout cross-staff slurs and ties
//---------------------------------------------------
Fraction stick = measures().front()->tick();
Fraction etick = measures().back()->endTick();
auto spanners = score()->spannerMap().findOverlapping(stick.ticks(), etick.ticks());
std::vector<Spanner*> spanner;
for (auto interval : spanners) {
Spanner* sp = interval.value;
if (sp->tick() < etick && sp->tick2() >= stick) {
if (sp->isSlur()) {
ChordRest* scr = sp->startCR();
ChordRest* ecr = sp->endCR();
int idx = sp->vStaffIdx();
if (scr && ecr && (scr->vStaffIdx() != idx || ecr->vStaffIdx() != idx))
sp->layoutSystem(this);
}
}
}
}
//---------------------------------------------------------
// setInstrumentNames
//---------------------------------------------------------
void System::setInstrumentNames(bool longName, Fraction tick)
{
//
// remark: add/remove instrument names is not undo/redoable
// as add/remove of systems is not undoable
//
if (vbox()) // ignore vbox
return;
if (!score()->showInstrumentNames()
|| (score()->styleB(Sid::hideInstrumentNameIfOneInstrument) && score()->parts().size() == 1)) {
for (SysStaff* staff : _staves) {
foreach (InstrumentName* t, staff->instrumentNames)
score()->removeElement(t);
}
return;
}
int staffIdx = 0;
for (SysStaff* staff : _staves) {
Staff* s = score()->staff(staffIdx);
if (!s->isTop() || !s->show()) {
for (InstrumentName* t : staff->instrumentNames)
score()->removeElement(t);
++staffIdx;
continue;
}
Part* part = s->part();
const QList<StaffName>& names = longName? part->longNames(tick) : part->shortNames(tick);
int idx = 0;
for (const StaffName& sn : names) {
InstrumentName* iname = staff->instrumentNames.value(idx);
if (iname == 0) {
iname = new InstrumentName(score());
// iname->setGenerated(true);
iname->setParent(this);
iname->setTrack(staffIdx * VOICES);
iname->setInstrumentNameType(longName ? InstrumentNameType::LONG : InstrumentNameType::SHORT);
iname->setLayoutPos(sn.pos());
score()->addElement(iname);
}
iname->setXmlText(sn.name());
++idx;
}
for (; idx < staff->instrumentNames.size(); ++idx)
score()->removeElement(staff->instrumentNames[idx]);
++staffIdx;
}
}
//---------------------------------------------------------
// y2staff
//---------------------------------------------------------
/**
Return staff number for canvas relative y position \a y
or -1 if not found.
To allow drag and drop above and below the staff, the actual y range
considered "inside" the staff is increased by "margin".
*/
int System::y2staff(qreal y) const
{
y -= pos().y();
int idx = 0;
qreal margin = spatium() * 2;
foreach (SysStaff* s, _staves) {
qreal y1 = s->bbox().top() - margin;
qreal y2 = s->bbox().bottom() + margin;
if (y >= y1 && y < y2)
return idx;
++idx;
}
return -1;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void System::add(Element* el)
{
if (!el)
return;
// qDebug("%p System::add: %p %s", this, el, el->name());
el->setParent(this);
switch(el->type()) {
case ElementType::INSTRUMENT_NAME:
// qDebug(" staffIdx %d, staves %d", el->staffIdx(), _staves.size());
_staves[el->staffIdx()]->instrumentNames.append(toInstrumentName(el));
break;
case ElementType::BEAM:
score()->addElement(el);
break;
case ElementType::BRACKET: {
Bracket* b = toBracket(el);
#if 0
int staffIdx = b->staffIdx();
int column = b->column();
if (column == -1) {
column = 0;
for (const Bracket* bb : _brackets) {
if (staffIdx >= bb->firstStaff() && staffIdx <= bb->lastStaff())
++column;
}
// b->setLevel(column);
// b->setSpan(1);
}
// b->staff()->setBracket(column, b->bracketType());
// b->staff()->setBracketSpan(column, b->span());
#endif
_brackets.append(b);
}
break;
case ElementType::MEASURE:
case ElementType::HBOX:
case ElementType::VBOX:
case ElementType::TBOX:
case ElementType::FBOX:
score()->addElement(el);
break;
case ElementType::TEXTLINE_SEGMENT:
case ElementType::HAIRPIN_SEGMENT:
case ElementType::OTTAVA_SEGMENT:
case ElementType::TRILL_SEGMENT:
case ElementType::VIBRATO_SEGMENT:
case ElementType::VOLTA_SEGMENT:
case ElementType::SLUR_SEGMENT:
case ElementType::TIE_SEGMENT:
case ElementType::PEDAL_SEGMENT:
case ElementType::LYRICSLINE_SEGMENT:
case ElementType::GLISSANDO_SEGMENT:
case ElementType::LET_RING_SEGMENT:
case ElementType::PALM_MUTE_SEGMENT:
{
SpannerSegment* ss = toSpannerSegment(el);
#ifndef NDEBUG
if (_spannerSegments.contains(ss))
qDebug("System::add() %s %p already there", ss->name(), ss);
else
#endif
_spannerSegments.append(ss);
}
break;
case ElementType::SYSTEM_DIVIDER:
{
SystemDivider* sd = toSystemDivider(el);
if (sd->dividerType() == SystemDivider::Type::LEFT)
_systemDividerLeft = sd;
else
_systemDividerRight = sd;
}
break;
default:
qDebug("System::add(%s) not implemented", el->name());
break;
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void System::remove(Element* el)
{
switch (el->type()) {
case ElementType::INSTRUMENT_NAME:
_staves[el->staffIdx()]->instrumentNames.removeOne(toInstrumentName(el));
break;
case ElementType::BEAM:
score()->removeElement(el);
break;
case ElementType::BRACKET:
{
Bracket* b = toBracket(el);
if (!_brackets.removeOne(b))
qDebug("System::remove: bracket not found");
}
break;
case ElementType::MEASURE:
case ElementType::HBOX:
case ElementType::VBOX:
case ElementType::TBOX:
case ElementType::FBOX:
score()->removeElement(el);
break;
case ElementType::TEXTLINE_SEGMENT:
case ElementType::HAIRPIN_SEGMENT:
case ElementType::OTTAVA_SEGMENT:
case ElementType::TRILL_SEGMENT:
case ElementType::VIBRATO_SEGMENT:
case ElementType::VOLTA_SEGMENT:
case ElementType::SLUR_SEGMENT:
case ElementType::TIE_SEGMENT:
case ElementType::PEDAL_SEGMENT:
case ElementType::LYRICSLINE_SEGMENT:
case ElementType::GLISSANDO_SEGMENT:
if (!_spannerSegments.removeOne(toSpannerSegment(el))) {
qDebug("System::remove: %p(%s) not found, score %p", el, el->name(), score());
Q_ASSERT(score() == el->score());
}
break;
case ElementType::SYSTEM_DIVIDER:
if (el == _systemDividerLeft)
_systemDividerLeft = 0;
else {
Q_ASSERT(_systemDividerRight == el);
_systemDividerRight = 0;
}
break;
default:
qDebug("System::remove(%s) not implemented", el->name());
break;
}
}
//---------------------------------------------------------
// change
//---------------------------------------------------------
void System::change(Element* o, Element* n)
{
remove(o);
add(n);
}
//---------------------------------------------------------
// snap
//---------------------------------------------------------
Fraction System::snap(const Fraction& tick, const QPointF p) const
{
for (const MeasureBase* m : ml) {
if (p.x() < m->x() + m->width())
return toMeasure(m)->snap(tick, p - m->pos()); //TODO: MeasureBase
}
return toMeasure(ml.back())->snap(tick, p-pos()); //TODO: MeasureBase
}
//---------------------------------------------------------
// snap
//---------------------------------------------------------
Fraction System::snapNote(const Fraction& tick, const QPointF p, int staff) const