Skip to content

Commit

Permalink
Made property types work in the Object Type editor
Browse files Browse the repository at this point in the history
* Code cleanups and simplifications (use QVariant in more places)
* Use 'text' and 'float' instead of 'QString' and 'double' type names
* Improved the Add Property dialog layout
* Remember last used type when adding new properties

Closes #365
  • Loading branch information
bjorn committed Feb 14, 2016
1 parent 6c92369 commit 8cc2b13
Show file tree
Hide file tree
Showing 23 changed files with 879 additions and 866 deletions.
11 changes: 7 additions & 4 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,14 @@ void MapReaderPrivate::readProperty(Properties *properties)
}
}

if(propertyType.isEmpty() || propertyType.isNull()){
propertyType = QLatin1String(QVariant::typeToName(QVariant::String)); // Default to String
}
QVariant variant(propertyValue);
variant.convert(QVariant::nameToType(propertyType.toStdString().c_str()));

if (!propertyType.isEmpty()) {
QVariant::Type type = nameToType(propertyType);
if (type != QVariant::Invalid)
variant.convert(nameToType(propertyType));
}

properties->insert(propertyName, variant);
}

Expand Down
13 changes: 8 additions & 5 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,16 @@ void MapWriterPrivate::writeProperties(QXmlStreamWriter &w,
w.writeStartElement(QLatin1String("property"));
w.writeAttribute(QLatin1String("name"), it.key());

QString typeName = typeToName(it.value().type());
if (typeName != QLatin1String("string"))
w.writeAttribute(QLatin1String("type"), typeName);

const QString &value = it.value().toString();
if (value.contains(QLatin1Char('\n'))) {
if (value.contains(QLatin1Char('\n')))
w.writeCharacters(value);
} else {
w.writeAttribute(QLatin1String("value"), it.value().toString());
w.writeAttribute(QLatin1String("type"), QLatin1String(it.value().typeName()));
}
else
w.writeAttribute(QLatin1String("value"), value);

w.writeEndElement();
}

Expand Down
24 changes: 12 additions & 12 deletions src/libtiled/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ class TILEDSHARED_EXPORT Object
/**
* Returns the value of the object's \a name property.
*/
QString property(const QString &name) const
{ return mProperties.value(name).toString(); }
QVariant property(const QString &name) const
{ return mProperties.value(name); }

QVariant::Type propertyType(const QString &name) const {
return mProperties.value(name).type();
}
/**
* Returns the value of the object's \a name property, as a string.
*
* This is a workaround for the Python plugin, because I do not know how
* to pass a QVariant back to Python.
*/
QString propertyAsString(const QString &name) const
{ return mProperties.value(name).toString(); }

/**
* Returns whether this object has a property with the given \a name.
Expand All @@ -105,13 +110,8 @@ class TILEDSHARED_EXPORT Object
/**
* Sets the value of the object's \a name property to \a value.
*/
void setProperty(const QString &name, const QString &value, QVariant::Type type = QVariant::String)
{
QVariant v;
v.setValue(value);
v.convert(type);
mProperties.insert(name, v);
}
void setProperty(const QString &name, const QVariant &value)
{ mProperties.insert(name, value); }

/**
* Removes the property with the given \a name.
Expand Down
24 changes: 23 additions & 1 deletion src/libtiled/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "properties.h"

using namespace Tiled;
namespace Tiled {

void Properties::merge(const Properties &other)
{
Expand Down Expand Up @@ -59,3 +59,25 @@ void AggregatedProperties::aggregate(const Properties &properties)

++mAggregatedCount;
}

QString typeToName(QVariant::Type type)
{
if (type == QVariant::String)
return QStringLiteral("string");
if (type == QVariant::Double)
return QStringLiteral("float");

return QLatin1String(QVariant::typeToName(type));
}

QVariant::Type nameToType(const QString &name)
{
if (name == QLatin1String("string"))
return QVariant::String;
if (name == QLatin1String("float"))
return QVariant::Double;

return QVariant::nameToType(name.toLatin1().constData());
}

} // namespace Tiled
3 changes: 3 additions & 0 deletions src/libtiled/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class TILEDSHARED_EXPORT AggregatedProperties : public QMap<QString, AggregatedP
};


TILEDSHARED_EXPORT QString typeToName(QVariant::Type);
TILEDSHARED_EXPORT QVariant::Type nameToType(const QString &name);

} // namespace Tiled

#endif // PROPERTIES_H
2 changes: 1 addition & 1 deletion src/plugins/csv/csvplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool CsvPlugin::write(const Map *map, const QString &fileName)
const Cell &cell = tileLayer->cellAt(x, y);
const Tile *tile = cell.tile;
if (tile && tile->hasProperty(QLatin1String("name"))) {
file.write(tile->property(QLatin1String("name")).toUtf8());
file.write(tile->property(QLatin1String("name")).toString().toUtf8());
} else {
const int id = tile ? tile->id() : -1;
file.write(QByteArray::number(id));
Expand Down
Loading

0 comments on commit 8cc2b13

Please sign in to comment.