/
chord.cpp
3304 lines (3023 loc) · 130 KB
/
chord.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-2013 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 Chord
*/
#include "chord.h"
#include "note.h"
#include "xml.h"
#include "style.h"
#include "segment.h"
#include "text.h"
#include "measure.h"
#include "system.h"
#include "tuplet.h"
#include "hook.h"
#include "tie.h"
#include "arpeggio.h"
#include "score.h"
#include "tremolo.h"
#include "glissando.h"
#include "staff.h"
#include "part.h"
#include "utils.h"
#include "articulation.h"
#include "undo.h"
#include "chordline.h"
#include "lyrics.h"
#include "navigate.h"
#include "stafftype.h"
#include "stem.h"
#include "mscore.h"
#include "accidental.h"
#include "noteevent.h"
#include "pitchspelling.h"
#include "stemslash.h"
#include "ledgerline.h"
#include "drumset.h"
#include "key.h"
#include "sym.h"
#include "stringdata.h"
#include "beam.h"
namespace Ms {
//---------------------------------------------------------
// upNote / downNote
//---------------------------------------------------------
Note* Chord::upNote() const
{
Note* result = _notes.back();
if (!staff())
return result;
if (staff()->isDrumStaff()) {
for (Note*n : _notes) {
if (n->line() < result->line()) {
result = n;
}
}
}
else if (staff()->isTabStaff()) {
StaffType* tab = staff()->staffType();
int line = tab->lines() - 1; // start at bottom line
int noteLine;
// scan each note: if TAB strings are not in sequential order,
// visual order of notes might not correspond to pitch order
for (Note*n : _notes) {
noteLine = tab->physStringToVisual(n->string());
if (noteLine < line) {
line = noteLine;
result = n;
}
}
}
return result;
}
Note* Chord::downNote() const
{
Note* result = _notes.front();
if (!staff())
return result;
if (staff()->isDrumStaff()) {
foreach(Note*n, _notes) {
if (n->line() > result->line()) {
result = n;
}
}
}
else if (staff()->isTabStaff()) {
StaffType* tab = staff()->staffType();
int line = 0; // start at top line
int noteLine;
// scan each note: if TAB strings are not in sequential order,
// visual order of notes might not correspond to pitch order
for (Note*n : _notes) {
noteLine = tab->physStringToVisual(n->string());
if (noteLine > line) {
line = noteLine;
result = n;
}
}
}
return result;
}
//---------------------------------------------------------
// upLine / downLine
//---------------------------------------------------------
int Chord::upLine() const
{
return (staff() && staff()->isTabStaff()) ? upString()*2 : upNote()->line();
}
int Chord::downLine() const
{
return (staff() && staff()->isTabStaff()) ? downString()*2 : downNote()->line();
}
//---------------------------------------------------------
// upString / downString
//
// return the topmost / bottommost string used by chord
// Top and bottom refer to the DRAWN position, not the position in the instrument
// (i.e., upside-down TAB are taken into account)
//
// If no staff, always return 0
// If staf is not a TAB, always returns TOP and BOTTOM staff lines
//---------------------------------------------------------
int Chord::upString() const
{
// if no staff or staff not a TAB, return 0 (=topmost line)
if(!staff() || !staff()->isTabStaff())
return 0;
StaffType* tab = staff()->staffType();
int line = tab->lines() - 1; // start at bottom line
int noteLine;
// scan each note: if TAB strings are not in sequential order,
// visual order of notes might not correspond to pitch order
int n = _notes.size();
for (int i = 0; i < n; ++i) {
noteLine = tab->physStringToVisual(_notes.at(i)->string());
if (noteLine < line)
line = noteLine;
}
return line;
}
int Chord::downString() const
{
if (!staff()) // if no staff, return 0
return 0;
if (!staff()->isTabStaff()) // if staff not a TAB, return bottom line
return staff()->lines()-1;
StaffType* tab = staff()->staffType();
int line = 0; // start at top line
int noteLine;
int n = _notes.size();
for (int i = 0; i < n; ++i) {
noteLine = tab->physStringToVisual(_notes.at(i)->string());
if (noteLine > line)
line = noteLine;
}
return line;
}
//---------------------------------------------------------
// Chord
//---------------------------------------------------------
Chord::Chord(Score* s)
: ChordRest(s)
{
_ledgerLines = 0;
_stem = 0;
_hook = 0;
_stemDirection = MScore::Direction::AUTO;
_arpeggio = 0;
_tremolo = 0;
_endsGlissando = false;
_noteType = NoteType::NORMAL;
_stemSlash = 0;
_noStem = false;
_playEventType = PlayEventType::Auto;
_crossMeasure = CrossMeasure::UNKNOWN;
_graceIndex = 0;
setFlags(ElementFlag::MOVABLE | ElementFlag::ON_STAFF);
}
Chord::Chord(const Chord& c, bool link)
: ChordRest(c, link)
{
if (link)
score()->undo(new Link(const_cast<Chord*>(&c), this));
_ledgerLines = 0;
for (Note* onote : c._notes) {
Note* nnote = new Note(*onote, link);
add(nnote);
}
for (Chord* gn : c.graceNotes()) {
Chord* nc = new Chord(*gn, link);
add(nc);
}
_stem = 0;
_hook = 0;
_endsGlissando = false;
_arpeggio = 0;
_stemSlash = 0;
_tremolo = 0;
_graceIndex = c._graceIndex;
_noStem = c._noStem;
_playEventType = c._playEventType;
_stemDirection = c._stemDirection;
_noteType = c._noteType;
_crossMeasure = CrossMeasure::UNKNOWN;
if (c._stem)
add(new Stem(*(c._stem)));
if (c._hook)
add(new Hook(*(c._hook)));
if (c._stemSlash)
add(new StemSlash(*(c._stemSlash)));
if (c._arpeggio) {
Arpeggio* a = new Arpeggio(*(c._arpeggio));
add(a);
if (link)
score()->undo(new Link(const_cast<Arpeggio*>(c._arpeggio), a));
}
if (c._tremolo) {
Tremolo* t = new Tremolo(*(c._tremolo));
if (link) {
score()->undo(new Link(const_cast<Tremolo*>(c._tremolo), t));
if (c._tremolo->twoNotes())
t->setChords(0, 0);
}
add(t);
}
for (Element* e : c.el()) {
if (e->type() == Element::Type::CHORDLINE) {
ChordLine* cl = static_cast<ChordLine*>(e);
ChordLine* ncl = new ChordLine(*cl);
add(ncl);
if (link)
score()->undo(new Link(const_cast<ChordLine*>(ncl), cl));
}
}
}
//---------------------------------------------------------
// undoUnlink
//---------------------------------------------------------
void Chord::undoUnlink()
{
ChordRest::undoUnlink();
for (Note* n : _notes)
n->undoUnlink();
for (Chord* gn : graceNotes())
gn->undoUnlink();
/* if (_glissando)
_glissando->undoUnlink(); */
if (_arpeggio)
_arpeggio->undoUnlink();
if (_tremolo && !_tremolo->twoNotes())
_tremolo->undoUnlink();
for (Element* e : el()) {
if (e->type() == Element::Type::CHORDLINE)
e->undoUnlink();
}
}
//---------------------------------------------------------
// ~Chord
//---------------------------------------------------------
Chord::~Chord()
{
delete _arpeggio;
if (_tremolo && _tremolo->chord1() == this) {
if (_tremolo->chord2())
_tremolo->chord2()->setTremolo(0);
delete _tremolo;
}
delete _stemSlash;
delete _stem;
delete _hook;
for (LedgerLine* ll = _ledgerLines; ll;) {
LedgerLine* llNext = ll->next();
delete ll;
ll = llNext;
}
qDeleteAll(_graceNotes);
qDeleteAll(_notes);
}
//---------------------------------------------------------
// stemPosX
// return Chord coordinates
//---------------------------------------------------------
qreal Chord::stemPosX() const
{
if (staff() && staff()->isTabStaff())
return staff()->staffType()->chordStemPosX(this) * spatium();
if (_up) {
qreal nhw = score()->noteHeadWidth();
if (_noteType != NoteType::NORMAL)
nhw *= score()->styleD(StyleIdx::graceNoteMag);
nhw *= mag();
return nhw;
}
return 0.0;
}
//---------------------------------------------------------
// stemPos
// return page coordinates
//---------------------------------------------------------
QPointF Chord::stemPos() const
{
qreal _spatium = spatium();
QPointF p(pagePos());
if (staff() && staff()->isTabStaff())
return staff()->staffType()->chordStemPos(this) * _spatium + p;
if (_up) {
qreal nhw = score()->noteHeadWidth();
if (_noteType != NoteType::NORMAL)
nhw *= score()->styleD(StyleIdx::graceNoteMag);
nhw *= mag();
p.rx() += nhw;
p.ry() += downNote()->pos().y();
}
else
p.ry() += upNote()->pos().y();
return p;
}
//---------------------------------------------------------
// stemPosBeam
// return stem position of note on beam side
// return canvas coordinates
//---------------------------------------------------------
QPointF Chord::stemPosBeam() const
{
qreal _spatium = spatium();
QPointF p(pagePos());
if (staff() && staff()->isTabStaff())
return staff()->staffType()->chordStemPosBeam(this) * _spatium + p;
if (_up) {
qreal nhw = score()->noteHeadWidth();
if (_noteType != NoteType::NORMAL)
nhw *= score()->styleD(StyleIdx::graceNoteMag);
p.rx() += nhw;
p.ry() += upNote()->pos().y();
}
else
p.ry() += downNote()->pos().y();
return p;
}
//---------------------------------------------------------
// setSelected
//---------------------------------------------------------
void Chord::setSelected(bool)
{
/* Element::setSelected(f);
int n = _notes.size();
for (int i = 0; i < n; ++i)
_notes.at(i)->setSelected(f);
*/
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void Chord::add(Element* e)
{
e->setParent(this);
e->setTrack(track());
switch(e->type()) {
case Element::Type::NOTE:
{
Note* note = static_cast<Note*>(e);
bool found = false;
// _notes should be sorted by line position,
// but it's often not yet possible since line is unknown
// use pitch instead, and line as a second sort critera.
for (int idx = 0; idx < _notes.size(); ++idx) {
if (note->pitch() <= _notes[idx]->pitch()) {
if (note->pitch() == _notes[idx]->pitch() && note->line() > _notes[idx]->line())
_notes.insert(idx+1, note);
else
_notes.insert(idx, note);
found = true;
break;
}
}
if (!found)
_notes.append(note);
if (note->tieFor()) {
if (note->tieFor()->endNote())
note->tieFor()->endNote()->setTieBack(note->tieFor());
}
if (voice() && measure() && note->visible())
measure()->mstaff(staffIdx())->hasVoices = true;
}
break;
case Element::Type::ARPEGGIO:
_arpeggio = static_cast<Arpeggio*>(e);
break;
case Element::Type::TREMOLO:
{
Tremolo* tr = static_cast<Tremolo*>(e);
if (tr->twoNotes()) {
if (!(_tremolo && _tremolo->twoNotes())) {
TDuration d = durationType();
int dots = d.dots();
d = d.shift(-1);
d.setDots(dots);
if (tr->chord1())
tr->chord1()->setDurationType(d);
if (tr->chord2())
tr->chord2()->setDurationType(d);
}
if (tr->chord2())
tr->chord2()->setTremolo(tr);
}
_tremolo = tr;
}
break;
case Element::Type::GLISSANDO:
_endsGlissando = true;
break;
case Element::Type::STEM:
Q_ASSERT(!_stem);
_stem = static_cast<Stem*>(e);
break;
case Element::Type::HOOK:
_hook = static_cast<Hook*>(e);
break;
case Element::Type::CHORDLINE:
_el.push_back(e);
break;
case Element::Type::STEM_SLASH:
Q_ASSERT(!_stemSlash);
_stemSlash = static_cast<StemSlash*>(e);
break;
case Element::Type::CHORD:
{
Chord* gc = static_cast<Chord*>(e);
Q_ASSERT(gc->noteType() != NoteType::NORMAL);
int idx = gc->graceIndex();
gc->setFlags(ElementFlag::MOVABLE);
_graceNotes.insert(_graceNotes.begin() + idx, gc);
}
break;
case Element::Type::LEDGER_LINE:
qFatal("Chord::add ledgerline");
break;
default:
ChordRest::add(e);
break;
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void Chord::remove(Element* e)
{
if (e == nullptr)
return;
switch(e->type()) {
case Element::Type::NOTE:
{
Note* note = static_cast<Note*>(e);
if (_notes.removeOne(note)) {
if (note->tieFor()) {
if (note->tieFor()->endNote())
note->tieFor()->endNote()->setTieBack(0);
}
for (Spanner* s : note->spannerBack()) {
note->removeSpannerBack(s);
}
for (Spanner* s : note->spannerFor()) {
note->removeSpannerFor(s);
}
}
else
qDebug("Chord::remove() note %p not found!", e);
if (voice() && measure() && note->visible())
measure()->checkMultiVoices(staffIdx());
}
break;
case Element::Type::ARPEGGIO:
_arpeggio = 0;
break;
case Element::Type::TREMOLO:
{
Tremolo* tremolo = static_cast<Tremolo*>(e);
if (tremolo->twoNotes()) {
TDuration d = durationType();
int dots = d.dots();
d = d.shift(1);
d.setDots(dots);
Fraction f = duration();
if (f.numerator() > 0)
d = TDuration(f);
if (tremolo->chord1())
tremolo->chord1()->setDurationType(d);
if (tremolo->chord2()) {
tremolo->chord2()->setDurationType(d);
tremolo->chord2()->setTremolo(0);
}
}
_tremolo = 0;
}
break;
case Element::Type::GLISSANDO:
_endsGlissando = false;
break;
case Element::Type::STEM:
_stem = 0;
break;
case Element::Type::HOOK:
_hook = 0;
break;
case Element::Type::STEM_SLASH:
Q_ASSERT(_stemSlash);
if (_stemSlash->selected() && score())
score()->deselect(_stemSlash);
_stemSlash = 0;
break;
case Element::Type::CHORDLINE:
_el.remove(e);
break;
case Element::Type::CHORD:
{
auto i = std::find(_graceNotes.begin(), _graceNotes.end(), static_cast<Chord*>(e));
Chord* grace = *i;
grace->setGraceIndex(i - _graceNotes.begin());
_graceNotes.erase(i);
}
break;
default:
ChordRest::remove(e);
break;
}
}
//---------------------------------------------------------
// maxHeadWidth
//---------------------------------------------------------
qreal Chord::maxHeadWidth() const
{
// determine max head width in chord
qreal hw = 0;
for (int i = 0; i < _notes.size(); i++) {
qreal t = _notes.at(i)->headWidth();
if (t > hw)
hw = t;
}
return hw;
}
//---------------------------------------------------------
// addLedgerLine
/// Add a ledger line to a chord.
/// \arg track track the ledger line belongs to
/// \arg line vertical position of line
/// \arg visible whether the line is visible or not
/// \arg x start x-position
/// \arg len line length
//---------------------------------------------------------
#if 0
void Chord::addLedgerLine(int track, int line, bool visible, qreal x, Spatium len)
{
qreal _spatium = spatium();
LedgerLine* h = new LedgerLine(score());
h->setParent(this);
h->setTrack(track);
h->setVisible(visible);
//
// Experimental:
// shorten ledger line to avoid collisions with accidentals
//
int n = _notes.size();
for (int i = 0; i < n; ++i) {
const Note* n = _notes.at(i);
if (n->line() >= (line-1) && n->line() <= (line+1) && n->accidental()) {
x += _spatium * .25;
len -= Spatium(.25);
break;
}
}
h->setLen(len);
h->setPos(x, line * _spatium * .5);
h->setNext(_ledgerLines);
_ledgerLines = h;
}
#endif
//---------------------------------------------------------
// createLedgerLines
/// Creates the ledger lines for a chord
/// \arg track track the ledger line belongs to
/// \arg lines a vector of LedgerLineData describing thelines to add
/// \arg visible whether the line is visible or not
//---------------------------------------------------------
void Chord::createLedgerLines(int track, vector<LedgerLineData>& vecLines, bool visible)
{
qreal _spatium = spatium();
for (auto lld : vecLines) {
LedgerLine* h = new LedgerLine(score());
h->setParent(this);
h->setTrack(track);
h->setVisible(lld.visible && visible);
h->setLen(Spatium( (lld.maxX - lld.minX) / _spatium) );
h->setPos(lld.minX, lld.line * _spatium * .5);
h->setNext(_ledgerLines);
_ledgerLines = h;
}
}
//---------------------------------------------------------
// addLedgerLines
//---------------------------------------------------------
void Chord::addLedgerLines()
{
LedgerLineData lld;
// initialize for palette
int track = 0; // the track lines belong to
// the line pos corresponding to the bottom line of the staff
int lineBelow = 8; // assuming 5-lined "staff"
qreal _mag = 1;
bool staffVisible = true;
if (segment()) { //not palette
int idx = staffIdx() + staffMove();
track = staff2track(idx);
Staff* st = score()->staff(idx);
lineBelow = (st->lines() - 1) * 2;
_mag = staff()->mag();
staffVisible = !staff()->invisible();
}
// the extra length of a ledger line with respect to notehead (half of it on each side)
qreal extraLen = score()->styleP(StyleIdx::ledgerLineLength) * _mag * 0.5;
qreal hw = _notes[0]->headWidth();
qreal minX, maxX; // note extrema in raster units
// qreal minXr, maxXr; // ledger line extrema in raster units
int minLine, maxLine;
bool visible = false;
qreal x;
// scan chord notes, collecting visibility and x and y extrema
// NOTE: notes are sorted from bottom to top (line no. decreasing)
// notes are scanned twice from outside (bottom or top) toward the staff
// each pass stops at the first note without ledger lines
int n = _notes.size();
for (int j = 0; j < 2; j++) { // notes are scanned twice...
int from, delta;
vector<LedgerLineData> vecLines;
minX = maxX = 0;
minLine = 0;
maxLine = lineBelow;
if (j == 0) { // ...once from lowest up...
from = 0;
delta = +1;
}
else {
from = n-1; // ...once from highest down
delta = -1;
}
for (int i = from; i < n && i >=0 ; i += delta) {
const Note* note = _notes.at(i);
int l = note->line();
if ( (!j && l < lineBelow) || // if 1st pass and note not below staff
(j && l >= 0) ) // or 2nd pass and note not above staff
break; // stop this pass
// round line number to even number toward 0
if (l < 0) l = (l+1) & ~ 1;
else l = l & ~ 1;
if (note->visible()) // if one note is visible,
visible = true; // all lines between it and the staff are visible
//
// Experimental:
// shorten ledger line to avoid collisions with accidentals
//
// bool accid = (note->accidental() && note->line() >= (l-1) && note->line() <= (l+1) );
//
// TODO : do something with this accid flag in the following code!
//
// check if note horiz. pos. is outside current range
// if more length on the right, increase range
x = note->pos().x();
if (x-extraLen < minX) {
minX = x - extraLen;
// minXr = minX - extraLen;
// increase width of all lines between this one and the staff
for (auto& d : vecLines)
if (!d.accidental && ((l < 0 && d.line >= l) || (l > 0 && d.line <= l)) )
d.minX = minX ;
}
// same for left side
if (x+hw+extraLen > maxX) {
maxX = x + hw + extraLen;
// maxXr = maxX + extraLen;
for (auto& d : vecLines)
if ( (l < 0 && d.line >= l) || (l > 0 && d.line <= l) )
d.maxX = maxX;
}
// check if note vert. pos. is outside current range
// and, in case, add data for new line(s)
if (l < minLine) {
for (int i = l; i < minLine; i += 2) {
lld.line = i;
lld.minX = minX;
lld.maxX = maxX;
lld.visible = visible;
lld.accidental = false;
vecLines.push_back(lld);
}
minLine = l;
}
if (l > maxLine) {
for (int i = maxLine+2; i <= l; i += 2) {
lld.line = i;
lld.minX = minX;
lld.maxX = maxX;
lld.visible = visible;
lld.accidental = false;
vecLines.push_back(lld);
}
maxLine = l;
}
}
if (minLine < 0 || maxLine > lineBelow)
createLedgerLines(track, vecLines, staffVisible);
}
}
//-----------------------------------------------------------------------------
// computeUp
// rules:
// single note:
// All notes beneath the middle line: upward stems
// All notes on or above the middle line: downward stems
// two notes:
// If the interval above the middle line is greater than the interval
// below the middle line: downward stems
// If the interval below the middle line is greater than the interval
// above the middle line: upward stems
// If the two notes are the same distance from the middle line:
// stem can go in either direction. but most engravers prefer
// downward stems
// > two notes:
// If the interval of the highest note above the middle line is greater
// than the interval of the lowest note below the middle line:
// downward stems
// If the interval of the lowest note below the middle line is greater
// than the interval of the highest note above the middle line:
// upward stem
// If the highest and the lowest notes are the same distance from
// the middle line:, use these rules to determine stem direction:
// - If the majority of the notes are above the middle:
// downward stems
// - If the majority of the notes are below the middle:
// upward stems
// TABlatures:
// stems beside staves:
// All stems are up / down according to TAB::stemsDown() setting
// stems through staves:
// Same rules as per pitched staves
//-----------------------------------------------------------------------------
void Chord::computeUp()
{
StaffType* tab = 0;
// TAB STAVES
if (staff() && staff()->isTabStaff()) {
tab = staff()->staffType();
// if no stems or stem beside staves
if (tab->slashStyle() || !tab->stemThrough()) {
// if measure has voices, set stem direction according to voice
if (measure()->mstaff(staffIdx())->hasVoices)
_up = !(track() % 2);
else // if only voice 1,
// uncondtionally set to down if not stems or according to TAB stem direction otherwise
// (even with no stems, stem direction controls position of slurs and ties)
_up = tab->slashStyle() ? false : !tab->stemsDown();
return;
}
// if TAB has stems through staves, chain into standard processing
}
// PITCHED STAVES (or TAB with stems through staves)
if (_stemDirection != MScore::Direction::AUTO) {
_up = _stemDirection == MScore::Direction::UP;
}
else if (!parent()) {
// hack for palette and drumset editor
if (upNote()->line() > 4)
_up = true;
else
_up = false;
}
else if (_noteType != NoteType::NORMAL) {
//
// stem direction for grace notes
//
if (measure()->mstaff(staffIdx())->hasVoices)
_up = !(track() % 2);
else
_up = true;
}
else if (staffMove()) {
_up = staffMove() > 0;
}
else if (measure()->mstaff(staffIdx())->hasVoices) {
_up = !(track() % 2);
}
else {
int dnMaxLine = staff()->staffType()->lines() - 1;
int ud = (tab ? upString()*2 : upNote()->line() ) - dnMaxLine;
// standard case: if only 1 note or cross beaming
if (_notes.size() == 1 || staffMove()) {
if (staffMove() > 0)
_up = true;
else if (staffMove() < 0)
_up = false;
else
_up = ud > 0;
}
// if more than 1 note, compare extrema (topmost and bottommost notes)
else {
int dd = (tab ? downString()*2 : downNote()->line() ) - dnMaxLine;
// if extrema symmetrical, average directions of intermediate notes
if (-ud == dd) {
int up = 0;
int n = _notes.size();
for (int i = 0; i < n; ++i) {
const Note* n = _notes.at(i);
int l = tab ? n->string()*2 : n->line();
if (l <= dnMaxLine)
--up;
else
++up;
}
_up = up > 0;
}
// if extrema not symmetrical, set _up to prevailing
else
_up = dd > -ud;
}
}
}
//---------------------------------------------------------
// selectedNote
//---------------------------------------------------------
Note* Chord::selectedNote() const
{
Note* note = 0;
int n = _notes.size();
for (int i = 0; i < n; ++i) {
Note* n = _notes.at(i);
if (n->selected()) {
if (note)
return 0;
note = n;
}
}
return note;
}
//---------------------------------------------------------
// Chord::write
//---------------------------------------------------------
void Chord::write(Xml& xml) const
{
for (Chord* c : _graceNotes) {
c->writeBeam(xml);
c->write(xml);
}
xml.stag("Chord");
ChordRest::writeProperties(xml);
switch (_noteType) {
case NoteType::INVALID:
case NoteType::NORMAL:
break;
case NoteType::ACCIACCATURA:
xml.tagE("acciaccatura");
break;
case NoteType::APPOGGIATURA:
xml.tagE("appoggiatura");
break;
case NoteType::GRACE4:
xml.tagE("grace4");
break;
case NoteType::GRACE16:
xml.tagE("grace16");
break;
case NoteType::GRACE32:
xml.tagE("grace32");
break;
case NoteType::GRACE8_AFTER:
xml.tagE("grace8after");
break;
case NoteType::GRACE16_AFTER:
xml.tagE("grace16after");
break;
case NoteType::GRACE32_AFTER:
xml.tagE("grace32after");
break;
default:
break;
}
if (_noStem)
xml.tag("noStem", _noStem);
else if (_stem && (_stem->isUserModified() || (_stem->userLen() != 0.0)))
_stem->write(xml);
if (_hook && _hook->isUserModified())
_hook->write(xml);
if (_stemSlash && _stemSlash->isUserModified())
_stemSlash->write(xml);
switch(_stemDirection) {
case MScore::Direction::UP: xml.tag("StemDirection", QVariant("up")); break;
case MScore::Direction::DOWN: xml.tag("StemDirection", QVariant("down")); break;
case MScore::Direction::AUTO: break;
}
for (Note* n : _notes)
n->write(xml);
if (_arpeggio)
_arpeggio->write(xml);
if (_tremolo && tremoloChordType() != TremoloChordType::TremoloSecondNote)
_tremolo->write(xml);
for (Element* e : _el)
e->write(xml);
xml.etag();
}
//---------------------------------------------------------
// Chord::read
//---------------------------------------------------------
void Chord::read(XmlReader& e)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "Note") {
Note* note = new Note(score());
// the note needs to know the properties of the track it belongs to
note->setTrack(track());