#ifndef CSM_WORLD_NESTEDADAPTORS_H #define CSM_WORLD_NESTEDADAPTORS_H #include #include #include "universalid.hpp" #include "nestedtablewrapper.hpp" #include #include "record.hpp" #include "refiddata.hpp" #include "refidadapter.hpp" #include namespace CSMWorld { template class InventoryHelper { CSMWorld::UniversalId::Type mType; public: InventoryHelper(CSMWorld::UniversalId::Type type) : mType(type) {}; void setNestedTable(const RefIdColumn* column, RefIdData& data, int index, const NestedTableWrapperBase& nestedTable) { getRecord(data, index).get().mInventory.mList = (static_cast >&>(nestedTable)).mNestedTable; } NestedTableWrapperBase* nestedTable(const RefIdColumn* column, const RefIdData& data, int index) const { return new NestedTableWrapper >(getRecord(data, index).get().mInventory.mList); } QVariant getNestedData(const CSMWorld::RefIdColumn* column, const CSMWorld::RefIdData& data, int index, int subRowIndex, int subColIndex) const { const ESM::ContItem& content = getRecord(data, index).get().mInventory.mList.at(subRowIndex); switch (subColIndex) { case 0: return QString::fromUtf8(content.mItem.toString().c_str()); case 1: return content.mCount; default: throw std::logic_error("Trying to access non-existing column in the nested table!"); } } void removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const { std::vector& list = getRecord(data, index).get().mInventory.mList; list.erase (list.begin () + rowToRemove); } void setNestedData (const RefIdColumn *column, RefIdData& data, int index, const QVariant& value, int subRowIndex, int subColIndex) const { switch(subColIndex) { case 0: getRecord(data, index).get().mInventory.mList.at(subRowIndex).mItem.assign(std::string(value.toString().toUtf8().constData())); break; case 1: getRecord(data, index).get().mInventory.mList.at(subRowIndex).mCount = value.toInt(); break; default: throw std::logic_error("Trying to access non-existing column in the nested table!"); } } private: const Record& getRecord(const RefIdData& data, int index) const { return dynamic_cast&> ( data.getRecord (RefIdData::LocalIndex (index, mType))); } Record& getRecord(RefIdData& data, int index) const { return dynamic_cast&> ( data.getRecord (RefIdData::LocalIndex (index, mType))); } }; } #endif