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
|
//=============================================================================
// 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
//=============================================================================
/**
\file
Implementation of classes SysStaff and System.
*/
#include "system.h"
#include "measure.h"
#include "segment.h"
#include "score.h"
#include "sig.h"
#include "key.h"
#include "xml.h"
#include "clef.h"
#include "text.h"
#include "navigate.h"
#include "select.h"
#include "staff.h"
#include "part.h"
#include "page.h"
#include "style.h"
#include "bracket.h"
#include "mscore.h"
#include "barline.h"
#include "lyrics.h"
#include "system.h"
#include "box.h"
#include "chordrest.h"
#include "iname.h"
#include "spanner.h"
namespace Ms {
//---------------------------------------------------------
// y
//---------------------------------------------------------
qreal SysStaff::y() const
{
return _bbox.y() + _yOff;
}
//---------------------------------------------------------
// setYOff
//---------------------------------------------------------
void SysStaff::setYOff(qreal offset)
{
_yOff = offset;
}
//---------------------------------------------------------
// SysStaff
//---------------------------------------------------------
SysStaff::SysStaff()
{
_yOff = 0.0;
idx = 0;
_show = true;
}
//---------------------------------------------------------
// ~SysStaff
//---------------------------------------------------------
SysStaff::~SysStaff()
{
qDeleteAll(instrumentNames);
}
//---------------------------------------------------------
// System
//---------------------------------------------------------
System::System(Score* s)
: Element(s)
{
_barLine = 0;
_leftMargin = 0.0;
_pageBreak = false;
_firstSystem = false;
_vbox = false;
_sameLine = false;
_addStretch = false;
}
//---------------------------------------------------------
// ~System
//---------------------------------------------------------
System::~System()
{
delete _barLine;
qDeleteAll(_staves);
qDeleteAll(_brackets);
}
//---------------------------------------------------------
// insertStaff
//---------------------------------------------------------
SysStaff* System::insertStaff(int idx)
{
SysStaff* staff = new SysStaff;
staff->idx = idx;
if (idx) {
// HACK: guess position
staff->rbb().setY(_staves[idx-1]->y() + 6 * spatium());
}
_staves.insert(idx, staff);
return staff;
}
//---------------------------------------------------------
// removeStaff
//---------------------------------------------------------
void System::removeStaff(int idx)
{
_staves.takeAt(idx);
}
//---------------------------------------------------------
// layout
/// Layout the System
// If first MeasureBase is a HBOX, then xo1 is the
// width of this box.
//---------------------------------------------------------
void System::layoutSystem(qreal xo1)
{
if (isVbox()) // ignore vbox
return;
static const Spatium instrumentNameOffset(1.0);
int nstaves = _staves.size();
if (nstaves != score()->nstaves())
qDebug("System::layout: nstaves %d != %d", nstaves, score()->nstaves());
//---------------------------------------------------
// find x position of staves
// create brackets
//---------------------------------------------------
qreal xoff2 = 0.0; // x offset for instrument name
int bracketLevels = 0;
for (int idx = 0; idx < nstaves; ++idx)
bracketLevels = qMax(bracketLevels, score()->staff(idx)->bracketLevels());
qreal bracketWidth[bracketLevels];
for (int i = 0; i < bracketLevels; ++i)
bracketWidth[i] = 0.0;
QList<Bracket*> bl = _brackets;
_brackets.clear();
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
Staff* s = score()->staff(staffIdx);
for (int i = 0; i < bracketLevels; ++i) {
if (s->bracket(i) == BracketType::NO_BRACKET)
continue;
int firstStaff = staffIdx;
int lastStaff = staffIdx + s->bracketSpan(i) - 1;
if (lastStaff >= nstaves)
lastStaff = nstaves - 1;
for (; firstStaff <= lastStaff; ++firstStaff) {
if (score()->staff(firstStaff)->show())
break;
}
for (; lastStaff >= firstStaff; --lastStaff) {
if (score()->staff(lastStaff)->show())
break;
}
int span = lastStaff - firstStaff + 1;
//
// do not show bracket if it only spans one
// system due to some invisible staves
//
if ((span > 1) || (s->bracketSpan(i) == span)) {
//
// this bracket is visible
//
Bracket* b = 0;
int track = staffIdx * VOICES;
for (int k = 0; k < bl.size(); ++k) {
if (bl[k]->track() == track && bl[k]->level() == i) {
b = bl.takeAt(k);
break;
}
}
if (b == 0) {
b = new Bracket(score());
b->setGenerated(true);
b->setParent(this);
b->setTrack(track);
b->setLevel(i);
score()->undoAddElement(b);
}
else
_brackets.append(b);
b->setFirstStaff(firstStaff);
b->setLastStaff(lastStaff);
b->setBracketType(s->bracket(i));
b->setSpan(s->bracketSpan(i));
bracketWidth[i] = qMax(bracketWidth[i], b->width());
}
}
if (!s->show())
continue;
for (InstrumentName* t : _staves[staffIdx]->instrumentNames) {
t->layout();
qreal w = t->width() + point(instrumentNameOffset);
if (w > xoff2)
xoff2 = w;
}
}
for (Bracket* b : bl)
score()->undoRemoveElement(b);
//---------------------------------------------------
// layout SysStaff and StaffLines
//---------------------------------------------------
// xoff2 += xo1;
_leftMargin = xoff2;
qreal bd = point(score()->styleS(StyleIdx::bracketDistance));
if ( _brackets.size() > 0) {
for (int i = 0; i < bracketLevels; ++i)
_leftMargin += bracketWidth[i] + bd;
}
int nVisible = 0;
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
SysStaff* s = _staves[staffIdx];
Staff* staff = score()->staff(staffIdx);
if (!staff->show() || !s->show()) {
s->setbbox(QRectF());
continue;
}
++nVisible;
qreal staffMag = staff->mag();
qreal h;
if (staff->lines() == 1)
h = 2;
else
h = (staff->lines()-1) * staff->lineDistance();
h = h * staffMag * spatium();
s->bbox().setRect(_leftMargin + xo1, 0.0, 0.0, h);
}
if ((nVisible > 1 && score()->styleB(StyleIdx::startBarlineMultiple)) || (nVisible <= 1 && score()->styleB(StyleIdx::startBarlineSingle))) {
if (_barLine == 0) {
BarLine* bl = new BarLine(score());
bl->setParent(this);
bl->setTrack(0);
bl->setGenerated(true);
score()->undoAddElement(bl);
}
}
else if (_barLine)
score()->undoRemoveElement(_barLine);
if (_barLine)
_barLine->rxpos() = _leftMargin + xo1;
//---------------------------------------------------
// layout brackets
//---------------------------------------------------
for (Bracket* b : _brackets) {
qreal xo = -xo1;
for (const Bracket* b2 : _brackets) {
if (b->level() > b2->level() &&
((b->firstStaff() >= b2->firstStaff() && b->firstStaff() <= b2->lastStaff()) ||
(b->lastStaff() >= b2->firstStaff() && b->lastStaff() <= b2->lastStaff())))
xo += b2->width() + bd;
}
b->rxpos() = _leftMargin - xo - b->width();
}
//---------------------------------------------------
// layout instrument names x position
//---------------------------------------------------
int idx = 0;
for (const Part* p : score()->parts()) {
SysStaff* s = staff(idx);
if (s->show() && p->show()) {
for (InstrumentName* t : s->instrumentNames) {
switch (t->textStyle().align() & AlignmentFlags::HMASK) {
case int(AlignmentFlags::LEFT):
t->rxpos() = 0;
break;
case int(AlignmentFlags::HCENTER):
t->rxpos() = (xoff2 - point(instrumentNameOffset) + xo1) * .5;
break;
case int(AlignmentFlags::RIGHT):
default:
t->rxpos() = xoff2 - point(instrumentNameOffset) + xo1;
break;
}
t->rxpos() += t->textStyle().offset(t->spatium()).x();
}
}
idx += p->nstaves();
}
}
//---------------------------------------------------------
// layout2
// called after measure layout
// adjusts staff distance
//---------------------------------------------------------
void System::layout2()
{
if (isVbox()) // ignore vbox
return;
int nstaves = _staves.size();
qreal _spatium = spatium();
qreal y = 0.0;
int lastStaffIdx = 0; // last visible staff
int firstStaffIdx = -1;
qreal lastStaffDistanceDown = 0.0;
for (int staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
Staff* staff = score()->staff(staffIdx);
StyleIdx downDistance;
qreal userDist = 0.0;
if ((staffIdx + 1) == nstaves) {
//
// last staff in system
//
MeasureBase* mb = ml.last();
bool nextMeasureIsVBOX = false;
if (mb->next()) {
Element::Type type = mb->next()->type();
if (type == Element::Type::VBOX || type == Element::Type::TBOX || type == Element::Type::FBOX)
nextMeasureIsVBOX = true;
}
downDistance = nextMeasureIsVBOX ? StyleIdx::systemFrameDistance : StyleIdx::minSystemDistance;
}
else if (staff->rstaff() < (staff->part()->staves()->size()-1)) {
//
// staff is not last staff in a part
//
downDistance = StyleIdx::akkoladeDistance;
userDist = score()->staff(staffIdx + 1)->userDist();
}
else {
downDistance = StyleIdx::staffDistance;
userDist = score()->staff(staffIdx + 1)->userDist();
}
SysStaff* s = _staves[staffIdx];
qreal distDown = score()->styleS(downDistance).val() * _spatium + userDist;
qreal nominalDistDown = distDown;
qreal distUp = 0.0;
int n = ml.size();
for (int i = 0; i < n; ++i) {
MeasureBase* m = ml.at(i);
distDown = qMax(distDown, m->distanceDown(staffIdx));
distUp = qMax(distUp, m->distanceUp(staffIdx));
}
s->setDistanceDown(distDown);
s->setDistanceUp(distUp);
if (!staff->show() || !s->show()) {
s->setbbox(QRectF()); // already done in layout() ?
continue;
}
qreal sHeight = staff->height();
qreal dup = staffIdx == 0 ? 0.0 : s->distanceUp();
// one-line staves get additional padding for their bbox
qreal off = staff->lines() == 1 ? _spatium * staff->mag() : 0.0;
s->setYOff(off);
s->bbox().setRect(_leftMargin, y + dup, width() - _leftMargin, sHeight);
y += dup + sHeight + s->distanceDown();
lastStaffIdx = staffIdx;
lastStaffDistanceDown = distDown - nominalDistDown;
if (firstStaffIdx == -1)
firstStaffIdx = staffIdx;
}
if (firstStaffIdx == -1)
firstStaffIdx = 0;
qreal systemHeight = staff(lastStaffIdx)->bbox().bottom();
if (lastStaffIdx < nstaves - 1)
systemHeight += lastStaffDistanceDown;
setHeight(systemHeight);
int n = ml.size();
for (int i = 0; i < n; ++i) {
MeasureBase* m = ml.at(i);
if (m->type() == Element::Type::MEASURE) {
// note that the factor 2 * _spatium must be corrected for when exporting
// system distance in MusicXML (issue #24733)
m->bbox().setRect(0.0, -_spatium, m->width(), systemHeight + 2 * _spatium);
}
else if (m->type() == Element::Type::HBOX) {
m->bbox().setRect(0.0, 0.0, m->width(), systemHeight);
static_cast<HBox*>(m)->layout2();
}
}
if (_barLine) {
_barLine->setTrack(firstStaffIdx * VOICES);
_barLine->setSpan(lastStaffIdx - firstStaffIdx + 1);
if (score()->staff(firstStaffIdx)->lines() == 1)
_barLine->setSpanFrom(BARLINE_SPAN_1LINESTAFF_FROM);
else
_barLine->setSpanFrom(0);
int spanTo = (score()->staff(lastStaffIdx)->lines() == 1) ?
BARLINE_SPAN_1LINESTAFF_TO :
(score()->staff(lastStaffIdx)->lines()-1)*2;
_barLine->setSpanTo(spanTo);
_barLine->layout();
}
//---------------------------------------------------
// layout brackets vertical position
//---------------------------------------------------
n = _brackets.size();
for (int i = 0; i < n; ++i) {
Bracket* b = _brackets.at(i);
int staffIdx1 = b->firstStaff();
int staffIdx2 = b->lastStaff();
qreal sy = 0; // assume bracket not visible
qreal ey = 0;
// if start staff not visible, try next staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx1]->show())
++staffIdx1;
// if end staff not visible, try prev staff
while (staffIdx1 <= staffIdx2 && !_staves[staffIdx2]->show())
--staffIdx2;
// the bracket will be shown IF:
// it spans at least 2 visible staves (staffIdx1 < staffIdx2) OR
// it spans just one visible staff (staffIdx1 == staffIdx2) but it is required to do so
// (the second case happens at least when the bracket is initially dropped)
bool notHidden = (staffIdx1 < staffIdx2) || (b->span() == 1 && staffIdx1 == staffIdx2);
if (notHidden) { // set vert. pos. and height to visible spanned staves
sy = _staves[staffIdx1]->bbox().top();
ey = _staves[staffIdx2]->bbox().bottom();
}
b->rypos() = sy;
b->setHeight(ey - sy);
b->layout();
}
//---------------------------------------------------
// layout instrument names
//---------------------------------------------------
int staffIdx = 0;
n = score()->parts().size();
for (Part* p : score()->parts()) {
SysStaff* s = staff(staffIdx);
SysStaff* s2;
int nstaves = p->nstaves();
if (s->show()) {
for (InstrumentName* t : s->instrumentNames) {
//
// override Text->layout()
//
qreal y1, y2;
switch (t->layoutPos()) {
default:
case 0: // center at part
y1 = s->bbox().top();
s2 = staff(staffIdx);
for (int i = staffIdx + nstaves - 1; i > 0; --i) {
SysStaff* s = staff(i);
if (s->show()) {
s2 = s;
break;
}
}
y2 = s2->bbox().bottom();
break;
case 1: // center at first staff
y1 = s->bbox().top();
y2 = s->bbox().bottom();
break;
// TODO:
// sort out invisible staves
case 2: // center between first and second staff
y1 = s->bbox().top();
y2 = staff(staffIdx + 1)->bbox().bottom();
break;
case 3: // center at second staff
y1 = staff(staffIdx + 1)->bbox().top();
y2 = staff(staffIdx + 1)->bbox().bottom();
break;
case 4: // center between first and second staff
y1 = staff(staffIdx + 1)->bbox().top();
y2 = staff(staffIdx + 2)->bbox().bottom();
break;
case 5: // center at third staff
y1 = staff(staffIdx + 2)->bbox().top();
y2 = staff(staffIdx + 2)->bbox().bottom();
break;
}
t->rypos() = y1 + (y2 - y1) * .5 + t->textStyle().offset(t->spatium()).y();
}
}
staffIdx += nstaves;
}
}
//---------------------------------------------------------
/// clear
/// Clear layout of System
//---------------------------------------------------------
void System::clear()
{
ml.clear();
foreach (SpannerSegment* ss, _spannerSegments) {
// qDebug("System::clear %s", ss->name());
if (ss->system() == this)
ss->setParent(0); // assume parent() is System
}
_spannerSegments.clear();
_vbox = false;
_firstSystem = false;
_pageBreak = false;
}
//---------------------------------------------------------
// setInstrumentNames
//---------------------------------------------------------
void System::setInstrumentNames(bool longName)
{
//
// remark: add/remove instrument names is not undo/redoable
// as add/remove of systems is not undoable
//
if (isVbox()) // ignore vbox
return;
if (!score()->showInstrumentNames()
|| (score()->styleB(StyleIdx::hideInstrumentNameIfOneInstrument) && score()->parts().size() == 1)) {
for (int staffIdx = 0; staffIdx < score()->nstaves(); ++staffIdx) {
SysStaff* staff = _staves[staffIdx];
foreach(InstrumentName* t, staff->instrumentNames)
score()->removeElement(t);
}
return;
}
// TODO: ml is normally empty here, so we are unable to retrieve tick
// thus, staff name does not reflect current instrument
int tick = ml.isEmpty() ? 0 : ml.front()->tick();
for (int staffIdx = 0; staffIdx < score()->nstaves(); ++staffIdx) {
SysStaff* staff = _staves[staffIdx];
Staff* s = score()->staff(staffIdx);
if (!s->isTop() || !s->show()) {
foreach(InstrumentName* t, staff->instrumentNames)
score()->removeElement(t);
continue;
}
Part* part = s->part();
const QList<StaffName>& names = longName? part->longNames(tick) : part->shortNames(tick);
int idx = 0;
foreach(const StaffName& sn, names) {
InstrumentName* iname = staff->instrumentNames.value(idx);
if (iname == 0) {
iname = new InstrumentName(score());
//iname->setGenerated(true);
iname->setParent(this);
iname->setTrack(staffIdx * VOICES);
score()->addElement(iname);
}
// set type inconditionally in case it changed
iname->setInstrumentNameType(longName ? InstrumentNameType::LONG : InstrumentNameType::SHORT);
iname->setXmlText(sn.name());
iname->setLayoutPos(sn.pos());
++idx;
}
for (; idx < staff->instrumentNames.size(); ++idx)
score()->removeElement(staff->instrumentNames[idx]);
}
}
//---------------------------------------------------------
// y2staff
//---------------------------------------------------------
/**
Return staff number for canvas relative y position \a y
or -1 if not found.
To allow drag and drop above and below the staff, the actual y range
considered "inside" the staff is increased by "margin".
*/
int System::y2staff(qreal y) const
{
y -= pos().y();
int idx = 0;
qreal margin = spatium() * 2;
foreach (SysStaff* s, _staves) {
qreal y1 = s->bbox().top() - margin;
qreal y2 = s->bbox().bottom() + margin;
if (y >= y1 && y < y2)
return idx;
++idx;
}
return -1;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void System::add(Element* el)
{
if (!el)
return;
// qDebug("%p System::add: %p %s", this, el, el->name());
el->setParent(this);
switch(el->type()) {
case Element::Type::INSTRUMENT_NAME:
// qDebug(" staffIdx %d, staves %d", el->staffIdx(), _staves.size());
_staves[el->staffIdx()]->instrumentNames.append(static_cast<InstrumentName*>(el));
break;
case Element::Type::BEAM:
score()->addElement(el);
break;
case Element::Type::BRACKET:
{
Bracket* b = static_cast<Bracket*>(el);
int staffIdx = b->staffIdx();
int level = b->level();
if (level == -1) {
level = 0;
foreach(Bracket* bb, _brackets) {
if (staffIdx >= bb->firstStaff() && staffIdx <= bb->lastStaff())
++level;
}
b->setLevel(level);
b->setSpan(1);
}
// b->staff()->setBracket(level, b->bracketType());
// b->staff()->setBracketSpan(level, b->span());
_brackets.append(b);
}
break;
case Element::Type::MEASURE:
case Element::Type::HBOX:
case Element::Type::VBOX:
case Element::Type::TBOX:
case Element::Type::FBOX:
score()->addElement(static_cast<MeasureBase*>(el));
break;
case Element::Type::TEXTLINE_SEGMENT:
case Element::Type::HAIRPIN_SEGMENT:
case Element::Type::OTTAVA_SEGMENT:
case Element::Type::TRILL_SEGMENT:
case Element::Type::VOLTA_SEGMENT:
case Element::Type::SLUR_SEGMENT:
case Element::Type::PEDAL_SEGMENT:
case Element::Type::LYRICSLINE_SEGMENT:
case Element::Type::GLISSANDO_SEGMENT:
{
SpannerSegment* ss = static_cast<SpannerSegment*>(el);
#ifndef NDEBUG
if (_spannerSegments.contains(ss))
qDebug("System::add() %s %p already there", ss->name(), ss);
else
#endif
_spannerSegments.append(ss);
}
break;
case Element::Type::BAR_LINE:
if (_barLine)
qDebug("%p System::add(%s, %p): there is already a barline %p", this, el->name(), el, _barLine);
_barLine = static_cast<BarLine*>(el);
break;
default:
qDebug("System::add(%s) not implemented", el->name());
break;
}
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void System::remove(Element* el)
{
switch (el->type()) {
case Element::Type::INSTRUMENT_NAME:
_staves[el->staffIdx()]->instrumentNames.removeOne(static_cast<InstrumentName*>(el));
break;
case Element::Type::BEAM:
score()->removeElement(el);
break;
case Element::Type::BRACKET:
{
Bracket* b = static_cast<Bracket*>(el);
if (!_brackets.removeOne(b))
qDebug("System::remove: bracket not found");
// b->staff()->setBracket(b->level(), NO_BRACKET);
}
break;
case Element::Type::MEASURE:
case Element::Type::HBOX:
case Element::Type::VBOX:
case Element::Type::TBOX:
case Element::Type::FBOX:
score()->removeElement(el);
break;
case Element::Type::TEXTLINE_SEGMENT:
case Element::Type::HAIRPIN_SEGMENT:
case Element::Type::OTTAVA_SEGMENT:
case Element::Type::TRILL_SEGMENT:
case Element::Type::VOLTA_SEGMENT:
case Element::Type::SLUR_SEGMENT:
case Element::Type::PEDAL_SEGMENT:
case Element::Type::LYRICSLINE_SEGMENT:
case Element::Type::GLISSANDO_SEGMENT:
if (!_spannerSegments.removeOne(static_cast<SpannerSegment*>(el))) {
qDebug("System::remove: %p(%s) not found, score %p", el, el->name(), score());
Q_ASSERT(score() == el->score());
}
break;
case Element::Type::BAR_LINE:
if (_barLine == 0)
qDebug("System::remove(%s): there is no barline", el->name());
_barLine = 0;
break;
default:
qDebug("System::remove(%s) not implemented", el->name());
break;
}
}
//---------------------------------------------------------
// change
//---------------------------------------------------------
void System::change(Element* o, Element* n)
{
if (o->type() == Element::Type::VBOX || o->type() == Element::Type::HBOX || o->type() == Element::Type::TBOX || o->type() == Element::Type::FBOX) {
int idx = ml.indexOf((MeasureBase*)o);
if (idx != -1)
ml.removeAt(idx);
ml.insert(idx, (MeasureBase*)n);
score()->measures()->change((MeasureBase*)o, (MeasureBase*)n);
}
else {
remove(o);
add(n);
}
}
//---------------------------------------------------------
// snap
//---------------------------------------------------------
int System::snap(int tick, const QPointF p) const
{
foreach(const MeasureBase* m, ml) {
if (p.x() < m->x() + m->width())
return ((Measure*)m)->snap(tick, p - m->pos()); //TODO: MeasureBase
}
return ((Measure*)ml.back())->snap(tick, p-pos()); //TODO: MeasureBase
}
//---------------------------------------------------------
// snap
//---------------------------------------------------------
int System::snapNote(int tick, const QPointF p, int staff) const
{
foreach(const MeasureBase* m, ml) {
if (p.x() < m->x() + m->width())
return ((Measure*)m)->snapNote(tick, p - m->pos(), staff); //TODO: MeasureBase
}
return ((Measure*)ml.back())->snap(tick, p-pos()); // TODO: MeasureBase
}
//---------------------------------------------------------
// firstMeasure
//---------------------------------------------------------
Measure* System::firstMeasure() const
{
if (ml.isEmpty())
return 0;
for (MeasureBase* mb = ml.front(); mb; mb = mb->nextMM()) {
if (mb->system() != this)
break;
if (mb->type() != Element::Type::MEASURE)
continue;
return static_cast<Measure*>(mb);
}
return 0;
}
//---------------------------------------------------------
// lastMeasure
//---------------------------------------------------------
Measure* System::lastMeasure() const
{
if (ml.isEmpty())
return 0;
for (MeasureBase* mb = ml.back(); mb; mb = mb->prev()) {
if (mb->type() != Element::Type::MEASURE)
continue;
return static_cast<Measure*>(mb);
}
return 0;
}
//---------------------------------------------------------
// prevMeasure
//---------------------------------------------------------
MeasureBase* System::prevMeasure(const MeasureBase* m) const
{
if (m == ml.front())
return 0;
return m->prev();
}
//---------------------------------------------------------
// nextMeasure
//---------------------------------------------------------
MeasureBase* System::nextMeasure(const MeasureBase* m) const
{
if (m == ml.back())
return 0;
return m->next();
}
//---------------------------------------------------------
// searchNextLyrics
//---------------------------------------------------------
/* Lyrics line segments are now Spanner's belonging to System's: System already takes care of them
static Lyrics* searchNextLyrics(Segment* s, int staffIdx, int verse)
{
Lyrics* l = 0;
while ((s = s->next1(Segment::Type::ChordRest))) {
int strack = staffIdx * VOICES;
int etrack = strack + VOICES;
// search through all tracks of current staff looking for a lyric in specified verse
for (int track = strack; track < etrack; ++track) {
ChordRest* cr = static_cast<ChordRest*>(s->element(track));
if (cr && !cr->lyricsList().isEmpty()) {
// cr with lyrics found, but does it have a syllable in specified verse?
l = cr->lyricsList().value(verse);
if (l)
break;
}
}
if (l)
break;
}
return l;
}
//---------------------------------------------------------
// layoutLyrics
// layout lyrics separator
//---------------------------------------------------------
void System::layoutLyrics(Lyrics* l, Segment* s, int staffIdx)
{
if ((l->syllabic() == Lyrics::Syllabic::SINGLE || l->syllabic() == Lyrics::Syllabic::END) && (l->ticks() == 0)) {
l->clearSeparator();
return;
}
qreal _spatium = spatium();
const TextStyle& ts = l->textStyle();
qreal lmag = qreal(ts.size()) / 11.0;
qreal staffMag = l->staff()->mag();
if (l->ticks()) {
// melisma
Segment* seg = score()->tick2segment(l->endTick());
if (seg == 0) {
qDebug("System::layoutLyrics: no segment found for tick %d", l->endTick());
return;
}
QList<Line*>* sl = l->separatorList();
QList<System*>* systems = score()->systems();
System* s1 = this;
System* s2 = seg->measure()->system();
int sysIdx1 = systems->indexOf(s1);
int sysIdx2 = systems->indexOf(s2);
qreal x1 = l->bbox().right(); // lyrics width
x1 += 0.2 * _spatium; // padding
QPointF p1(x1, 0); // melisma y is at base line
int segIdx = 0;
for (int i = sysIdx1; i <= sysIdx2; ++i, ++segIdx) {
System* system = (*systems)[i];
Line* line = sl->value(segIdx);
if (line == 0) {
line = new Line(l->score(), false);
l->add(line);
}
line->setLineWidth(Spatium(0.1 * lmag * staffMag));
line->setPos(p1);
if (sysIdx1 == sysIdx2) {
// single segment
qreal headWidth = score()->noteHeadWidth();
qreal len = seg->pagePos().x() - l->pagePos().x() - x1 + headWidth;
if (len <= 0.0) {
l->clearSeparator();
return;
}
line->setLen(Spatium(len / _spatium));
Lyrics* nl = searchNextLyrics(seg, staffIdx, l->no());
// small correction if next lyrics is moved? not needed if on another system
if (nl && nl->measure()->system() == s1) {
qreal x2 = nl->bbox().left() + nl->pagePos().x();
qreal lx2 = line->pagePos().x() + len;
//qDebug("Line %f text %f", lx2, x2);
if (lx2 > x2)
len -= (lx2 - x2);
}
line->setLen(Spatium(len / _spatium));
}
else if (i == sysIdx1) {
// start segment
qreal w = system->staff(l->staffIdx())->right();
qreal x = system->pagePos().x() + w;
qreal len = x - l->pagePos().x() - x1;
line->setLen(Spatium(len / _spatium));
}
else if (i > 0 && i != sysIdx2) {
qDebug("Lyrics: melisma middle segment not implemented");
// middle segment
}
else if (i == sysIdx2) {
// end segment
qDebug("Lyrics: melisma end segment not implemented");
}
line->layout();
}
return;
}
//
// we have to layout a separator to the next
// Lyric syllable
//
int verse = l->no();
Segment* ns = s;
// TODO: the next two values should be style parameters
// TODO: as well as the factor a few lines below
const qreal maxl = 0.5 * _spatium * lmag * staffMag; // lyrics hyphen length
const Spatium hlw(0.14 * lmag * staffMag); // hyphen line width
Lyrics* nl = searchNextLyrics(ns, staffIdx, verse);
if (!nl) {
l->clearSeparator();
return;
}
ns = nl->chordRest()->segment();
QList<Line*>* sl = l->separatorList();
Line* line;
if (sl->isEmpty()) {
line = new Line(l->score(), false);
l->add(line);
}
else {
line = (*sl)[0];
}
qreal x = l->bbox().right();
// convert font size to raster units, scaling if spatium-dependent
qreal size = ts.size();
if (ts.sizeIsSpatiumDependent())
size *= _spatium / SPATIUM20;
qreal y = -size * staffMag * 0.30; // TODO: make this a style parameter (for now, a conventional percentage of the whole font height)
qreal x1 = x + l->pagePos().x();
qreal x2 = nl->bbox().left() + nl->pagePos().x();
qreal len;
if (x2 < x1 && s->measure()->system() == ns->measure()->system()) {
// first syllable overlaps second
// no separator needed
l->clearSeparator();
return;
}
else if (s->measure()->system() != ns->measure()->system()) {
// second syllable not on same system as first (perhaps not even same page)
// use right edge of first system as substitute for second syllable
// so hyphen is centered within the space remaining on system
System* system = s->measure()->system();
x2 = system->pagePos().x() + system->bbox().width();
}
qreal gap = x2 - x1;
// leave 0.1sp padding on each side
// space for this is allocated in layoutX() and computeMinWidth(),
// so that hyphens are not shortened too much
len = gap - 0.2 * _spatium * lmag * staffMag;
if (len > maxl)
len = maxl;
qreal xo = (gap - len) * .5;
line->setLineWidth(hlw);
line->setPos(QPointF(x + xo, y));
line->setLen(Spatium(len / _spatium));
line->layout();
}
*/
//---------------------------------------------------------
// scanElements
// collect all visible elements
//---------------------------------------------------------
void System::scanElements(void* data, void (*func)(void*, Element*), bool all)
{
if (isVbox())
return;
if (_barLine)
func(data, _barLine);
for (Bracket* b : _brackets)
func(data, b);
int idx = 0;
for (const SysStaff* st : _staves) {
if (all || st->show()) {
for (InstrumentName* t : st->instrumentNames)
func(data, t);
}
++idx;
}
for (SpannerSegment* ss : _spannerSegments) {
int staffIdx = ss->spanner()->staffIdx();
if (staffIdx == -1) {
qDebug("System::scanElements: staffIDx == -1: %s %p", ss->spanner()->name(), ss->spanner());
staffIdx = 0;
}
bool v = true;
Spanner* spanner = ss->spanner();
if (spanner->anchor() == Spanner::Anchor::SEGMENT || spanner->anchor() == Spanner::Anchor::CHORD) {
Element* se = spanner->startElement();
Element* ee = spanner->endElement();
bool v1 = true;
if (se && (se->type() == Element::Type::CHORD || se->type() == Element::Type::REST)) {
ChordRest* cr = static_cast<ChordRest*>(se);
Measure* m = cr->measure();
MStaff* mstaff = m->mstaff(cr->staffIdx());
v1 = mstaff->visible();
}
bool v2 = true;
if (!v1 && ee && (ee->type() == Element::Type::CHORD || ee->type() == Element::Type::REST)) {
ChordRest* cr = static_cast<ChordRest*>(ee);
Measure* m = cr->measure();
MStaff* mstaff = m->mstaff(cr->staffIdx());
v2 = mstaff->visible();
}
v = v1 || v2; // hide spanner if both chords are hidden
}
if (all || (score()->staff(staffIdx)->show() && _staves[staffIdx]->show() && v) || (spanner->type() == Element::Type::VOLTA))
ss->scanElements(data, func, all);
}
}
//---------------------------------------------------------
// staffYpage
// return page coordinates
//---------------------------------------------------------
qreal System::staffYpage(int staffIdx) const
{
if (_staves.size() <= staffIdx || staffIdx < 0) {
qDebug("staffY: staves %d: bad staffIdx %d, vbox %d",
_staves.size(), staffIdx, _vbox);
// abort();
return pagePos().y();
}
return _staves[staffIdx]->y() + y(); // pagePos().y();
}
//---------------------------------------------------------
// staffCanvasYpage
// return canvas coordinates
//---------------------------------------------------------
qreal System::staffCanvasYpage(int staffIdx) const
{
return _staves[staffIdx]->y() + y() + page()->canvasPos().y();
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void System::write(Xml& xml) const
{
xml.stag("System");
// bar line is always generated
// if (_barLine && !_barLine->generated())
// _barLine->write(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void System::read(XmlReader& e)
{
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "BarLine") {
// _barLine = new BarLine(score());
// _barLine->read(e);
// _barLine->setTrack(0);
// _barLine->setParent(this);
// read the bar line for backward compatibility, but ignore it
BarLine* bl = new BarLine(score());
bl->read(e);
delete bl;
}
else
e.unknown();
}
}
//---------------------------------------------------------
// nextElement
//---------------------------------------------------------
Element* System::nextElement()
{
Measure* m = firstMeasure();
if (m) {
Segment* firstSeg = m->segments()->first();
if (firstSeg)
return firstSeg->element(0);
}
return score()->firstElement();
}
//---------------------------------------------------------
// prevElement
//---------------------------------------------------------
Element* System::prevElement()
{
Segment* seg = firstMeasure()->first();
Element* re = 0;
while (!re) {
seg = seg->prev1MM();
if (!seg)
return score()->lastElement();
if (seg->segmentType() == Segment::Type::EndBarLine)
score()->inputState().setTrack((score()->staves().size() - 1) * VOICES); //corection
re = seg->lastElement(score()->staves().size() - 1);
}
return re;
}
}
|