From 52d81dc9fa9170d2d39d10c088c90513bf610ba2 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Mon, 13 May 2013 13:11:43 +0200 Subject: [PATCH] added columns for enchantments --- apps/opencs/model/world/refidadapterimp.hpp | 81 ++++++++++++++++++--- apps/opencs/model/world/refidcollection.cpp | 15 +++- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/apps/opencs/model/world/refidadapterimp.hpp b/apps/opencs/model/world/refidadapterimp.hpp index fc641e2afa..4eac0fa4b2 100644 --- a/apps/opencs/model/world/refidadapterimp.hpp +++ b/apps/opencs/model/world/refidadapterimp.hpp @@ -223,11 +223,11 @@ namespace CSMWorld InventoryColumns (const NameColumns& base) : NameColumns (base) {} }; - /// \brief Adapter for IDs with names (all but levelled lists and statics) + /// \brief Adapter for IDs that can go into an inventory template class InventoryRefIdAdapter : public NameRefIdAdapter { - InventoryColumns mName; + InventoryColumns mInventory; public: @@ -244,7 +244,7 @@ namespace CSMWorld template InventoryRefIdAdapter::InventoryRefIdAdapter (UniversalId::Type type, const InventoryColumns& columns) - : NameRefIdAdapter (type, columns), mName (columns) + : NameRefIdAdapter (type, columns), mInventory (columns) {} template @@ -254,13 +254,13 @@ namespace CSMWorld const Record& record = static_cast&> ( data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); - if (column==mName.mIcon) + if (column==mInventory.mIcon) return QString::fromUtf8 (record.get().mIcon.c_str()); - if (column==mName.mWeight) + if (column==mInventory.mWeight) return record.get().mData.mWeight; - if (column==mName.mValue) + if (column==mInventory.mValue) return record.get().mData.mValue; return NameRefIdAdapter::getData (column, data, index); @@ -273,11 +273,11 @@ namespace CSMWorld Record& record = static_cast&> ( data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); - if (column==mName.mIcon) + if (column==mInventory.mIcon) record.get().mIcon = value.toString().toUtf8().constData(); - else if (column==mName.mWeight) + else if (column==mInventory.mWeight) record.get().mData.mWeight = value.toFloat(); - else if (column==mName.mValue) + else if (column==mInventory.mValue) record.get().mData.mValue = value.toInt(); else NameRefIdAdapter::setData (column, data, index, value); @@ -299,6 +299,69 @@ namespace CSMWorld ///< If the data type does not match an exception is thrown. }; + struct EnchantableColumns : public InventoryColumns + { + const RefIdColumn *mEnchantment; + const RefIdColumn *mEnchantmentPoints; + + EnchantableColumns (const InventoryColumns& base) : InventoryColumns (base) {} + }; + + /// \brief Adapter for enchantable IDs + template + class EnchantableRefIdAdapter : public InventoryRefIdAdapter + { + EnchantableColumns mEnchantable; + + public: + + EnchantableRefIdAdapter (UniversalId::Type type, const EnchantableColumns& 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 + EnchantableRefIdAdapter::EnchantableRefIdAdapter (UniversalId::Type type, + const EnchantableColumns& columns) + : InventoryRefIdAdapter (type, columns), mEnchantable (columns) + {} + + template + QVariant EnchantableRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data, + int index) const + { + const Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); + + if (column==mEnchantable.mEnchantment) + return QString::fromUtf8 (record.get().mEnchant.c_str()); + + if (column==mEnchantable.mEnchantmentPoints) + return static_cast (record.get().mData.mEnchant); + + return InventoryRefIdAdapter::getData (column, data, index); + } + + template + void EnchantableRefIdAdapter::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==mEnchantable.mEnchantment) + record.get().mEnchant = value.toString().toUtf8().constData(); + else if (column==mEnchantable.mEnchantmentPoints) + record.get().mData.mEnchant = value.toInt(); + else + InventoryRefIdAdapter::setData (column, data, index, value); + } + class ApparatusRefIdAdapter : public InventoryRefIdAdapter { const RefIdColumn *mType; diff --git a/apps/opencs/model/world/refidcollection.cpp b/apps/opencs/model/world/refidcollection.cpp index bc18f90aef..8ed927c70c 100644 --- a/apps/opencs/model/world/refidcollection.cpp +++ b/apps/opencs/model/world/refidcollection.cpp @@ -67,6 +67,13 @@ CSMWorld::RefIdCollection::RefIdCollection() mColumns.push_back (RefIdColumn ("Value", ColumnBase::Display_Integer)); inventoryColumns.mValue = &mColumns.back(); + EnchantableColumns enchantableColumns (inventoryColumns); + + mColumns.push_back (RefIdColumn ("Enchantment", ColumnBase::Display_String)); + enchantableColumns.mEnchantment = &mColumns.back(); + mColumns.push_back (RefIdColumn ("Enchantment Points", ColumnBase::Display_Integer)); + enchantableColumns.mEnchantmentPoints = &mColumns.back(); + mColumns.push_back (RefIdColumn ("Auto Calc", ColumnBase::Display_Boolean)); const RefIdColumn *autoCalc = &mColumns.back(); @@ -83,11 +90,11 @@ CSMWorld::RefIdCollection::RefIdCollection() mAdapters.insert (std::make_pair (UniversalId::Type_Apparatus, new ApparatusRefIdAdapter (inventoryColumns, apparatusType, quality))); mAdapters.insert (std::make_pair (UniversalId::Type_Armor, - new InventoryRefIdAdapter (UniversalId::Type_Armor, inventoryColumns))); + new EnchantableRefIdAdapter (UniversalId::Type_Armor, enchantableColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Book, - new InventoryRefIdAdapter (UniversalId::Type_Book, inventoryColumns))); + new EnchantableRefIdAdapter (UniversalId::Type_Book, enchantableColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Clothing, - new InventoryRefIdAdapter (UniversalId::Type_Clothing, inventoryColumns))); + new EnchantableRefIdAdapter (UniversalId::Type_Clothing, enchantableColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Container, new NameRefIdAdapter (UniversalId::Type_Container, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Creature, @@ -117,7 +124,7 @@ CSMWorld::RefIdCollection::RefIdCollection() mAdapters.insert (std::make_pair (UniversalId::Type_Static, new ModelRefIdAdapter (UniversalId::Type_Static, modelColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Weapon, - new InventoryRefIdAdapter (UniversalId::Type_Weapon, inventoryColumns))); + new EnchantableRefIdAdapter (UniversalId::Type_Weapon, enchantableColumns))); } CSMWorld::RefIdCollection::~RefIdCollection()