MuseScore/miditools/xmlreader.cpp

151 lines
4.2 KiB
C++
Raw Normal View History

2013-04-08 16:28:21 +02:00
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2011 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "xmlreader.h"
//---------------------------------------------------------
// XmlReader
//---------------------------------------------------------
2020-05-29 18:47:27 +02:00
XmlReader::XmlReader(QFile* d)
: QXmlStreamReader(d)
2020-05-26 15:54:26 +02:00
{
docName = d->fileName();
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// intAttribute
//---------------------------------------------------------
int XmlReader::intAttribute(const char* s, int _default, int base) const
2020-05-26 15:54:26 +02:00
{
bool ok;
if (attributes().hasAttribute(s)) {
return attributes().value(s).toString().toInt(&ok, base);
} else {
return _default;
}
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// doubleAttribute
//---------------------------------------------------------
double XmlReader::doubleAttribute(const char* s) const
2020-05-26 15:54:26 +02:00
{
return attributes().value(s).toString().toDouble();
}
2013-04-08 16:28:21 +02:00
double XmlReader::doubleAttribute(const char* s, double _default) const
2020-05-26 15:54:26 +02:00
{
if (attributes().hasAttribute(s)) {
return attributes().value(s).toUtf8().toDouble();
} else {
return _default;
}
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// attribute
//---------------------------------------------------------
QString XmlReader::attribute(const char* s, const QString& _default) const
2020-05-26 15:54:26 +02:00
{
if (attributes().hasAttribute(s)) {
return attributes().value(s).toString();
} else {
return _default;
}
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// hasAttribute
//---------------------------------------------------------
bool XmlReader::hasAttribute(const char* s) const
2020-05-26 15:54:26 +02:00
{
return attributes().hasAttribute(s);
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// readPoint
//---------------------------------------------------------
QPointF XmlReader::readPoint()
2020-05-26 15:54:26 +02:00
{
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
QPointF p;
p.setX(doubleAttribute("x", 0.0));
p.setY(doubleAttribute("y", 0.0));
readNext();
return p;
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// readSize
//---------------------------------------------------------
QSizeF XmlReader::readSize()
2020-05-26 15:54:26 +02:00
{
Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
QSizeF p;
p.setWidth(doubleAttribute("w", 0.0));
p.setHeight(doubleAttribute("h", 0.0));
skipCurrentElement();
return p;
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// readRect
//---------------------------------------------------------
QRectF XmlReader::readRect()
2020-05-26 15:54:26 +02:00
{
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;
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// unknown
// unknown tag read
//---------------------------------------------------------
void XmlReader::unknown() const
2020-05-26 15:54:26 +02:00
{
if (QXmlStreamReader::error()) {
qDebug("StreamReaderError: %s", qPrintable(errorString()));
}
qDebug("%s: xml read error at line %lld col %lld: %s",
qPrintable(docName), lineNumber(), columnNumber(),
name().toUtf8().data());
}
2013-04-08 16:28:21 +02:00
//---------------------------------------------------------
// error
//---------------------------------------------------------
void XmlReader::error(const QString& s) const
2020-05-26 15:54:26 +02:00
{
if (QXmlStreamReader::error()) {
qDebug("StreamReaderError: %s", qPrintable(errorString()));
}
qDebug("%s: %s at line %lld col %lld: %s",
qPrintable(docName), qPrintable(s), lineNumber(), columnNumber(),
name().toUtf8().data());
}