Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

support converting object reference property to and from int #2734

Merged
merged 2 commits into from Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions src/libtiled/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@

namespace Tiled {

QString FilePath::toString(const FilePath &path)
{
return path.url.toString(QUrl::PreferLocalFile);
}

FilePath FilePath::fromString(const QString &string)
{
QUrl url(string);
if (url.isRelative())
url = QUrl::fromLocalFile(string);
return { url };
}


void mergeProperties(Properties &target, const Properties &source)
{
// Based on QMap::unite, but using insert instead of insertMulti
Expand Down Expand Up @@ -158,13 +172,11 @@ QVariant toExportValue(const QVariant &value)
return color.isValid() ? color.name(QColor::HexArgb) : QString();
}

if (type == filePathTypeId()) {
const FilePath filePath = value.value<FilePath>();
return filePath.url.toString(QUrl::PreferLocalFile);
}
if (type == filePathTypeId())
return FilePath::toString(value.value<FilePath>());

if (type == objectRefTypeId())
return value.value<ObjectRef>().id;
return ObjectRef::toInt(value.value<ObjectRef>());

return value;
}
Expand All @@ -177,15 +189,11 @@ QVariant fromExportValue(const QVariant &value, int type)
if (value.userType() == type)
return value;

if (type == filePathTypeId()) {
QUrl url(value.toString());
if (url.isRelative())
url = QUrl::fromLocalFile(value.toString());
return QVariant::fromValue(FilePath { url });
}
if (type == filePathTypeId())
return QVariant::fromValue(FilePath::fromString(value.toString()));

if (type == objectRefTypeId())
return QVariant::fromValue(ObjectRef { value.toInt() });
return QVariant::fromValue(ObjectRef::fromInt(value.toInt()));

QVariant variant(value);
variant.convert(type);
Expand All @@ -212,4 +220,13 @@ QVariant fromExportValue(const QVariant &value, int type, const QDir &dir)
return fromExportValue(value, type);
}

void initializeMetatypes()
{
QMetaType::registerConverter<ObjectRef, int>(&ObjectRef::toInt);
QMetaType::registerConverter<int, ObjectRef>(&ObjectRef::fromInt);

QMetaType::registerConverter<FilePath, QString>(&FilePath::toString);
QMetaType::registerConverter<QString, FilePath>(&FilePath::fromString);
}

} // namespace Tiled
8 changes: 8 additions & 0 deletions src/libtiled/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class TILEDSHARED_EXPORT FilePath

public:
QUrl url;

static QString toString(const FilePath &path);
static FilePath fromString(const QString &string);
};

struct TILEDSHARED_EXPORT ObjectRef
Expand All @@ -55,6 +58,9 @@ struct TILEDSHARED_EXPORT ObjectRef

public:
int id;

static int toInt(const ObjectRef &ref) { return ref.id; }
static ObjectRef fromInt(int id) { return ObjectRef { id }; }
};

class TILEDSHARED_EXPORT AggregatedPropertyData
Expand Down Expand Up @@ -123,6 +129,8 @@ TILEDSHARED_EXPORT QVariant fromExportValue(const QVariant &value, int type);
TILEDSHARED_EXPORT QVariant toExportValue(const QVariant &value, const QDir &dir);
TILEDSHARED_EXPORT QVariant fromExportValue(const QVariant &value, int type, const QDir &dir);

TILEDSHARED_EXPORT void initializeMetatypes();

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::FilePath)
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ int main(int argc, char *argv[])

TiledApplication a(argc, argv);

initializeMetatypes();

a.setOrganizationDomain(QLatin1String("mapeditor.org"));
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
a.setApplicationName(QLatin1String("Tiled"));
Expand Down
1 change: 1 addition & 0 deletions src/tiled/propertiesdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ void PropertiesDock::showContextMenu(const QPoint& pos)
QVariant::Color,
QVariant::Double,
filePathTypeId(),
objectRefTypeId(),
QVariant::Int,
QVariant::String
};
Expand Down
19 changes: 10 additions & 9 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,17 @@ void PropertyBrowser::propertyAdded(Object *object, const QString &name)
{
if (!objectPropertiesRelevant(mDocument, object))
return;
if (mNameToProperty.contains(name)) {
if (QtVariantProperty *property = mNameToProperty.value(name)) {
if (propertyValueAffected(mObject, object, name))
setCustomPropertyValue(mNameToProperty[name], object->property(name));
setCustomPropertyValue(property, object->property(name));
} else {
QVariant value;
if (mObject->hasProperty(name))
value = mObject->property(name);
else
value = predefinedPropertyValue(mObject, name);

createCustomProperty(name, value);
createCustomProperty(name, toDisplayValue(value));
}
updateCustomPropertyColor(name);
}
Expand Down Expand Up @@ -1512,9 +1512,8 @@ QtVariantProperty *PropertyBrowser::createCustomProperty(const QString &name, co
}

mUpdating = true;
const QVariant displayValue = toDisplayValue(value);
QtVariantProperty *property = createProperty(CustomProperty, displayValue.userType(), name);
property->setValue(displayValue);
QtVariantProperty *property = createProperty(CustomProperty, value.userType(), name);
property->setValue(value);
mCustomPropertiesGroup->insertSubProperty(property, precedingProperty);

// Collapse custom color properties, to save space
Expand All @@ -1535,20 +1534,22 @@ void PropertyBrowser::deleteCustomProperty(QtVariantProperty *property)
void PropertyBrowser::setCustomPropertyValue(QtVariantProperty *property,
const QVariant &value)
{
if (value.userType() != property->valueType()) {
const QVariant displayValue = toDisplayValue(value);

if (displayValue.userType() != property->valueType()) {
// Re-creating the property is necessary to change its type
const QString name = property->propertyName();
const bool wasCurrent = currentItem() && currentItem()->property() == property;

deleteCustomProperty(property);
property = createCustomProperty(name, value);
property = createCustomProperty(name, displayValue);
updateCustomPropertyColor(name);

if (wasCurrent)
setCurrentItem(items(property).constFirst());
} else {
mUpdating = true;
property->setValue(toDisplayValue(value));
property->setValue(displayValue);
mUpdating = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/variantpropertymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ bool VariantPropertyManager::isPropertyTypeSupported(int propertyType) const
int VariantPropertyManager::valueType(int propertyType) const
{
if (propertyType == filePathTypeId())
return QVariant::String;
return propertyType;
if (propertyType == displayObjectRefTypeId())
return QVariant::String;
return propertyType;
if (propertyType == tilesetParametersTypeId())
return qMetaTypeId<TilesetDocument*>();
if (propertyType == alignmentTypeId())
Expand Down