diff --git a/apps/opencs/model/world/columnimp.cpp b/apps/opencs/model/world/columnimp.cpp index a9b2c867a0..d42560b5e3 100644 --- a/apps/opencs/model/world/columnimp.cpp +++ b/apps/opencs/model/world/columnimp.cpp @@ -8,10 +8,40 @@ #include +#include #include namespace CSMWorld { + namespace + { + struct GetStringId + { + std::string operator()(ESM::EmptyRefId /*value*/) const { return std::string(); } + + std::string operator()(ESM::StringRefId value) const { return value.getValue(); } + + std::string operator()(ESM::IndexRefId value) const + { + switch (value.getRecordType()) + { + case ESM::REC_SKIL: + return ESM::Skill::sSkillNames[value.getValue()]; + default: + break; + } + + return value.toDebugString(); + } + + template + std::string operator()(const T& value) const + { + return value.toDebugString(); + } + }; + } + /* LandTextureNicknameColumn */ LandTextureNicknameColumn::LandTextureNicknameColumn() : Column(Columns::ColumnId_TextureNickname, ColumnBase::Display_String) @@ -299,4 +329,17 @@ namespace CSMWorld { return true; } + + std::optional getSkillIndex(std::string_view value) + { + const auto it = std::find(std::begin(ESM::Skill::sSkillNames), std::end(ESM::Skill::sSkillNames), value); + if (it == std::end(ESM::Skill::sSkillNames)) + return std::nullopt; + return static_cast(it - std::begin(ESM::Skill::sSkillNames)); + } + + std::string getStringId(ESM::RefId value) + { + return visit(GetStringId{}, value); + } } diff --git a/apps/opencs/model/world/columnimp.hpp b/apps/opencs/model/world/columnimp.hpp index 91db8d0c41..ea8bff74b7 100644 --- a/apps/opencs/model/world/columnimp.hpp +++ b/apps/opencs/model/world/columnimp.hpp @@ -18,6 +18,8 @@ #include #include +#include + #include #include #include @@ -31,6 +33,10 @@ namespace CSMWorld { + std::optional getSkillIndex(std::string_view value); + + std::string getStringId(ESM::RefId value); + /// \note Shares ID with VarValueColumn. A table can not have both. template struct FloatValueColumn : public Column @@ -63,7 +69,7 @@ namespace CSMWorld QVariant get(const Record& record) const override { - return QString::fromStdString(record.get().mId.toString()); + return QString::fromStdString(getStringId(record.get().mId)); } bool isEditable() const override { return false; } @@ -403,25 +409,16 @@ namespace CSMWorld QVariant get(const Record& record) const override { - int skill = record.get().mData.getSkill(mIndex, mMajor); - - return QString::fromStdString(ESM::Skill::indexToRefId(skill).toString()); + return QString::fromStdString(ESM::Skill::sSkillNames[record.get().mData.getSkill(mIndex, mMajor)]); } void set(Record& record, const QVariant& data) override { - std::istringstream stream(data.toString().toUtf8().constData()); - - int index = -1; - char c; - - stream >> c >> index; - - if (index != -1) + if (const auto index = getSkillIndex(data.toString().toStdString())) { ESXRecordT record2 = record.get(); - record2.mData.getSkill(mIndex, mMajor) = index; + record2.mData.getSkill(mIndex, mMajor) = static_cast(*index); record.setModified(record2); }