Skip to content

Commit

Permalink
Support nested list properties in TMX format
Browse files Browse the repository at this point in the history
Also switched from "value" to "item" elements.
  • Loading branch information
bjorn committed Jan 22, 2025
1 parent a000a8b commit 334deed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ QVariant MapReaderPrivate::readPropertyValue(const ExportContext &context)
} else if (xml.isStartElement()) {
if (xml.name() == QLatin1String("properties"))
exportValue.value = readProperties();
else if (xml.name() == QLatin1String("value"))
else if (xml.name() == QLatin1String("item"))
values.append(readPropertyValue(context));
else
readUnknownElement();
Expand Down
74 changes: 39 additions & 35 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class MapWriterPrivate
void writeGroupLayer(QXmlStreamWriter &w, const GroupLayer &groupLayer);
void writeProperties(QXmlStreamWriter &w,
const Properties &properties);
void writeExportValue(QXmlStreamWriter &w,
const QVariant &value, const ExportContext &context);
void writeImage(QXmlStreamWriter &w,
const QUrl &source,
const QPixmap &image,
Expand Down Expand Up @@ -902,48 +904,50 @@ void MapWriterPrivate::writeProperties(QXmlStreamWriter &w,
w.writeStartElement(QStringLiteral("property"));
w.writeAttribute(QStringLiteral("name"), it.key());

const auto exportValue = context.toExportValue(it.value());
if (exportValue.typeName != QLatin1String("string"))
w.writeAttribute(QStringLiteral("type"), exportValue.typeName);
if (!exportValue.propertyTypeName.isEmpty())
w.writeAttribute(QStringLiteral("propertytype"), exportValue.propertyTypeName);

switch (exportValue.value.userType()) {
case QMetaType::QVariantList: {
const auto values = exportValue.value.toList();
for (const QVariant &v : values) {
const auto exportValue = context.toExportValue(v);
w.writeStartElement(QStringLiteral("value"));
if (exportValue.typeName != QLatin1String("string"))
w.writeAttribute(QStringLiteral("type"), exportValue.typeName);
if (!exportValue.propertyTypeName.isEmpty())
w.writeAttribute(QStringLiteral("propertytype"), exportValue.propertyTypeName);
w.writeCharacters(exportValue.value.toString());
w.writeEndElement();
}
break;
}
case QMetaType::QVariantMap:
// Write out the original value, so that the propertytype attribute
// can also be written for their members where applicable.
writeProperties(w, it.value().value<PropertyValue>().value.toMap());
break;
default:
const QString value = exportValue.value.toString();

if (value.contains(QLatin1Char('\n')))
w.writeCharacters(value);
else
w.writeAttribute(QStringLiteral("value"), value);
break;
}
writeExportValue(w, it.value(), context);

w.writeEndElement(); // </property>
}

w.writeEndElement(); // </properties>
}

void MapWriterPrivate::writeExportValue(QXmlStreamWriter &w,
const QVariant &value,
const ExportContext &context)
{
const auto exportValue = context.toExportValue(value);
if (exportValue.typeName != QLatin1String("string"))
w.writeAttribute(QStringLiteral("type"), exportValue.typeName);
if (!exportValue.propertyTypeName.isEmpty())
w.writeAttribute(QStringLiteral("propertytype"), exportValue.propertyTypeName);

switch (exportValue.value.userType()) {
case QMetaType::QVariantList: {
const auto values = exportValue.value.toList();
for (const QVariant &value : values) {
w.writeStartElement(QStringLiteral("item"));
writeExportValue(w, value, context);
w.writeEndElement(); // </item>
}
break;
}
case QMetaType::QVariantMap:
// Write out the original value, so that the propertytype attribute
// can also be written for their members where applicable.
writeProperties(w, value.value<PropertyValue>().value.toMap());
break;
default:
const QString value = exportValue.value.toString();

if (value.contains(QLatin1Char('\n')))
w.writeCharacters(value);
else
w.writeAttribute(QStringLiteral("value"), value);
break;
}
}

void MapWriterPrivate::writeImage(QXmlStreamWriter &w,
const QUrl &source,
const QPixmap &image,
Expand Down

0 comments on commit 334deed

Please sign in to comment.