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
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2012 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 "line.h"
#include "barline.h"
#include "chord.h"
#include "lyrics.h"
#include "measure.h"
#include "note.h"
#include "part.h"
#include "score.h"
#include "segment.h"
#include "staff.h"
#include "system.h"
#include "textline.h"
#include "utils.h"
#include "xml.h"
namespace Ms {
//---------------------------------------------------------
// LineSegment
//---------------------------------------------------------
LineSegment::LineSegment(const LineSegment& s)
: SpannerSegment(s)
{
_p2 = s._p2;
_userOff2 = s._userOff2;
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
bool LineSegment::readProperties(XmlReader& e)
{
const QStringRef& tag(e.name());
if (tag == "subtype")
setSpannerSegmentType(SpannerSegmentType(e.readInt()));
else if (tag == "off1") // off1 is obsolete
setUserOff(e.readPoint() * spatium());
else if (tag == "off2")
setUserOff2(e.readPoint() * spatium());
else if (tag == "pos") {
if (score()->mscVersion() > 114) {
qreal _spatium = score()->spatium();
setUserOff(QPointF());
setReadPos(e.readPoint() * _spatium);
if (e.pasteMode()) // x position will be wrong
setReadPos(QPointF());
}
else
e.readNext();
}
else if (!SpannerSegment::readProperties(e)) {
e.unknown();
return false;
}
return true;
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void LineSegment::read(XmlReader& e)
{
while (e.readNextStartElement())
readProperties(e);
}
//---------------------------------------------------------
// updateGrips
//---------------------------------------------------------
void LineSegment::updateGrips(Grip* defaultGrip, QVector<QRectF>& grip) const
{
*defaultGrip = Grip::END;
QPointF pp(pagePos());
grip[int(Grip::START)].translate(pp);
grip[int(Grip::END)].translate(pos2() + pp);
grip[int(Grip::MIDDLE)].translate(pos2() * .5 + pp);
}
//---------------------------------------------------------
// setGrip
//---------------------------------------------------------
void LineSegment::setGrip(Grip grip, const QPointF& p)
{
QPointF pt(p * spatium());
switch (grip) {
case Grip::START: {
QPointF delta(pt - userOff());
setUserOff(pt);
setUserOff2(userOff2() - delta);
}
break;
case Grip::END:
setUserOff2(pt);
break;
case Grip::MIDDLE:
setUserOff(pt);
break;
case Grip::APERTURE:
default:
break;
}
layout(); // needed?
}
//---------------------------------------------------------
// getGrip
//---------------------------------------------------------
QPointF LineSegment::getGrip(Grip grip) const
{
QPointF p;
switch (grip) {
case Grip::START:
p = userOff();
break;
case Grip::END:
p = userOff2();
break;
case Grip::MIDDLE:
p = userOff();
break;
case Grip::APERTURE:
default:
break;
}
p /= spatium();
return p;
}
//---------------------------------------------------------
// gripAnchor
// return page coordinates
//---------------------------------------------------------
QPointF LineSegment::gripAnchor(Grip grip) const
{
// Middle or aperture grip have no anchor
if (grip == Grip::MIDDLE || grip == Grip::APERTURE)
return QPointF(0, 0);
// note-anchored spanners are relative to the system
qreal y = spanner()->anchor() == Spanner::Anchor::NOTE ?
system()->pos().y() : system()->staffYpage(staffIdx());
if (spannerSegmentType() == SpannerSegmentType::MIDDLE) {
qreal x;
switch (grip) {
case Grip::START:
x = system()->firstMeasure()->abbox().left();
break;
case Grip::END:
x = system()->lastMeasure()->abbox().right();
break;
default:
x = 0; // No Anchor
y = 0;
break;
}
return QPointF(x, y);
}
else {
if ((grip == Grip::END && spannerSegmentType() == SpannerSegmentType::BEGIN)
|| (grip == Grip::START && spannerSegmentType() == SpannerSegmentType::END)
)
return QPointF(0, 0);
else {
System* s;
QPointF p(line()->linePos(grip, &s));
p.ry() += y - system()->pos().y();
if (s)
p += s->pos(); // to page coordinates
return p;
}
}
}
//---------------------------------------------------------
// edit
// return true if event is accepted
//---------------------------------------------------------
bool LineSegment::edit(MuseScoreView* sv, Grip curGrip, int key, Qt::KeyboardModifiers modifiers, const QString&)
{
if (!((modifiers & Qt::ShiftModifier)
&& ((spannerSegmentType() == SpannerSegmentType::SINGLE)
|| (spannerSegmentType() == SpannerSegmentType::BEGIN && curGrip == Grip::START)
|| (spannerSegmentType() == SpannerSegmentType::END && curGrip == Grip::END))))
return false;
LineSegment* ls = 0;
SLine* l = line();
SpannerSegmentType st = spannerSegmentType();
int track = l->track();
int track2 = l->track2(); // assumed to be same as track
switch(l->anchor()) {
case Spanner::Anchor::SEGMENT:
{
Segment* s1 = spanner()->startSegment();
Segment* s2 = spanner()->endSegment();
// check for line going to end of score
if (spanner()->tick2() >= score()->lastSegment()->tick()) {
// endSegment calculated above will be the last chord/rest of score
// but that is not correct - it should be an imaginary note *after* the end of the score
// best we can do is set s2 to lastSegment (probably the end barline)
s2 = score()->lastSegment();
}
if (!s1 && !s2) {
qDebug("LineSegment::edit: no start/end segment");
return true;
}
if (key == Qt::Key_Left) {
if (curGrip == Grip::START)
s1 = prevSeg1(s1, track);
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE)
s2 = prevSeg1(s2, track2);
}
else if (key == Qt::Key_Right) {
if (curGrip == Grip::START)
s1 = nextSeg1(s1, track);
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE) {
Segment* ns2 = nextSeg1(s2, track2);
if (ns2)
s2 = ns2;
else
s2 = score()->lastSegment();
}
}
if (s1 == 0 || s2 == 0 || s1->tick() >= s2->tick())
return true;
if (s1->tick() != spanner()->tick())
spanner()->setTick(s1->tick());
if (s2->tick() != spanner()->tick2())
spanner()->setTick2(s2->tick());
}
break;
case Spanner::Anchor::NOTE:
{
Note* note1 = static_cast<Note*>(l->startElement());
Note* note2 = static_cast<Note*>(l->endElement());
Note* oldNote1 = note1;
Note* oldNote2 = note2;
if (!note1 && !note2) {
qDebug("LineSegment::edit: no start/end note");
return true; // accept the event without doing anything
}
switch(key) {
case Qt::Key_Left:
if (curGrip == Grip::START)
note1 = prevChordNote(note1);
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE)
note2 = prevChordNote(note2);
break;
case Qt::Key_Right:
if (curGrip == Grip::START)
note1 = nextChordNote(note1);
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE)
note2 = nextChordNote(note2);
break;
case Qt::Key_Up:
if (curGrip == Grip::START)
note1 = static_cast<Note*>(score()->upAlt(note1));
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE)
note2 = static_cast<Note*>(score()->upAlt(note2));
break;
case Qt::Key_Down:
if (curGrip == Grip::START)
note1 = static_cast<Note*>(score()->downAlt(note1));
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE)
note2 = static_cast<Note*>(score()->downAlt(note2));
break;
default:
return true;
}
// check prevChordNote() and nextchordNote() didn't return null
// OR Score::upAlt() and Score::downAlt() didn't return non-Note (notably rests)
// OR spanner duration is > 0
// OR note1 and note2 didn't end up in different instruments
// if this is the case, accepts the event and return without doing nothing
if (note1 == 0 || note2 == 0
|| note1->type() != Element::Type::NOTE || note2->type() != Element::Type::NOTE
|| note1->chord()->tick() >= note2->chord()->tick()
|| note1->chord()->staff()->part()->instrument(note1->chord()->tick())
!= note2->chord()->staff()->part()->instrument(note2->chord()->tick()) )
return true;
if (note1 != oldNote1 || note2 != oldNote2) {
spanner()->setNoteSpan(note1, note2); // set new spanner span
}
}
break;
default:
{
Measure* m1 = l->startMeasure();
Measure* m2 = l->endMeasure();
if (key == Qt::Key_Left) {
if (curGrip == Grip::START) {
if (m1->prevMeasure())
m1 = m1->prevMeasure();
}
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE) {
Measure* m = m2->prevMeasure();
if (m)
m2 = m;
}
}
else if (key == Qt::Key_Right) {
if (curGrip == Grip::START) {
if (m1->nextMeasure())
m1 = m1->nextMeasure();
}
else if (curGrip == Grip::END || curGrip == Grip::MIDDLE) {
if (m2->nextMeasure())
m2 = m2->nextMeasure();
}
}
if (m1->tick() > m2->tick())
return true;
if (l->startElement() != m1) {
l->setTick(m1->tick());
l->setTicks(m2->endTick() - m1->tick());
}
else if (l->endElement() != m2) {
l->setTicks(m2->endTick() - m1->tick());
}
}
}
_score->doLayout(); // needed to compute multi measure rests
// l->layout();
LineSegment* nls = 0;
if (st == SpannerSegmentType::SINGLE) {
if (curGrip == Grip::START)
nls = l->frontSegment();
else if (curGrip == Grip::END)
nls = l->backSegment();
}
else if (st == SpannerSegmentType::BEGIN)
nls = l->frontSegment();
else if (st == SpannerSegmentType::END)
nls = l->backSegment();
if (nls && (nls != this))
sv->changeEditElement(nls);
if (ls)
_score->undoRemoveElement(ls);
_score->setLayoutAll(true);
return true;
}
//---------------------------------------------------------
// editDrag
//---------------------------------------------------------
void LineSegment::editDrag(const EditData& ed)
{
// Only for resizing according to the diagonal properties
QPointF deltaResize(ed.delta.x(), line()->diagonal() ? ed.delta.y() : 0.0);
// Only for moving, no y limitaion
QPointF deltaMove(ed.delta.x(), ed.delta.y());
switch (ed.curGrip) {
case Grip::START: // Resize the begin of element (left grip)
setUserOff(userOff() + deltaResize);
_userOff2 -= deltaResize;
break;
case Grip::END: // Resize the end of element (rigth grip)
_userOff2 += deltaResize;
break;
case Grip::MIDDLE: // Move the element (middle grip)
setUserOff(userOff() + deltaMove);
break;
default:
break;
}
if ((line()->anchor() == Spanner::Anchor::NOTE)
&& (ed.curGrip == Grip::START || ed.curGrip == Grip::END)) {
//
// if we touch a different note, change anchor
//
Element* e = ed.view->elementNear(ed.pos);
if (e && e->type() == Element::Type::NOTE) {
SLine* l = line();
if (ed.curGrip == Grip::END && e != line()->endElement()) {
qDebug("LineSegment: move end anchor");
Note* noteOld = static_cast<Note*>(l->endElement());
Note* noteNew = static_cast<Note*>(e);
noteOld->removeSpannerBack(l);
noteNew->addSpannerBack(l);
l->setEndElement(noteNew);
_userOff2 += noteOld->canvasPos() - noteNew->canvasPos();
}
else if (ed.curGrip == Grip::START && e != l->startElement()) {
qDebug("LineSegment: move start anchor (not impl.)");
}
}
}
line()->layout();
}
//---------------------------------------------------------
// spatiumChanged
//---------------------------------------------------------
void LineSegment::spatiumChanged(qreal ov, qreal nv)
{
Element::spatiumChanged(ov, nv);
_userOff2 *= nv / ov;
}
//---------------------------------------------------------
// localSpatiumChanged
//---------------------------------------------------------
void LineSegment::localSpatiumChanged(qreal ov, qreal nv)
{
Element::localSpatiumChanged(ov, nv);
_userOff2 *= nv / ov;
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant LineSegment::getProperty(P_ID id) const
{
switch (id) {
case P_ID::DIAGONAL:
case P_ID::LINE_COLOR:
case P_ID::LINE_WIDTH:
case P_ID::LINE_STYLE:
return line()->getProperty(id);
default:
return SpannerSegment::getProperty(id);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool LineSegment::setProperty(P_ID id, const QVariant& val)
{
switch (id) {
case P_ID::DIAGONAL:
case P_ID::LINE_COLOR:
case P_ID::LINE_WIDTH:
case P_ID::LINE_STYLE:
return line()->setProperty(id, val);
default:
return SpannerSegment::setProperty(id, val);
}
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant LineSegment::propertyDefault(P_ID id) const
{
return line()->propertyDefault(id);
}
//---------------------------------------------------------
// dragAnchor
//---------------------------------------------------------
QLineF LineSegment::dragAnchor() const
{
if (spannerSegmentType() != SpannerSegmentType::SINGLE && spannerSegmentType() != SpannerSegmentType::BEGIN)
return QLineF();
System* s;
QPointF p = line()->linePos(Grip::START, &s);
p += QPointF(s->canvasPos().x(), s->staffCanvasYpage(line()->staffIdx()));
return QLineF(p, canvasPos());
}
//---------------------------------------------------------
// SLine
//---------------------------------------------------------
SLine::SLine(Score* s)
: Spanner(s)
{
_diagonal = false;
_lineColor = MScore::defaultColor;
_lineWidth = Spatium(0.15);
_lineStyle = Qt::SolidLine;
setTrack(0);
}
SLine::SLine(const SLine& s)
: Spanner(s)
{
_diagonal = s._diagonal;
_lineWidth = s._lineWidth;
_lineColor = s._lineColor;
_lineStyle = s._lineStyle;
}
//---------------------------------------------------------
// linePos
// Anchor::NOTE: return anchor note position in system coordinates
// Other: return (x position (relative to what?), 0)
//---------------------------------------------------------
QPointF SLine::linePos(Grip grip, System** sys) const
{
qreal x = 0.0;
qreal sp = staff()->spatium();
switch (anchor()) {
case Spanner::Anchor::SEGMENT:
{
ChordRest* cr;
if (grip == Grip::START) {
cr = static_cast<ChordRest*>(startElement());
if (cr && type() == Element::Type::OTTAVA) {
// some sources say to center the text over the note head
// others say to start the text just to left of notehead
// some say to include accidental, others don't
// our compromise - left align, but account for accidental
if (cr->durationType() == TDuration::DurationType::V_MEASURE && !cr->measure()->hasVoices(cr->staffIdx()))
x = cr->x(); // center for measure rests
else if (cr->space().lw() > 0.0)
x = -cr->space().lw(); // account for accidentals, etc
}
}
else {
cr = static_cast<ChordRest*>(endElement());
if (type() == Element::Type::OTTAVA) {
if (cr && cr->durationType() == TDuration::DurationType::V_MEASURE) {
x = cr->x() + cr->width() + sp;
}
else if (cr) {
// lay out just past right edge of all notes for this segment on this staff
// note: the cheap solution is to simply use segment width,
// but that would account for notes in other staves unnecessarily
// and in any event, segment bboxes seem unreliable
qreal width = 0;
Segment* s = cr->segment();
int n = staffIdx() * VOICES;
for (int i = 0; i < VOICES; ++i) {
ChordRest* vcr = static_cast<ChordRest*>(s->element(n + i));
if (vcr)
width = qMax(width, vcr->space().rw());
}
// extend past chord/rest
x = width + sp;
// but don't overlap next chord/rest
Segment* ns = s->next();
bool crFound = false;
while (ns) {
for (int i = 0; i < VOICES; ++i) {
if (ns->element(n + i)) {
crFound = true;
break;
}
}
if (crFound)
break;
ns = ns->next();
}
if (crFound) {
qreal nextNoteDistance = ns->x() - s->x() + lineWidth().val() * sp;
if (x > nextNoteDistance)
x = qMax(width, nextNoteDistance);
}
}
}
else if (type() == Element::Type::LYRICSLINE && static_cast<Lyrics*>(parent())->ticks() > 0) {
// melisma line
// it is possible CR won't be in correct track
// prefer element in current track if available
if (!cr)
qDebug("no end for lyricsline segment - start %d, ticks %d", tick(), ticks());
else if (cr->track() != track()) {
Element* e = cr->segment()->element(track());
if (e)
cr = static_cast<ChordRest*>(e);
}
// layout to right edge of CR
if (cr) {
qreal maxRight = 0.0;
if (cr->type() == Element::Type::CHORD) {
// chord bbox() is unreliable, look at notes
// this also allows us to more easily ignore ledger lines
for (Note* n : static_cast<Chord*>(cr)->notes())
maxRight = qMax(maxRight, cr->x() + n->x() + n->headWidth());
}
else {
// rest - won't normally happen
maxRight = cr->x() + cr->width();
}
x = maxRight; // cr->width()
}
}
else if (type() == Element::Type::HAIRPIN || type() == Element::Type::TRILL
|| type() == Element::Type::TEXTLINE || type() == Element::Type::LYRICSLINE) {
// (for LYRICSLINE, this is hyphen; melisma line is handled above)
// lay out to just before next chordrest on this staff, or barline
// tick2 actually tells us the right chordrest to look for
if (cr && endElement()->parent() && endElement()->parent()->type() == Element::Type::SEGMENT) {
qreal x2 = cr->x() + cr->space().rw();
Segment* currentSeg = static_cast<Segment*>(endElement()->parent());
Segment* seg = score()->tick2segmentMM(tick2(), false, Segment::Type::ChordRest);
if (!seg) {
// no end segment found, use measure width
x2 = endElement()->parent()->parent()->width() - sp;
}
else if (currentSeg->measure() == seg->measure()) {
// next chordrest found in same measure;
// end line 1sp to left
x2 = qMax(x2, seg->x() - sp);
}
else {
// next chordrest is in next measure
// lay out to end (barline) of current measure instead
seg = currentSeg->next(Segment::Type::EndBarLine);
if (!seg)
seg = currentSeg->measure()->last();
// allow lyrics hyphen to extend to barline
// other lines stop 1sp short
qreal gap = (type() == Element::Type::LYRICSLINE) ? 0.0 : sp;
x2 = qMax(x2, seg->x() - gap);
}
x = x2 - endElement()->parent()->x();
}
}
}
int t = grip == Grip::START ? tick() : tick2();
Measure* m = cr ? cr->measure() : score()->tick2measure(t);
if (m) {
x += cr ? cr->segment()->pos().x() + m->pos().x() : m->tick2pos(t);
*sys = m->system();
}
else
*sys = 0;
}
break;
case Spanner::Anchor::MEASURE:
{
// anchor() == Anchor::MEASURE
const Measure* m;
if (grip == Grip::START) {
m = startMeasure();
// start after clef/key
qreal offset = 0.0;
Segment* s = m->first(Segment::Type::ChordRest);
if (s) {
s = s->prev();
if (s) {
offset = s->x();
Element* e = s->element(staffIdx() * VOICES);
if (e)
offset += e->width();
}
}
x = m->pos().x() + offset;
if (score()->styleB(StyleIdx::createMultiMeasureRests) && m->hasMMRest()) {
x = m->mmRest()->pos().x();
}
}
else {
qreal _spatium = spatium();
m = endMeasure();
x = m->pos().x() + m->bbox().right();
if (score()->styleB(StyleIdx::createMultiMeasureRests)) {
//find the actual measure where the volta should stop
Measure* sm = startMeasure();
Measure* m = sm;
if (sm->hasMMRest())
m = sm->mmRest();
while (m->nextMeasureMM() && (m->endTick() < tick2()))
m = m->nextMeasureMM();
x = m->pos().x() + m->bbox().right();
}
Segment* seg = m->last();
if (seg->segmentType() == Segment::Type::EndBarLine) {
Element* e = seg->element(0);
if (e && e->type() == Element::Type::BAR_LINE) {
if (static_cast<BarLine*>(e)->barLineType() == BarLineType::START_REPEAT)
x -= e->width() - _spatium * .5;
else
x -= _spatium * .5;
}
}
}
if (score()->styleB(StyleIdx::createMultiMeasureRests))
m = m->mmRest1();
Q_ASSERT(m->system());
*sys = m->system();
}
break;
case Spanner::Anchor::NOTE:
{
Element* e = grip == Grip::START ? startElement() : endElement();
if (!e)
return QPointF();
System* s = static_cast<Note*>(e)->chord()->segment()->system();
*sys = s;
// return the position of the anchor note relative to the system
// QPointF elemPagePos = e->pagePos(); // DEBUG
// QPointF systPagePos = s->pagePos();
// qreal staffYPage = s->staffYpage(e->staffIdx());
return e->pagePos() - s->pagePos();
}
case Spanner::Anchor::CHORD:
qFatal("Sline::linePos(): anchor not implemented");
break;
}
return QPointF(x, 0.0);
}
//---------------------------------------------------------
// layout
// compute segments from tick1 tick2
//---------------------------------------------------------
void SLine::layout()
{
if (score() == gscore || tick() == -1 || tick2() == 1) {
//
// when used in a palette or while dragging from palette,
// SLine has no parent and
// tick and tick2 has no meaning so no layout is
// possible and needed
//
if (!spannerSegments().isEmpty()) {
LineSegment* lineSegm = frontSegment();
lineSegm->layout();
setbbox(lineSegm->bbox());
}
return;
}
computeStartElement();
computeEndElement();
System* s1;
System* s2;
QPointF p1(linePos(Grip::START, &s1));
QPointF p2(linePos(Grip::END, &s2));
QList<System*>* systems = score()->systems();
int sysIdx1 = systems->indexOf(s1);
int sysIdx2 = systems->indexOf(s2);
int segmentsNeeded = 0;
if (sysIdx1 == -1 || sysIdx2 == -1)
return;
for (int i = sysIdx1; i < sysIdx2+1; ++i) {
if (systems->at(i)->isVbox())
continue;
++segmentsNeeded;
}
int segCount = spannerSegments().size();
if (segmentsNeeded != segCount) {
if (segmentsNeeded > segCount) {
int n = segmentsNeeded - segCount;
for (int i = 0; i < n; ++i) {
LineSegment* lineSegm = createLineSegment();
add(lineSegm);
// set user offset to previous segment's offset
if (segCount > 0)
lineSegm->setUserOff(QPointF(0, segmentAt(segCount+i-1)->userOff().y()));
else
lineSegm->setUserOff(QPointF(0, userOff().y()));
}
}
else {
int n = segCount - segmentsNeeded;
// qDebug("SLine: segments %d needed %d, remove %d", segCount, segmentsNeeded, n);
for (int i = 0; i < n; ++i) {
if (spannerSegments().isEmpty()) {
qDebug("SLine::layout(): no segment %d, %d expected", i, n);
break;
}
else {
/*LineSegment* lineSegm =*/ takeLastSegment();
// delete lineSegm;
}
}
}
}
int segIdx = 0;
for (int i = sysIdx1; i <= sysIdx2; ++i) {
System* system = systems->at(i);
if (system->isVbox())
continue;
LineSegment* lineSegm = segmentAt(segIdx++);
lineSegm->setTrack(track()); // DEBUG
lineSegm->setSystem(system);
Measure* firstMeas = system->firstMeasure();
Segment* firstCRSeg = firstMeas->first(Segment::Type::ChordRest);
if (sysIdx1 == sysIdx2) {
// single segment
lineSegm->setSpannerSegmentType(SpannerSegmentType::SINGLE);
qreal len = p2.x() - p1.x();
// enforcing a minimum length would be possible but inadvisable
// the line length calculations are tuned well enough that this should not be needed
//if (anchor() == Anchor::SEGMENT && type() != Element::Type::PEDAL)
// len = qMax(1.0 * spatium(), len);
lineSegm->setPos(p1);
lineSegm->setPos2(QPointF(len, p2.y() - p1.y()));
}
else if (i == sysIdx1) {
// start segment
lineSegm->setSpannerSegmentType(SpannerSegmentType::BEGIN);
lineSegm->setPos(p1);
qreal x2 = system->bbox().right();
lineSegm->setPos2(QPointF(x2 - p1.x(), 0.0));
}
else if (i > 0 && i != sysIdx2) {
// middle segment
lineSegm->setSpannerSegmentType(SpannerSegmentType::MIDDLE);
qreal x1 = (firstCRSeg ? firstCRSeg->pos().x() : 0) + firstMeas->pos().x();
qreal x2 = system->bbox().right();
lineSegm->setPos(QPointF(x1, p1.y()));
lineSegm->setPos2(QPointF(x2 - x1, 0.0));
}
else if (i == sysIdx2) {
// end segment
qreal offset = 0.0;
qreal minLen = 0.0;
if (anchor() == Anchor::SEGMENT || anchor() == Anchor::MEASURE) {
// start line just after previous element (eg, key signature)
firstCRSeg = firstCRSeg->prev();
Element* e = firstCRSeg ? firstCRSeg->element(staffIdx() * VOICES) : nullptr;
if (e)
offset = e->width();
// enforcing a minimum length would be possible but inadvisable
// the line length calculations are tuned well enough that this should not be needed
//if (type() != Element::Type::PEDAL)
// minLen = 1.0 * spatium();
}
// qreal firstCRSegX = firstCRSeg ? firstCRSeg->pos().x() : 0; // DEBUG
// qreal firstMeasX = firstMeas ? firstMeas->pos().x() : 0;
qreal x1 = (firstCRSeg ? firstCRSeg->pos().x() : 0) + firstMeas->pos().x() + offset;
qreal len = qMax(minLen, p2.x() - x1);
lineSegm->setSpannerSegmentType(SpannerSegmentType::END);
lineSegm->setPos(QPointF(p2.x() - len, p2.y()));
lineSegm->setPos2(QPointF(len, 0.0));
}
lineSegm->layout();
}
adjustReadPos();
}
//---------------------------------------------------------
// writeProperties
// write properties different from prototype
//---------------------------------------------------------
void SLine::writeProperties(Xml& xml) const
{
if (!endElement()) {
xml.tag("ticks", ticks());
}
Spanner::writeProperties(xml);
if (_diagonal)
xml.tag("diagonal", _diagonal);
if (propertyStyle(P_ID::LINE_WIDTH) != PropertyStyle::STYLED)
xml.tag("lineWidth", lineWidth().val());
if (propertyStyle(P_ID::LINE_STYLE) == PropertyStyle::UNSTYLED || (lineStyle() != Qt::SolidLine))
if (propertyStyle(P_ID::LINE_STYLE) != PropertyStyle::STYLED)
xml.tag("lineStyle", int(lineStyle()));
if (propertyStyle(P_ID::LINE_COLOR) == PropertyStyle::UNSTYLED || (lineColor() != MScore::defaultColor))
xml.tag("lineColor", lineColor());
writeProperty(xml, P_ID::ANCHOR);
if (score() == gscore) {
// when used as icon
if (!spannerSegments().isEmpty()) {
LineSegment* s = frontSegment();
xml.tag("length", s->pos2().x());
}
else
xml.tag("length", spatium() * 4);
return;
}
//
// check if user has modified the default layout
//
bool modified = false;
int n = spannerSegments().size();
for (int i = 0; i < n; ++i) {
const LineSegment* seg = segmentAt(i);
if (!seg->userOff().isNull()
|| !seg->userOff2().isNull()
|| !seg->visible()) {
modified = true;
break;
}
}
if (!modified)
return;
//
// write user modified layout
//
qreal _spatium = spatium();
for (int i = 0; i < n; ++i) {
const LineSegment* seg = segmentAt(i);
xml.stag("Segment");
xml.tag("subtype", int(seg->spannerSegmentType()));
xml.tag("off2", seg->userOff2() / _spatium);
seg->Element::writeProperties(xml);
xml.etag();
}
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
bool SLine::readProperties(XmlReader& e)
{
const QStringRef& tag(e.name());
if (tag == "tick2") { // obsolete
if (tick() == -1) // not necessarily set (for first note of score?) #30151
setTick(e.tick());
setTick2(e.readInt());
}
else if (tag == "tick") // obsolete
setTick(e.readInt());
else if (tag == "ticks")
setTicks(e.readInt());
else if (tag == "Segment") {
LineSegment* ls = createLineSegment();
ls->setTrack(track()); // needed in read to get the right staff mag
ls->read(e);
add(ls);
// in v1.x "visible" is a property of the segment only;
// we must ensure that it propagates also to the parent element.
// That's why the visibility is set after adding the segment
// to the corresponding spanner
if (score()->mscVersion() <= 114)
ls->setVisible(ls->visible());
else
ls->setVisible(visible());
}
else if (tag == "length")
setLen(e.readDouble());
else if (tag == "diagonal")
setDiagonal(e.readInt());
else if (tag == "anchor")
setAnchor(Anchor(e.readInt()));
else if (tag == "lineWidth")
_lineWidth = Spatium(e.readDouble());
else if (tag == "lineStyle")
_lineStyle = Qt::PenStyle(e.readInt());
else if (tag == "lineColor")
_lineColor = e.readColor();
else if (Element::readProperties(e))
;
else
return false;
return true;
}
//---------------------------------------------------------
// setLen
// used to create an element suitable for palette
//---------------------------------------------------------
void SLine::setLen(qreal l)
{
if (spannerSegments().isEmpty())
add(createLineSegment());
LineSegment* s = frontSegment();
s->setPos(QPointF());
s->setPos2(QPointF(l, 0));
}
//---------------------------------------------------------
// bbox
// used by palette: only one segment
//---------------------------------------------------------
const QRectF& SLine::bbox() const
{
if (spannerSegments().isEmpty())
setbbox(QRectF());
else
setbbox(segmentAt(0)->bbox());
return Element::bbox();
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void SLine::write(Xml& xml) const
{
int id = xml.spannerId(this);
xml.stag(QString("%1 id=\"%2\"").arg(name()).arg(id));
SLine::writeProperties(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void SLine::read(XmlReader& e)
{
foreach(SpannerSegment* seg, spannerSegments())
delete seg;
spannerSegments().clear();
e.addSpanner(e.intAttribute("id", -1), this);
while (e.readNextStartElement()) {
if (!SLine::readProperties(e))
e.unknown();
}
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant SLine::getProperty(P_ID id) const
{
switch (id) {
case P_ID::DIAGONAL:
return _diagonal;
case P_ID::LINE_COLOR:
return _lineColor;
case P_ID::LINE_WIDTH:
return _lineWidth.val();
case P_ID::LINE_STYLE:
return QVariant(int(_lineStyle));
default:
return Spanner::getProperty(id);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool SLine::setProperty(P_ID id, const QVariant& v)
{
switch (id) {
case P_ID::DIAGONAL:
_diagonal = v.toBool();
break;
case P_ID::LINE_COLOR:
_lineColor = v.value<QColor>();
break;
case P_ID::LINE_WIDTH:
_lineWidth = Spatium(v.toDouble());
break;
case P_ID::LINE_STYLE:
_lineStyle = Qt::PenStyle(v.toInt());
break;
default:
return Spanner::setProperty(id, v);
}
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant SLine::propertyDefault(P_ID id) const
{
switch (id) {
case P_ID::DIAGONAL:
return false;
case P_ID::LINE_COLOR:
return MScore::defaultColor;
case P_ID::LINE_WIDTH:
return 0.15;
case P_ID::LINE_STYLE:
return int(Qt::SolidLine);
default:
return Spanner::propertyDefault(id);
}
}
}
|