diff --git a/apps/opencs/model/doc/savingstages.cpp b/apps/opencs/model/doc/savingstages.cpp index 17c2b4dca2..d62947df6a 100644 --- a/apps/opencs/model/doc/savingstages.cpp +++ b/apps/opencs/model/doc/savingstages.cpp @@ -284,20 +284,6 @@ void CSMDoc::WriteCellCollectionStage::perform (int stage, Messages& messages) // write references if (references!=mState.getSubRecords().end()) { - // first pass: find highest RefNum - int lastRefNum = -1; - - for (std::vector::const_iterator iter (references->second.begin()); - iter!=references->second.end(); ++iter) - { - const CSMWorld::Record& ref = - mDocument.getData().getReferences().getRecord (*iter); - - if (ref.get().mRefNum.mContentFile==0 && ref.get().mRefNum.mIndex>lastRefNum) - lastRefNum = ref.get().mRefNum.mIndex; - } - - // second pass: write for (std::vector::const_iterator iter (references->second.begin()); iter!=references->second.end(); ++iter) { @@ -307,20 +293,7 @@ void CSMDoc::WriteCellCollectionStage::perform (int stage, Messages& messages) if (ref.mState==CSMWorld::RecordBase::State_Modified || ref.mState==CSMWorld::RecordBase::State_ModifiedOnly) { - if (ref.get().mRefNum.mContentFile==-2) - { - if (lastRefNum>=0xffffff) - throw std::runtime_error ( - "RefNums exhausted in cell: " + cell.get().mId); - - ESM::CellRef ref2 = ref.get(); - ref2.mRefNum.mContentFile = 0; - ref2.mRefNum.mIndex = ++lastRefNum; - - ref2.save (mState.getWriter()); - } - else - ref.get().save (mState.getWriter()); + ref.get().save (mState.getWriter()); } else if (ref.mState==CSMWorld::RecordBase::State_Deleted) { diff --git a/apps/opencs/model/world/ref.cpp b/apps/opencs/model/world/ref.cpp index d1bd771f80..f3c1b0b73f 100644 --- a/apps/opencs/model/world/ref.cpp +++ b/apps/opencs/model/world/ref.cpp @@ -4,7 +4,5 @@ CSMWorld::CellRef::CellRef() { mRefNum.mIndex = 0; - - // special marker: This reference does not have a RefNum assign to it yet. - mRefNum.mContentFile = -2; + mRefNum.mContentFile = 0; } \ No newline at end of file diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp index afddda4544..099fdd09c6 100644 --- a/apps/opencs/view/world/genericcreator.cpp +++ b/apps/opencs/view/world/genericcreator.cpp @@ -61,6 +61,11 @@ CSMWorld::Data& CSVWorld::GenericCreator::getData() const return mData; } +QUndoStack& CSVWorld::GenericCreator::getUndoStack() +{ + return mUndoStack; +} + const CSMWorld::UniversalId& CSVWorld::GenericCreator::getCollectionId() const { return mListId; diff --git a/apps/opencs/view/world/genericcreator.hpp b/apps/opencs/view/world/genericcreator.hpp index 4cd2fce2fc..a9ef87749b 100644 --- a/apps/opencs/view/world/genericcreator.hpp +++ b/apps/opencs/view/world/genericcreator.hpp @@ -67,6 +67,8 @@ namespace CSVWorld CSMWorld::Data& getData() const; + QUndoStack& getUndoStack(); + const CSMWorld::UniversalId& getCollectionId() const; std::string getNamespace() const; diff --git a/apps/opencs/view/world/referencecreator.cpp b/apps/opencs/view/world/referencecreator.cpp index 6b8e02da0d..46d759ff27 100644 --- a/apps/opencs/view/world/referencecreator.cpp +++ b/apps/opencs/view/world/referencecreator.cpp @@ -23,6 +23,41 @@ void CSVWorld::ReferenceCreator::configureCreateCommand (CSMWorld::CreateCommand command.addValue (index, mCell->text()); } +void CSVWorld::ReferenceCreator::pushCommand (std::auto_ptr command, + const std::string& id) +{ + // get the old count + std::string cellId = mCell->text().toUtf8().constData(); + + CSMWorld::IdTable& cellTable = dynamic_cast ( + *getData().getTableModel (CSMWorld::UniversalId::Type_Cells)); + + int countColumn = cellTable.findColumnIndex (CSMWorld::Columns::ColumnId_RefNumCounter); + + QModelIndex countIndex = cellTable.getModelIndex (cellId, countColumn); + + int count = cellTable.data (countIndex).toInt(); + + // command for setting the refnum in the newly created reference + CSMWorld::IdTable& referenceTable = dynamic_cast ( + *getData().getTableModel (CSMWorld::UniversalId::Type_References)); + + int refNumColumn = referenceTable.findColumnIndex (CSMWorld::Columns::ColumnId_RefNum); + + std::auto_ptr setRefNum (new CSMWorld::ModifyCommand + (referenceTable, referenceTable.getModelIndex (id, refNumColumn), count)); + + // command for incrementing counter + std::auto_ptr increment (new CSMWorld::ModifyCommand + (cellTable, countIndex, count+1)); + + getUndoStack().beginMacro (command->text()); + GenericCreator::pushCommand (command, id); + getUndoStack().push (setRefNum.release()); + getUndoStack().push (increment.release()); + getUndoStack().endMacro(); +} + CSVWorld::ReferenceCreator::ReferenceCreator (CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id) : GenericCreator (data, undoStack, id) diff --git a/apps/opencs/view/world/referencecreator.hpp b/apps/opencs/view/world/referencecreator.hpp index 12fb12dd90..a0e98a9a4b 100644 --- a/apps/opencs/view/world/referencecreator.hpp +++ b/apps/opencs/view/world/referencecreator.hpp @@ -20,12 +20,15 @@ namespace CSVWorld virtual void configureCreateCommand (CSMWorld::CreateCommand& command) const; + virtual void pushCommand (std::auto_ptr command, + const std::string& id); + public: ReferenceCreator (CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id); - virtual void cloneMode(const std::string& originId, + virtual void cloneMode(const std::string& originId, const CSMWorld::UniversalId::Type type); virtual void reset();