/
timesig.cpp
591 lines (514 loc) · 20.9 KB
/
timesig.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
//=============================================================================
// 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
//=============================================================================
#include "timesig.h"
#include "xml.h"
#include "score.h"
#include "style.h"
#include "sym.h"
#include "symbol.h"
#include "staff.h"
#include "stafftype.h"
namespace Ms {
//---------------------------------------------------------
// TimeSig
// Constructs an invalid time signature element.
// After construction first call setTrack() then
// call setSig().
// Layout() is static and called in setSig().
//---------------------------------------------------------
TimeSig::TimeSig(Score* s)
: Element(s)
{
setFlags(ElementFlag::SELECTABLE | ElementFlag::ON_STAFF);
_showCourtesySig = true;
customText = false;
_stretch.set(1, 1);
_sig.set(0, 1); // initialize to invalid
_timeSigType = TimeSigType::NORMAL;
_largeParentheses = false;
_needLayout = true;
}
//---------------------------------------------------------
// clone
//---------------------------------------------------------
TimeSig* TimeSig::clone() const
{
TimeSig* ts = new TimeSig(*this);
ts->setNeedLayout(true); // relayout of the ts is needed in parts #24571
return ts;
}
//---------------------------------------------------------
// mag
//---------------------------------------------------------
qreal TimeSig::mag() const
{
return staff() ? staff()->mag() : 1.0;
}
//---------------------------------------------------------
// setSig
// custom text has to be set after setSig()
//---------------------------------------------------------
void TimeSig::setSig(const Fraction& f, TimeSigType st)
{
if (!_sig.identical(f)) {
_sig = f;
_needLayout = true;
}
if (_timeSigType != st) {
_timeSigType = st;
_needLayout = true;
}
customText = false;
_largeParentheses = false;
}
//---------------------------------------------------------
// acceptDrop
//---------------------------------------------------------
bool TimeSig::acceptDrop(const DropData& data) const
{
return data.element->type() == Element::Type::TIMESIG;
}
//---------------------------------------------------------
// drop
//---------------------------------------------------------
Element* TimeSig::drop(const DropData& data)
{
Element* e = data.element;
if (e->type() == Element::Type::TIMESIG) {
// change timesig applies to all staves, can't simply set subtype
// for this one only
// ownership of e is transferred to cmdAddTimeSig
score()->cmdAddTimeSig(measure(), staffIdx(), static_cast<TimeSig*>(e), false);
return 0;
}
delete e;
return 0;
}
//---------------------------------------------------------
// setNumeratorString
// setSig() has to be called first
//---------------------------------------------------------
void TimeSig::setNumeratorString(const QString& a)
{
if (_timeSigType == TimeSigType::NORMAL) {
_numeratorString = a;
customText = (_denominatorString != QString::number(_sig.denominator()))
|| (_numeratorString != QString::number(_sig.numerator()));
_needLayout = true;
}
}
//---------------------------------------------------------
// setDenominatorString
// setSig() has to be called first
//---------------------------------------------------------
void TimeSig::setDenominatorString(const QString& a)
{
if (_timeSigType == TimeSigType::NORMAL) {
_denominatorString = a;
customText = (_denominatorString != QString::number(_sig.denominator()))
|| (_numeratorString != QString::number(_sig.numerator()));
_needLayout = true;
}
}
//---------------------------------------------------------
// write TimeSig
//---------------------------------------------------------
void TimeSig::write(Xml& xml) const
{
xml.stag("TimeSig");
if (timeSigType() != TimeSigType::NORMAL)
xml.tag("subtype", int(timeSigType()));
Element::writeProperties(xml);
xml.tag("sigN", _sig.numerator());
xml.tag("sigD", _sig.denominator());
if (stretch() != Fraction(1,1)) {
xml.tag("stretchN", stretch().numerator());
xml.tag("stretchD", stretch().denominator());
}
if (customText) {
xml.tag("textN", _numeratorString);
xml.tag("textD", _denominatorString);
}
if (!_groups.empty())
_groups.write(xml);
xml.tag("showCourtesySig", _showCourtesySig);
xml.etag();
}
//---------------------------------------------------------
// TimeSig::read
//---------------------------------------------------------
void TimeSig::read(XmlReader& e)
{
int n=0, z1=0, z2=0, z3=0, z4=0;
bool old = false;
customText = false;
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "den") {
old = true;
n = e.readInt();
}
else if (tag == "nom1") {
old = true;
z1 = e.readInt();
}
else if (tag == "nom2") {
old = true;
z2 = e.readInt();
}
else if (tag == "nom3") {
old = true;
z3 = e.readInt();
}
else if (tag == "nom4") {
old = true;
z4 = e.readInt();
}
else if (tag == "subtype") {
int i = e.readInt();
if (score()->mscVersion() < 122 && score()->mscVersion() > 114) {
setSig(Fraction(
((i >> 24) & 0x3f)
+ ((i >> 18) & 0x3f)
+ ((i >> 12) & 0x3f)
+ ((i >> 6) & 0x3f), i & 0x3f), TimeSigType::NORMAL);
}
else if (score()->mscVersion() <= 114) {
if (i == 0x40000104)
_timeSigType = TimeSigType::FOUR_FOUR;
else if (i == 0x40002084)
_timeSigType = TimeSigType::ALLA_BREVE;
else
_timeSigType = TimeSigType::NORMAL;
}
else
_timeSigType = TimeSigType(i);
}
else if (tag == "showCourtesySig")
_showCourtesySig = e.readInt();
else if (tag == "sigN")
_sig.setNumerator(e.readInt());
else if (tag == "sigD")
_sig.setDenominator(e.readInt());
else if (tag == "stretchN")
_stretch.setNumerator(e.readInt());
else if (tag == "stretchD")
_stretch.setDenominator(e.readInt());
else if (tag == "textN")
setNumeratorString(e.readElementText());
else if (tag == "textD")
setDenominatorString(e.readElementText());
else if (tag == "Groups")
_groups.read(e);
else if (!Element::readProperties(e))
e.unknown();
}
if (old) {
_sig.set(z1+z2+z3+z4, n);
customText = false;
}
_stretch.reduce();
_needLayout = true;
}
//---------------------------------------------------------
// layout1
//---------------------------------------------------------
void TimeSig::layout1()
{
qreal _spatium = spatium();
setbbox(QRectF()); // prepare for an empty time signature
pointLargeLeftParen = QPointF();
pz = QPointF();
pn = QPointF();
pointLargeRightParen = QPointF();
qreal lineDist = 1.0; // assume dimensions a standard staff
int numOfLines = 5;
TimeSigType sigType = timeSigType();
Staff* _staff = staff();
if (_staff) {
// if staff is without time sig, format as if no text at all
if (!_staff->staffType()->genTimesig() ) {
// reset position and box sizes to 0
pointLargeLeftParen.rx() = 0.0;
pn.rx() = 0.0;
pz.rx() = 0.0;
pointLargeRightParen.rx() = 0.0;
setbbox(QRectF());
// leave everything else as it is:
// draw() will anyway skip any drawing if staff type has no time sigs
return;
}
// update to real staff values
numOfLines = _staff->lines();
lineDist = _staff->lineDistance();
}
// if some symbol
// compute vert. displacement to center in the staff height
// determine middle staff position:
qreal yoff = _spatium * (numOfLines-1) *.5 * lineDist;
// C and Ccut are placed at the middle of the staff: use yoff directly
if (sigType == TimeSigType::FOUR_FOUR) {
pz = QPointF(0.0, yoff);
setbbox(symBbox(SymId::timeSigCommon).translated(pz));
_numeratorString = score()->scoreFont()->toString(SymId::timeSigCommon);
_denominatorString.clear();
}
else if (sigType == TimeSigType::ALLA_BREVE) {
pz = QPointF(0.0, yoff);
setbbox(symBbox(SymId::timeSigCutCommon).translated(pz));
_numeratorString = score()->scoreFont()->toString(SymId::timeSigCutCommon);
_denominatorString.clear();
}
else {
if (!customText) {
_numeratorString = QString("%1").arg(_sig.numerator()); // build numerator string
_denominatorString = QString("%1").arg(_sig.denominator()); // build denominator string
}
QString ns = toTimeSigString(_numeratorString);
QString ds = toTimeSigString(_denominatorString);
QFont font = score()->scoreFont()->font();
QFontMetricsF fm(font);
qreal mag = magS();
QRectF numRect = fm.tightBoundingRect(ns); // get 'tight' bounding boxes for strings
QRectF denRect = fm.tightBoundingRect(ds);
// really Qt does not provide a QRectF::scale() method?!!
numRect = QRect(numRect.x()*mag, numRect.y()*mag, fm.width(ns)*mag, numRect.height()*mag);
denRect = QRect(denRect.x()*mag, denRect.y()*mag, fm.width(ds)*mag, denRect.height()*mag);
// position numerator and denominator; vertical displacement:
// number of lines is odd: 0.0 (strings are directly above and below the middle line)
// number of lines even: 0.05 (strings are moved up/down to leave 1/10sp between them)
qreal displ = (numOfLines & 1) ? 0.0 : (0.05 * _spatium);
//align on the wider
if (numRect.width() >= denRect.width()) {
// numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
pz = QPointF(0.0, yoff - ((denRect.width() < 0.01) ? 0.0 : (displ + _spatium)) );
// denominator: horiz: centred around centre of numerator | vert: one space below centre line
pn = QPointF((numRect.width() - denRect.width())*.5, yoff + displ + _spatium);
}
else {
// denominator: horiz: centred around centre of numerator | vert: one space below centre line
pn = QPointF(0.0, yoff + displ + _spatium);
// numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
pz = QPointF((denRect.width() - numRect.width())*.5, yoff - ((denRect.width() < 0.01) ? 0.0 : (displ + _spatium)) );
}
// centering of parenthesis so the middle of the parenthesis is at the divisor marking level
int centerY = yoff/2 + _spatium;
int widestPortion = numRect.width() > denRect.width() ? numRect.width() : denRect.width();
pointLargeLeftParen = QPointF(-_spatium, centerY);
pointLargeRightParen = QPointF(widestPortion + _spatium, centerY);
setbbox(numRect.translated(pz)); // translate bounding boxes to actual string positions
addbbox(denRect.translated(pn));
if (_largeParentheses) {
addbbox(QRect(pointLargeLeftParen.x(), pointLargeLeftParen.y() - denRect.height(), _spatium / 2, numRect.height() + denRect.height()));
addbbox(QRect(pointLargeRightParen.x(), pointLargeRightParen.y() - denRect.height(), _spatium / 2, numRect.height() + denRect.height()));
}
}
_needLayout = false;
}
//---------------------------------------------------------
// draw
//---------------------------------------------------------
void TimeSig::draw(QPainter* painter) const
{
if (staff() && !staff()->staffType()->genTimesig())
return;
painter->setPen(curColor());
QString ns = toTimeSigString(_numeratorString);
QString ds = toTimeSigString(_denominatorString);
drawSymbols(ns, painter, pz);
drawSymbols(ds, painter, pn);
if (_largeParentheses) {
drawSymbol(SymId::timeSigParensLeft, painter, pointLargeLeftParen);
drawSymbol(SymId::timeSigParensRight, painter, pointLargeRightParen);
}
}
//---------------------------------------------------------
// space
//---------------------------------------------------------
Space TimeSig::space() const
{
return Space(point(score()->styleS(StyleIdx::timesigLeftMargin)), width());
}
//---------------------------------------------------------
// setFrom
//---------------------------------------------------------
void TimeSig::setFrom(const TimeSig* ts)
{
_timeSigType = ts->timeSigType();
_numeratorString = ts->_numeratorString;
_denominatorString = ts->_denominatorString;
_sig = ts->_sig;
_stretch = ts->_stretch;
customText = ts->customText;
_needLayout = true;
}
//---------------------------------------------------------
// ssig
//---------------------------------------------------------
QString TimeSig::ssig() const
{
return QString("%1/%2").arg(_sig.numerator()).arg(_sig.denominator());
}
//---------------------------------------------------------
// setSSig
//---------------------------------------------------------
void TimeSig::setSSig(const QString& s)
{
QStringList sl = s.split("/");
if (sl.size() == 2) {
_sig.setNumerator(sl[0].toInt());
_sig.setDenominator(sl[1].toInt());
}
}
//---------------------------------------------------------
// undoSetShowCourtesySig
//---------------------------------------------------------
void TimeSig::undoSetShowCourtesySig(bool v)
{
score()->undoChangeProperty(this, P_ID::SHOW_COURTESY, v);
}
//---------------------------------------------------------
// undoSetNumeratorString
//---------------------------------------------------------
void TimeSig::undoSetNumeratorString(const QString& s)
{
score()->undoChangeProperty(this, P_ID::NUMERATOR_STRING, s);
}
//---------------------------------------------------------
// undoSetDenominatorString
//---------------------------------------------------------
void TimeSig::undoSetDenominatorString(const QString& s)
{
score()->undoChangeProperty(this, P_ID::DENOMINATOR_STRING, s);
}
//---------------------------------------------------------
// undoSetGroups
//---------------------------------------------------------
void TimeSig::undoSetGroups(const Groups& g)
{
score()->undoChangeProperty(this, P_ID::GROUPS, QVariant::fromValue(g));
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant TimeSig::getProperty(P_ID propertyId) const
{
switch(propertyId) {
case P_ID::SHOW_COURTESY: return int(showCourtesySig());
case P_ID::NUMERATOR_STRING: return numeratorString();
case P_ID::DENOMINATOR_STRING: return denominatorString();
case P_ID::GROUPS: return QVariant::fromValue(groups());
case P_ID::TIMESIG: return QVariant::fromValue(_sig);
case P_ID::TIMESIG_GLOBAL: return QVariant::fromValue(globalSig());
case P_ID::TIMESIG_TYPE: return QVariant::fromValue(int(_timeSigType));
default:
return Element::getProperty(propertyId);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool TimeSig::setProperty(P_ID propertyId, const QVariant& v)
{
switch(propertyId) {
case P_ID::SHOW_COURTESY:
setShowCourtesySig(v.toBool());
break;
case P_ID::NUMERATOR_STRING:
setNumeratorString(v.toString());
break;
case P_ID::DENOMINATOR_STRING:
setDenominatorString(v.toString());
break;
case P_ID::GROUPS:
setGroups(v.value<Groups>());
break;
case P_ID::TIMESIG:
setSig(v.value<Fraction>());
break;
case P_ID::TIMESIG_GLOBAL:
setGlobalSig(v.value<Fraction>());
break;
case P_ID::TIMESIG_TYPE:
_timeSigType = (TimeSigType)(v.toInt());
break;
default:
if (!Element::setProperty(propertyId, v))
return false;
break;
}
_needLayout = true;
score()->setLayoutAll(true);
setGenerated(false);
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant TimeSig::propertyDefault(P_ID id) const
{
switch(id) {
case P_ID::SHOW_COURTESY: return true;
case P_ID::NUMERATOR_STRING: return QString();
case P_ID::DENOMINATOR_STRING: return QString();
case P_ID::TIMESIG: return QVariant::fromValue(Fraction(4,4));
case P_ID::TIMESIG_GLOBAL: return QVariant::fromValue(Fraction(1,1));
case P_ID::TIMESIG_TYPE: return int(TimeSigType::NORMAL);
default: return Element::propertyDefault(id);
}
}
//---------------------------------------------------------
// spatiumChanged
//---------------------------------------------------------
void TimeSig::spatiumChanged(qreal /*oldValue*/, qreal /*newValue*/)
{
_needLayout = true;
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void TimeSig::layout()
{
if (_needLayout)
layout1();
}
//---------------------------------------------------------
// nextElement
//---------------------------------------------------------
Element* TimeSig::nextElement()
{
return segment()->firstInNextSegments(staffIdx());
}
//---------------------------------------------------------
// prevElement
//---------------------------------------------------------
Element* TimeSig::prevElement()
{
return segment()->lastInPrevSegments(staffIdx());
}
//---------------------------------------------------------
// accessibleInfo
//---------------------------------------------------------
QString TimeSig::accessibleInfo()
{
QString timeSigString;
switch (timeSigType()) {
case TimeSigType::FOUR_FOUR:
timeSigString = tr("Common time");
case TimeSigType::ALLA_BREVE:
timeSigString = tr("Cut time");
default:
timeSigString = tr("%1/%2 time").arg(QString::number(numerator())).arg(QString::number(denominator()));
}
return QString("%1: %2").arg(Element::accessibleInfo()).arg(timeSigString);
}
}