From a8befc0f968dfec3913c53f312c1937de5e4c0b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Tue, 18 Jun 2024 15:57:03 +0200 Subject: [PATCH] Minor code style changes --- src/libtiled/propertytype.cpp | 31 +++++++++++++------------ src/libtiled/propertytype.h | 3 ++- src/tiled/editableproject.cpp | 39 +++++++++++++++++--------------- src/tiled/editableproject.h | 5 +++- src/tiled/scriptpropertytype.cpp | 8 +++---- src/tiled/scriptpropertytype.h | 17 ++++++-------- 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/libtiled/propertytype.cpp b/src/libtiled/propertytype.cpp index fe58bb07d3..3e3cf1b6f3 100644 --- a/src/libtiled/propertytype.cpp +++ b/src/libtiled/propertytype.cpp @@ -522,6 +522,22 @@ void PropertyTypes::mergeObjectTypes(const QVector &objectTypes) } } +/** + * Returns the index of the type of the given name, or -1 if no such type is + * found. + */ +int PropertyTypes::findIndexByName(const QString &name) const +{ + if (name.isEmpty()) + return -1; + + for (int i = 0; i < mTypes.count(); i++) + if (mTypes[i]->name == name) + return i; + + return -1; +} + /** * Returns a pointer to the PropertyType matching the given \a typeId, or * nullptr if it can't be found. @@ -534,21 +550,6 @@ const PropertyType *PropertyTypes::findTypeById(int typeId) const return it == mTypes.end() ? nullptr : *it; } -/** - * Returns the index of the type of the given name, - * or -1 if no such type is found. - */ -const int PropertyTypes::findIndexByName(const QString &name) const -{ - if (name.isEmpty()) - return -1; - - for (int i =0; iname == name) - return i; - - return -1; -} /** * Returns a pointer to the PropertyType matching the given \a name and * \a usageFlags, or nullptr if it can't be found. diff --git a/src/libtiled/propertytype.h b/src/libtiled/propertytype.h index 8d41892a19..e5c8f5188d 100644 --- a/src/libtiled/propertytype.h +++ b/src/libtiled/propertytype.h @@ -200,8 +200,9 @@ class TILEDSHARED_EXPORT PropertyTypes void merge(PropertyTypes types); void mergeObjectTypes(const QVector &objectTypes); + int findIndexByName(const QString &name) const; + const PropertyType *findTypeById(int typeId) const; - const int findIndexByName(const QString &name) const; const PropertyType *findTypeByName(const QString &name, int usageFlags = ClassPropertyType::AnyUsage) const; const PropertyType *findPropertyValueType(const QString &name) const; const ClassPropertyType *findClassFor(const QString &name, const Object &object) const; diff --git a/src/tiled/editableproject.cpp b/src/tiled/editableproject.cpp index 5e8a851d2d..e08694c07a 100644 --- a/src/tiled/editableproject.cpp +++ b/src/tiled/editableproject.cpp @@ -20,6 +20,8 @@ */ #include "editableproject.h" + +#include "preferences.h" #include "projectdocument.h" #include "projectmanager.h" @@ -31,6 +33,11 @@ EditableProject::EditableProject(ProjectDocument *projectDocument, QObject *pare setDocument(projectDocument); } +bool EditableProject::isReadOnly() const +{ + return false; +} + QString EditableProject::extensionsPath() const { return project()->mExtensionsPath; @@ -51,10 +58,12 @@ QStringList EditableProject::folders() const return project()->folders(); } - -bool EditableProject::isReadOnly() const +QVectorEditableProject::propertyTypes() const { - return false; + QVector scriptTypes; + for (const PropertyType *type : *project()->propertyTypes()) + scriptTypes.append(toScriptType(type)); + return scriptTypes; } QSharedPointer EditableProject::createDocument() @@ -64,19 +73,21 @@ QSharedPointer EditableProject::createDocument() return nullptr; } - ScriptPropertyType *EditableProject::toScriptType(const PropertyType *type) const { if (!type) return nullptr; - if (type->isEnum()) - return new ScriptEnumPropertyType(static_cast(type)); - - if (type->isClass()) + switch (type->type) { + case PropertyType::PT_Invalid: + break; + case PropertyType::PT_Class: return new ScriptClassPropertyType(static_cast(type)); + case PropertyType::PT_Enum: + return new ScriptEnumPropertyType(static_cast(type)); + } - return new ScriptPropertyType(type); + return nullptr; } ScriptPropertyType *EditableProject::findTypeByName(const QString &name) @@ -88,7 +99,7 @@ ScriptPropertyType *EditableProject::findTypeByName(const QString &name) void EditableProject::removeTypeByName(const QString &name) { int index = project()->propertyTypes()->findIndexByName(name); - if (index < 0 ) + if (index < 0) return // TODO the type isn't actually being deleted even when index >= 0 @@ -96,14 +107,6 @@ void EditableProject::removeTypeByName(const QString &name) applyPropertyChanges(); } -QVectorEditableProject::propertyTypes() const -{ - QVector scriptTypes; - for (const PropertyType *type : *project()->propertyTypes()) - scriptTypes.append(toScriptType(type)); - return scriptTypes; -} - void EditableProject::applyPropertyChanges() { emit Preferences::instance()->propertyTypesChanged(); diff --git a/src/tiled/editableproject.h b/src/tiled/editableproject.h index 7a26069f8f..08a2612b6d 100644 --- a/src/tiled/editableproject.h +++ b/src/tiled/editableproject.h @@ -40,6 +40,7 @@ class EditableProject final : public EditableAsset Q_PROPERTY(QString fileName READ fileName) Q_PROPERTY(QStringList folders READ folders) Q_PROPERTY(QVector propertyTypes READ propertyTypes) + public: EditableProject(ProjectDocument *projectDocument, QObject *parent = nullptr); @@ -50,13 +51,15 @@ class EditableProject final : public EditableAsset QString automappingRulesFile() const; QString fileName() const; QStringList folders() const; + QVector propertyTypes() const; + Project *project() const; QSharedPointer createDocument() override; Q_INVOKABLE void removeTypeByName(const QString &name); Q_INVOKABLE ScriptPropertyType *findTypeByName(const QString &name); - QVector propertyTypes() const; + private: ScriptPropertyType *toScriptType(const PropertyType *type) const; void applyPropertyChanges(); diff --git a/src/tiled/scriptpropertytype.cpp b/src/tiled/scriptpropertytype.cpp index a5c2768384..3dc21eb42a 100644 --- a/src/tiled/scriptpropertytype.cpp +++ b/src/tiled/scriptpropertytype.cpp @@ -1,6 +1,6 @@ /* - * scriptimage.cpp - * Copyright 2020, Thorbjørn Lindeijer + * scriptpropertytype.cpp + * Copyright 2024, chris * * This file is part of Tiled. * @@ -19,12 +19,10 @@ */ #include "scriptpropertytype.h" -#include "project.h" -#include "projectmanager.h" namespace Tiled { -QString ScriptPropertyType::name() const +const QString &ScriptPropertyType::name() const { return mType->name; } diff --git a/src/tiled/scriptpropertytype.h b/src/tiled/scriptpropertytype.h index bf3554acf1..456db69be2 100644 --- a/src/tiled/scriptpropertytype.h +++ b/src/tiled/scriptpropertytype.h @@ -1,6 +1,6 @@ /* - * scriptimage.h - * Copyright 2020, Thorbjørn Lindeijer + * scriptpropertytype.h + * Copyright 2024, chris * * This file is part of Tiled. * @@ -20,7 +20,6 @@ #pragma once -#include "preferences.h" #include "propertytype.h" #include @@ -44,7 +43,7 @@ class ScriptPropertyType : public QObject : mType(propertyType) {} - QString name() const; + const QString &name() const; bool isClass() const { return mType->isClass(); } bool isEnum() const { return mType->isEnum(); } QVariant defaultValue() { return mType->defaultValue(); } @@ -62,8 +61,8 @@ class ScriptEnumPropertyType : public ScriptPropertyType public: ScriptEnumPropertyType(const EnumPropertyType *propertyType) - : mEnumType(propertyType), - ScriptPropertyType(propertyType) + : ScriptPropertyType(propertyType) + , mEnumType(propertyType) {} // copied from propertytype.h enum StorageType { @@ -89,8 +88,8 @@ class ScriptClassPropertyType : public ScriptPropertyType public: ScriptClassPropertyType(const ClassPropertyType *propertyType) - : mClassType(propertyType), - ScriptPropertyType(propertyType) + : ScriptPropertyType(propertyType) + , mClassType(propertyType) {} // TODO: a way to avoid duplicating this again? @@ -121,7 +120,6 @@ class ScriptClassPropertyType : public ScriptPropertyType //void setUsageFlags(int value) { mClassType->setUsageFlags(value); } private: - const ClassPropertyType *mClassType; }; @@ -131,4 +129,3 @@ void registerPropertyTypes(QJSEngine *jsEngine); } // namespace Tiled Q_DECLARE_METATYPE(Tiled::ScriptPropertyType*) -