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 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2010-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 "connector.h"
#include "score.h"
#include "spanner.h"
#include "system.h"
#include "chordrest.h"
#include "chord.h"
#include "segment.h"
#include "measure.h"
#include "undo.h"
#include "staff.h"
#include "lyrics.h"
#include "musescoreCore.h"
namespace Ms {
//-----------------------------------------------------------------------------
// @@ SpannerWriter
/// Helper class for writing Spanners
//-----------------------------------------------------------------------------
class SpannerWriter : public ConnectorInfoWriter {
protected:
const char* tagName() const override { return "Spanner"; }
public:
SpannerWriter(XmlWriter& xml, const Element* current, const Spanner* spanner, int track, Fraction frac, bool start);
static void fillSpannerPosition(Location& l, const MeasureBase* endpoint, const Fraction& tick, bool clipboardmode);
};
//---------------------------------------------------------
// SpannerSegment
//---------------------------------------------------------
SpannerSegment::SpannerSegment(Spanner* sp, Score* s, ElementFlags f)
: Element(s, f)
{
_spanner = sp;
setSpannerSegmentType(SpannerSegmentType::SINGLE);
}
SpannerSegment::SpannerSegment(Score* s, ElementFlags f)
: Element(s, f)
{
setSpannerSegmentType(SpannerSegmentType::SINGLE);
_spanner = 0;
}
SpannerSegment::SpannerSegment(const SpannerSegment& s)
: Element(s)
{
_spanner = s._spanner;
_spannerSegmentType = s._spannerSegmentType;
_p2 = s._p2;
_offset2 = s._offset2;
}
//---------------------------------------------------------
// mag
//---------------------------------------------------------
qreal SpannerSegment::mag() const
{
if (spanner()->systemFlag())
return 1.0;
return staff() ? staff()->mag(spanner()->tick()) : 1.0;
}
//---------------------------------------------------------
// setSystem
//---------------------------------------------------------
void SpannerSegment::setSystem(System* s)
{
if (system() != s) {
if (system())
system()->remove(this);
if (s)
s->add(this);
else
setParent(0);
}
}
//---------------------------------------------------------
// mimeData
//---------------------------------------------------------
QByteArray SpannerSegment::mimeData(const QPointF& dragOffset) const
{
if (dragOffset.isNull()) // where is dragOffset used?
return spanner()->mimeData(dragOffset);
return Element::mimeData(dragOffset);
}
//---------------------------------------------------------
// propertyDelegate
//---------------------------------------------------------
Element* SpannerSegment::propertyDelegate(Pid pid)
{
if (pid == Pid::COLOR || pid == Pid::VISIBLE || pid == Pid::PLACEMENT)
return spanner();
return 0;
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant SpannerSegment::getProperty(Pid pid) const
{
if (Element* e = const_cast<SpannerSegment*>(this)->propertyDelegate(pid))
return e->getProperty(pid);
switch (pid) {
case Pid::OFFSET2:
return _offset2;
default:
return Element::getProperty(pid);
}
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool SpannerSegment::setProperty(Pid pid, const QVariant& v)
{
if (Element* e = propertyDelegate(pid))
return e->setProperty(pid, v);
switch (pid) {
case Pid::OFFSET2:
_offset2 = v.toPointF();
score()->setLayoutAll();
break;
default:
return Element::setProperty(pid, v);
}
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant SpannerSegment::propertyDefault(Pid pid) const
{
if (Element* e = const_cast<SpannerSegment*>(this)->propertyDelegate(pid))
return e->propertyDefault(pid);
switch (pid) {
case Pid::OFFSET2:
return QVariant();
default:
return Element::propertyDefault(pid);
}
}
//---------------------------------------------------------
// getPropertyStyle
//---------------------------------------------------------
Sid SpannerSegment::getPropertyStyle(Pid pid) const
{
if (Element* e = const_cast<SpannerSegment*>(this)->propertyDelegate(pid))
return e->getPropertyStyle(pid);
return Element::getPropertyStyle(pid);
}
//---------------------------------------------------------
// propertyFlags
//---------------------------------------------------------
PropertyFlags SpannerSegment::propertyFlags(Pid pid) const
{
if (Element* e = const_cast<SpannerSegment*>(this)->propertyDelegate(pid))
return e->propertyFlags(pid);
return Element::propertyFlags(pid);
}
//---------------------------------------------------------
// resetProperty
//---------------------------------------------------------
void SpannerSegment::resetProperty(Pid pid)
{
if (Element* e = propertyDelegate(pid))
return e->resetProperty(pid);
return Element::resetProperty(pid);
}
//---------------------------------------------------------
// styleChanged
//---------------------------------------------------------
void SpannerSegment::styleChanged()
{
spanner()->styleChanged();
}
//---------------------------------------------------------
// reset
//---------------------------------------------------------
void SpannerSegment::reset()
{
undoChangeProperty(Pid::OFFSET2, QPointF());
Element::reset();
spanner()->reset();
}
//---------------------------------------------------------
// undoChangeProperty
//---------------------------------------------------------
void SpannerSegment::undoChangeProperty(Pid pid, const QVariant& val, PropertyFlags ps)
{
if (pid == Pid::AUTOPLACE && (val.toBool() == true && !autoplace())) {
// Switching autoplacement on. Save user-defined
// placement properties to undo stack.
undoPushProperty(Pid::OFFSET2);
// other will be saved in Element::undoChangeProperty
}
Element::undoChangeProperty(pid, val, ps);
}
//---------------------------------------------------------
// setSelected
//---------------------------------------------------------
void SpannerSegment::setSelected(bool f)
{
for (SpannerSegment* ss : _spanner->spannerSegments())
ss->Element::setSelected(f);
_spanner->setSelected(f);
}
//---------------------------------------------------------
// setVisible
//---------------------------------------------------------
void SpannerSegment::setVisible(bool f)
{
if (_spanner) {
for (SpannerSegment* ss : _spanner->spannerSegments())
ss->Element::setVisible(f);
_spanner->setVisible(f);
}
else
Element::setVisible(f);
}
//---------------------------------------------------------
// setColor
//---------------------------------------------------------
void SpannerSegment::setColor(const QColor& col)
{
if (_spanner) {
for (SpannerSegment* ss : _spanner->spannerSegments())
ss->_color = col;
_spanner->_color = col;
}
else
_color = col;
}
//---------------------------------------------------------
// nextSegmentElement
//---------------------------------------------------------
Element* SpannerSegment::nextSegmentElement()
{
return spanner()->nextSegmentElement();
}
//---------------------------------------------------------
// prevSegmentElement
//---------------------------------------------------------
Element* SpannerSegment::prevSegmentElement()
{
return spanner()->prevSegmentElement();
}
//---------------------------------------------------------
// accessibleInfo
//---------------------------------------------------------
QString SpannerSegment::accessibleInfo() const
{
return spanner()->accessibleInfo();
}
//---------------------------------------------------------
// triggerLayout
//---------------------------------------------------------
void SpannerSegment::triggerLayout() const
{
if (_spanner)
_spanner->triggerLayout();
}
//---------------------------------------------------------
// Spanner
//---------------------------------------------------------
Spanner::Spanner(Score* s, ElementFlags f)
: Element(s, f)
{
}
Spanner::Spanner(const Spanner& s)
: Element(s)
{
_anchor = s._anchor;
_startElement = s._startElement;
_endElement = s._endElement;
_tick = s._tick;
_ticks = s._ticks;
_track2 = s._track2;
}
Spanner::~Spanner()
{
qDeleteAll(segments);
qDeleteAll(unusedSegments);
}
//---------------------------------------------------------
// mag
//---------------------------------------------------------
qreal Spanner::mag() const
{
if (systemFlag())
return 1.0;
return staff() ? staff()->mag(tick()) : 1.0;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void Spanner::add(Element* e)
{
SpannerSegment* ls = toSpannerSegment(e);
ls->setSpanner(this);
ls->setSelected(selected());
ls->setTrack(track());
// ls->setAutoplace(autoplace());
segments.push_back(ls);
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void Spanner::remove(Element* e)
{
SpannerSegment* ss = toSpannerSegment(e);
if (ss->system())
ss->system()->remove(ss);
segments.erase(std::remove(segments.begin(), segments.end(), ss), segments.end());
}
//---------------------------------------------------------
// removeUnmanaged
//
// Remove the Spanner and its segments from objects which may know about them
//
// This method and the following are used for spanners which are contained within compound elements
// which manage their parts themselves without using the standard management supplied by Score;
// Example can be the LyricsLine within a Lyrics element or the FiguredBassLine within a FiguredBass
// (not implemented yet).
//---------------------------------------------------------
void Spanner::removeUnmanaged()
{
for (SpannerSegment* ss : spannerSegments())
if (ss->system()) {
// ss->system()->remove(ss);
ss->setSystem(nullptr);
}
score()->removeUnmanagedSpanner(this);
}
//---------------------------------------------------------
// insertTimeUnmanaged
//---------------------------------------------------------
void Spanner::insertTimeUnmanaged(const Fraction& fromTick, const Fraction& len)
{
Fraction newTick1 = tick();
Fraction newTick2 = tick2();
// check spanner start and end point
if (len > Fraction(0,1)) { // adding time
if (tick() > fromTick) // start after insertion point: shift start to right
newTick1 += len;
if (tick2() > fromTick) // end after insertion point: shift end to right
newTick2 += len;
}
if (len < Fraction(0,1)) { // removing time
Fraction toTick = fromTick - len;
if (tick() > fromTick) { // start after beginning of removed time
if (tick() < toTick) { // start within removed time: bring start at removing point
if (parent()) {
parent()->remove(this);
return;
}
else
newTick1 = fromTick;
}
else // start after removed time: shift start to left
newTick1 += len;
}
if (tick2() > fromTick) { // end after start of removed time
if (tick2() < toTick) // end within removed time: bring end at removing point
newTick2 = fromTick;
else // end after removed time: shift end to left
newTick2 += len;
}
}
// update properties as required
if (newTick2 <= newTick1) { // if no longer any span: remove it
if (parent())
parent()->remove(this);
}
else { // if either TICKS or TICK did change, update property
if (newTick2-newTick1 != tick2()- tick())
setProperty(Pid::SPANNER_TICKS, newTick2-newTick1);
if (newTick1 != tick())
setProperty(Pid::SPANNER_TICK, newTick1);
}
}
//---------------------------------------------------------
// scanElements
// used in palettes
//---------------------------------------------------------
void Spanner::scanElements(void* data, void (*func)(void*, Element*), bool all)
{
Q_UNUSED(all);
for (SpannerSegment* seg : segments)
seg->scanElements(data, func, true);
}
//---------------------------------------------------------
// setScore
//---------------------------------------------------------
void Spanner::setScore(Score* s)
{
Element::setScore(s);
foreach(SpannerSegment* seg, segments)
seg->setScore(s);
}
//---------------------------------------------------------
// getProperty
//---------------------------------------------------------
QVariant Spanner::getProperty(Pid propertyId) const
{
switch (propertyId) {
case Pid::SPANNER_TICK:
return _tick;
case Pid::SPANNER_TICKS:
return _ticks;
case Pid::SPANNER_TRACK2:
return track2();
case Pid::ANCHOR:
return int(anchor());
case Pid::LOCATION_STAVES:
return (track2() / VOICES) - (track() / VOICES);
case Pid::LOCATION_VOICES:
return (track2() % VOICES) - (track() / VOICES);
case Pid::LOCATION_FRACTIONS:
return _ticks;
case Pid::LOCATION_MEASURES:
case Pid::LOCATION_GRACE:
case Pid::LOCATION_NOTE:
return Location::getLocationProperty(propertyId, startElement(), endElement());
default:
break;
}
return Element::getProperty(propertyId);
}
//---------------------------------------------------------
// setProperty
//---------------------------------------------------------
bool Spanner::setProperty(Pid propertyId, const QVariant& v)
{
switch (propertyId) {
case Pid::SPANNER_TICK:
setTick(v.value<Fraction>());
setStartElement(0); // invalidate
setEndElement(0); //
if (score() && score()->spannerMap().removeSpanner(this))
score()->addSpanner(this);
break;
case Pid::SPANNER_TICKS:
setTicks(v.value<Fraction>());
setEndElement(0); // invalidate
break;
case Pid::TRACK:
setTrack(v.toInt());
setStartElement(0); // invalidate
break;
case Pid::SPANNER_TRACK2:
setTrack2(v.toInt());
setEndElement(0); // invalidate
break;
case Pid::ANCHOR:
setAnchor(Anchor(v.toInt()));
break;
default:
return Element::setProperty(propertyId, v);
}
triggerLayout();
return true;
}
//---------------------------------------------------------
// propertyDefault
//---------------------------------------------------------
QVariant Spanner::propertyDefault(Pid propertyId) const
{
switch (propertyId) {
case Pid::ANCHOR:
return int(Anchor::SEGMENT);
default:
break;
}
return Element::propertyDefault(propertyId);
}
//---------------------------------------------------------
// computeStartElement
//---------------------------------------------------------
void Spanner::computeStartElement()
{
switch (_anchor) {
case Anchor::SEGMENT: {
Segment* seg = score()->tick2segmentMM(tick(), false, SegmentType::ChordRest);
int strack = (track() / VOICES) * VOICES;
int etrack = strack + VOICES;
_startElement = 0;
if (seg) {
for (int t = strack; t < etrack; ++t) {
if (seg->element(t)) {
_startElement = seg->element(t);
break;
}
}
}
}
break;
case Anchor::MEASURE:
_startElement = score()->tick2measure(tick());
break;
case Anchor::CHORD:
case Anchor::NOTE:
return;
}
}
//---------------------------------------------------------
// computeEndElement
//---------------------------------------------------------
void Spanner::computeEndElement()
{
switch (_anchor) {
case Anchor::SEGMENT: {
if (track2() == -1)
setTrack2(track());
if (ticks().isZero() && isTextLine() && parent()) // special case palette
setTicks(score()->lastSegment()->tick() - _tick);
if (isLyricsLine() && toLyricsLine(this)->isEndMelisma()) {
// lyrics endTick should already indicate the segment we want
// except for TEMP_MELISMA_TICKS case
Lyrics* l = toLyricsLine(this)->lyrics();
Fraction tick = (l->ticks().ticks() == Lyrics::TEMP_MELISMA_TICKS) ? l->tick() : l->endTick();
Segment* s = score()->tick2segment(tick, true, SegmentType::ChordRest);
if (!s) {
qDebug("%s no end segment for tick %d", name(), tick.ticks());
return;
}
int t = trackZeroVoice(track2());
// take the first chordrest we can find;
// linePos will substitute one in current voice if available
for (int v = 0; v < VOICES; ++v) {
_endElement = s->element(t + v);
if (_endElement)
break;
}
}
else {
// find last cr on this staff that ends before tick2
_endElement = score()->findCRinStaff(tick2(), track2() / VOICES);
}
if (!_endElement) {
qDebug("%s no end element for tick %d", name(), tick2().ticks());
return;
}
if (!endCR()->measure()->isMMRest()) {
ChordRest* cr = endCR();
Fraction nticks = cr->tick() + cr->actualTicks() - _tick;
if ((_ticks - nticks).isNotZero()) {
qDebug("%s ticks changed, %d -> %d", name(), _ticks.ticks(), nticks.ticks());
setTicks(nticks);
if (isOttava())
staff()->updateOttava();
}
}
}
break;
case Anchor::MEASURE:
_endElement = score()->tick2measure(tick2() - Fraction(1, 1920));
if (!_endElement) {
qDebug("Spanner::computeEndElement(), measure not found for tick %d\n", tick2().ticks()-1);
_endElement = score()->lastMeasure();
}
break;
case Anchor::CHORD:
case Anchor::NOTE:
break;
}
}
//---------------------------------------------------------
// startElementFromSpanner
//
// Given a Spanner and an end element, determines a start element suitable for the end
// element of a new Spanner, so that it is 'parallel' to the old one.
// Can be used while cloning a linked Spanner, to update the cloned spanner start and end elements
// (Spanner(const Spanner&) copies start and end elements from the original to the copy).
// NOTES: Only spanners with Anchor::NOTE are currently supported.
// Going back from end to start ensures the 'other' anchor of this is already set up
// (for instance, while cloning staves)
//---------------------------------------------------------
Note* Spanner::startElementFromSpanner(Spanner* sp, Element* newEnd)
{
if (sp->anchor() != Anchor::NOTE)
return nullptr;
Note* oldStart = toNote(sp->startElement());
Note* oldEnd = toNote(sp->endElement());
if (oldStart == nullptr || oldEnd == nullptr)
return nullptr;
Note* newStart = nullptr;
Score* score = newEnd->score();
// determine the track where to expect the 'parallel' start element
int newTrack = (newEnd->track() - oldEnd->track()) + oldStart->track();
// look in notes linked to oldStart for a note with the
// same score as new score and appropriate track
for (ScoreElement* newEl : oldStart->linkList())
if (toNote(newEl)->score() == score && toNote(newEl)->track() == newTrack) {
newStart = toNote(newEl);
break;
}
return newStart;
}
//---------------------------------------------------------
// endElementFromSpanner
//
// Given a Spanner and a start element, determines an end element suitable for the start
// element of a new Spanner, so that it is 'parallel' to the old one.
// Can be used while cloning a linked Spanner, to update the cloned spanner start and end elements
// (Spanner(const Spanner&) copies start and end elements from the original to the copy).
// NOTES: Only spanners with Anchor::NOTE are currently supported.
//---------------------------------------------------------
Note* Spanner::endElementFromSpanner(Spanner* sp, Element* newStart)
{
if (sp->anchor() != Anchor::NOTE)
return nullptr;
Note* oldStart = toNote(sp->startElement());
Note* oldEnd = toNote(sp->endElement());
if (oldStart == nullptr || oldEnd == nullptr)
return nullptr;
Note* newEnd = nullptr;
Score* score = newStart->score();
// determine the track where to expect the 'parallel' start element
int newTrack = newStart->track() + (oldEnd->track() - oldStart->track());
// look in notes linked to oldEnd for a note with the
// same score as new score and appropriate track
for (ScoreElement* newEl : oldEnd->linkList())
if (toNote(newEl)->score() == score && toNote(newEl)->track() == newTrack) {
newEnd = toNote(newEl);
break;
}
return newEnd;
}
//---------------------------------------------------------
// setNoteSpan
//
// Sets up all the variables congruent with given start and end note anchors.
//---------------------------------------------------------
void Spanner::setNoteSpan(Note* startNote, Note* endNote)
{
if (_anchor != Anchor::NOTE)
return;
setScore(startNote->score());
setParent(startNote);
setStartElement(startNote);
setEndElement(endNote);
setTick(startNote->chord()->tick());
setTick2(endNote->chord()->tick());
setTrack(startNote->track());
setTrack2(endNote->track());
}
//---------------------------------------------------------
// startChord
//---------------------------------------------------------
Chord* Spanner::startChord()
{
Q_ASSERT(_anchor == Anchor::CHORD);
if (!_startElement)
_startElement = score()->findCR(tick(), track());
return toChord(_startElement);
}
//---------------------------------------------------------
// endChord
//---------------------------------------------------------
Chord* Spanner::endChord()
{
Q_ASSERT(_anchor == Anchor::CHORD);
if (!_endElement && type() == ElementType::SLUR) {
Segment* s = score()->tick2segmentMM(tick2(), false, SegmentType::ChordRest);
_endElement = s ? toChordRest(s->element(track2())) : nullptr;
if (!_endElement->isChord())
_endElement = nullptr;
}
return toChord(_endElement);
}
//---------------------------------------------------------
// startCR
//---------------------------------------------------------
ChordRest* Spanner::startCR()
{
Q_ASSERT(_anchor == Anchor::SEGMENT || _anchor == Anchor::CHORD);
if (!_startElement || _startElement->score() != score())
_startElement = score()->findCR(tick(), track());
return toChordRest(_startElement);
}
//---------------------------------------------------------
// endCR
//---------------------------------------------------------
ChordRest* Spanner::endCR()
{
Q_ASSERT(_anchor == Anchor::SEGMENT || _anchor == Anchor::CHORD);
if ((!_endElement || _endElement->score() != score())) {
Segment* s = score()->tick2segmentMM(tick2(), false, SegmentType::ChordRest);
const int tr2 = (track2() == -1) ? track() : track2();
_endElement = s ? toChordRest(s->element(tr2)) : nullptr;
}
return toChordRest(_endElement);
}
//---------------------------------------------------------
// startSegment
//---------------------------------------------------------
Segment* Spanner::startSegment() const
{
Q_ASSERT(score() != NULL);
return score()->tick2rightSegment(tick());
}
//---------------------------------------------------------
// endSegment
//---------------------------------------------------------
Segment* Spanner::endSegment() const
{
return score()->tick2leftSegment(tick2());
}
//---------------------------------------------------------
// startMeasure
//---------------------------------------------------------
Measure* Spanner::startMeasure() const
{
return toMeasure(_startElement);
}
//---------------------------------------------------------
// endMeasure
//---------------------------------------------------------
Measure* Spanner::endMeasure() const
{
return toMeasure(_endElement);
}
//---------------------------------------------------------
// setSelected
//---------------------------------------------------------
void Spanner::setSelected(bool f)
{
for (SpannerSegment* ss : spannerSegments())
ss->Element::setSelected(f);
Element::setSelected(f);
}
//---------------------------------------------------------
// setVisible
//---------------------------------------------------------
void Spanner::setVisible(bool f)
{
for (SpannerSegment* ss : spannerSegments())
ss->Element::setVisible(f);
Element::setVisible(f);
}
//---------------------------------------------------------
// setAutoplace
//---------------------------------------------------------
void Spanner::setAutoplace(bool f)
{
for (SpannerSegment* ss : spannerSegments())
ss->Element::setAutoplace(f);
Element::setAutoplace(f);
}
//---------------------------------------------------------
// setColor
//---------------------------------------------------------
void Spanner::setColor(const QColor& col)
{
for (SpannerSegment* ss : spannerSegments())
ss->setColor(col);
_color = col;
}
//---------------------------------------------------------
// setStartElement
//---------------------------------------------------------
void Spanner::setStartElement(Element* e)
{
#ifndef NDEBUG
if (_anchor == Anchor::NOTE)
Q_ASSERT(!e || e->type() == ElementType::NOTE);
#endif
_startElement = e;
}
//---------------------------------------------------------
// setEndElement
//---------------------------------------------------------
void Spanner::setEndElement(Element* e)
{
#ifndef NDEBUG
if (_anchor == Anchor::NOTE)
Q_ASSERT(!e || e->type() == ElementType::NOTE);
#endif
_endElement = e;
}
//---------------------------------------------------------
// nextSpanner
//---------------------------------------------------------
Spanner* Spanner::nextSpanner(Element* e, int activeStaff)
{
std::multimap<int, Spanner*> mmap = score()->spanner();
auto range = mmap.equal_range(tick().ticks());
if (range.first != range.second) { // range not empty
for (auto i = range.first; i != range.second; ++i) {
if (i->second == e) {
while (i != range.second) {
++i;
if (i == range.second)
return nullptr;
Spanner* s = i->second;
Element* st = s->startElement();
if (!st)
continue;
if (s->startSegment() == toSpanner(e)->startSegment() &&
st->staffIdx() == activeStaff)
return s;
//else
//return nullptr;
}
break;
/* else {
break;
}*/
}
}
}
return nullptr;
}
//---------------------------------------------------------
// prevSpanner
//---------------------------------------------------------
Spanner* Spanner::prevSpanner(Element* e, int activeStaff)
{
std::multimap<int, Spanner*> mmap = score()->spanner();
auto range = mmap.equal_range(tick().ticks());
if (range.first != range.second) { // range not empty
for (auto i = range.first; i != range.second; ++i) {
if (i->second == e) {
if (i == range.first)
return nullptr;
while (i != range.first) {
--i;
Spanner* s = i->second;
if (s->startSegment() == toSpanner(e)->startSegment() &&
s->startElement()->staffIdx() == activeStaff)
return s;
}
break;
}
}
}
return nullptr;
}
//---------------------------------------------------------
// nextSegmentElement
//---------------------------------------------------------
Element* Spanner::nextSegmentElement()
{
Segment* s = startSegment();
if (s)
return s->firstElement(staffIdx());
return score()->firstElement();
}
//---------------------------------------------------------
// prevSegmentElement
//---------------------------------------------------------
Element* Spanner::prevSegmentElement()
{
Segment* s = endSegment();
if (s)
return s->lastElement(staffIdx());
return score()->lastElement();
}
//---------------------------------------------------------
// setTick
//---------------------------------------------------------
void Spanner::setTick(const Fraction& v)
{
_tick = v;
if (score())
score()->spannerMap().setDirty();
}
//---------------------------------------------------------
// setTick2
//---------------------------------------------------------
void Spanner::setTick2(const Fraction& f)
{
setTicks(f - _tick);
}
//---------------------------------------------------------
// setTicks
//---------------------------------------------------------
void Spanner::setTicks(const Fraction& f)
{
_ticks = f;
if (score())
score()->spannerMap().setDirty();
}
//---------------------------------------------------------
// triggerLayout
//---------------------------------------------------------
void Spanner::triggerLayout() const
{
score()->setLayout(_tick);
score()->setLayout(_tick + _ticks);
}
//---------------------------------------------------------
// pushUnusedSegment
//---------------------------------------------------------
void Spanner::pushUnusedSegment(SpannerSegment* seg)
{
if (!seg)
return;
seg->setSystem(nullptr);
unusedSegments.push_back(seg);
}
//---------------------------------------------------------
// popUnusedSegment
// Take the next unused segment for reusing it.
// If there is no unused segments left returns nullptr.
//---------------------------------------------------------
SpannerSegment* Spanner::popUnusedSegment()
{
if (unusedSegments.empty())
return nullptr;
SpannerSegment* seg = unusedSegments.front();
unusedSegments.pop_front();
return seg;
}
//---------------------------------------------------------
// reuse
// called when segment from unusedSegments is added
// back to the spanner.
//---------------------------------------------------------
void Spanner::reuse(SpannerSegment* seg)
{
add(seg);
}
//---------------------------------------------------------
// reuseSegments
// Adds \p number segments from unusedSegments to this
// spanner via reuse() call. Returns number of new
// segments that still need to be created, that is,
// returns (number - nMovedSegments).
//---------------------------------------------------------
int Spanner::reuseSegments(int number)
{
while (number > 0) {
SpannerSegment* seg = popUnusedSegment();
if (!seg)
break;
reuse(seg);
--number;
}
return number;
}
//---------------------------------------------------------
// fixupSegments
// Makes number of segments match targetNumber.
// Tries to reuse unused segments. If there are no
// unused segments left, uses \p createSegment to create
// the needed segments.
// Previously unused segments are added via reuse() call
//---------------------------------------------------------
void Spanner::fixupSegments(unsigned int targetNumber, std::function<SpannerSegment*()> createSegment)
{
const int diff = targetNumber - int(nsegments());
if (diff == 0)
return;
if (diff > 0) {
const int ncreate = reuseSegments(diff);
for (int i = 0; i < ncreate; ++i)
add(createSegment());
}
else { // diff < 0
const int nremove = -diff;
for (int i = 0; i < nremove; ++i) {
SpannerSegment* seg = segments.back();
segments.pop_back();
pushUnusedSegment(seg);
}
}
}
//---------------------------------------------------------
// eraseSpannerSegments
// Completely erase all spanner segments, both used and
// unused.
//---------------------------------------------------------
void Spanner::eraseSpannerSegments()
{
qDeleteAll(segments);
qDeleteAll(unusedSegments);
segments.clear();
unusedSegments.clear();
}
//---------------------------------------------------------
// layoutSystem
//---------------------------------------------------------
SpannerSegment* Spanner::layoutSystem(System*)
{
qDebug(" %s", name());
return 0;
}
//---------------------------------------------------------
// getNextLayoutSystemSegment
//---------------------------------------------------------
SpannerSegment* Spanner::getNextLayoutSystemSegment(System* system, std::function<SpannerSegment*()> createSegment)
{
SpannerSegment* seg = nullptr;
for (SpannerSegment* ss : spannerSegments()) {
if (!ss->system()) {
seg = ss;
break;
}
}
if (!seg) {
if ((seg = popUnusedSegment()))
reuse(seg);
else {
seg = createSegment();
Q_ASSERT(seg);
add(seg);
}
}
seg->setSystem(system);
seg->setSpanner(this);
seg->setTrack(track());
seg->setVisible(visible());
return seg;
}
//---------------------------------------------------------
// layoutSystemsDone
// Called after layout of all systems is done so precise
// number of systems for this spanner becomes available.
//---------------------------------------------------------
void Spanner::layoutSystemsDone()
{
std::vector<SpannerSegment*> validSegments;
for (SpannerSegment* seg : segments) {
if (seg->system())
validSegments.push_back(seg);
else // TODO: score()->selection().remove(ss); needed?
pushUnusedSegment(seg);
}
segments = std::move(validSegments);
}
//--------------------------------------------------
// fraction
//---------------------------------------------------------
static Fraction fraction(const XmlWriter& xml, const Element* current, const Fraction& t)
{
Fraction tick(t);
if (!xml.clipboardmode()) {
const Measure* m = toMeasure(current->findMeasure());
if (m)
tick -= m->tick();
}
return tick;
}
//---------------------------------------------------------
// Spanner::readProperties
//---------------------------------------------------------
bool Spanner::readProperties(XmlReader& e)
{
const QStringRef tag(e.name());
if (e.pasteMode()) {
if (tag == "ticks_f") {
setTicks(e.readFraction());
return true;
}
}
return Element::readProperties(e);
}
//---------------------------------------------------------
// Spanner::writeProperties
//---------------------------------------------------------
void Spanner::writeProperties(XmlWriter& xml) const
{
if (xml.clipboardmode())
xml.tag("ticks_f", ticks());
Element::writeProperties(xml);
}
//--------------------------------------------------
// Spanner::writeSpannerStart
//---------------------------------------------------------
void Spanner::writeSpannerStart(XmlWriter& xml, const Element* current, int track, Fraction tick) const
{
Fraction frac = fraction(xml, current, tick);
SpannerWriter w(xml, current, this, track, frac, true);
w.write();
}
//--------------------------------------------------
// Spanner::writeSpannerEnd
//---------------------------------------------------------
void Spanner::writeSpannerEnd(XmlWriter& xml, const Element* current, int track, Fraction tick) const
{
Fraction frac = fraction(xml, current, tick);
SpannerWriter w(xml, current, this, track, frac, false);
w.write();
}
//--------------------------------------------------
// Spanner::readSpanner
//---------------------------------------------------------
void Spanner::readSpanner(XmlReader& e, Element* current, int track)
{
std::unique_ptr<ConnectorInfoReader> info(new ConnectorInfoReader(e, current, track));
ConnectorInfoReader::readConnector(std::move(info), e);
}
//--------------------------------------------------
// Spanner::readSpanner
//---------------------------------------------------------
void Spanner::readSpanner(XmlReader& e, Score* current, int track)
{
std::unique_ptr<ConnectorInfoReader> info(new ConnectorInfoReader(e, current, track));
ConnectorInfoReader::readConnector(std::move(info), e);
}
//---------------------------------------------------------
// SpannerWriter::fillSpannerPosition
//---------------------------------------------------------
void SpannerWriter::fillSpannerPosition(Location& l, const MeasureBase* m, const Fraction& tick, bool clipboardmode)
{
if (clipboardmode) {
l.setMeasure(0);
l.setFrac(tick);
}
else {
if (!m) {
qWarning("fillSpannerPosition: couldn't find spanner's endpoint's measure");
l.setMeasure(0);
l.setFrac(tick);
return;
}
l.setMeasure(m->measureIndex());
l.setFrac(tick - m->tick());
}
}
//---------------------------------------------------------
// SpannerWriter::SpannerWriter
//---------------------------------------------------------
SpannerWriter::SpannerWriter(XmlWriter& xml, const Element* current, const Spanner* sp, int track, Fraction frac, bool start)
: ConnectorInfoWriter(xml, current, sp, track, frac)
{
const bool clipboardmode = xml.clipboardmode();
if (!sp->startElement() || !sp->endElement()) {
qWarning("SpannerWriter: spanner (%s) doesn't have an endpoint!", sp->name());
return;
}
if (current->isMeasure() || current->isSegment() || (sp->startElement()->type() != current->type())) {
// (The latter is the hairpins' case, for example, though they are
// covered by the other checks too.)
// We cannot determine position of the spanner from its start/end
// elements and will try to obtain this info from the spanner itself.
if (!start) {
_prevLoc.setTrack(sp->track());
Measure* m = sp->score()->tick2measure(sp->tick());
fillSpannerPosition(_prevLoc, m, sp->tick(), clipboardmode);
}
else {
const int track2 = (sp->track2() != -1) ? sp->track2() : sp->track();
_nextLoc.setTrack(track2);
Measure* m = sp->score()->tick2measure(sp->tick2());
fillSpannerPosition(_nextLoc, m, sp->tick2(), clipboardmode);
}
}
else {
// We can obtain the spanner position info from its start/end
// elements and will prefer this source of information.
// Reason: some spanners contain no or wrong information (e.g. Ties).
if (!start)
updateLocation(sp->startElement(), _prevLoc, clipboardmode);
else
updateLocation(sp->endElement(), _nextLoc, clipboardmode);
}
}
//---------------------------------------------------------
// autoplaceSpannerSegment
//---------------------------------------------------------
void SpannerSegment::autoplaceSpannerSegment()
{
if (!parent()) {
setOffset(QPointF());
return;
}
if (isStyled(Pid::OFFSET))
setOffset(spanner()->propertyDefault(Pid::OFFSET).toPointF());
if (spanner()->anchor() == Spanner::Anchor::NOTE)
return;
// rebase vertical offset on drag
qreal rebase = 0.0;
if (offsetChanged() != OffsetChange::NONE)
rebase = rebaseOffset();
if (autoplace()) {
qreal sp = score()->spatium();
if (!systemFlag() && !spanner()->systemFlag())
sp *= staff()->mag(spanner()->tick());
qreal md = minDistance().val() * sp;
bool above = spanner()->placeAbove();
SkylineLine sl(!above);
Shape sh = shape();
sl.add(sh.translated(pos()));
qreal yd = 0.0;
if (above) {
qreal d = system()->topDistance(staffIdx(), sl);
if (d > -md)
yd = -(d + md);
}
else {
qreal d = system()->bottomDistance(staffIdx(), sl);
if (d > -md)
yd = d + md;
}
if (yd != 0.0) {
if (offsetChanged() != OffsetChange::NONE) {
// user moved element within the skyline
// we may need to adjust minDistance, yd, and/or offset
qreal adj = pos().y() + rebase;
bool inStaff = above ? sh.bottom() + adj > 0.0 : sh.top() + adj < staff()->height();
rebaseMinDistance(md, yd, sp, rebase, above, inStaff);
}
rypos() += yd;
}
}
setOffsetChanged(false);
}
//---------------------------------------------------------
// undoChangeProperty
//---------------------------------------------------------
void Spanner::undoChangeProperty(Pid id, const QVariant& v, PropertyFlags ps)
{
if (id == Pid::PLACEMENT) {
ScoreElement::undoChangeProperty(id, v, ps);
// change offset of all segments if styled
for (SpannerSegment* s : segments) {
if (s->isStyled(Pid::OFFSET)) {
s->setOffset(s->propertyDefault(Pid::OFFSET).toPointF());
s->triggerLayout();
}
}
MuseScoreCore::mscoreCore->updateInspector();
return;
}
Element::undoChangeProperty(id, v, ps);
}
}
|