diff --git a/apps/opencs/model/world/refidadapterimp.hpp b/apps/opencs/model/world/refidadapterimp.hpp index 4eac0fa4b2..8fc16af630 100644 --- a/apps/opencs/model/world/refidadapterimp.hpp +++ b/apps/opencs/model/world/refidadapterimp.hpp @@ -362,6 +362,68 @@ namespace CSMWorld InventoryRefIdAdapter::setData (column, data, index, value); } + struct ToolColumns : public InventoryColumns + { + const RefIdColumn *mQuality; + const RefIdColumn *mUses; + + ToolColumns (const InventoryColumns& base) : InventoryColumns (base) {} + }; + + /// \brief Adapter for tools with limited uses IDs (lockpick, repair, probes) + template + class ToolRefIdAdapter : public InventoryRefIdAdapter + { + ToolColumns mTools; + + public: + + ToolRefIdAdapter (UniversalId::Type type, const ToolColumns& 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 + ToolRefIdAdapter::ToolRefIdAdapter (UniversalId::Type type, const ToolColumns& columns) + : InventoryRefIdAdapter (type, columns), mTools (columns) + {} + + template + QVariant ToolRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data, + int index) const + { + const Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, BaseRefIdAdapter::getType()))); + + if (column==mTools.mQuality) + return record.get().mData.mQuality; + + if (column==mTools.mUses) + return record.get().mData.mUses; + + return InventoryRefIdAdapter::getData (column, data, index); + } + + template + void ToolRefIdAdapter::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==mTools.mQuality) + record.get().mData.mQuality = value.toFloat(); + else if (column==mTools.mUses) + record.get().mData.mUses = 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 8ed927c70c..8295767257 100644 --- a/apps/opencs/model/world/refidcollection.cpp +++ b/apps/opencs/model/world/refidcollection.cpp @@ -74,21 +74,25 @@ CSMWorld::RefIdCollection::RefIdCollection() mColumns.push_back (RefIdColumn ("Enchantment Points", ColumnBase::Display_Integer)); enchantableColumns.mEnchantmentPoints = &mColumns.back(); + ToolColumns toolsColumns (inventoryColumns); + + mColumns.push_back (RefIdColumn ("Quality", ColumnBase::Display_Float)); + toolsColumns.mQuality = &mColumns.back(); + mColumns.push_back (RefIdColumn ("Uses", ColumnBase::Display_Integer)); + toolsColumns.mUses = &mColumns.back(); + mColumns.push_back (RefIdColumn ("Auto Calc", ColumnBase::Display_Boolean)); const RefIdColumn *autoCalc = &mColumns.back(); mColumns.push_back (RefIdColumn ("Apparatus Type", ColumnBase::Display_ApparatusType)); const RefIdColumn *apparatusType = &mColumns.back(); - mColumns.push_back (RefIdColumn ("Quality", ColumnBase::Display_Float)); - const RefIdColumn *quality = &mColumns.back(); - mAdapters.insert (std::make_pair (UniversalId::Type_Activator, new NameRefIdAdapter (UniversalId::Type_Activator, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Potion, new PotionRefIdAdapter (inventoryColumns, autoCalc))); mAdapters.insert (std::make_pair (UniversalId::Type_Apparatus, - new ApparatusRefIdAdapter (inventoryColumns, apparatusType, quality))); + new ApparatusRefIdAdapter (inventoryColumns, apparatusType, toolsColumns.mQuality))); mAdapters.insert (std::make_pair (UniversalId::Type_Armor, new EnchantableRefIdAdapter (UniversalId::Type_Armor, enchantableColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Book, @@ -111,16 +115,16 @@ CSMWorld::RefIdCollection::RefIdCollection() mAdapters.insert (std::make_pair (UniversalId::Type_Light, new InventoryRefIdAdapter (UniversalId::Type_Light, inventoryColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Lockpick, - new InventoryRefIdAdapter (UniversalId::Type_Lockpick, inventoryColumns))); + new ToolRefIdAdapter (UniversalId::Type_Lockpick, toolsColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Miscellaneous, new InventoryRefIdAdapter (UniversalId::Type_Miscellaneous, inventoryColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Npc, new NameRefIdAdapter (UniversalId::Type_Npc, nameColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Probe, - new InventoryRefIdAdapter (UniversalId::Type_Probe, inventoryColumns))); + new ToolRefIdAdapter (UniversalId::Type_Probe, toolsColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Repair, - new InventoryRefIdAdapter (UniversalId::Type_Repair, inventoryColumns))); + new ToolRefIdAdapter (UniversalId::Type_Repair, toolsColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Static, new ModelRefIdAdapter (UniversalId::Type_Static, modelColumns))); mAdapters.insert (std::make_pair (UniversalId::Type_Weapon,