set refnum and increment refnum counter when creating a new reference

deque
Marc Zinnschlag 10 years ago
parent 33243c8c16
commit e93ab383ea

@ -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<int>::const_iterator iter (references->second.begin());
iter!=references->second.end(); ++iter)
{
const CSMWorld::Record<CSMWorld::CellRef>& 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<int>::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)
{

@ -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;
}

@ -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;

@ -67,6 +67,8 @@ namespace CSVWorld
CSMWorld::Data& getData() const;
QUndoStack& getUndoStack();
const CSMWorld::UniversalId& getCollectionId() const;
std::string getNamespace() const;

@ -23,6 +23,41 @@ void CSVWorld::ReferenceCreator::configureCreateCommand (CSMWorld::CreateCommand
command.addValue (index, mCell->text());
}
void CSVWorld::ReferenceCreator::pushCommand (std::auto_ptr<QUndoCommand> command,
const std::string& id)
{
// get the old count
std::string cellId = mCell->text().toUtf8().constData();
CSMWorld::IdTable& cellTable = dynamic_cast<CSMWorld::IdTable&> (
*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<CSMWorld::IdTable&> (
*getData().getTableModel (CSMWorld::UniversalId::Type_References));
int refNumColumn = referenceTable.findColumnIndex (CSMWorld::Columns::ColumnId_RefNum);
std::auto_ptr<CSMWorld::ModifyCommand> setRefNum (new CSMWorld::ModifyCommand
(referenceTable, referenceTable.getModelIndex (id, refNumColumn), count));
// command for incrementing counter
std::auto_ptr<CSMWorld::ModifyCommand> 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)

@ -20,12 +20,15 @@ namespace CSVWorld
virtual void configureCreateCommand (CSMWorld::CreateCommand& command) const;
virtual void pushCommand (std::auto_ptr<QUndoCommand> 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();

Loading…
Cancel
Save