From 7ccf53e750781476e256cdb0ed1e6919edc6e9be Mon Sep 17 00:00:00 2001 From: cc9cii Date: Sun, 12 Apr 2015 20:03:55 +1000 Subject: [PATCH] Changed over inventory adapters. Fixed a few places where modified records were not set properly. --- .../model/world/nestedcoladapterimp.cpp | 28 +++- .../model/world/nestedcoladapterimp.hpp | 12 +- apps/opencs/model/world/refidadapterimp.cpp | 6 +- apps/opencs/model/world/refidadapterimp.hpp | 147 +++++++++++++++++- apps/opencs/model/world/refidcollection.cpp | 8 +- 5 files changed, 186 insertions(+), 15 deletions(-) diff --git a/apps/opencs/model/world/nestedcoladapterimp.cpp b/apps/opencs/model/world/nestedcoladapterimp.cpp index 2bf1ffadd..785817d33 100644 --- a/apps/opencs/model/world/nestedcoladapterimp.cpp +++ b/apps/opencs/model/world/nestedcoladapterimp.cpp @@ -81,13 +81,17 @@ namespace CSMWorld void PathgridPointListAdapter::setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mPoints = + Pathgrid pathgrid = record.get(); + + pathgrid.mPoints = static_cast(nestedTable).mRecord.mPoints; - record.get().mData.mS2 = + pathgrid.mData.mS2 = static_cast(nestedTable).mRecord.mData.mS2; // also update edges in case points were added/removed - record.get().mEdges = + pathgrid.mEdges = static_cast(nestedTable).mRecord.mEdges; + + record.setModified (pathgrid); } NestedTableWrapperBase* PathgridPointListAdapter::nestedTable(const Record& record) const @@ -180,8 +184,12 @@ namespace CSMWorld void PathgridEdgeListAdapter::setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mEdges = + Pathgrid pathgrid = record.get(); + + pathgrid.mEdges = static_cast &>(nestedTable).mNestedTable; + + record.setModified (pathgrid); } NestedTableWrapperBase* PathgridEdgeListAdapter::nestedTable(const Record& record) const @@ -277,8 +285,12 @@ namespace CSMWorld void FactionReactionsAdapter::setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mReactions = + ESM::Faction faction = record.get(); + + faction.mReactions = static_cast >&>(nestedTable).mNestedTable; + + record.setModified (faction); } NestedTableWrapperBase* FactionReactionsAdapter::nestedTable(const Record& record) const @@ -393,8 +405,12 @@ namespace CSMWorld void RegionSoundListAdapter::setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mSoundList = + ESM::Region region = record.get(); + + region.mSoundList = static_cast >&>(nestedTable).mNestedTable; + + record.setModified (region); } NestedTableWrapperBase* RegionSoundListAdapter::nestedTable(const Record& record) const diff --git a/apps/opencs/model/world/nestedcoladapterimp.hpp b/apps/opencs/model/world/nestedcoladapterimp.hpp index 7fed74868..387b07e5c 100644 --- a/apps/opencs/model/world/nestedcoladapterimp.hpp +++ b/apps/opencs/model/world/nestedcoladapterimp.hpp @@ -173,8 +173,12 @@ namespace CSMWorld virtual void setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mPowers.mList = + ESXRecordT raceOrBthSgn = record.get(); + + raceOrBthSgn.mPowers.mList = static_cast >&>(nestedTable).mNestedTable; + + record.setModified (raceOrBthSgn); } virtual NestedTableWrapperBase* nestedTable(const Record& record) const @@ -277,8 +281,12 @@ namespace CSMWorld virtual void setNestedTable(Record& record, const NestedTableWrapperBase& nestedTable) const { - record.get().mEffects.mList = + ESXRecordT magic = record.get(); + + magic.mEffects.mList = static_cast >&>(nestedTable).mNestedTable; + + record.setModified (magic); } virtual NestedTableWrapperBase* nestedTable(const Record& record) const diff --git a/apps/opencs/model/world/refidadapterimp.cpp b/apps/opencs/model/world/refidadapterimp.cpp index 426bfe3d1..42d1aa52e 100644 --- a/apps/opencs/model/world/refidadapterimp.cpp +++ b/apps/opencs/model/world/refidadapterimp.cpp @@ -26,7 +26,7 @@ QVariant CSMWorld::PotionRefIdAdapter::getData (const RefIdColumn *column, const return record.get().mData.mAutoCalc!=0; if (column==mColumns.mEffects) - return true; // Required to show nested tables in dialogue subview + return true; // to show nested tables in dialogue subview, see IdTree::hasChildren() return InventoryRefIdAdapter::getData (column, data, index); } @@ -213,7 +213,7 @@ QVariant CSMWorld::ContainerRefIdAdapter::getData (const RefIdColumn *column, return (record.get().mFlags & ESM::Container::Respawn)!=0; if (column==mContent) - return true; // required by IdTree::hasChildren() + return true; // Required to show nested tables in dialogue subview return NameRefIdAdapter::getData (column, data, index); } @@ -497,7 +497,7 @@ QVariant CSMWorld::NpcRefIdAdapter::getData (const RefIdColumn *column, const Re return QString::fromUtf8 (record.get().mHead.c_str()); if (column==mColumns.mDestinations) - return true; // required by IdTree::hasChildren() + return true; // to show nested tables in dialogue subview, see IdTree::hasChildren() std::map::const_iterator iter = mColumns.mFlags.find (column); diff --git a/apps/opencs/model/world/refidadapterimp.hpp b/apps/opencs/model/world/refidadapterimp.hpp index b12d0cb6f..08e2e7493 100644 --- a/apps/opencs/model/world/refidadapterimp.hpp +++ b/apps/opencs/model/world/refidadapterimp.hpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include "record.hpp" #include "refiddata.hpp" @@ -545,10 +548,10 @@ namespace CSMWorld return record.get().mAiData.mAlarm; if (column==mActors.mInventory) - return true; // required by IdTree::hasChildren() + return true; // to show nested tables in dialogue subview, see IdTree::hasChildren() if (column==mActors.mSpells) - return true; // required by IdTree::hasChildren() + return true; // to show nested tables in dialogue subview, see IdTree::hasChildren() std::map::const_iterator iter = mActors.mServices.find (column); @@ -908,7 +911,7 @@ namespace CSMWorld virtual int getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const { - const Record record; + const Record record; // not used, just a dummy return EffectsListAdapter::getNestedColumnsCount(record); } @@ -919,6 +922,144 @@ namespace CSMWorld return EffectsListAdapter::getNestedRowsCount(record); } }; + + template + class NestedInventoryRefIdAdapter : public NestedRefIdAdapterBase + { + UniversalId::Type mType; + + // not implemented + NestedInventoryRefIdAdapter (const NestedInventoryRefIdAdapter&); + NestedInventoryRefIdAdapter& operator= (const NestedInventoryRefIdAdapter&); + + public: + + NestedInventoryRefIdAdapter(UniversalId::Type type) :mType(type) {} + + virtual ~NestedInventoryRefIdAdapter() {} + + virtual void addNestedRow (const RefIdColumn *column, + RefIdData& data, int index, int position) const + { + Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + ESXRecordT container = record.get(); + + std::vector& list = container.mInventory.mList; + + ESM::ContItem newRow = {0, {""}}; + + if (position >= (int)list.size()) + list.push_back(newRow); + else + list.insert(list.begin()+position, newRow); + + record.setModified (container); + } + + virtual void removeNestedRow (const RefIdColumn *column, + RefIdData& data, int index, int rowToRemove) const + { + Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + ESXRecordT container = record.get(); + + std::vector& list = container.mInventory.mList; + + if (rowToRemove < 0 || rowToRemove >= static_cast (list.size())) + throw std::runtime_error ("index out of range"); + + list.erase (list.begin () + rowToRemove); + + record.setModified (container); + } + + virtual void setNestedTable (const RefIdColumn* column, + RefIdData& data, int index, const NestedTableWrapperBase& nestedTable) const + { + Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + ESXRecordT container = record.get(); + + container.mInventory.mList = + static_cast >&>(nestedTable).mNestedTable; + + record.setModified (container); + } + + virtual NestedTableWrapperBase* nestedTable (const RefIdColumn* column, + const RefIdData& data, int index) const + { + const Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + + // deleted by dtor of NestedTableStoring + return new NestedTableWrapper >(record.get().mInventory.mList); + } + + virtual QVariant getNestedData (const RefIdColumn *column, + const RefIdData& data, int index, int subRowIndex, int subColIndex) const + { + const Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + + const std::vector& list = record.get().mInventory.mList; + + if (subRowIndex < 0 || subRowIndex >= static_cast (list.size())) + throw std::runtime_error ("index out of range"); + + const ESM::ContItem& content = list.at(subRowIndex); + + switch (subColIndex) + { + case 0: return QString::fromUtf8(content.mItem.toString().c_str()); + case 1: return content.mCount; + default: + throw std::runtime_error("Trying to access non-existing column in the nested table!"); + } + } + + virtual void setNestedData (const RefIdColumn *column, + RefIdData& data, int row, const QVariant& value, int subRowIndex, int subColIndex) const + { + Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (row, mType))); + ESXRecordT container = record.get(); + std::vector& list = container.mInventory.mList; + + if (subRowIndex < 0 || subRowIndex >= static_cast (list.size())) + throw std::runtime_error ("index out of range"); + + switch(subColIndex) + { + case 0: + list.at(subRowIndex).mItem.assign(std::string(value.toString().toUtf8().constData())); + break; + + case 1: + list.at(subRowIndex).mCount = value.toInt(); + break; + + default: + throw std::runtime_error("Trying to access non-existing column in the nested table!"); + } + + record.setModified (container); + } + + virtual int getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const + { + return 2; + } + + virtual int getNestedRowsCount(const RefIdColumn *column, const RefIdData& data, int index) const + { + const Record& record = + static_cast&> (data.getRecord (RefIdData::LocalIndex (index, mType))); + + return static_cast(record.get().mInventory.mList.size()); + } + }; } #endif diff --git a/apps/opencs/model/world/refidcollection.cpp b/apps/opencs/model/world/refidcollection.cpp index 929a3b245..755a1f56f 100644 --- a/apps/opencs/model/world/refidcollection.cpp +++ b/apps/opencs/model/world/refidcollection.cpp @@ -76,7 +76,7 @@ CSMWorld::RefIdCollection::RefIdCollection() mColumns.push_back (RefIdColumn (Columns::ColumnId_EffectList, ColumnBase::Display_NestedHeader, ColumnBase::Flag_Dialogue)); potionColumns.mEffects = &mColumns.back(); // see refidadapterimp.hpp mNestedAdapters.insert (std::make_pair(&mColumns.back(), - new EffectsRefIdAdapter (UniversalId::Type_Potion))); + new EffectsRefIdAdapter (UniversalId::Type_Potion))); mColumns.back().addColumn( new NestedChildColumn (Columns::ColumnId_EffectId, ColumnBase::Display_String/*, false*/)); mColumns.back().addColumn( @@ -124,6 +124,10 @@ CSMWorld::RefIdCollection::RefIdCollection() // Nested table mColumns.push_back(RefIdColumn (Columns::ColumnId_ActorInventory, ColumnBase::Display_NestedItemList, ColumnBase::Flag_Dialogue)); actorsColumns.mInventory = &mColumns.back(); + mNestedAdapters.insert (std::make_pair(&mColumns.back(), + new NestedInventoryRefIdAdapter (UniversalId::Type_Npc))); + mNestedAdapters.insert (std::make_pair(&mColumns.back(), + new NestedInventoryRefIdAdapter (UniversalId::Type_Creature))); mColumns.back().addColumn( new RefIdColumn (Columns::ColumnId_InventoryItemId, CSMWorld::ColumnBase::Display_String)); mColumns.back().addColumn( @@ -205,6 +209,8 @@ CSMWorld::RefIdCollection::RefIdCollection() // Nested table mColumns.push_back(RefIdColumn (Columns::ColumnId_ContainerContent, ColumnBase::Display_NestedItemList, ColumnBase::Flag_Dialogue)); const RefIdColumn *content = &mColumns.back(); + mNestedAdapters.insert (std::make_pair(&mColumns.back(), + new NestedInventoryRefIdAdapter (UniversalId::Type_Container))); mColumns.back().addColumn( new RefIdColumn (Columns::ColumnId_InventoryItemId, CSMWorld::ColumnBase::Display_String)); mColumns.back().addColumn(