fix: editing non-string values causes crash

This commit is contained in:
maelstrom 2025-01-29 23:19:04 +01:00
parent 49066db8fb
commit 4ed32e44f0
5 changed files with 14 additions and 7 deletions

View file

@ -1,4 +1,5 @@
#include "propertiesmodel.h"
#include "datatypes/base.h"
#include "qnamespace.h"
PropertiesModel::PropertiesModel(InstanceRef selectedItem, QWidget *parent)
@ -21,8 +22,9 @@ QVariant PropertiesModel::data(const QModelIndex &index, int role) const {
case Qt::DisplayRole:
if (index.column() == 0)
return QString::fromStdString(propertyName);
else if (index.column() == 1)
else if (index.column() == 1) {
return QString::fromStdString(selectedItem->GetPropertyValue(propertyName).value().ToString());
}
// case Qt::DecorationRole:
// return iconOf(item->GetClass());
}
@ -33,8 +35,12 @@ QVariant PropertiesModel::data(const QModelIndex &index, int role) const {
bool PropertiesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (index.column() != 1 && role != Qt::EditRole) return false;
if (selectedItem->GetPropertyMeta(propertiesList[index.row()])->type == &Data::String::TYPE) {
selectedItem->SetPropertyValue(propertiesList[index.row()], value.toString().toStdString());
return true;
}
return false;
}
Qt::ItemFlags PropertiesModel::flags(const QModelIndex &index) const {

View file

@ -18,6 +18,7 @@ public: \
namespace Data {
struct TypeInfo {
std::string name;
TypeInfo(const TypeInfo&) = delete;
};
class String;

View file

@ -30,7 +30,7 @@ Instance::Instance(InstanceType* type) {
this->memberMap = std::make_unique<MemberMap>( MemberMap {
.super = std::nullopt,
.members = {
{ "Name", { .backingField = &name, .codec = fieldCodecOf<Data::String, std::string>() } }
{ "Name", { .backingField = &name, .type = &Data::String::TYPE, .codec = fieldCodecOf<Data::String, std::string>() } }
}
});
}

View file

@ -34,7 +34,7 @@ constexpr FieldCodec fieldCodecOf() {
struct PropertyMeta {
void* backingField;
Data::TypeInfo type;
const Data::TypeInfo* type;
FieldCodec codec;
};

View file

@ -27,7 +27,7 @@ Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.positi
this->memberMap = std::make_unique<MemberMap>(MemberMap {
.super = std::move(this->memberMap),
.members = {
{ "Anchored", { .backingField = &anchored, .codec = fieldCodecOf<Data::Bool, bool>() } }
{ "Anchored", { .backingField = &anchored, .type = &Data::Bool::TYPE, .codec = fieldCodecOf<Data::Bool, bool>() } }
}
});
}