Skip to content

Commit

Permalink
Minor code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn authored and dogboydog committed Sep 7, 2024
1 parent 17fbc28 commit fe5c55a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
31 changes: 16 additions & 15 deletions src/libtiled/propertytype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,22 @@ void PropertyTypes::mergeObjectTypes(const QVector<ObjectType> &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.
Expand All @@ -536,21 +552,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; i<mTypes.count(); i++)
if (mTypes[i]->name == 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.
Expand Down
3 changes: 2 additions & 1 deletion src/libtiled/propertytype.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ class TILEDSHARED_EXPORT PropertyTypes
void merge(PropertyTypes types);
void mergeObjectTypes(const QVector<ObjectType> &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;
Expand Down
39 changes: 21 additions & 18 deletions src/tiled/editableproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

#include "editableproject.h"

#include "preferences.h"
#include "projectdocument.h"
#include "projectmanager.h"

Expand All @@ -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;
Expand All @@ -51,10 +58,12 @@ QStringList EditableProject::folders() const
return project()->folders();
}


bool EditableProject::isReadOnly() const
QVector<ScriptPropertyType *>EditableProject::propertyTypes() const
{
return false;
QVector<ScriptPropertyType*> scriptTypes;
for (const PropertyType *type : *project()->propertyTypes())
scriptTypes.append(toScriptType(type));
return scriptTypes;
}

QSharedPointer<Document> EditableProject::createDocument()
Expand All @@ -64,19 +73,21 @@ QSharedPointer<Document> EditableProject::createDocument()
return nullptr;
}


ScriptPropertyType *EditableProject::toScriptType(const PropertyType *type) const
{
if (!type)
return nullptr;

if (type->isEnum())
return new ScriptEnumPropertyType(static_cast<const EnumPropertyType *>(type));

if (type->isClass())
switch (type->type) {
case PropertyType::PT_Invalid:
break;
case PropertyType::PT_Class:
return new ScriptClassPropertyType(static_cast<const ClassPropertyType *>(type));
case PropertyType::PT_Enum:
return new ScriptEnumPropertyType(static_cast<const EnumPropertyType *>(type));
}

return new ScriptPropertyType(type);
return nullptr;
}

ScriptPropertyType *EditableProject::findTypeByName(const QString &name)
Expand All @@ -88,22 +99,14 @@ 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
project()->propertyTypes()->removeAt(index);
applyPropertyChanges();
}

QVector<ScriptPropertyType *>EditableProject::propertyTypes() const
{
QVector<ScriptPropertyType*> scriptTypes;
for (const PropertyType *type : *project()->propertyTypes())
scriptTypes.append(toScriptType(type));
return scriptTypes;
}

void EditableProject::applyPropertyChanges()
{
emit Preferences::instance()->propertyTypesChanged();
Expand Down
5 changes: 4 additions & 1 deletion src/tiled/editableproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EditableProject final : public EditableAsset
Q_PROPERTY(QString fileName READ fileName)
Q_PROPERTY(QStringList folders READ folders)
Q_PROPERTY(QVector<ScriptPropertyType *> propertyTypes READ propertyTypes)

public:
EditableProject(ProjectDocument *projectDocument, QObject *parent = nullptr);

Expand All @@ -50,13 +51,15 @@ class EditableProject final : public EditableAsset
QString automappingRulesFile() const;
QString fileName() const;
QStringList folders() const;
QVector<ScriptPropertyType*> propertyTypes() const;

Project *project() const;

QSharedPointer<Document> createDocument() override;

Q_INVOKABLE void removeTypeByName(const QString &name);
Q_INVOKABLE ScriptPropertyType *findTypeByName(const QString &name);
QVector<ScriptPropertyType*> propertyTypes() const;

private:
ScriptPropertyType *toScriptType(const PropertyType *type) const;
void applyPropertyChanges();
Expand Down
8 changes: 3 additions & 5 deletions src/tiled/scriptpropertytype.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* scriptimage.cpp
* Copyright 2020, Thorbjørn Lindeijer <[email protected]>
* scriptpropertytype.cpp
* Copyright 2024, chris <[email protected]>
*
* This file is part of Tiled.
*
Expand All @@ -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;
}
Expand Down
17 changes: 7 additions & 10 deletions src/tiled/scriptpropertytype.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* scriptimage.h
* Copyright 2020, Thorbjørn Lindeijer <[email protected]>
* scriptpropertytype.h
* Copyright 2024, chris <[email protected]>
*
* This file is part of Tiled.
*
Expand All @@ -20,7 +20,6 @@

#pragma once

#include "preferences.h"
#include "propertytype.h"

#include <QJSEngine>
Expand All @@ -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(); }
Expand All @@ -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 {
Expand All @@ -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?
Expand Down Expand Up @@ -121,7 +120,6 @@ class ScriptClassPropertyType : public ScriptPropertyType
//void setUsageFlags(int value) { mClassType->setUsageFlags(value); }

private:

const ClassPropertyType *mClassType;
};

Expand All @@ -131,4 +129,3 @@ void registerPropertyTypes(QJSEngine *jsEngine);
} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::ScriptPropertyType*)

0 comments on commit fe5c55a

Please sign in to comment.