Skip to content

Commit

Permalink
prevent duplicate names in property types during adding or rename. al…
Browse files Browse the repository at this point in the history
…low removing members from a class
  • Loading branch information
dogboydog committed Mar 6, 2025
1 parent da70b69 commit 024610a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/tiled/editableproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,21 @@ bool EditableProject::removeTypeByName(const QString &name)
}
ScriptPropertyType *EditableProject::addClassType(const QString &name)
{
if (project()->propertyTypes()->findTypeByName(name)) {
project()->throwDuplicateNameError(name);
return nullptr;
}
SharedPropertyType newClassType = SharedPropertyType(new ClassPropertyType(name));
project()->propertyTypes()->add(newClassType);
applyPropertyChanges();
return findTypeByName(name);
}
ScriptPropertyType *EditableProject::addEnumType(const QString &name)
{
if (project()->propertyTypes()->findTypeByName(name)) {
project()->throwDuplicateNameError(name);
return nullptr;
}
SharedPropertyType newEnumType = SharedPropertyType(new EnumPropertyType(name));
project()->propertyTypes()->add(newEnumType);
applyPropertyChanges();
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#include "project.h"
#include "properties.h"
#include "savefile.h"
#include "scriptmanager.h"

#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QCoreApplication>

namespace Tiled {

Expand Down Expand Up @@ -160,4 +162,9 @@ void Project::removeFolder(int index)
mFolders.removeAt(index);
}

void Project::throwDuplicateNameError(const QString &name)
{
ScriptManager::instance().throwError(QCoreApplication::translate("Error Renaming Type",
"The name '%1' is already in use.").arg(name));
}
} // namespace Tiled
1 change: 1 addition & 0 deletions src/tiled/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class TILED_EDITOR_EXPORT Project : public Object
QString mAutomappingRulesFile;
QVector<Command> mCommands;
CompatibilityVersion mCompatibilityVersion = Tiled_Current;
void throwDuplicateNameError(const QString &name);

private:
QDateTime mLastSaved;
Expand Down
40 changes: 39 additions & 1 deletion src/tiled/scriptpropertytype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,31 @@
#include "preferences.h"
#include "project.h"
#include "projectmanager.h"
#include <QCoreApplication>
#include "scriptpropertytype.h"
#include "scriptmanager.h"

namespace Tiled {

const QString &ScriptPropertyType::name() const
{
return mType->name;
}

void ScriptPropertyType::setName(const QString &name)
{
if (this->name() == name)
{
// nothing to do
return;
}
Project &project = ProjectManager::instance()->project();
if (project.propertyTypes()->findTypeByName(name)) {
project.throwDuplicateNameError(name);
return;
}
mType->name = name;
applyPropertyChanges();
}
void registerPropertyTypes(QJSEngine *jsEngine)
{
jsEngine->globalObject().setProperty(QStringLiteral("EnumPropertyType"),
Expand All @@ -51,6 +67,28 @@ void ScriptPropertyType::applyPropertyChanges()
Project &project = ProjectManager::instance()->project();
project.save();
}
void ScriptClassPropertyType::addMember(const QString &name)
{
if (this->mClassType->members.contains(name))
{
ScriptManager::instance()
.throwError(QCoreApplication::translate("Script Errors", "A class member of the specified name '%1' already exists").arg(name));
return;
}
// todo, how will the user specify the type of a new member ? could be a primitive or a class
this->applyPropertyChanges();
}
void ScriptClassPropertyType::removeMember(const QString &name)
{
if (!this->mClassType->members.contains(name))
{
ScriptManager::instance()
.throwError(QCoreApplication::translate("Script Errors", "No class member of the specified name '%1' exists").arg(name));
return;
}
this->mClassType->members.remove(name);
this->applyPropertyChanges();
}
} // namespace Tiled

#include "moc_scriptpropertytype.cpp"
10 changes: 3 additions & 7 deletions src/tiled/scriptpropertytype.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ class ScriptPropertyType : public QObject
{}

const QString &name() const;
void setName(const QString &value)
{
mType->name =value;
applyPropertyChanges();
}
void setName(const QString &name);
bool isClass() const { return mType->isClass(); }
bool isEnum() const { return mType->isEnum(); }
QVariant defaultValue() { return mType->defaultValue(); }
Expand Down Expand Up @@ -135,7 +131,8 @@ class ScriptClassPropertyType : public ScriptPropertyType
applyPropertyChanges();
}
QVariantMap members() const {return mClassType->members; }
// todo add members, remove members
Q_INVOKABLE void removeMember(const QString& name);
Q_INVOKABLE void addMember(const QString &name);

bool drawFill() const { return mClassType->drawFill; }
void setDrawFill(bool value)
Expand All @@ -159,7 +156,6 @@ class ScriptClassPropertyType : public ScriptPropertyType


void registerPropertyTypes(QJSEngine *jsEngine);

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::ScriptPropertyType*)

0 comments on commit 024610a

Please sign in to comment.