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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
|
//=============================================================================
// 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 "mscore.h"
#include "staff.h"
#include "part.h"
#include "clef.h"
#include "xml.h"
#include "score.h"
#include "bracket.h"
#include "keysig.h"
#include "segment.h"
#include "style.h"
#include "measure.h"
#include "stringdata.h"
#include "stafftype.h"
#include "undo.h"
#include "cleflist.h"
#include "timesig.h"
#include "instrtemplate.h"
#include "barline.h"
#include "ottava.h"
#include "harmony.h"
// #define DEBUG_CLEFS
#ifdef DEBUG_CLEFS
#define DUMP_CLEFS(s) dumpClefs(s)
#else
#define DUMP_CLEFS(s)
#endif
namespace Ms {
//---------------------------------------------------------
// idx
//---------------------------------------------------------
int Staff::idx() const
{
return _score->staffIdx(this);
}
//---------------------------------------------------------
// bracket
//---------------------------------------------------------
BracketType Staff::bracket(int idx) const
{
if (idx < _brackets.size())
return _brackets[idx]._bracket;
return BracketType::NO_BRACKET;
}
//---------------------------------------------------------
// bracketSpan
//---------------------------------------------------------
int Staff::bracketSpan(int idx) const
{
if (idx < _brackets.size())
return _brackets[idx]._bracketSpan;
return 0;
}
//---------------------------------------------------------
// setBracket
//---------------------------------------------------------
void Staff::setBracket(int idx, BracketType val)
{
for (int i = _brackets.size(); i <= idx; ++i)
_brackets.append(BracketItem());
_brackets[idx]._bracket = val;
while (!_brackets.isEmpty() && (_brackets.last()._bracket == BracketType::NO_BRACKET))
_brackets.removeLast();
}
//---------------------------------------------------------
// setBracketSpan
//---------------------------------------------------------
void Staff::setBracketSpan(int idx, int val)
{
Q_ASSERT(idx >= 0);
Q_ASSERT(val >= 0);
for (int i = _brackets.size(); i <= idx; ++i)
_brackets.append(BracketItem());
_brackets[idx]._bracketSpan = val;
}
//---------------------------------------------------------
// addBracket
//---------------------------------------------------------
void Staff::addBracket(BracketItem b)
{
if (!_brackets.isEmpty() && _brackets[0]._bracket == BracketType::NO_BRACKET) {
_brackets[0] = b;
}
else {
//
// create new bracket level
//
foreach(Staff* s, _score->staves()) {
if (s == this)
s->_brackets.append(b);
else
s->_brackets.append(BracketItem());
}
}
}
//---------------------------------------------------------
// cleanupBrackets
//---------------------------------------------------------
void Staff::cleanupBrackets()
{
int index = idx();
int n = _score->nstaves();
for (int i = 0; i < _brackets.size(); ++i) {
if (_brackets[i]._bracket == BracketType::NO_BRACKET)
continue;
int span = _brackets[i]._bracketSpan;
if (span > (n - index)) {
span = n - index;
_brackets[i]._bracketSpan = span;
}
}
for (int i = 0; i < _brackets.size(); ++i) {
if (_brackets[i]._bracket == BracketType::NO_BRACKET)
continue;
int span = _brackets[i]._bracketSpan;
if (span <= 1)
_brackets[i] = BracketItem();
else {
// delete all other brackets with same span
for (int k = i + 1; k < _brackets.size(); ++k) {
if (span == _brackets[k]._bracketSpan)
_brackets[k] = BracketItem();
}
}
}
}
//---------------------------------------------------------
// partName
//---------------------------------------------------------
QString Staff::partName() const
{
return _part->partName();
}
//---------------------------------------------------------
// Staff
//---------------------------------------------------------
Staff::Staff(Score* s)
: ScoreElement(s)
{
_barLineTo = (lines() - 1) * 2;
}
//---------------------------------------------------------
// ~Staff
//---------------------------------------------------------
Staff::~Staff()
{
if (_linkedStaves) {
_linkedStaves->remove(this);
if (_linkedStaves->isEmpty())
delete _linkedStaves;
}
}
//---------------------------------------------------------
// Staff::clefType
//---------------------------------------------------------
ClefTypeList Staff::clefType(int tick) const
{
ClefTypeList ct = clefs.clef(tick);
if (ct._concertClef == ClefType::INVALID) {
switch(_staffType.group()) {
case StaffGroup::TAB:
ct = ClefTypeList(ClefType(score()->styleI(StyleIdx::tabClef)));
break;
case StaffGroup::STANDARD:
ct = defaultClefType();
break;
case StaffGroup::PERCUSSION:
ct = ClefTypeList(ClefType::PERC);
break;
}
}
return ct;
}
//---------------------------------------------------------
// Staff::clef
//---------------------------------------------------------
ClefType Staff::clef(int tick) const
{
ClefTypeList c = clefType(tick);
return score()->styleB(StyleIdx::concertPitch) ? c._concertClef : c._transposingClef;
}
#ifndef NDEBUG
//---------------------------------------------------------
// dumpClef
//---------------------------------------------------------
void Staff::dumpClefs(const char* title) const
{
qDebug("dump clefs (%zd): %s", clefs.size(), title);
for (auto& i : clefs) {
qDebug(" %d: %d %d", i.first, int(i.second._concertClef), int(i.second._transposingClef));
}
}
//---------------------------------------------------------
// dumpKeys
//---------------------------------------------------------
void Staff::dumpKeys(const char* title) const
{
qDebug("dump keys (%zd): %s", _keys.size(), title);
for (auto& i : _keys) {
qDebug(" %d: %d", i.first, int(i.second.key()));
}
}
//---------------------------------------------------------
// dumpTimeSigs
//---------------------------------------------------------
void Staff::dumpTimeSigs(const char* title) const
{
qDebug("dump timesig size (%zd) staffIdx %d: %s", timesigs.size(), idx(), title);
for (auto& i : timesigs) {
qDebug(" %d: %d/%d", i.first, i.second->sig().numerator(), i.second->sig().denominator());
}
}
#endif
//---------------------------------------------------------
// setClef
//---------------------------------------------------------
void Staff::setClef(Clef* clef)
{
if (clef->generated())
return;
int tick = clef->segment()->tick();
for (Segment* s = clef->segment()->next(); s && s->tick() == tick; s = s->next()) {
if (s->segmentType() == Segment::Type::Clef && s->element(clef->track())) {
// adding this clef has no effect on the clefs list
return;
}
}
clefs.setClef(clef->segment()->tick(), clef->clefTypeList());
DUMP_CLEFS("setClef");
}
//---------------------------------------------------------
// removeClef
//---------------------------------------------------------
void Staff::removeClef(Clef* clef)
{
if (clef->generated())
return;
int tick = clef->segment()->tick();
for (Segment* s = clef->segment()->next(); s && s->tick() == tick; s = s->next()) {
if (s->segmentType() == Segment::Type::Clef && s->element(clef->track())) {
// removal of this clef has no effect on the clefs list
return;
}
}
clefs.erase(clef->segment()->tick());
for (Segment* s = clef->segment()->prev(); s && s->tick() == tick; s = s->prev()) {
if (s->segmentType() == Segment::Type::Clef
&& s->element(clef->track())
&& !s->element(clef->track())->generated()) {
// a previous clef at the same tick position gets valid
clefs.setClef(tick, static_cast<Clef*>(s->element(clef->track()))->clefTypeList());
break;
}
}
DUMP_CLEFS("removeClef");
}
//---------------------------------------------------------
// timeStretch
//---------------------------------------------------------
Fraction Staff::timeStretch(int tick) const
{
TimeSig* timesig = timeSig(tick);
return timesig == 0 ? Fraction(1,1) : timesig->stretch();
}
//---------------------------------------------------------
// timeSig
// lookup time signature before or at tick
//---------------------------------------------------------
TimeSig* Staff::timeSig(int tick) const
{
auto i = timesigs.upper_bound(tick);
if (i != timesigs.begin())
--i;
else if (tick < i->first)
return 0;
return (i == timesigs.end()) ? 0 : i->second;
}
//---------------------------------------------------------
// group
//---------------------------------------------------------
const Groups& Staff::group(int tick) const
{
TimeSig* ts = timeSig(tick);
if (ts) {
if (!ts->groups().empty())
return ts->groups();
return Groups::endings(ts->sig());
}
Measure* m = score()->tick2measure(tick);
return Groups::endings(m ? m->timesig() : Fraction(4,4));
}
//---------------------------------------------------------
// addTimeSig
//---------------------------------------------------------
void Staff::addTimeSig(TimeSig* timesig)
{
if (timesig->segment()->segmentType() == Segment::Type::TimeSig)
timesigs[timesig->segment()->tick()] = timesig;
// dumpTimeSigs("after addTimeSig");
}
//---------------------------------------------------------
// removeTimeSig
//---------------------------------------------------------
void Staff::removeTimeSig(TimeSig* timesig)
{
if (timesig->segment()->segmentType() == Segment::Type::TimeSig)
timesigs.erase(timesig->segment()->tick());
// dumpTimeSigs("after removeTimeSig");
}
//---------------------------------------------------------
// clearTimeSig
//---------------------------------------------------------
void Staff::clearTimeSig()
{
timesigs.clear();
}
//---------------------------------------------------------
// Staff::keySigEvent
//
// locates the key sig currently in effect at tick
//---------------------------------------------------------
KeySigEvent Staff::keySigEvent(int tick) const
{
return _keys.key(tick);
}
//---------------------------------------------------------
// setKey
//---------------------------------------------------------
void Staff::setKey(int tick, KeySigEvent k)
{
_keys.setKey(tick, k);
// dumpKeys("setKey");
}
//---------------------------------------------------------
// removeKey
//---------------------------------------------------------
void Staff::removeKey(int tick)
{
_keys.erase(tick);
// dumpKeys("removeKey");
}
//---------------------------------------------------------
// prevkey
//---------------------------------------------------------
KeySigEvent Staff::prevKey(int tick) const
{
return _keys.prevKey(tick);
}
//---------------------------------------------------------
// Staff::nextKeyTick
//
// return the tick at which the key sig after tick is located
// return 0, if no such a key sig
//---------------------------------------------------------
int Staff::nextKeyTick(int tick) const
{
return _keys.nextKeyTick(tick);
}
//---------------------------------------------------------
// Staff::currentKeyTick
//
// return the tick position of the key currently
// in effect at tick
// return 0, if no such a key sig
//---------------------------------------------------------
int Staff::currentKeyTick(int tick) const
{
return _keys.currentKeyTick(tick);
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Staff::write(Xml& xml) const
{
int idx = score()->staffIdx(this);
xml.stag(QString("Staff id=\"%1\"").arg(idx + 1));
if (linkedStaves()) {
Score* s = score();
if (s->parentScore())
s = s->parentScore();
foreach(Staff* staff, linkedStaves()->staves()) {
if ((staff->score() == s) && (staff != this))
xml.tag("linkedTo", s->staffIdx(staff) + 1);
}
}
// for copy/paste we need to know the actual transposition
if (xml.clipboardmode) {
Interval v = part()->instrument()->transpose();
if (v.diatonic)
xml.tag("transposeDiatonic", v.diatonic);
if (v.chromatic)
xml.tag("transposeChromatic", v.chromatic);
}
_staffType.write(xml);
ClefTypeList ct = _defaultClefType;
if (ct._concertClef == ct._transposingClef) {
if (ct._concertClef != ClefType::G)
xml.tag("defaultClef", ClefInfo::tag(ct._concertClef));
}
else {
xml.tag("defaultConcertClef", ClefInfo::tag(ct._concertClef));
xml.tag("defaultTransposingClef", ClefInfo::tag(ct._transposingClef));
}
if (small() && !xml.excerptmode) // switch small staves to normal ones when extracting part
xml.tag("small", small());
if (invisible())
xml.tag("invisible", invisible());
if (neverHide())
xml.tag("neverHide", neverHide());
if (showIfEmpty())
xml.tag("showIfSystemEmpty", showIfEmpty());
if (_hideSystemBarLine)
xml.tag("hideSystemBarLine", _hideSystemBarLine);
for (const BracketItem& i : _brackets)
xml.tagE(QString("bracket type=\"%1\" span=\"%2\"").arg((signed char)(i._bracket)).arg(i._bracketSpan));
// for economy and consistency, only output "from" and "to" attributes if different from default
int defaultLineFrom = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_FROM : 0);
int defaultLineTo;
if (_barLineSpan == 0) // if no bar line at all
defaultLineTo = _barLineTo; // whatever the current spanTo is, use as default
else { // if some bar line, default is the default for span target staff
int targetStaffIdx = idx + _barLineSpan - 1;
if (targetStaffIdx >= score()->nstaves()) {
qFatal("bad _barLineSpan %d for staff %d (nstaves %d)",
_barLineSpan, idx, score()->nstaves());
}
int targetStaffLines = score()->staff(targetStaffIdx)->lines();
defaultLineTo = (targetStaffLines == 1 ? BARLINE_SPAN_1LINESTAFF_TO : (targetStaffLines-1) * 2);
}
if (_barLineSpan != 1 || _barLineFrom != defaultLineFrom || _barLineTo != defaultLineTo) {
if(_barLineFrom != defaultLineFrom || _barLineTo != defaultLineTo)
xml.tag(QString("barLineSpan from=\"%1\" to=\"%2\"").arg(_barLineFrom).arg(_barLineTo), _barLineSpan);
else
xml.tag("barLineSpan", _barLineSpan);
}
if (_userDist != 0.0)
xml.tag("distOffset", _userDist / score()->spatium());
writeProperty(xml, P_ID::MAG);
writeProperty(xml, P_ID::COLOR);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Staff::read(XmlReader& e)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "type") { // obsolete
int staffTypeIdx = e.readInt();
qDebug("obsolete: Staff::read staffTypeIdx %d", staffTypeIdx);
_staffType = *StaffType::preset(StaffTypes(staffTypeIdx));
// set default barLineFrom and barLineTo according to staff type num. of lines
// (1-line staff bar lines are special)
_barLineFrom = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_FROM : 0);
_barLineTo = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_TO : (lines() - 1) * 2);
}
else if (tag == "StaffType") {
_staffType.read(e);
// set default barLineFrom and barLineTo according to staff type num. of lines
// (1-line staff bar lines are special)
_barLineFrom = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_FROM : 0);
_barLineTo = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_TO : (lines() - 1) * 2);
}
else if (tag == "defaultClef") { // sets both default transposing and concert clef
QString val(e.readElementText());
ClefType ct = Clef::clefType(val);
setDefaultClefType(ClefTypeList(ct, ct));
}
else if (tag == "defaultConcertClef") {
QString val(e.readElementText());
setDefaultClefType(ClefTypeList(Clef::clefType(val), defaultClefType()._transposingClef));
}
else if (tag == "defaultTransposingClef") {
QString val(e.readElementText());
setDefaultClefType(ClefTypeList(defaultClefType()._concertClef, Clef::clefType(val)));
}
else if (tag == "small")
setSmall(e.readInt());
else if (tag == "invisible")
setInvisible(e.readInt());
else if (tag == "neverHide")
setNeverHide(e.readInt());
else if (tag == "showIfSystemEmpty")
setShowIfEmpty(e.readInt());
else if (tag == "hideSystemBarLine")
_hideSystemBarLine = e.readInt();
else if (tag == "keylist")
_keys.read(e, _score);
else if (tag == "bracket") {
BracketItem b;
b._bracket = BracketType(e.intAttribute("type", -1));
b._bracketSpan = e.intAttribute("span", 0);
_brackets.append(b);
e.readNext();
}
else if (tag == "barLineSpan") {
// WARNING: following statement assumes number of staff lines to be correctly set
// must read <StaffType> before reading the <barLineSpan>
int defaultSpan = (lines() == 1 ? BARLINE_SPAN_1LINESTAFF_FROM : 0);
_barLineFrom = e.intAttribute("from", defaultSpan);
// the proper default SpanTo depends upon the barLineSpan
// as we do not know it yet, set a generic (UNKNOWN) default
defaultSpan = UNKNOWN_BARLINE_TO;
_barLineTo = e.intAttribute("to", defaultSpan);
// ready to read the main value...
_barLineSpan = e.readInt();
//...and to adjust the SpanTo value if the source did not provide an explicit value
// if no bar line or single staff span, set _barLineTo to this staff height
// if span to another staff (yet to be read), leave as unknown
// (Score::read() will retrieve the correct height of the target staff)
if (_barLineTo == UNKNOWN_BARLINE_TO && _barLineSpan <= 1)
_barLineTo = lines() == 1 ? BARLINE_SPAN_1LINESTAFF_TO : (lines() - 1) * 2;
}
else if (tag == "distOffset")
_userDist = e.readDouble() * score()->spatium();
else if (tag == "mag")
_userMag = e.readDouble(0.1, 10.0);
else if (tag == "linkedTo") {
int v = e.readInt() - 1;
//
// if this is an excerpt, link staff to parentScore()
//
if (score()->parentScore()) {
Staff* st = score()->parentScore()->staff(v);
if (st)
linkTo(st);
else {
qDebug("staff %d not found in parent", v);
}
}
else {
int idx = score()->staffIdx(this);
if (v >= 0 && v < idx)
linkTo(score()->staff(v));
}
}
else if (tag == "color")
_color = e.readColor();
else if (tag == "transposeDiatonic")
e.setTransposeDiatonic(e.readInt());
else if (tag == "transposeChromatic")
e.setTransposeChromatic(e.readInt());
else
e.unknown();
}
}
//---------------------------------------------------------
// height
//---------------------------------------------------------
qreal Staff::height() const
{
return (lines() == 1 ? 2 : lines()-1) * spatium() * _staffType.lineDistance().val();
}
//---------------------------------------------------------
// spatium
//---------------------------------------------------------
qreal Staff::spatium() const
{
return _score->spatium() * mag();
}
//---------------------------------------------------------
// mag
//---------------------------------------------------------
qreal Staff::mag() const
{
return (_small ? score()->styleD(StyleIdx::smallStaffMag) : 1.0) * userMag();
}
//---------------------------------------------------------
// swing
//---------------------------------------------------------
SwingParameters Staff::swing(int tick) const
{
SwingParameters sp;
int swingUnit = 0;
QString unit = score()->styleSt(StyleIdx::swingUnit);
int swingRatio = score()->styleI(StyleIdx::swingRatio);
if (unit == TDuration(TDuration::DurationType::V_EIGHTH).name()) {
swingUnit = MScore::division / 2;
}
else if (unit == TDuration(TDuration::DurationType::V_16TH).name())
swingUnit = MScore::division / 4;
else if (unit == TDuration(TDuration::DurationType::V_ZERO).name())
swingUnit = 0;
sp.swingRatio = swingRatio;
sp.swingUnit = swingUnit;
if (_swingList.isEmpty())
return sp;
QMap<int, SwingParameters>::const_iterator i = _swingList.upperBound(tick);
if (i == _swingList.begin())
return sp;
--i;
return i.value();
}
//---------------------------------------------------------
// channel
//---------------------------------------------------------
int Staff::channel(int tick, int voice) const
{
if (_channelList[voice].isEmpty())
return 0;
QMap<int, int>::const_iterator i = _channelList[voice].upperBound(tick);
if (i == _channelList[voice].begin())
return 0;
--i;
return i.value();
}
//---------------------------------------------------------
// lines
//---------------------------------------------------------
int Staff::lines() const
{
return _staffType.lines();
}
//---------------------------------------------------------
// setLines
//---------------------------------------------------------
void Staff::setLines(int val)
{
if (val == lines())
return;
_staffType.setLines(val); // TODO: make undoable
}
//---------------------------------------------------------
// lineDistance
//---------------------------------------------------------
qreal Staff::lineDistance() const
{
return _staffType.lineDistance().val();
}
//---------------------------------------------------------
// slashStyle
//---------------------------------------------------------
bool Staff::slashStyle() const
{
return _staffType.slashStyle();
}
//---------------------------------------------------------
// setSlashStyle
//---------------------------------------------------------
void Staff::setSlashStyle(bool val)
{
_staffType.setSlashStyle(val);
}
//---------------------------------------------------------
// linkTo
//---------------------------------------------------------
void Staff::linkTo(Staff* staff)
{
if (!_linkedStaves) {
if (staff->linkedStaves()) {
_linkedStaves = staff->linkedStaves();
}
else {
_linkedStaves = new LinkedStaves;
_linkedStaves->add(staff);
staff->setLinkedStaves(_linkedStaves);
}
_linkedStaves->add(this);
}
else {
_linkedStaves->add(staff);
if (!staff->linkedStaves())
staff->_linkedStaves = _linkedStaves;
}
}
//---------------------------------------------------------
// unlink
//---------------------------------------------------------
void Staff::unlink(Staff* staff)
{
if (!_linkedStaves)
return;
if (!_linkedStaves->staves().contains(staff))
return;
_linkedStaves->remove(staff);
if (_linkedStaves->staves().size() <= 1) {
delete _linkedStaves;
_linkedStaves = 0;
}
staff->_linkedStaves = 0;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void LinkedStaves::add(Staff* staff)
{
if (!_staves.contains(staff))
_staves.append(staff);
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void LinkedStaves::remove(Staff* staff)
{
_staves.removeOne(staff);
}
//---------------------------------------------------------
// isLinked
/// return true if staff is different and
/// linked to this staff
//---------------------------------------------------------
bool Staff::isLinked(Staff* staff)
{
if (staff == this || !_linkedStaves)
return false;
for(Staff* s : _linkedStaves->staves()) {
if(s == staff)
return true;
}
return false;
}
//---------------------------------------------------------
// primaryStaff
/// if there are linked staves, the primary staff is
/// the one who is played back and it's not a tab staff
/// because we don't have enough information to play
/// e.g ornaments. NOTE: it's not necessarily the top staff!
//---------------------------------------------------------
bool Staff::primaryStaff() const
{
if (!_linkedStaves)
return true;
QList<Staff*> s;
QList<Staff*> ss;
foreach(Staff* staff, _linkedStaves->staves()) {
if (staff->score() == score()) {
s.append(staff);
if (!staff->isTabStaff())
ss.append(staff);
}
}
if (s.size() == 1) // the linked staves are in different scores
return s.front() == this;
else // return a non tab linked staff in this score
return ss.front() == this;
}
//---------------------------------------------------------
// setStaffType
//---------------------------------------------------------
void Staff::setStaffType(const StaffType* st)
{
if (_staffType == *st)
return;
int linesOld = lines();
int linesNew = st->lines();
_staffType = *st;
if (linesNew != linesOld) {
int sIdx = score()->staffIdx(this);
if (sIdx < 0) { // staff does not belong to score (yet?)
if (linesNew == 1) { // 1-line staves have special bar lines
_barLineFrom = BARLINE_SPAN_1LINESTAFF_FROM;
_barLineTo = BARLINE_SPAN_1LINESTAFF_TO;
}
else { // set default barLineFrom/to (from first to last staff line)
_barLineFrom = 0;
_barLineTo = (linesNew-1)*2;
}
}
else // update barLineFrom/To in whole score context
score()->updateBarLineSpans(sIdx, linesOld, linesNew /*, true*/);
}
}
//---------------------------------------------------------
// init
//---------------------------------------------------------
void Staff::init(const InstrumentTemplate* t, const StaffType* staffType, int cidx)
{
// set staff-type-independent parameters
if (cidx > MAX_STAVES) {
setSmall(false);
}
else {
setSmall(t->smallStaff[cidx]);
setBracket(0, t->bracket[cidx]);
setBracketSpan(0, t->bracketSpan[cidx]);
setBarLineSpan(t->barlineSpan[cidx]);
}
const StaffType* pst = staffType ? staffType : t->staffTypePreset;
if (!pst)
pst = StaffType::getDefaultPreset(t->staffGroup);
setStaffType(pst);
setDefaultClefType(t->clefType(cidx));
}
//---------------------------------------------------------
// init
//---------------------------------------------------------
void Staff::init(const Staff* s)
{
setStaffType(s->staffType());
setDefaultClefType(s->defaultClefType());
setSmall(s->small());
_brackets = s->_brackets;
_barLineSpan = s->_barLineSpan;
_barLineFrom = s->_barLineFrom;
_barLineTo = s->_barLineTo;
_invisible = s->_invisible;
_neverHide = s->_neverHide;
_showIfEmpty = s->_showIfEmpty;
_hideSystemBarLine = s->_hideSystemBarLine;
_color = s->_color;
_userDist = s->_userDist;
_userMag = s->_userMag;
}
//---------------------------------------------------------
// initFromStaffType
//---------------------------------------------------------
void Staff::initFromStaffType(const StaffType* staffType)
{
// get staff type if given (if none, get default preset for default staff group)
if (!staffType)
staffType = StaffType::getDefaultPreset(StaffGroup::STANDARD);
// use selected staff type
setStaffType(staffType);
}
//---------------------------------------------------------
// spatiumChanged
//---------------------------------------------------------
void Staff::spatiumChanged(qreal oldValue, qreal newValue)
{
_userDist = (_userDist / oldValue) * newValue;
}
//---------------------------------------------------------
// show
//---------------------------------------------------------
bool Staff::show() const
{
return _part->show();
}
//---------------------------------------------------------
// genKeySig
//---------------------------------------------------------
bool Staff::genKeySig()
{
if (_staffType.group() == StaffGroup::TAB)
return false;
else
return _staffType.genKeysig();
}
//---------------------------------------------------------
// showLedgerLines
//---------------------------------------------------------
bool Staff::showLedgerLines()
{
if (_staffType.group() == StaffGroup::TAB)
return false;
else
return _staffType.showLedgerLines();
}
//---------------------------------------------------------
// updateOttava
//---------------------------------------------------------
void Staff::updateOttava()
{
int staffIdx = idx();
_pitchOffsets.clear();
for (auto i : score()->spanner()) {
const Spanner* s = i.second;
if (s->type() == Element::Type::OTTAVA && s->staffIdx() == staffIdx) {
const Ottava* o = static_cast<const Ottava*>(s);
_pitchOffsets.setPitchOffset(o->tick(), o->pitchShift());
_pitchOffsets.setPitchOffset(o->tick2(), 0);
}
}
}
//---------------------------------------------------------
// undoSetColor
//---------------------------------------------------------
void Staff::undoSetColor(const QColor& /*val*/)
{
// score()->undoChangeProperty(this, P_ID::COLOR, val);
}
//---------------------------------------------------------
// insertTime
//---------------------------------------------------------
void Staff::insertTime(int tick, int len)
{
if (len == 0)
return;
// move all keys and clefs >= tick
if (len < 0) {
// remove entries between tickpos >= tick and tickpos < (tick+len)
_keys.erase(_keys.lower_bound(tick), _keys.lower_bound(tick-len));
clefs.erase(clefs.lower_bound(tick), clefs.lower_bound(tick-len));
}
KeyList kl2;
for (auto i = _keys.lower_bound(tick); i != _keys.end();) {
KeySigEvent kse = i->second;
int tick = i->first;
_keys.erase(i++);
kl2[tick + len] = kse;
}
_keys.insert(kl2.begin(), kl2.end());
// check if there is a clef at the end of measure
// before tick
Clef* clef = 0;
Measure* m = _score->tick2measure(tick);
if (m && (m->tick() == tick) && (m->prevMeasure())) {
m = m->prevMeasure();
Segment* s = m->findSegment(Segment::Type::Clef, tick);
if (s) {
int track = idx() * VOICES;
clef = static_cast<Clef*>(s->element(track));
}
}
ClefList cl2;
for (auto i = clefs.lower_bound(tick); i != clefs.end();) {
ClefTypeList ctl = i->second;
int t = i->first;
if (clef && tick == t) {
++i;
continue;
}
clefs.erase(i++);
cl2.setClef(t + len, ctl);
}
clefs.insert(cl2.begin(), cl2.end());
// check if there is a clef at the end of measure
// before tick: do not remove from clefs list
if (clef)
setClef(clef);
updateOttava();
DUMP_CLEFS(" insertTime");
}
//---------------------------------------------------------
// staffList
// return list of linked staves
//---------------------------------------------------------
QList<Staff*> Staff::staffList() const
{
QList<Staff*> staffList;
if (_linkedStaves)
staffList = _linkedStaves->staves();
else
staffList.append(const_cast<Staff*>(this));
return staffList;
}
//---------------------------------------------------------
// setBarLineTo
//---------------------------------------------------------
void Staff::setBarLineTo(int val)
{
_barLineTo = val;
}
//---------------------------------------------------------
// rstaff
//---------------------------------------------------------
int Staff::rstaff() const
{
return _part->staves()->indexOf((Staff*)this, 0);
}
//---------------------------------------------------------
// isTop
//---------------------------------------------------------
bool Staff::isTop() const
{
return _part->staves()->front() == this;
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant Staff::getProperty(P_ID id) const
{
switch (id) {
case P_ID::MAG:
return userMag();
case P_ID::COLOR:
return color();
case P_ID::SMALL:
return small();
default:
qDebug("Staff::getProperty: unhandled id");
return QVariant();
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Staff::setProperty(P_ID id, const QVariant& v)
{
switch (id) {
case P_ID::MAG: {
double oldVal = mag();
setUserMag(v.toDouble());
scaleChanged(oldVal, mag());
}
break;
case P_ID::COLOR:
setColor(v.value<QColor>());
break;
case P_ID::SMALL: {
double oldVal = mag();
setSmall(v.toBool());
scaleChanged(oldVal, mag());
}
break;
default:
qDebug("Staff::setProperty: unhandled id");
break;
}
score()->setLayoutAll(true);
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Staff::propertyDefault(P_ID id) const
{
switch (id) {
case P_ID::MAG:
return 1.0;
case P_ID::COLOR:
return QColor(Qt::black);
case P_ID::SMALL:
return false;
default:
return QVariant();
}
}
//---------------------------------------------------------
// scaleChanged
//---------------------------------------------------------
void Staff::scaleChanged(double oldVal, double newVal)
{
int staffIdx = idx();
int startTrack = staffIdx * VOICES;
int endTrack = startTrack + VOICES;
for (Segment* s = score()->firstSegment(); s; s = s->next1()) {
for (Element* e : s->annotations())
e->localSpatiumChanged(oldVal, newVal);
for (int track = startTrack; track < endTrack; ++track) {
if (s->element(track))
s->element(track)->localSpatiumChanged(oldVal, newVal);
}
}
for (auto i : score()->spanner()) {
Spanner* spanner = i.second;
if (spanner->staffIdx() == staffIdx) {
for (auto k : spanner->spannerSegments())
k->localSpatiumChanged(oldVal, newVal);
}
}
}
}
|