-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
fret.cpp
1664 lines (1428 loc) · 56.6 KB
/
fret.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) 2010-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
//=============================================================================
#include "fret.h"
#include "measure.h"
#include "system.h"
#include "score.h"
#include "stringdata.h"
#include "chord.h"
#include "note.h"
#include "segment.h"
#include "mscore.h"
#include "harmony.h"
#include "staff.h"
#include "undo.h"
namespace Ms {
// parent() is Segment or Box
//
//---------------------------------------------------------
// fretStyle
//---------------------------------------------------------
static const ElementStyle fretStyle {
{ Sid::fretNumPos, Pid::FRET_NUM_POS },
{ Sid::fretMag, Pid::MAG },
{ Sid::fretPlacement, Pid::PLACEMENT },
{ Sid::fretStrings, Pid::FRET_STRINGS },
{ Sid::fretFrets, Pid::FRET_FRETS },
{ Sid::fretNut, Pid::FRET_NUT },
{ Sid::fretMinDistance, Pid::MIN_DISTANCE },
{ Sid::fretOrientation, Pid::ORIENTATION },
};
//---------------------------------------------------------
// FretDiagram
//---------------------------------------------------------
FretDiagram::FretDiagram(Score* score)
: Element(score, ElementFlag::MOVABLE | ElementFlag::ON_STAFF)
{
font.setFamily("FreeSans");
font.setPointSize(4.0 * mag());
initElementStyle(&fretStyle);
}
FretDiagram::FretDiagram(const FretDiagram& f)
: Element(f)
{
_strings = f._strings;
_frets = f._frets;
_fretOffset = f._fretOffset;
_maxFrets = f._maxFrets;
font = f.font;
_userMag = f._userMag;
_numPos = f._numPos;
_dots = f._dots;
_markers = f._markers;
_barres = f._barres;
_showNut = f._showNut;
_orientation= f._orientation;
if (f._harmony) {
Harmony* h = new Harmony(*f._harmony);
add(h);
}
}
FretDiagram::~FretDiagram()
{
if (_harmony)
delete _harmony;
}
//---------------------------------------------------------
// linkedClone
//---------------------------------------------------------
Element* FretDiagram::linkedClone()
{
FretDiagram* e = clone();
e->setAutoplace(true);
if (_harmony) {
Element* newHarmony = _harmony->linkedClone();
e->add(newHarmony);
}
score()->undo(new Link(e, this));
return e;
}
//---------------------------------------------------------
// fromString
/// Create diagram from string like "XO-123"
/// Always assume barre on the first visible fret
//---------------------------------------------------------
FretDiagram* FretDiagram::fromString(Score* score, const QString &s)
{
FretDiagram* fd = new FretDiagram(score);
int strings = s.size();
fd->setStrings(strings);
fd->setFrets(4);
fd->setPropertyFlags(Pid::FRET_STRINGS, PropertyFlags::UNSTYLED);
fd->setPropertyFlags(Pid::FRET_FRETS, PropertyFlags::UNSTYLED);
int offset = 0;
int barreString = -1;
std::vector<std::pair<int, int>> dotsToAdd;
for (int i = 0; i < strings; i++) {
QChar c = s.at(i);
if (c == 'X' || c == 'O') {
FretMarkerType mt = (c == 'X' ? FretMarkerType::CROSS : FretMarkerType::CIRCLE);
fd->setMarker(i, mt);
}
else if (c == '-' && barreString == -1) {
barreString = i;
}
else {
int fret = c.digitValue();
if (fret != -1) {
dotsToAdd.push_back(std::make_pair(i, fret));
if (fret - 3 > 0 && offset < fret - 3)
offset = fret - 3;
}
}
}
if (offset > 0)
fd->setFretOffset(offset);
for (std::pair<int, int> d : dotsToAdd) {
fd->setDot(d.first, d.second - offset, true);
}
// This assumes that any barre goes to the end of the fret
if (barreString >= 0)
fd->setBarre(barreString, -1, 1);
return fd;
}
//---------------------------------------------------------
// pagePos
//---------------------------------------------------------
QPointF FretDiagram::pagePos() const
{
if (parent() == 0)
return pos();
if (parent()->isSegment()) {
Measure* m = toSegment(parent())->measure();
System* system = m->system();
qreal yp = y();
if (system)
yp += system->staffYpage(staffIdx());
return QPointF(pageX(), yp);
}
else
return Element::pagePos();
}
//---------------------------------------------------------
// dragAnchorLines
//---------------------------------------------------------
QVector<QLineF> FretDiagram::dragAnchorLines() const
{
return genericDragAnchorLines();
#if 0 // TODOxx
if (parent()->isSegment()) {
Segment* s = toSegment(parent());
Measure* m = s->measure();
System* system = m->system();
qreal yp = system->staff(staffIdx())->y() + system->y();
qreal xp = m->tick2pos(s->tick()) + m->pagePos().x();
QPointF p1(xp, yp);
qreal x = 0.0;
qreal y = 0.0;
qreal tw = width();
qreal th = height();
if (_align & Align::BOTTOM)
y = th;
else if (_align & Align::VCENTER)
y = (th * .5);
else if (_align & Align::BASELINE)
y = baseLine();
if (_align & Align::RIGHT)
x = tw;
else if (_align & Align::HCENTER)
x = (tw * .5);
return QLineF(p1, abbox().topLeft() + QPointF(x, y));
}
return QLineF(parent()->pagePos(), abbox().topLeft());
#endif
}
//---------------------------------------------------------
// setStrings
//---------------------------------------------------------
void FretDiagram::setStrings(int n)
{
int difference = n - _strings;
if (difference == 0 || n <= 0)
return;
// Move all dots, makers, barres to the RIGHT, so we add strings to the left
// This is more useful - few instruments need strings added to the right.
DotMap tempDots;
MarkerMap tempMarkers;
for (int string = 0; string < _strings; ++string) {
if (string + difference < 0)
continue;
for (auto const& d : dot(string)) {
if (d.exists())
tempDots[string + difference].push_back(FretItem::Dot(d));
}
if (marker(string).exists())
tempMarkers[string + difference] = marker(string);
}
_dots = tempDots;
_markers = tempMarkers;
for (int fret = 1; fret <= _frets; ++fret) {
if (barre(fret).exists()) {
if (_barres[fret].startString + difference <= 0) {
removeBarre(fret);
continue;
}
_barres[fret].startString = qMax(0, _barres[fret].startString + difference);
_barres[fret].endString = _barres[fret].endString == -1 ? -1 : _barres[fret].endString + difference;
}
}
_strings = n;
}
//---------------------------------------------------------
// init
//---------------------------------------------------------
void FretDiagram::init(StringData* stringData, Chord* chord)
{
if (!stringData)
setStrings(6);
else
setStrings(stringData->strings());
if (stringData) {
for (int string = 0; string < _strings; ++string)
setMarker(string, FretMarkerType::CROSS);
for (const Note* note : chord->notes()) {
int string;
int fret;
if (stringData->convertPitch(note->pitch(), chord->staff(), chord->segment()->tick(), &string, &fret))
setDot(string, fret);
}
_maxFrets = stringData->frets();
}
else
_maxFrets = 6;
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void FretDiagram::draw(QPainter* painter) const
{
QPointF translation = -QPointF(stringDist * (_strings - 1), 0);
if (_orientation == Orientation::HORIZONTAL) {
painter->save();
painter->rotate(-90);
painter->translate(translation);
}
// Init pen and other values
qreal _spatium = spatium() * _userMag;
QPen pen(curColor());
pen.setCapStyle(Qt::FlatCap);
painter->setBrush(QBrush(QColor(painter->pen().color())));
// x2 is the x val of the rightmost string
qreal x2 = (_strings-1) * stringDist;
// Draw the nut
pen.setWidthF(nutLw);
painter->setPen(pen);
painter->drawLine(QLineF(-stringLw * .5, 0.0, x2 + stringLw * .5, 0.0));
// Draw strings and frets
pen.setWidthF(stringLw);
painter->setPen(pen);
// y2 is the y val of the bottom fretline
qreal y2 = fretDist * (_frets + .5);
for (int i = 0; i < _strings; ++i) {
qreal x = stringDist * i;
painter->drawLine(QLineF(x, _fretOffset ? -_spatium * .2 : 0.0, x, y2));
}
for (int i = 1; i <= _frets; ++i) {
qreal y = fretDist * i;
painter->drawLine(QLineF(0.0, y, x2, y));
}
// dotd is the diameter of a dot
qreal dotd = _spatium * .49 * score()->styleD(Sid::fretDotSize);
// Draw dots, sym pen is used to draw them (and markers)
QPen symPen(pen);
symPen.setCapStyle(Qt::RoundCap);
qreal symPenWidth = stringLw * 1.2;
symPen.setWidthF(symPenWidth);
for (auto const& i : _dots) {
for (auto const& d : i.second) {
if (!d.exists())
continue;
int string = i.first;
int fret = d.fret - 1;
// Calculate coords of the top left corner of the dot
qreal x = stringDist * string - dotd * .5;
qreal y = fretDist * fret + fretDist * .5 - dotd * .5;
// Draw different symbols
painter->setPen(symPen);
switch (d.dtype) {
case FretDotType::CROSS:
// Give the cross a slightly larger width
symPen.setWidthF(symPenWidth * 1.5);
painter->setPen(symPen);
painter->drawLine(QLineF(x, y, x + dotd, y + dotd));
painter->drawLine(QLineF(x + dotd, y, x, y + dotd));
symPen.setWidthF(symPenWidth);
break;
case FretDotType::SQUARE:
painter->setBrush(Qt::NoBrush);
painter->drawRect(QRectF(x, y, dotd, dotd));
break;
case FretDotType::TRIANGLE:
painter->drawLine(QLineF(x, y + dotd, x + .5 * dotd, y));
painter->drawLine(QLineF(x + .5 * dotd, y, x + dotd, y + dotd));
painter->drawLine(QLineF(x + dotd, y + dotd, x, y + dotd));
break;
case FretDotType::NORMAL:
default:
painter->setBrush(symPen.color());
painter->setPen(Qt::NoPen);
painter->drawEllipse(QRectF(x, y, dotd, dotd));
break;
}
}
}
// Draw markers
symPen.setWidthF(symPenWidth * 1.2);
painter->setBrush(Qt::NoBrush);
painter->setPen(symPen);
for (auto const& i : _markers) {
int string = i.first;
FretItem::Marker marker = i.second;
if (!marker.exists())
continue;
qreal x = stringDist * string - markerSize * .5;
qreal y = -fretDist - markerSize * .5;
if (marker.mtype == FretMarkerType::CIRCLE) {
painter->drawEllipse(QRectF(x, y, markerSize, markerSize));
}
else if (marker.mtype == FretMarkerType::CROSS) {
painter->drawLine(QPointF(x, y), QPointF(x + markerSize, y + markerSize));
painter->drawLine(QPointF(x, y + markerSize), QPointF(x + markerSize, y));
}
}
// Draw barres
for (auto const& i : _barres) {
int fret = i.first;
int startString = i.second.startString;
int endString = i.second.endString;
qreal x1 = stringDist * startString;
qreal newX2 = endString == -1 ? x2 : stringDist * endString;
qreal y = fretDist * (fret - 1) + fretDist * .5;
pen.setWidthF(dotd * score()->styleD(Sid::barreLineWidth));
pen.setCapStyle(Qt::RoundCap);
painter->setPen(pen);
painter->drawLine(QLineF(x1, y, newX2, y));
}
// Draw fret offset number
if (_fretOffset > 0) {
qreal fretNumMag = score()->styleD(Sid::fretNumMag);
QFont scaledFont(font);
scaledFont.setPointSizeF(font.pointSize() * _userMag * (spatium() / SPATIUM20) * MScore::pixelRatio * fretNumMag);
painter->setFont(scaledFont);
QString text = QString("%1").arg(_fretOffset+1);
if (_orientation == Orientation::VERTICAL) {
if (_numPos == 0) {
painter->drawText(QRectF(-stringDist * .4, .0, .0, fretDist),
Qt::AlignVCenter|Qt::AlignRight|Qt::TextDontClip, text);
}
else {
painter->drawText(QRectF(x2 + (stringDist * .4), .0, .0, fretDist),
Qt::AlignVCenter|Qt::AlignLeft|Qt::TextDontClip,
QString("%1").arg(_fretOffset+1));
}
}
else if (_orientation == Orientation::HORIZONTAL) {
painter->save();
painter->translate(-translation);
painter->rotate(90);
if (_numPos == 0) {
painter->drawText(QRectF(.0, stringDist * (_strings - 1), .0, .0),
Qt::AlignLeft|Qt::TextDontClip, text);
}
else {
painter->drawText(QRectF(.0, .0, .0, .0),
Qt::AlignBottom|Qt::AlignLeft|Qt::TextDontClip, text);
}
painter->restore();
}
painter->setFont(font);
}
// NOTE:JT possible future todo - draw fingerings
if (_orientation == Orientation::HORIZONTAL) {
painter->restore();
}
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void FretDiagram::layout()
{
qreal _spatium = spatium() * _userMag;
stringLw = _spatium * 0.08;
nutLw = (_fretOffset || !_showNut) ? stringLw : _spatium * 0.2;
stringDist = score()->styleP(Sid::fretStringSpacing) * _userMag;
fretDist = score()->styleP(Sid::fretFretSpacing) * _userMag;
markerSize = stringDist * .8;
qreal w = stringDist * (_strings - 1) + markerSize;
qreal h = (_frets + 1) * fretDist + markerSize;
qreal y = -(markerSize * .5 + fretDist);
qreal x = -(markerSize * .5);
// Allocate space for fret offset number
if (_fretOffset > 0) {
QFont scaledFont(font);
scaledFont.setPointSize(font.pointSize() * _userMag);
QFontMetricsF fm(scaledFont, MScore::paintDevice());
qreal fretNumMag = score()->styleD(Sid::fretNumMag);
scaledFont.setPointSizeF(scaledFont.pointSizeF() * fretNumMag);
QFontMetricsF fm2(scaledFont, MScore::paintDevice());
qreal numw = fm2.width(QString("%1").arg(_fretOffset+1));
qreal xdiff = numw + stringDist * .4;
w += xdiff;
x += (_numPos == 0) == (_orientation == Orientation::VERTICAL) ? -xdiff : 0;
}
if (_orientation == Orientation::HORIZONTAL) {
qreal tempW = w,
tempX = x;
w = h;
h = tempW;
x = y;
y = tempX;
}
bbox().setRect(x, y, w, h);
if (!parent() || !parent()->isSegment()) {
setPos(QPointF());
return;
}
// We need to get the width of the notehead/rest in order to position the fret diagram correctly
Segment* pSeg = toSegment(parent());
qreal noteheadWidth = 0;
if (pSeg->isChordRestType()) {
int idx = staff()->idx();
for (Element* e = pSeg->firstElementOfSegment(pSeg, idx); e; e = pSeg->nextElementOfSegment(pSeg, e, idx)) {
if (e->isRest()) {
Rest* r = toRest(e);
noteheadWidth = symWidth(r->sym());
break;
}
else if (e->isNote()) {
Note* n = toNote(e);
noteheadWidth = n->headWidth();
break;
}
}
}
qreal mainWidth = 0.0;
if (_orientation == Orientation::VERTICAL)
mainWidth = stringDist * (_strings - 1);
else if (_orientation == Orientation::HORIZONTAL)
mainWidth = fretDist * (_frets + 0.5);
setPos((noteheadWidth - mainWidth)/2, -(h + styleP(Sid::fretY)));
autoplaceSegmentElement();
// don't display harmony in palette
if (!parent())
return;
if (_harmony)
_harmony->layout();
if (_harmony && _harmony->autoplace() && _harmony->parent()) {
Segment* s = toSegment(parent());
Measure* m = s->measure();
int si = staffIdx();
SysStaff* ss = m->system()->staff(si);
QRectF r = _harmony->bbox().translated(m->pos() + s->pos() + pos() + _harmony->pos() + QPointF(_harmony->xShapeOffset(), 0.0));
qreal minDistance = _harmony->minDistance().val() * spatium();
SkylineLine sk(false);
sk.add(r.x(), r.bottom(), r.width());
qreal d = sk.minDistance(ss->skyline().north());
if (d > -minDistance) {
qreal yd = d + minDistance;
yd *= -1.0;
_harmony->rypos() += yd;
r.translate(QPointF(0.0, yd));
}
if (_harmony->addToSkyline())
ss->skyline().add(r);
}
}
//---------------------------------------------------------
// centerX
/// used by harmony for layout. Keep in sync with layout, same dotd and x as above
// also used in Element::canvasPos().
//---------------------------------------------------------
qreal FretDiagram::centerX() const
{
qreal dotd = spatium() * _userMag * .49 * score()->styleD(Sid::fretDotSize);
qreal x = -((dotd+stringLw) * .5);
return bbox().right() * .5 + x;
}
//---------------------------------------------------------
// write
// NOTICE: if you are looking to change how fret diagrams are
// written, edit the writeNew function. writeOld is purely compatibility.
//---------------------------------------------------------
static const std::array<Pid, 8> pids { {
Pid::MIN_DISTANCE,
Pid::FRET_OFFSET,
Pid::FRET_FRETS,
Pid::FRET_STRINGS,
Pid::FRET_NUT,
Pid::MAG,
Pid::FRET_NUM_POS,
Pid::ORIENTATION
} };
void FretDiagram::write(XmlWriter& xml) const
{
if (!xml.canWrite(this))
return;
xml.stag(this);
// Write properties first and only once
for (Pid p : pids) {
writeProperty(xml, p);
}
Element::writeProperties(xml);
if (_harmony)
_harmony->write(xml);
// Lowercase f indicates new writing format
// TODO: in the next score format version (4) use only write new + props and discard
// the compatibility writing.
xml.stag("fretDiagram");
writeNew(xml);
xml.etag();
writeOld(xml);
xml.etag();
}
//---------------------------------------------------------
// writeOld
// This is the old method of writing. This is for backwards
// compatibility with < 3.1 versions.
//---------------------------------------------------------
void FretDiagram::writeOld(XmlWriter& xml) const
{
int lowestDotFret = -1;
int furthestLeftLowestDot = -1;
// Do some checks for details needed for checking whether to add barres
for (int i = 0; i < _strings; ++i) {
std::vector<FretItem::Dot> allDots = dot(i);
bool dotExists = false;
for (auto const& d : allDots) {
if (d.exists()) {
dotExists = true;
break;
}
}
if (!dotExists)
continue;
for (auto const& d : allDots) {
if (d.exists()) {
if (d.fret < lowestDotFret || lowestDotFret == -1) {
lowestDotFret = d.fret;
furthestLeftLowestDot = i;
}
else if (d.fret == lowestDotFret && (i < furthestLeftLowestDot || furthestLeftLowestDot == -1)) {
furthestLeftLowestDot = i;
}
}
}
}
// The old system writes a barre as a bool, which causes no problems in any way, not at all.
// So, only write that if the barre is on the lowest fret with a dot,
// and there are no other dots on its fret, and it goes all the way to the right.
int barreStartString = -1;
int barreFret = -1;
for (auto const& i : _barres) {
FretItem::Barre b = i.second;
if (b.exists()) {
int fret = i.first;
if (fret <= lowestDotFret && b.endString == -1 && !(fret == lowestDotFret && b.startString > furthestLeftLowestDot)) {
barreStartString = b.startString;
barreFret = fret;
break;
}
}
}
for (int i = 0; i < _strings; ++i) {
FretItem::Marker m = marker(i);
std::vector<FretItem::Dot> allDots = dot(i);
bool dotExists = false;
for (auto const& d : allDots) {
if (d.exists()) {
dotExists = true;
break;
}
}
if (!dotExists && !m.exists() && i != barreStartString)
continue;
xml.stag(QString("string no=\"%1\"").arg(i));
if (m.exists())
xml.tag("marker", FretItem::markerToChar(m.mtype).unicode());
for (auto const& d : allDots) {
if (d.exists() && !(i == barreStartString && d.fret == barreFret)) {
xml.tag("dot", d.fret);
}
}
// Add dot so barre will display in pre-3.1
if (barreStartString == i) {
xml.tag("dot", barreFret);
}
xml.etag();
}
if (barreFret > 0)
xml.tag("barre", 1);
}
//---------------------------------------------------------
// writeNew
// This is the important one for 3.1+
//---------------------------------------------------------
void FretDiagram::writeNew(XmlWriter& xml) const
{
for (int i = 0; i < _strings; ++i) {
FretItem::Marker m = marker(i);
std::vector<FretItem::Dot> allDots = dot(i);
bool dotExists = false;
for (auto const& d : allDots) {
if (d.exists()) {
dotExists = true;
break;
}
}
// Only write a string if we have anything to write
if (!dotExists && !m.exists())
continue;
// Start the string writing
xml.stag(QString("string no=\"%1\"").arg(i));
// Write marker
if (m.exists())
xml.tag("marker", FretItem::markerTypeToName(m.mtype));
// Write any dots
for (auto const& d : allDots) {
if (d.exists()) {
// TODO: write fingering
xml.tag(QString("dot fret=\"%1\"").arg(d.fret), FretItem::dotTypeToName(d.dtype));
}
}
xml.etag();
}
for (int f = 1; f <= _frets; ++f) {
FretItem::Barre b = barre(f);
if (!b.exists())
continue;
xml.tag(QString("barre start=\"%1\" end=\"%2\"").arg(b.startString).arg(b.endString), f);
}
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void FretDiagram::read(XmlReader& e)
{
// Read the old format first
bool hasBarre = false;
bool haveReadNew = false;
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
// Check for new format fret diagram
if (haveReadNew) {
e.skipCurrentElement();
continue;
}
if (tag == "fretDiagram") {
readNew(e);
haveReadNew = true;
}
// Check for new properties
else if (tag == "showNut")
readProperty(e, Pid::FRET_NUT);
else if (tag == "orientation")
readProperty(e, Pid::ORIENTATION);
// Then read the rest if there is no new format diagram (compatibility read)
else if (tag == "strings")
readProperty(e, Pid::FRET_STRINGS);
else if (tag == "frets")
readProperty(e, Pid::FRET_FRETS);
else if (tag == "fretOffset")
readProperty(e, Pid::FRET_OFFSET);
else if (tag == "string") {
int no = e.intAttribute("no");
while (e.readNextStartElement()) {
const QStringRef& t(e.name());
if (t == "dot")
setDot(no, e.readInt());
else if (t == "marker")
setMarker(no, QChar(e.readInt()) == 'X' ? FretMarkerType::CROSS : FretMarkerType::CIRCLE);
/*else if (t == "fingering")
setFingering(no, e.readInt());*/
else
e.unknown();
}
}
else if (tag == "barre")
hasBarre = e.readBool();
else if (tag == "mag")
readProperty(e, Pid::MAG);
else if (tag == "Harmony") {
Harmony* h = new Harmony(score());
h->read(e);
add(h);
}
else if (!Element::readProperties(e))
e.unknown();
}
// Old handling of barres
if (hasBarre) {
for (int s = 0; s < _strings; ++s) {
for (auto& d : dot(s)) {
if (d.exists()) {
setBarre(s, -1, d.fret);
return;
}
}
}
}
}
//---------------------------------------------------------
// readNew
// read the new 'fretDiagram' tag
//---------------------------------------------------------
void FretDiagram::readNew(XmlReader& e)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "string") {
int no = e.intAttribute("no");
while (e.readNextStartElement()) {
const QStringRef& t(e.name());
if (t == "dot") {
int fret = e.intAttribute("fret", 0);
FretDotType dtype = FretItem::nameToDotType(e.readElementText());
setDot(no, fret, true, dtype);
}
else if (t == "marker") {
FretMarkerType mtype = FretItem::nameToMarkerType(e.readElementText());
setMarker(no, mtype);
}
else if (t == "fingering") {
e.readElementText();
/*setFingering(no, e.readInt()); NOTE:JT todo */
}
else
e.unknown();
}
}
else if (tag == "barre") {
int start = e.intAttribute("start", -1);
int end = e.intAttribute("end", -1);
int fret = e.readInt();
setBarre(start, end, fret);
}
else if (!Element::readProperties(e))
e.unknown();
}
}
//---------------------------------------------------------
// setDot
// take a fret value of 0 to mean remove the dot, except with add
// where we actually need to pass a fret val.
//---------------------------------------------------------
void FretDiagram::setDot(int string, int fret, bool add /*= false*/, FretDotType dtype /*= FretDotType::NORMAL*/)
{
if (fret == 0)
removeDot(string, fret);
else if (string >= 0 && string < _strings) {
// Special case - with add, if there is a dot in the position, remove it
// If not, add it.
if (add) {
if (dot(string, fret)[0].exists()) {
removeDot(string, fret);
return; // We are done here, all we needed to do was remove a single dot
}
}
else
_dots[string].clear();
_dots[string].push_back(FretItem::Dot(fret, dtype));
setMarker(string, FretMarkerType::NONE);
}
}
//---------------------------------------------------------
// setMarker
// Remove any dots and barres if the marker is being set to anything other than none.
//---------------------------------------------------------
void FretDiagram::setMarker(int string, FretMarkerType mtype)
{
if (string >= 0 && string < _strings) {
_markers[string] = FretItem::Marker(mtype);
if (mtype != FretMarkerType::NONE) {
removeDot(string);
removeBarres(string);
}
}
}
//---------------------------------------------------------
// setFingering
// NOTE:JT: todo possible future feature
//---------------------------------------------------------
#if 0
void FretDiagram::setFingering(int string, int finger)
{
if (_dots.find(string) != _dots.end()) {
_dots[string].fingering = finger;
qDebug("set finger: s %d finger %d", string, finger);
}
}
#endif
//---------------------------------------------------------
// setBarre
// We'll accept a value of -1 for the end string, to denote
// that the barre goes as far right as possible.
// Take a start string value of -1 to mean 'remove this barre'
//---------------------------------------------------------
void FretDiagram::setBarre(int startString, int endString, int fret)
{
if (startString == -1)
removeBarre(fret);
else if (startString >= 0 && endString >= -1 && startString < _strings && endString < _strings)
_barres[fret] = FretItem::Barre(startString, endString);
}
//---------------------------------------------------------
// This version is for clicks on a dot with shift.
// If there is no barre at fret, then add one with the string as the start.
// If there is a barre with a -1 end string, set the end string to string.
// If there is a barre with a set start and end, remove it.
// Add may be used in the future if we decide to add dots as default with barres.
//---------------------------------------------------------
void FretDiagram::setBarre(int string, int fret, bool add /*= false*/)
{
Q_UNUSED(add);
FretItem::Barre b = barre(fret);
if (!b.exists()) {
if (string < _strings - 1) {
_barres[fret] = FretItem::Barre(string, -1);
removeDotsMarkers(string, -1, fret);
}
}
else if (b.endString == -1 && b.startString < string) {
_barres[fret].endString = string;
}
else {
removeDotsMarkers(b.startString, b.endString, fret);
removeBarre(fret);
}
}
//---------------------------------------------------------
// undoSetFretDot
//---------------------------------------------------------
void FretDiagram::undoSetFretDot(int _string, int _fret, bool _add /*= true*/, FretDotType _dtype /*= FretDotType::NORMAl*/)
{
for (ScoreElement* e : linkList()) {
FretDiagram* fd = toFretDiagram(e);
fd->score()->undo(new FretDot(fd, _string, _fret, _add, _dtype));
}
}
//---------------------------------------------------------
// undoSetFretMarker
//---------------------------------------------------------
void FretDiagram::undoSetFretMarker(int _string, FretMarkerType _mtype)
{
for (ScoreElement* e : linkList()) {
FretDiagram* fd = toFretDiagram(e);
fd->score()->undo(new FretMarker(fd, _string, _mtype));