/
figuredbass.cpp
1611 lines (1458 loc) · 57.9 KB
/
figuredbass.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
// $Id: figuredbass.cpp 5526 2012-04-09 10:17:11Z lvinken $
//
// 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
//=============================================================================
#include "figuredbass.h"
#include "score.h"
#include "note.h"
#include "measure.h"
#include "system.h"
#include "segment.h"
#define FBIDigitNone 0
// the array of configured fonts
static QList<FiguredBassFont> g_FBFonts;
//---------------------------------------------------------
// F I G U R E D B A S S I T E M
//---------------------------------------------------------
// used for indexed access to parenthesis chars
// (these is no normAccidToChar[], as accidentals may use mult. chars in normalized display):
const QChar FiguredBassItem::normParenthToChar[NumOfParentheses] =
{ 0, '(', ')', '[', ']'};
FiguredBassItem::FiguredBassItem(Score* s, int l)
: Element(s), ord(l)
{
_prefix = _suffix = ModifierNone;
_digit = FBIDigitNone;
parenth[0] = parenth[1] = parenth[2] = parenth[3] = parenth[4] = ParenthesisNone;
_contLine = false;
}
FiguredBassItem::FiguredBassItem(const FiguredBassItem& item)
: Element(item)
{
ord = item.ord;
_prefix = item._prefix;
_digit = item._digit;
_suffix = item._suffix;
parenth[0] = item.parenth[0];
parenth[1] = item.parenth[1];
parenth[2] = item.parenth[2];
parenth[3] = item.parenth[3];
parenth[4] = item.parenth[4];
_contLine = item._contLine;
textWidth = item.textWidth;
_displayText= item._displayText;
}
FiguredBassItem::~FiguredBassItem()
{
}
//---------------------------------------------------------
// FiguredBassItem parse()
//
// converts a string into a property-based representation, if possible;
// return true on success | false if the string is non-conformant
//---------------------------------------------------------
bool FiguredBassItem::parse(QString& str)
{
int retVal;
parseParenthesis(str, 0);
retVal = parsePrefixSuffix(str, true); // prefix
if(retVal == -1)
return false;
parseParenthesis(str, 1);
retVal = parseDigit(str); // digit
if(retVal == -1)
return false;
parseParenthesis(str, 2);
retVal = parsePrefixSuffix(str, false); // suffix
if(retVal == -1)
return false;
parseParenthesis(str, 3);
// check for a possible cont. line symbol(s)
_contLine = false; // contLine
while(str[0] == '-' || str[0] == '_') {
_contLine = true;
str.remove(0, 1);
}
parseParenthesis(str, 4);
// remove useless parentheses
if(_prefix == ModifierNone && parenth[1] == ParenthesisNone) {
parenth[1] = parenth[0];
parenth[0] = ParenthesisNone;
}
if(_digit == FBIDigitNone && parenth[2] == ParenthesisNone) {
parenth[2] = parenth[1];
parenth[1] = ParenthesisNone;
}
if(!_contLine && parenth[3] == ParenthesisNone) {
parenth[3] = parenth[4];
parenth[4] = ParenthesisNone;
}
if(_suffix == ModifierNone && parenth[2] == ParenthesisNone) {
parenth[2] = parenth[3];
parenth[3] = ParenthesisNone;
}
// some checks:
// if some extra input, str is not conformant
if(str.size())
return false;
// can't have BOTH prefix and suffix
// prefix, digit, suffix and cont.line cannot be ALL empty
// suffix cannot combine with empty digit
if( (_prefix != ModifierNone && _suffix != ModifierNone)
|| (_prefix == ModifierNone && _digit == FBIDigitNone && _suffix == ModifierNone && !_contLine)
|| ( (_suffix == ModifierPlus || _suffix == ModifierBackslash || _suffix == ModifierSlash)
&& _digit == FBIDigitNone) )
return false;
return true;
}
//---------------------------------------------------------
// FiguredBassItem parsePrefixSuffix()
//
// scans str to extract prefix or suffix properties. Stops at the first char which cannot fit.
// Fitting chars are removed from str. DOES NOT generate any display text
//
// returns the number of QChar's read from str or -1 if prefix / suffix has an illegal format
// (no prefix / suffix at all IS legal)
//---------------------------------------------------------
int FiguredBassItem::parsePrefixSuffix(QString& str, bool bPrefix)
{
Modifier * dest = bPrefix ? &_prefix : &_suffix;
bool done = false;
int size = str.size();
str = str.trimmed();
*dest = ModifierNone;
while(str.size()) {
switch(str.at(0).unicode())
{
case 'b':
if(*dest != ModifierNone) {
if(*dest == ModifierFlat) // FLAT may double a previous FLAT
*dest = ModifierDoubleFlat;
else
return -1; // but no other combination is acceptable
}
*dest = ModifierFlat;
break;
case 'h':
if(*dest != ModifierNone) // cannot combine with any other accidental
return -1;
*dest = ModifierNatural;
break;
case '#':
if(*dest != ModifierNone) {
if(*dest == ModifierSharp) // SHARP may double a preivous SHARP
*dest = ModifierDoubleSharp;
else
return -1; // but no other combination is acceptable
}
*dest = ModifierSharp;
break;
// '+', '\\' and '/' go into the suffix
case '+':
if(_suffix != ModifierNone) // cannot combine with any other accidental
return -1;
_suffix = ModifierPlus;
break;
case '\\':
if(_suffix != ModifierNone) // cannot combine with any other accidental
return -1;
_suffix = ModifierBackslash;
break;
case '/':
if(_suffix != ModifierNone) // cannot combine with any other accidental
return -1;
_suffix = ModifierSlash;
break;
default: // any other char: no longer in prefix/suffix
done = true;
break;
}
if(done)
break;
str.remove(0,1); // 'eat' the char and continue
}
return size - str.size(); // return how many chars we had read into prefix/suffix
}
//---------------------------------------------------------
// FiguredBassItem parseDigit()
//
// scans str to extract digit properties. Stops at the first char which cannot belong to digit part.
// Fitting chars are removed from str. DOES NOT generate any display text
//
// returns the number of QChar's read from str or -1 if no legal digit can be constructed
// (no digit at all IS legal)
//---------------------------------------------------------
int FiguredBassItem::parseDigit(QString& str)
{
int size = str.size();
str = str.trimmed();
_digit = FBIDigitNone;
while(str.size()) {
// any digit acceptable, if no previous digit
if(str[0] >= '1' && str[0] <= '9') {
if(_digit == FBIDigitNone) {
_digit = str[0].unicode() - '0';
str.remove(0, 1);
}
else
return -1;
}
// anything else: no longer in digit part
else
break;
}
return size - str.size();
}
//---------------------------------------------------------
// FiguredBassItem parseParenthesis()
//
// scans str to extract a (possible) parenthesis, stores its code into parenth[parenthIdx]
// and removes it from str. Only looks at first str char.
//
// returns the number of QChar's read from str (actually 0 or 1).
//---------------------------------------------------------
int FiguredBassItem::parseParenthesis(QString& str, int parenthIdx)
{
int c = str[0].unicode();
Parenthesis code = ParenthesisNone;
switch(c)
{
case '(':
code =ParenthesisRoundOpen;
break;
case ')':
code =ParenthesisRoundClosed;
break;
case '[':
code =ParenthesisSquaredOpen;
break;
case ']':
code =ParenthesisSquaredClosed;
break;
default:
break;
}
parenth[parenthIdx] = code;
if(code != ParenthesisNone) {
str.remove(0, 1);
return 1;
}
return 0;
}
//---------------------------------------------------------
// FiguredBassItem normalizedText()
//
// returns a string with the normalized text, i.e. the text displayed while editing;
// this is a standard textual representation of the item properties
//---------------------------------------------------------
QString FiguredBassItem::normalizedText() const
{
QString str = QString();
if(parenth[0] != ParenthesisNone)
str.append(normParenthToChar[parenth[0]]);
if(_prefix != ModifierNone) {
switch(_prefix)
{
case ModifierFlat:
str.append('b');
break;
case ModifierNatural:
str.append('h');
break;
case ModifierSharp:
str.append('#');
break;
case ModifierDoubleFlat:
str.append("bb");
break;
case ModifierDoubleSharp:
str.append("##");
break;
default:
break;
}
}
if(parenth[1] != ParenthesisNone)
str.append(normParenthToChar[parenth[1]]);
// digit
if(_digit != FBIDigitNone)
str.append(QChar('0' + _digit));
if(parenth[2] != ParenthesisNone)
str.append(normParenthToChar[parenth[2]]);
// suffix
if(_suffix != ModifierNone) {
switch(_suffix)
{
case ModifierFlat:
str.append('b');
break;
case ModifierNatural:
str.append('h');
break;
case ModifierSharp:
str.append('#');
break;
case ModifierPlus:
str.append('+');
break;
case ModifierBackslash:
str.append('\\');
break;
case ModifierSlash:
str.append('/');
break;
case ModifierDoubleFlat:
str.append("bb");
break;
case ModifierDoubleSharp:
str.append("##");
break;
default:
break;
}
}
if(parenth[3] != ParenthesisNone)
str.append(normParenthToChar[parenth[3]]);
if(_contLine)
str.append('_');
if(parenth[4] != ParenthesisNone)
str.append(normParenthToChar[parenth[4]]);
return str;
}
//---------------------------------------------------------
// FiguredBassItem write()
//---------------------------------------------------------
void FiguredBassItem::write(Xml& xml) const
{
xml.stag("FiguredBassItem");
xml.tagE(QString("brackets b0=\"%1\" b1=\"%2\" b2=\"%3\" b3=\"%4\" b4=\"%5\"")
.arg(parenth[0]) .arg(parenth[1]) .arg(parenth[2]) .arg(parenth[3]) .arg(parenth[4]) );
if(_prefix != ModifierNone)
xml.tag(QString("prefix"), _prefix);
if(_digit != FBIDigitNone)
xml.tag(QString("digit"), _digit);
if(_suffix != ModifierNone)
xml.tag(QString("suffix"), _suffix);
if(_contLine)
xml.tag("continuationLine", _contLine);
xml.etag();
}
//---------------------------------------------------------
// FiguredBassItem read()
//---------------------------------------------------------
void FiguredBassItem::read(const QDomElement& de)
{
for (QDomElement e = de.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString& tag(e.tagName());
const QString& val(e.text());
int iVal = val.toInt();
if(tag == "brackets") {
parenth[0] = (Parenthesis)e.attribute("b0").toInt();
parenth[1] = (Parenthesis)e.attribute("b1").toInt();
parenth[2] = (Parenthesis)e.attribute("b2").toInt();
parenth[3] = (Parenthesis)e.attribute("b3").toInt();
parenth[4] = (Parenthesis)e.attribute("b4").toInt();
}
else if(tag == "prefix")
_prefix = (Modifier)iVal;
else if(tag == "digit")
_digit = iVal;
else if(tag == "suffix")
_suffix = (Modifier)iVal;
else if(tag == "continuationLine")
_contLine = iVal;
else if(!Element::readProperties(e))
domError(e);
}
}
//---------------------------------------------------------
// FiguredBassItem layout()
// creates the display text (set as element text) and computes
// the horiz. offset needed to align the right part as well as the vert. offset
//---------------------------------------------------------
void FiguredBassItem::layout()
{
qreal h, w, x, x1, x2, y;
// contruct font metrics
int fontIdx = 0;
QFont f(g_FBFonts.at(fontIdx).family);
// font size in points, scaled according to spatium()
qreal m = score()->styleD(ST_figuredBassFontSize) * spatium() / ( SPATIUM20 * MScore::DPI);
f.setPointSizeF(m);
QFontMetrics fm(f);
QString str = QString();
x = symbols[score()->symIdx()][quartheadSym].width(magS()) * .5;
x1 = x2 = 0.0;
// create display text
int font = 0;
int style = score()->styleI(ST_figuredBassStyle);
if(parenth[0] != ParenthesisNone)
str.append(g_FBFonts.at(font).displayParenthesis[parenth[0]]);
// prefix
if(_prefix != ModifierNone) {
// if no digit, the string created so far 'hangs' to the left of the note
if(_digit == FBIDigitNone)
x1 = fm.width(str);
str.append(g_FBFonts.at(font).displayAccidental[_prefix]);
// if no digit, the string from here onward 'hangs' to the right of the note
if(_digit == FBIDigitNone)
x2 = fm.width(str);
}
if(parenth[1] != ParenthesisNone)
str.append(g_FBFonts.at(font).displayParenthesis[parenth[1]]);
// digit
if(_digit != FBIDigitNone) {
// if some digit, the string created so far 'hangs' to the left of the note
x1 = fm.width(str);
// if suffix is a combining shape, combine it with digit
// unless there is a parenthesis in between
if( (_suffix == ModifierPlus || _suffix == ModifierBackslash || _suffix == ModifierSlash)
&& parenth[2] == ParenthesisNone)
str.append(g_FBFonts.at(font).displayDigit[style][_digit][_suffix-(ModifierPlus-1)]);
else
str.append(g_FBFonts.at(font).displayDigit[style][_digit][0]);
// if some digit, the string from here onward 'hangs' to the right of the note
x2 = fm.width(str);
}
if(parenth[2] != ParenthesisNone)
str.append(g_FBFonts.at(font).displayParenthesis[parenth[2]]);
// suffix
// append only if non-combining shape or cannot combine (no digit or parenthesis in between)
if( _suffix != ModifierNone
&& ( (_suffix != ModifierPlus && _suffix != ModifierBackslash && _suffix != ModifierSlash)
|| _digit == FBIDigitNone
|| parenth[2] != ParenthesisNone) )
str.append(g_FBFonts.at(font).displayAccidental[_suffix]);
if(parenth[3] != ParenthesisNone)
str.append(g_FBFonts.at(font).displayParenthesis[parenth[3]]);
setDisplayText(str); // this text will be displayed
// position the text so that [x1<-->x2] is centered below the note
x = x - (x1+x2) * 0.5;
h = fm.lineSpacing();
h *= score()->styleD(ST_figuredBassLineHeight);
w = fm.width(str);
textWidth = w;
int lineLen;
if(_contLine && (lineLen=figuredBass()->lineLength(0)) > w)
w = lineLen;
y = h * ord;
setPos(x, y);
setbbox(QRect(0, 0, w, h));
}
//---------------------------------------------------------
// FiguredBassItem draw()
//---------------------------------------------------------
void FiguredBassItem::draw(QPainter* painter) const
{
int font = 0;
// set font from general style
QFont f(g_FBFonts.at(font).family);
#ifdef USE_GLYPHS
f.setHintingPreference(QFont::PreferVerticalHinting);
#endif
// font size in pixels, scaled according to spatium()
qreal m = score()->styleD(ST_figuredBassFontSize) * MScore::DPI / PPI;
m *= spatium() / (SPATIUM20 * MScore::DPI); // make spatium dependent
f.setPixelSize(lrint(m));
painter->setFont(f);
painter->setBrush(Qt::NoBrush);
painter->setPen(figuredBass()->curColor());
painter->drawText(bbox(), Qt::TextDontClip | Qt::AlignLeft | Qt::AlignTop, displayText());
// drawFrame(p);
// continuation line
qreal len = 0.0;
if(_contLine) {
len = figuredBass()->lineLength(0);
if(len > 0.0) {
qreal h = bbox().height() * 0.75;
painter->drawLine(textWidth, h, len - ipos().x(), h);
}
}
// closing cont.line parenthesis
if(parenth[4] != ParenthesisNone) {
int x = len > 0.0 ? len : textWidth;
painter->drawText(QRectF(x, 0, bbox().width(), bbox().height()), Qt::AlignLeft | Qt::AlignTop,
g_FBFonts.at(font).displayParenthesis[parenth[4]]);
}
}
//---------------------------------------------------------
// PROPERTY METHODS
//---------------------------------------------------------
QVariant FiguredBassItem::getProperty(P_ID propertyId) const
{
switch(propertyId) {
case P_FBPREFIX:
return _prefix;
case P_FBDIGIT:
return _digit;
case P_FBSUFFIX:
return _suffix;
case P_FBCONTINUATIONLINE:
return _contLine;
case P_FBPARENTHESIS1:
return parenth[0];
case P_FBPARENTHESIS2:
return parenth[1];
case P_FBPARENTHESIS3:
return parenth[2];
case P_FBPARENTHESIS4:
return parenth[3];
case P_FBPARENTHESIS5:
return parenth[4];
default:
return Element::getProperty(propertyId);
}
}
bool FiguredBassItem::setProperty(P_ID propertyId, const QVariant& v)
{
score()->addRefresh(canvasBoundingRect());
int val = v.toInt();
switch(propertyId) {
case P_FBPREFIX:
if(val < ModifierNone || val >= ModifierPlus)
return false;
_prefix = (Modifier)val;
break;
case P_FBDIGIT:
if(val < 1 || val > 9)
return false;
_digit = val;
break;
case P_FBSUFFIX:
if(val < ModifierNone || val >= NumOfModifiers)
return false;
_suffix = (Modifier)val;
break;
case P_FBCONTINUATIONLINE:
_contLine = v.toBool();
break;
case P_FBPARENTHESIS1:
if(val < ParenthesisNone || val > ParenthesisSquaredClosed)
return false;
parenth[0] = (Parenthesis)val;
break;
case P_FBPARENTHESIS2:
if(val < ParenthesisNone || val > ParenthesisSquaredClosed)
return false;
parenth[1] = (Parenthesis)val;
break;
case P_FBPARENTHESIS3:
if(val < ParenthesisNone || val > ParenthesisSquaredClosed)
return false;
parenth[2] = (Parenthesis)val;
break;
case P_FBPARENTHESIS4:
if(val < ParenthesisNone || val > ParenthesisSquaredClosed)
return false;
parenth[3] = (Parenthesis)val;
break;
case P_FBPARENTHESIS5:
if(val < ParenthesisNone || val > ParenthesisSquaredClosed)
return false;
parenth[4] = (Parenthesis)val;
break;
default:
return Element::setProperty(propertyId, v);
}
score()->setLayoutAll(true);
return true;
}
QVariant FiguredBassItem::propertyDefault(P_ID id) const
{
switch(id) {
case P_FBPREFIX:
case P_FBSUFFIX:
return ModifierNone;
case P_FBDIGIT:
return FBIDigitNone;
case P_FBCONTINUATIONLINE:
return false;
default:
return Element::propertyDefault(id);
}
}
//---------------------------------------------------------
// UNDOABLE PROPERTY SETTERS
//---------------------------------------------------------
void FiguredBassItem::undoSetPrefix(Modifier pref)
{
if(pref < ModifierPlus) {
score()->undoChangeProperty(this, P_FBPREFIX, (int)pref);
// if setting some prefix and there is a suffix already, clear suffix
if(pref != ModifierNone && _suffix != ModifierNone)
score()->undoChangeProperty(this, P_FBSUFFIX, ModifierNone);
layout(); // re-generate displayText
}
}
void FiguredBassItem::undoSetDigit(int digit)
{
if(digit >= 0 && digit <= 9) {
score()->undoChangeProperty(this, P_FBDIGIT, digit);
layout(); // re-generate displayText
}
}
void FiguredBassItem::undoSetSuffix(Modifier suff)
{
score()->undoChangeProperty(this, P_FBSUFFIX, suff);
// if setting some suffix and there is a prefix already, clear prefix
if(suff != ModifierNone && _prefix != ModifierNone)
score()->undoChangeProperty(this, P_FBPREFIX, ModifierNone);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetContLine(bool val)
{
score()->undoChangeProperty(this, P_FBCONTINUATIONLINE, val);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetParenth1(Parenthesis par)
{
score()->undoChangeProperty(this, P_FBPARENTHESIS1, par);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetParenth2(Parenthesis par)
{
score()->undoChangeProperty(this, P_FBPARENTHESIS2, par);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetParenth3(Parenthesis par)
{
score()->undoChangeProperty(this, P_FBPARENTHESIS3, par);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetParenth4(Parenthesis par)
{
score()->undoChangeProperty(this, P_FBPARENTHESIS4, par);
layout(); // re-generate displayText
}
void FiguredBassItem::undoSetParenth5(Parenthesis par)
{
score()->undoChangeProperty(this, P_FBPARENTHESIS5, par);
layout(); // re-generate displayText
}
//---------------------------------------------------------
//
// MusicXML I/O
//
//---------------------------------------------------------
//---------------------------------------------------------
// Convert MusicXML prefix/suffix to Modifier
//---------------------------------------------------------
// TODO add missing non-accidental types
FiguredBassItem::Modifier FiguredBassItem::MusicXML2Modifier(const QString prefix) const
{
if (prefix == "sharp")
return ModifierSharp;
else if (prefix == "flat")
return ModifierFlat;
else if (prefix == "natural")
return ModifierNatural;
else if (prefix == "double-sharp")
return ModifierDoubleSharp;
else if (prefix == "flat-flat")
return ModifierDoubleFlat;
else if (prefix == "sharp-sharp")
return ModifierDoubleSharp;
else if (prefix == "slash")
return ModifierSlash;
else
return ModifierNone;
}
//---------------------------------------------------------
// Convert Modifier to MusicXML prefix/suffix
//---------------------------------------------------------
// TODO add missing non-accidental types
QString FiguredBassItem::Modifier2MusicXML(FiguredBassItem::Modifier prefix) const
{
switch (prefix) {
case ModifierNone: return "";
case ModifierDoubleFlat: return "flat-flat";
case ModifierFlat: return "flat";
case ModifierNatural: return "natural";
case ModifierSharp: return "sharp";
case ModifierDoubleSharp: return "double-sharp";
case ModifierPlus: return ""; // TODO TBD
case ModifierBackslash: return ""; // TODO TBD
case ModifierSlash: return "slash";
case NumOfModifiers: return ""; // prevent gcc "‘FBINumOfAccid’ not handled in switch" warning
}
return "";
}
//---------------------------------------------------------
// Read MusicXML
//
// Set the FiguredBassItem state based on the MusicXML <figure> node de.
// In MusicXML, parentheses is set to "yes" or "no" for the figured-bass
// node instead of for each individual <figure> node.
//---------------------------------------------------------
void FiguredBassItem::readMusicXML(const QDomElement& de, bool paren)
{
// read the <figure> node de
for (QDomElement e = de.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString& tag(e.tagName());
const QString& val(e.text());
int iVal = val.toInt();
if (tag == "extend")
; // TODO
else if (tag == "figure-number") {
// MusicXML spec states figure-number is a number
// MuseScore can only handle single digit
if (1 <= iVal && iVal <= 9)
_digit = iVal;
}
else if (tag == "prefix")
_prefix = MusicXML2Modifier(val);
else if (tag == "suffix")
_suffix = MusicXML2Modifier(val);
else
domError(e);
}
// set parentheses
if (paren) {
// parenthesis open
if (_prefix != ModifierNone)
parenth[0] = ParenthesisRoundOpen; // before prefix
else if (_digit != FBIDigitNone)
parenth[1] = ParenthesisRoundOpen; // before digit
else if (_suffix != ModifierNone)
parenth[2] = ParenthesisRoundOpen; // before suffix
// parenthesis close
if (_suffix != ModifierNone)
parenth[3] = ParenthesisRoundClosed; // after suffix
else if (_digit != FBIDigitNone)
parenth[2] = ParenthesisRoundClosed; // after digit
else if (_prefix != ModifierNone)
parenth[1] = ParenthesisRoundClosed; // after prefix
}
}
//---------------------------------------------------------
// Write MusicXML
//---------------------------------------------------------
void FiguredBassItem::writeMusicXML(Xml& xml) const
{
xml.stag("figure");
QString strPrefix = Modifier2MusicXML(_prefix);
if (strPrefix != "")
xml.tag("prefix", strPrefix);
if (_digit != FBIDigitNone)
xml.tag("figure-number", _digit);
QString strSuffix = Modifier2MusicXML(_suffix);
if (strSuffix != "")
xml.tag("suffix", strSuffix);
xml.etag();
}
//---------------------------------------------------------
// startsWithParenthesis
//---------------------------------------------------------
bool FiguredBassItem::startsWithParenthesis() const
{
if (_prefix != ModifierNone)
return (parenth[0] != ParenthesisNone);
if (_digit != FBIDigitNone)
return (parenth[1] != ParenthesisNone);
if (_suffix != ModifierNone)
return (parenth[2] != ParenthesisNone);
return false;
}
//---------------------------------------------------------
// F I G U R E D B A S S
//---------------------------------------------------------
#include "chord.h"
#include "rest.h"
FiguredBass::FiguredBass(Score* s)
: Text(s)
{
setOnNote(true);
TextStyle st("local", g_FBFonts[0].family, score()->styleD(ST_figuredBassFontSize),
false, false, false, ALIGN_LEFT | ALIGN_TOP);
st.setSizeIsSpatiumDependent(true);
setTextStyle(st);
setTicks(0);
items.clear();
}
FiguredBass::FiguredBass(const FiguredBass& fb)
: Text(fb)
{
setOnNote(fb.onNote());
setTicks(fb.ticks());
items = fb.items;
}
FiguredBass::~FiguredBass()
{
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void FiguredBass::write(Xml& xml) const
{
xml.stag("FiguredBass");
if(!onNote())
xml.tag("onNote", onNote());
if (ticks() > 0)
xml.tag("ticks", ticks());
foreach(FiguredBassItem item, items)
item.write(xml);
Element::writeProperties(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void FiguredBass::read(const QDomElement& de)
{
QString normalizedText = QString();
int idx = 0;
for (QDomElement e = de.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
const QString& tag(e.tagName());
const QString& val(e.text());
if(tag == "ticks")
setTicks(val.toInt());
else if(tag == "onNote")
setOnNote(val.toInt() != 0l);
else if (tag == "FiguredBassItem") {
FiguredBassItem * pItem = new FiguredBassItem(score(), idx++);
pItem->setTrack(track());
pItem->setParent(this);
pItem->read(e);
items.append(*pItem);
// add item normalized text
if(!normalizedText.isEmpty())
normalizedText.append('\n');
normalizedText.append(pItem->normalizedText());
}
else if(!Element::readProperties(e))
domError(e);
}
setText(normalizedText); // this is the text to show while editing
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void FiguredBass::layout()
{
qreal y;
// if(items.size()) {
TextStyle st("local", g_FBFonts[0].family, score()->styleD(ST_figuredBassFontSize),
false, false, false, ALIGN_LEFT | ALIGN_TOP);
st.setSizeIsSpatiumDependent(true);
setTextStyle(st);
// }
layoutLines();
// vertical position
y = 0; // default vert. pos.
// if a staff exists for this F.B., use its y position
if(parent() && track() >= 0) {
System* sys = ((Segment*)parent())->measure()->system();
if (sys == 0)
qDebug("FiguredBass layout: no system!");
else {
SysStaff* staff = sys->staff(staffIdx());
y = staff->y();
}
}
y += point(score()->styleS(ST_figuredBassYOffset));
// bounding box
if(editMode()) {
qreal h, w, w1;
QFontMetricsF fm(textStyle().font(spatium()));
// box width
w = 0;
QStringList list = getText().split('\n');
foreach(QString str, list) {
w1 = fm.width(str);
if(w1 > w)
w = w1;
}
// bbox height
h = fm.lineSpacing();
h *= score()->styleD(ST_figuredBassLineHeight);
h *= (list.size() > 1 ? list.size() : 1); // at least 1 line
// ready to set position and bbox
setPos(0, y);
setbbox(QRectF(0-2, 0-2, w+4, h+4));
}
else
{
setPos(0, y);
setbbox(QRectF(0, 0, _lineLenghts.at(0), 0));
// if element could be parsed into items, layout each element
if(items.size() > 0) {
// layout each item and enlarge bbox to include items bboxes
for(int i=0; i < items.size(); i++) {
items[i].layout();
addbbox(items[i].bbox().translated(items[i].pos()));
}
}
else
// if not, fall back to standard Text layout
Text::layout();
}
adjustReadPos();
}
//---------------------------------------------------------
// layoutLines
//
// lays out the duration indicator line(s), filling the _lineLengths array