From e24ecfd5d26516d323caa05a4b9cc08fc83910bf Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 11 May 2013 18:43:01 +0200 Subject: [PATCH] added name column --- apps/opencs/model/world/refidadapterimp.hpp | 56 +++++++++++++++++++++ apps/opencs/model/world/refidcollection.cpp | 40 ++++++++------- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/apps/opencs/model/world/refidadapterimp.hpp b/apps/opencs/model/world/refidadapterimp.hpp index 55aaa536e4..1978ae8384 100644 --- a/apps/opencs/model/world/refidadapterimp.hpp +++ b/apps/opencs/model/world/refidadapterimp.hpp @@ -148,6 +148,62 @@ namespace CSMWorld else BaseRefIdAdapter::setData (column, data, index, value); } + + struct NameColumns : public ModelColumns + { + const RefIdColumn *mName; + + NameColumns (const ModelColumns& base) : ModelColumns (base) {} + }; + + /// \brief Adapter for IDs with names (all but levelled lists and statics) + template + class NameRefIdAdapter : public ModelRefIdAdapter + { + NameColumns mName; + + public: + + NameRefIdAdapter (UniversalId::Type type, const NameColumns& columns); + + virtual QVariant getData (const RefIdColumn *column, const RefIdData& data, int index) + const; + + virtual void setData (const RefIdColumn *column, RefIdData& data, int index, + const QVariant& value) const; + ///< If the data type does not match an exception is thrown. + }; + + template + NameRefIdAdapter::NameRefIdAdapter (UniversalId::Type type, const NameColumns& columns) + : ModelRefIdAdapter (type, columns), mName (columns) + {} + + template + QVariant NameRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data, + int index) const + { + const Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); + + if (column==mName.mName) + return QString::fromUtf8 (record.get().mName.c_str()); + + return ModelRefIdAdapter::getData (column, data, index); + } + + template + void NameRefIdAdapter::setData (const RefIdColumn *column, RefIdData& data, int index, + const QVariant& value) const + { + Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); + + if (column==mName.mName) + record.get().mName = value.toString().toUtf8().constData(); + else + ModelRefIdAdapter::setData (column, data, index, value); + } } #endif diff --git a/apps/opencs/model/world/refidcollection.cpp b/apps/opencs/model/world/refidcollection.cpp index 9806f260f9..9968532cba 100644 --- a/apps/opencs/model/world/refidcollection.cpp +++ b/apps/opencs/model/world/refidcollection.cpp @@ -45,54 +45,58 @@ CSMWorld::RefIdCollection::RefIdCollection() mColumns.push_back (RefIdColumn ("Type", ColumnBase::Display_Integer, ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false)); baseColumns.mType = &mColumns.back(); -// mColumns.push_back (RefIdColumn ("Name", ColumnBase::Display_String)); ModelColumns modelColumns (baseColumns); mColumns.push_back (RefIdColumn ("Model", ColumnBase::Display_String)); modelColumns.mModel = &mColumns.back(); + NameColumns nameColumns (modelColumns); + + mColumns.push_back (RefIdColumn ("Name", ColumnBase::Display_String)); + nameColumns.mName = &mColumns.back(); + mAdapters.insert (std::make_pair (UniversalId::Type_Activator, - new ModelRefIdAdapter (UniversalId::Type_Activator, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Activator, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Potion, - new ModelRefIdAdapter (UniversalId::Type_Potion, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Potion, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Apparatus, - new ModelRefIdAdapter (UniversalId::Type_Apparatus, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Apparatus, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Armor, - new ModelRefIdAdapter (UniversalId::Type_Armor, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Armor, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Book, - new ModelRefIdAdapter (UniversalId::Type_Book, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Book, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Clothing, - new ModelRefIdAdapter (UniversalId::Type_Clothing, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Clothing, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Container, - new ModelRefIdAdapter (UniversalId::Type_Container, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Container, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Creature, - new ModelRefIdAdapter (UniversalId::Type_Creature, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Creature, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Door, - new ModelRefIdAdapter (UniversalId::Type_Door, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Door, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Ingredient, - new ModelRefIdAdapter (UniversalId::Type_Ingredient, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Ingredient, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_CreatureLevelledList, new BaseRefIdAdapter ( UniversalId::Type_CreatureLevelledList, baseColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_ItemLevelledList, new BaseRefIdAdapter (UniversalId::Type_ItemLevelledList, baseColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Light, - new ModelRefIdAdapter (UniversalId::Type_Light, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Light, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Lockpick, - new ModelRefIdAdapter (UniversalId::Type_Lockpick, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Lockpick, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Miscellaneous, - new ModelRefIdAdapter (UniversalId::Type_Miscellaneous, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Miscellaneous, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Npc, - new ModelRefIdAdapter (UniversalId::Type_Npc, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Npc, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Probe, - new ModelRefIdAdapter (UniversalId::Type_Probe, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Probe, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Repair, - new ModelRefIdAdapter (UniversalId::Type_Repair, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Repair, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Static, new ModelRefIdAdapter (UniversalId::Type_Static, modelColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Weapon, - new ModelRefIdAdapter (UniversalId::Type_Weapon, modelColumns))); + new NameRefIdAdapter (UniversalId::Type_Weapon, nameColumns))); } CSMWorld::RefIdCollection::~RefIdCollection()