From db29e411c4a28805611ce7d90e0ee8294af09cb8 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Mon, 3 Dec 2012 13:56:02 +0100 Subject: [PATCH] added merge functions; temporarily merge on document creation --- apps/opencs/editor.cpp | 2 ++ apps/opencs/model/world/columns.hpp | 3 +++ apps/opencs/model/world/data.cpp | 5 ++++ apps/opencs/model/world/data.hpp | 3 +++ apps/opencs/model/world/idcollection.hpp | 30 ++++++++++++++++++++++++ apps/opencs/model/world/record.hpp | 30 ++++++++++++++++++++++-- 6 files changed, 71 insertions(+), 2 deletions(-) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 6977a22f0..1632ed220 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -35,6 +35,8 @@ void CS::Editor::createDocument() document->getData().getGlobals().add (record); } + document->getData().merge(); /// \todo remove once proper ESX loading is implemented + mViewManager.addView (document); } diff --git a/apps/opencs/model/world/columns.hpp b/apps/opencs/model/world/columns.hpp index e81780cee..483ce929d 100644 --- a/apps/opencs/model/world/columns.hpp +++ b/apps/opencs/model/world/columns.hpp @@ -51,6 +51,9 @@ namespace CSMWorld virtual QVariant get (const Record& record) const { + if (record.mState==Record::State_Erased) + return static_cast (Record::State_Deleted); + return static_cast (record.mState); } diff --git a/apps/opencs/model/world/data.cpp b/apps/opencs/model/world/data.cpp index c0df54c10..f350299ec 100644 --- a/apps/opencs/model/world/data.cpp +++ b/apps/opencs/model/world/data.cpp @@ -46,4 +46,9 @@ QAbstractTableModel *CSMWorld::Data::getTableModel (const UniversalId& id) throw std::logic_error ("No table model available for " + id.toString()); return iter->second; +} + +void CSMWorld::Data::merge() +{ + mGlobals.merge(); } \ No newline at end of file diff --git a/apps/opencs/model/world/data.hpp b/apps/opencs/model/world/data.hpp index 11073c5e3..a8a21e205 100644 --- a/apps/opencs/model/world/data.hpp +++ b/apps/opencs/model/world/data.hpp @@ -33,6 +33,9 @@ namespace CSMWorld QAbstractTableModel *getTableModel (const UniversalId& id); ///< If no table model is available for \æ id, an exception is thrown. + + void merge(); + ///< Merge modified into base. }; } diff --git a/apps/opencs/model/world/idcollection.hpp b/apps/opencs/model/world/idcollection.hpp index 41cd352ce..d9ab16747 100644 --- a/apps/opencs/model/world/idcollection.hpp +++ b/apps/opencs/model/world/idcollection.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -58,6 +59,12 @@ namespace CSMWorld virtual void setData (int index, int column, const QVariant& data) = 0; virtual bool isEditable (int column) const = 0; + + virtual void merge() = 0; + ///< Merge modified into base. + + virtual void purge() = 0; + ///< Remove records that are flagged as erased. }; ///< \brief Collection of ID-based records @@ -95,6 +102,12 @@ namespace CSMWorld virtual bool isEditable (int column) const; + virtual void merge(); + ///< Merge modified into base. + + virtual void purge(); + ///< Remove records that are flagged as erased. + void addColumn (Column *column); }; @@ -181,6 +194,23 @@ namespace CSMWorld { mColumns.push_back (column); } + + template + void IdCollection::merge() + { + for (typename std::vector >::iterator iter (mRecords.begin()); iter!=mRecords.end(); ++iter) + iter->merge(); + + purge(); + } + + template + void IdCollection::purge() + { + mRecords.erase (std::remove_if (mRecords.begin(), mRecords.end(), + std::mem_fun_ref (&Record::isErased) // I want lambda :( + ), mRecords.end()); + } } #endif diff --git a/apps/opencs/model/world/record.hpp b/apps/opencs/model/world/record.hpp index 950d7f176..c08d2e0d1 100644 --- a/apps/opencs/model/world/record.hpp +++ b/apps/opencs/model/world/record.hpp @@ -13,7 +13,8 @@ namespace CSMWorld State_BaseOnly, // defined in base only State_Modified, // exists in base, but has been modified State_ModifiedOnly, // newly created in modified - State_Deleted // exists in base, but has been deleted + State_Deleted, // exists in base, but has been deleted + State_Erased // does not exist at all (we mostly treat that the same way as deleted) }; ESXRecordT mBase; @@ -22,6 +23,8 @@ namespace CSMWorld bool isDeleted() const; + bool isErased() const; + bool isModified() const; const ESXRecordT& get() const; @@ -32,12 +35,21 @@ namespace CSMWorld void setModified (const ESXRecordT& modified); ///< Throws an exception, if the record is deleted. + + void merge(); + ///< Merge modified into base. }; template bool Record::isDeleted() const { - return mState==State_Deleted; + return mState==State_Deleted || mState==State_Erased; + } + + template + bool Record::isErased() const + { + return mState==State_Erased; } template @@ -75,6 +87,20 @@ namespace CSMWorld if (mState!=State_ModifiedOnly) mState = mBase==mModified ? State_BaseOnly : State_Modified; } + + template + void Record::merge() + { + if (isModified()) + { + mBase = mModified; + mState = State_BaseOnly; + } + else if (mState==State_Deleted) + { + mState = State_Erased; + } + } } #endif