2016-11-19 11:51:21 +01:00
|
|
|
//=============================================================================
|
|
|
|
// MuseScore
|
|
|
|
// Music Composition & Notation
|
|
|
|
//
|
|
|
|
// Copyright (C) 2002-2016 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 "xml.h"
|
|
|
|
#include "layoutbreak.h"
|
2018-06-02 20:41:28 +02:00
|
|
|
#include "measure.h"
|
2018-06-17 07:06:25 +02:00
|
|
|
#include "score.h"
|
2016-11-19 11:51:21 +01:00
|
|
|
#include "spanner.h"
|
2018-06-17 07:06:25 +02:00
|
|
|
#include "staff.h"
|
2016-11-19 11:51:21 +01:00
|
|
|
#include "beam.h"
|
|
|
|
#include "tuplet.h"
|
|
|
|
#include "sym.h"
|
|
|
|
#include "note.h"
|
|
|
|
#include "barline.h"
|
2018-06-11 14:16:16 +02:00
|
|
|
#include "style.h"
|
2016-11-19 11:51:21 +01:00
|
|
|
|
|
|
|
namespace Ms {
|
|
|
|
|
2018-05-26 20:01:06 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// ~XmlReader
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
XmlReader::~XmlReader()
|
|
|
|
{
|
2018-12-02 00:38:46 +01:00
|
|
|
if (!_connectors.empty() || !_pendingConnectors.empty()) {
|
2018-05-26 20:01:06 +02:00
|
|
|
qDebug("XmlReader::~XmlReader: there are unpaired connectors left");
|
2018-12-02 00:38:46 +01:00
|
|
|
for (auto& c : _connectors) {
|
|
|
|
Element* conn = c->releaseConnector();
|
2018-05-26 20:01:06 +02:00
|
|
|
if (conn && !conn->isTuplet()) // tuplets are added to score even when not finished
|
|
|
|
delete conn;
|
|
|
|
}
|
2018-12-02 00:38:46 +01:00
|
|
|
for (auto& c : _pendingConnectors)
|
|
|
|
delete c->releaseConnector();
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 11:51:21 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// intAttribute
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
int XmlReader::intAttribute(const char* s, int _default) const
|
|
|
|
{
|
|
|
|
if (attributes().hasAttribute(s))
|
|
|
|
// return attributes().value(s).toString().toInt();
|
|
|
|
return attributes().value(s).toInt();
|
|
|
|
else
|
|
|
|
return _default;
|
|
|
|
}
|
|
|
|
|
|
|
|
int XmlReader::intAttribute(const char* s) const
|
|
|
|
{
|
|
|
|
return attributes().value(s).toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// doubleAttribute
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
double XmlReader::doubleAttribute(const char* s) const
|
|
|
|
{
|
|
|
|
return attributes().value(s).toDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
double XmlReader::doubleAttribute(const char* s, double _default) const
|
|
|
|
{
|
|
|
|
if (attributes().hasAttribute(s))
|
|
|
|
return attributes().value(s).toDouble();
|
|
|
|
else
|
|
|
|
return _default;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// attribute
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QString XmlReader::attribute(const char* s, const QString& _default) const
|
|
|
|
{
|
|
|
|
if (attributes().hasAttribute(s))
|
|
|
|
return attributes().value(s).toString();
|
|
|
|
else
|
|
|
|
return _default;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// hasAttribute
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
bool XmlReader::hasAttribute(const char* s) const
|
|
|
|
{
|
|
|
|
return attributes().hasAttribute(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readPoint
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QPointF XmlReader::readPoint()
|
|
|
|
{
|
|
|
|
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!attributes().hasAttribute("x")) {
|
|
|
|
QXmlStreamAttributes map = attributes();
|
|
|
|
qDebug("XmlReader::readPoint: x attribute missing: %s (%d)",
|
|
|
|
name().toUtf8().data(), map.size());
|
|
|
|
for (int i = 0; i < map.size(); ++i) {
|
|
|
|
const QXmlStreamAttribute& a = map.at(i);
|
|
|
|
qDebug(" attr <%s> <%s>", a.name().toUtf8().data(), a.value().toUtf8().data());
|
|
|
|
}
|
|
|
|
unknown();
|
|
|
|
}
|
|
|
|
if (!attributes().hasAttribute("y")) {
|
|
|
|
qDebug("XmlReader::readPoint: y attribute missing: %s", name().toUtf8().data());
|
|
|
|
unknown();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
qreal x = doubleAttribute("x", 0.0);
|
|
|
|
qreal y = doubleAttribute("y", 0.0);
|
|
|
|
readNext();
|
|
|
|
return QPointF(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readColor
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QColor XmlReader::readColor()
|
|
|
|
{
|
|
|
|
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
|
|
|
QColor c;
|
|
|
|
c.setRed(intAttribute("r"));
|
|
|
|
c.setGreen(intAttribute("g"));
|
|
|
|
c.setBlue(intAttribute("b"));
|
|
|
|
c.setAlpha(intAttribute("a", 255));
|
|
|
|
skipCurrentElement();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readSize
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QSizeF XmlReader::readSize()
|
|
|
|
{
|
|
|
|
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
|
|
|
QSizeF p;
|
|
|
|
p.setWidth(doubleAttribute("w", 0.0));
|
|
|
|
p.setHeight(doubleAttribute("h", 0.0));
|
|
|
|
skipCurrentElement();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readRect
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
QRectF XmlReader::readRect()
|
|
|
|
{
|
|
|
|
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
|
|
|
QRectF p;
|
|
|
|
p.setX(doubleAttribute("x", 0.0));
|
|
|
|
p.setY(doubleAttribute("y", 0.0));
|
|
|
|
p.setWidth(doubleAttribute("w", 0.0));
|
|
|
|
p.setHeight(doubleAttribute("h", 0.0));
|
|
|
|
skipCurrentElement();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readFraction
|
|
|
|
// recognizes this two styles:
|
|
|
|
// <move z="2" n="4"/> (old style)
|
|
|
|
// <move>2/4</move> (new style)
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
Fraction XmlReader::readFraction()
|
|
|
|
{
|
|
|
|
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
|
|
|
|
int z = attribute("z", "0").toInt();
|
|
|
|
int n = attribute("n", "1").toInt();
|
|
|
|
const QString& s(readElementText());
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
int i = s.indexOf('/');
|
2019-01-30 15:13:54 +01:00
|
|
|
if (i == -1) {
|
|
|
|
qDebug("reading ticks <%s>", qPrintable(s));
|
|
|
|
return Fraction::fromTicks(s.toInt());
|
|
|
|
}
|
2016-11-19 11:51:21 +01:00
|
|
|
else {
|
|
|
|
z = s.left(i).toInt();
|
|
|
|
n = s.mid(i+1).toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Fraction(z, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// unknown
|
|
|
|
// unknown tag read
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::unknown()
|
|
|
|
{
|
|
|
|
if (QXmlStreamReader::error())
|
2017-10-12 11:10:30 +02:00
|
|
|
qDebug("%s ", qPrintable(errorString()));
|
|
|
|
if (!docName.isEmpty())
|
|
|
|
qDebug("tag in <%s> line %lld col %lld: %s",
|
|
|
|
qPrintable(docName), lineNumber(), columnNumber(), name().toUtf8().data());
|
|
|
|
else
|
2018-05-04 14:39:14 +02:00
|
|
|
qDebug("line %lld col %lld: %s", lineNumber(), columnNumber(), name().toUtf8().data());
|
2016-11-19 11:51:21 +01:00
|
|
|
skipCurrentElement();
|
|
|
|
}
|
|
|
|
|
2018-08-14 23:15:43 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// location
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
Location XmlReader::location(bool forceAbsFrac) const
|
|
|
|
{
|
|
|
|
Location l = Location::absolute();
|
|
|
|
fillLocation(l, forceAbsFrac);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// fillLocation
|
|
|
|
// fills location fields which have default values with
|
|
|
|
// values relevant for the current reader's position.
|
2018-06-17 07:06:25 +02:00
|
|
|
// When in paste mode (or forceAbsFrac is true) absolute
|
|
|
|
// fraction values are used and measure number is set to
|
|
|
|
// zero.
|
2018-08-14 23:15:43 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::fillLocation(Location& l, bool forceAbsFrac) const
|
|
|
|
{
|
|
|
|
constexpr Location defaults = Location::absolute();
|
|
|
|
const bool absFrac = (pasteMode() || forceAbsFrac);
|
|
|
|
if (l.track() == defaults.track())
|
|
|
|
l.setTrack(track());
|
|
|
|
if (l.frac() == defaults.frac())
|
2019-01-30 15:13:54 +01:00
|
|
|
l.setFrac(absFrac ? tick() : rtick());
|
2018-08-14 23:15:43 +02:00
|
|
|
if (l.measure() == defaults.measure())
|
|
|
|
l.setMeasure(absFrac ? 0 : currentMeasureIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// setLocation
|
|
|
|
// sets a new reading location, taking into account its
|
|
|
|
// type (absolute or relative).
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::setLocation(const Location& l)
|
|
|
|
{
|
|
|
|
if (l.isRelative()) {
|
|
|
|
Location newLoc = l;
|
|
|
|
newLoc.toAbsolute(location());
|
2019-02-18 09:58:34 +01:00
|
|
|
int intTicks = l.frac().ticks();
|
|
|
|
if (_tick == Fraction::fromTicks(_intTick + intTicks)) {
|
|
|
|
_intTick += intTicks;
|
|
|
|
setTrack(newLoc.track() - _trackOffset);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-14 23:15:43 +02:00
|
|
|
setLocation(newLoc); // recursion
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTrack(l.track() - _trackOffset);
|
2019-01-30 15:13:54 +01:00
|
|
|
setTick(l.frac() - _tickOffset);
|
2018-08-14 23:15:43 +02:00
|
|
|
if (!pasteMode()) {
|
|
|
|
Q_ASSERT(l.measure() == currentMeasureIndex());
|
2019-01-30 15:13:54 +01:00
|
|
|
incTick(currentMeasure()->tick());
|
2018-08-14 23:15:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 11:51:21 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// addBeam
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::addBeam(Beam* s)
|
|
|
|
{
|
|
|
|
_beams.insert(s->id(), s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// addTuplet
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::addTuplet(Tuplet* s)
|
|
|
|
{
|
|
|
|
_tuplets.insert(s->id(), s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readDouble
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
double XmlReader::readDouble(double min, double max)
|
|
|
|
{
|
|
|
|
double val = readElementText().toDouble();
|
|
|
|
if (val < min)
|
|
|
|
val = min;
|
|
|
|
else if (val > max)
|
|
|
|
val = max;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readBool
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
bool XmlReader::readBool()
|
|
|
|
{
|
|
|
|
bool val;
|
|
|
|
QXmlStreamReader::TokenType tt = readNext();
|
|
|
|
if (tt == QXmlStreamReader::Characters) {
|
|
|
|
val = text().toInt() != 0;
|
|
|
|
readNext();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
val = true;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// checkTuplets
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::checkTuplets()
|
|
|
|
{
|
|
|
|
for (Tuplet* tuplet : tuplets()) {
|
|
|
|
if (tuplet->elements().empty()) {
|
|
|
|
// this should not happen and is a sign of input file corruption
|
|
|
|
qDebug("Measure:read(): empty tuplet id %d (%p), input file corrupted?",
|
|
|
|
tuplet->id(), tuplet);
|
|
|
|
delete tuplet;
|
|
|
|
}
|
2016-11-17 14:28:05 +01:00
|
|
|
else {
|
|
|
|
//sort tuplet elements. Needed for nested tuplets #22537
|
|
|
|
tuplet->sortElements();
|
|
|
|
tuplet->sanitizeTuplet();
|
|
|
|
}
|
2016-11-19 11:51:21 +01:00
|
|
|
}
|
2018-03-20 04:52:08 +01:00
|
|
|
// This requires a separate pass in case of nested tuplets that required sanitizing
|
|
|
|
for (Tuplet* tuplet : tuplets())
|
|
|
|
tuplet->addMissingElements();
|
2016-11-19 11:51:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// htmlToString
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::htmlToString(int level, QString* s)
|
|
|
|
{
|
|
|
|
*s += QString("<%1").arg(name().toString());
|
|
|
|
for (const QXmlStreamAttribute& a : attributes())
|
|
|
|
*s += QString(" %1=\"%2\"").arg(a.name().toString()).arg(a.value().toString());
|
|
|
|
*s += ">";
|
|
|
|
++level;
|
|
|
|
for (;;) {
|
|
|
|
QXmlStreamReader::TokenType t = readNext();
|
|
|
|
switch(t) {
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
htmlToString(level, s);
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
*s += QString("</%1>").arg(name().toString());
|
|
|
|
--level;
|
|
|
|
return;
|
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
if (!isWhitespace())
|
|
|
|
*s += text().toString().toHtmlEscaped();
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qDebug("htmlToString: read token: %s", qPrintable(tokenString()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// readXml
|
|
|
|
// read verbatim until end tag of current level is reached
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
|
|
|
QString XmlReader::readXml()
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
int level = 1;
|
2018-08-14 10:03:49 +02:00
|
|
|
for (QXmlStreamReader::TokenType t = readNext(); t != QXmlStreamReader::EndElement; t = readNext()) {
|
2018-12-07 15:06:03 +01:00
|
|
|
switch (t) {
|
2016-11-19 11:51:21 +01:00
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
htmlToString(level, &s);
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::EndElement:
|
2018-08-14 10:03:49 +02:00
|
|
|
break;
|
2016-11-19 11:51:21 +01:00
|
|
|
case QXmlStreamReader::Characters:
|
|
|
|
s += text().toString().toHtmlEscaped();
|
|
|
|
break;
|
|
|
|
case QXmlStreamReader::Comment:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qDebug("htmlToString: read token: %s", qPrintable(tokenString()));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readPlacement
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
PlaceText readPlacement(XmlReader& e)
|
|
|
|
{
|
|
|
|
const QString& s(e.readElementText());
|
|
|
|
if (s == "auto" || s == "0")
|
|
|
|
return PlaceText::AUTO;
|
|
|
|
if (s == "above" || s == "1")
|
|
|
|
return PlaceText::ABOVE;
|
|
|
|
if (s == "below" || s == "2")
|
|
|
|
return PlaceText::BELOW;
|
|
|
|
if (s == "left" || s == "3")
|
|
|
|
return PlaceText::LEFT;
|
|
|
|
qDebug("unknown placement value <%s>", qPrintable(s));
|
|
|
|
return PlaceText::AUTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// spannerValues
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
const SpannerValues* XmlReader::spannerValues(int id) const
|
|
|
|
{
|
|
|
|
for (const SpannerValues& v : _spannerValues) {
|
|
|
|
if (v.spannerId == id)
|
|
|
|
return &v;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// addSpanner
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::addSpanner(int id, Spanner* s)
|
|
|
|
{
|
|
|
|
_spanner.append(std::pair<int, Spanner*>(id, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// removeSpanner
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::removeSpanner(const Spanner* s)
|
|
|
|
{
|
|
|
|
for (auto i : _spanner) {
|
|
|
|
if (i.second == s) {
|
|
|
|
_spanner.removeOne(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// findSpanner
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
Spanner* XmlReader::findSpanner(int id)
|
|
|
|
{
|
|
|
|
for (auto i : _spanner) {
|
|
|
|
if (i.first == id)
|
|
|
|
return i.second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// spannerId
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
int XmlReader::spannerId(const Spanner* s)
|
|
|
|
{
|
|
|
|
for (auto i : _spanner) {
|
|
|
|
if (i.second == s)
|
|
|
|
return i.first;
|
|
|
|
}
|
|
|
|
qDebug("XmlReader::spannerId not found");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:16:16 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// addUserTextStyle
|
|
|
|
// return false if mapping is not possible
|
|
|
|
// (too many user text styles)
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-08-01 11:46:07 +02:00
|
|
|
Tid XmlReader::addUserTextStyle(const QString& name)
|
2018-06-11 14:16:16 +02:00
|
|
|
{
|
|
|
|
qDebug("%s", qPrintable(name));
|
2018-08-01 11:46:07 +02:00
|
|
|
Tid id = Tid::TEXT_STYLES;
|
2018-06-11 14:16:16 +02:00
|
|
|
if (userTextStyles.size() == 0)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER1;
|
2018-06-11 14:16:16 +02:00
|
|
|
else if (userTextStyles.size() == 1)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER2;
|
2018-06-11 14:16:16 +02:00
|
|
|
else if (userTextStyles.size() == 2)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER3;
|
2018-06-11 14:16:16 +02:00
|
|
|
else if (userTextStyles.size() == 3)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER4;
|
2018-06-11 14:16:16 +02:00
|
|
|
else if (userTextStyles.size() == 4)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER5;
|
2018-06-11 14:16:16 +02:00
|
|
|
else if (userTextStyles.size() == 5)
|
2018-08-01 11:46:07 +02:00
|
|
|
id = Tid::USER6;
|
2018-06-11 14:16:16 +02:00
|
|
|
else
|
|
|
|
qDebug("too many user defined textstyles");
|
2018-08-01 11:46:07 +02:00
|
|
|
if (id != Tid::TEXT_STYLES)
|
2018-06-11 14:16:16 +02:00
|
|
|
userTextStyles.push_back({name, id});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// lookupUserTextStyle
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-08-01 11:46:07 +02:00
|
|
|
Tid XmlReader::lookupUserTextStyle(const QString& name)
|
2018-06-11 14:16:16 +02:00
|
|
|
{
|
|
|
|
for (const auto& i : userTextStyles) {
|
|
|
|
if (i.name == name)
|
|
|
|
return i.ss;
|
|
|
|
}
|
2018-08-01 11:46:07 +02:00
|
|
|
return Tid::TEXT_STYLES; // not found
|
2018-06-11 14:16:16 +02:00
|
|
|
}
|
2018-05-26 20:01:06 +02:00
|
|
|
|
2018-12-12 17:28:59 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// performReadAhead
|
|
|
|
// If f is called, the device will be non-sequential and
|
|
|
|
// open. Reading position equals to the current value of
|
|
|
|
// characterOffset(), but it is possible to seek for any
|
|
|
|
// other position inside f.
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::performReadAhead(std::function<void(QIODevice&)> f)
|
|
|
|
{
|
|
|
|
if (!_readAheadDevice || _readAheadDevice->isSequential())
|
|
|
|
return;
|
|
|
|
if (!_readAheadDevice->isOpen())
|
|
|
|
_readAheadDevice->open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
const auto pos = _readAheadDevice->pos();
|
|
|
|
_readAheadDevice->seek(characterOffset());
|
|
|
|
f(*_readAheadDevice);
|
|
|
|
_readAheadDevice->seek(pos);
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:01:06 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// addConnectorInfo
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-12-02 00:38:46 +01:00
|
|
|
void XmlReader::addConnectorInfo(std::unique_ptr<ConnectorInfoReader> c)
|
2018-05-26 20:01:06 +02:00
|
|
|
{
|
2018-12-02 00:38:46 +01:00
|
|
|
_connectors.push_back(std::move(c));
|
|
|
|
ConnectorInfoReader* c1 = _connectors.back().get();
|
|
|
|
c1->update();
|
|
|
|
for (std::unique_ptr<ConnectorInfoReader>& c2 : _connectors) {
|
|
|
|
if (c2->connect(c1)) {
|
|
|
|
if (c2->finished()) {
|
|
|
|
c2->addToScore(pasteMode());
|
|
|
|
removeConnector(c2.get());
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// removeConnector
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2018-12-02 00:38:46 +01:00
|
|
|
void XmlReader::removeConnector(const ConnectorInfoReader* c)
|
2018-05-26 20:01:06 +02:00
|
|
|
{
|
|
|
|
while (c->prev())
|
|
|
|
c = c->prev();
|
|
|
|
while (c) {
|
2018-09-07 14:13:44 +02:00
|
|
|
ConnectorInfoReader* next = c->next();
|
2018-12-02 00:38:46 +01:00
|
|
|
for (auto it = _connectors.begin(); it != _connectors.end(); ++it) {
|
|
|
|
if (it->get() == c) {
|
|
|
|
_connectors.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-07 14:13:44 +02:00
|
|
|
c = next;
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// checkConnectors
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::checkConnectors()
|
|
|
|
{
|
2018-12-02 00:38:46 +01:00
|
|
|
for (std::unique_ptr<ConnectorInfoReader>& c : _pendingConnectors)
|
|
|
|
addConnectorInfo(std::move(c));
|
2018-05-26 20:01:06 +02:00
|
|
|
_pendingConnectors.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// distanceSort
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
static bool distanceSort(const QPair<int, QPair<ConnectorInfoReader*, ConnectorInfoReader*>>& p1, const QPair<int, QPair<ConnectorInfoReader*, ConnectorInfoReader*>>& p2)
|
|
|
|
{
|
|
|
|
return p1.first < p2.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// reconnectBrokenConnectors
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::reconnectBrokenConnectors()
|
|
|
|
{
|
2018-12-02 00:38:46 +01:00
|
|
|
if (_connectors.empty())
|
2018-05-26 20:01:06 +02:00
|
|
|
return;
|
2018-12-02 00:38:46 +01:00
|
|
|
qDebug("Reconnecting broken connectors (%d nodes)", int(_connectors.size()));
|
2018-05-26 20:01:06 +02:00
|
|
|
QList<QPair<int, QPair<ConnectorInfoReader*, ConnectorInfoReader*>>> brokenPairs;
|
2018-12-02 00:38:46 +01:00
|
|
|
for (int i = 1; i < int(_connectors.size()); ++i) {
|
2018-05-26 20:01:06 +02:00
|
|
|
for (int j = 0; j < i; ++j) {
|
2018-12-02 00:38:46 +01:00
|
|
|
ConnectorInfoReader* c1 = _connectors[i].get();
|
|
|
|
ConnectorInfoReader* c2 = _connectors[j].get();
|
|
|
|
int d = c1->connectionDistance(*c2);
|
2018-05-26 20:01:06 +02:00
|
|
|
if (d >= 0)
|
2018-12-02 00:38:46 +01:00
|
|
|
brokenPairs.append(qMakePair(d, qMakePair(c1, c2)));
|
2018-05-26 20:01:06 +02:00
|
|
|
else
|
2018-12-02 00:38:46 +01:00
|
|
|
brokenPairs.append(qMakePair(-d, qMakePair(c2, c1)));
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(brokenPairs.begin(), brokenPairs.end(), distanceSort);
|
|
|
|
for (auto& distPair : brokenPairs) {
|
|
|
|
if (distPair.first == INT_MAX)
|
|
|
|
continue;
|
|
|
|
auto& pair = distPair.second;
|
|
|
|
if (pair.first->next() || pair.second->prev())
|
|
|
|
continue;
|
|
|
|
pair.first->forceConnect(pair.second);
|
|
|
|
}
|
|
|
|
QSet<ConnectorInfoReader*> reconnected;
|
2018-12-02 00:38:46 +01:00
|
|
|
for (auto& conn : _connectors) {
|
|
|
|
ConnectorInfoReader* c = conn.get();
|
|
|
|
if (c->finished())
|
|
|
|
reconnected.insert(static_cast<ConnectorInfoReader*>(c->start()));
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
for (ConnectorInfoReader* cptr : reconnected) {
|
|
|
|
cptr->addToScore(pasteMode());
|
2018-12-02 00:38:46 +01:00
|
|
|
removeConnector(cptr);
|
2018-05-26 20:01:06 +02:00
|
|
|
}
|
|
|
|
qDebug("reconnected %d broken connectors", reconnected.count());
|
|
|
|
}
|
2018-06-17 07:06:25 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// addLink
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::addLink(Staff* s, LinkedElements* link)
|
|
|
|
{
|
|
|
|
int staff = s->idx();
|
|
|
|
const bool masterScore = s->score()->isMaster();
|
|
|
|
if (!masterScore)
|
|
|
|
staff *= -1;
|
|
|
|
|
|
|
|
QList<QPair<LinkedElements*, Location>>& staffLinks = _staffLinkedElements[staff];
|
|
|
|
if (!masterScore) {
|
|
|
|
if (!staffLinks.empty()
|
|
|
|
&& (link->mainElement()->score() != staffLinks.front().first->mainElement()->score())
|
|
|
|
)
|
|
|
|
staffLinks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Location l = location(true);
|
|
|
|
_linksIndexer.assignLocalIndex(l);
|
|
|
|
staffLinks.push_back(qMakePair(link, l));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// getLink
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
LinkedElements* XmlReader::getLink(bool masterScore, const Location& l, int localIndexDiff)
|
|
|
|
{
|
|
|
|
int staff = l.staff();
|
|
|
|
if (!masterScore)
|
|
|
|
staff *= -1;
|
|
|
|
const int localIndex = _linksIndexer.assignLocalIndex(l) + localIndexDiff;
|
|
|
|
QList<QPair<LinkedElements*, Location>>& staffLinks = _staffLinkedElements[staff];
|
2019-11-03 13:08:46 +01:00
|
|
|
|
|
|
|
if (!staffLinks.isEmpty() && staffLinks.constLast().second == l) {
|
|
|
|
// This element potentially affects local index for "main"
|
|
|
|
// elements that may go afterwards at the same tick, so
|
|
|
|
// append it to staffLinks as well.
|
|
|
|
staffLinks.push_back(staffLinks.constLast()); // nothing should reference exactly this local index, so it shouldn't matter what to append
|
|
|
|
}
|
|
|
|
|
2018-06-17 07:06:25 +02:00
|
|
|
for (int i = 0; i < staffLinks.size(); ++i) {
|
|
|
|
if (staffLinks[i].second == l) {
|
|
|
|
if (localIndex == 0)
|
|
|
|
return staffLinks[i].first;
|
|
|
|
i += localIndex;
|
|
|
|
if ((i < 0) || (i >= staffLinks.size()))
|
|
|
|
return nullptr;
|
|
|
|
if (staffLinks[i].second == l)
|
|
|
|
return staffLinks[i].first;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// assignLocalIndex
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
int LinksIndexer::assignLocalIndex(const Location& mainElementLocation)
|
|
|
|
{
|
|
|
|
if (_lastLinkedElementLoc == mainElementLocation)
|
|
|
|
return (++_lastLocalIndex);
|
|
|
|
_lastLocalIndex = 0;
|
|
|
|
_lastLinkedElementLoc = mainElementLocation;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-30 15:13:54 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// rtick
|
|
|
|
// return relative position in measure
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
Fraction XmlReader::rtick() const
|
|
|
|
{
|
|
|
|
return _curMeasure ? _tick - _curMeasure->tick() : _tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// setTick
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::setTick(const Fraction& f)
|
|
|
|
{
|
|
|
|
_tick = f.reduced();
|
2019-02-18 09:58:34 +01:00
|
|
|
_intTick = _tick.ticks();
|
2019-01-30 15:13:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// incTick
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
void XmlReader::incTick(const Fraction& f)
|
|
|
|
{
|
|
|
|
_tick += f;
|
|
|
|
_tick.reduce();
|
2019-02-18 09:58:34 +01:00
|
|
|
_intTick += f.ticks();
|
2019-01-30 15:13:54 +01:00
|
|
|
}
|
2016-11-19 11:51:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|