From c12ee129f7019f5664f3a25b28e8f8961b5121bb Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Thu, 6 Dec 2012 15:18:41 +0100 Subject: [PATCH] added delete command --- apps/opencs/model/world/commands.cpp | 33 +++++++++++++++++++++++++ apps/opencs/model/world/commands.hpp | 21 ++++++++++++++++ apps/opencs/model/world/record.hpp | 6 ++--- apps/opencs/view/world/table.cpp | 37 ++++++++++++++++++++++++++++ apps/opencs/view/world/table.hpp | 3 +++ 5 files changed, 97 insertions(+), 3 deletions(-) diff --git a/apps/opencs/model/world/commands.cpp b/apps/opencs/model/world/commands.cpp index 96a1b639c..e22ecf992 100644 --- a/apps/opencs/model/world/commands.cpp +++ b/apps/opencs/model/world/commands.cpp @@ -70,6 +70,39 @@ void CSMWorld::RevertCommand::redo() } void CSMWorld::RevertCommand::undo() +{ + mModel.setRecord (*mOld); +} + +CSMWorld::DeleteCommand::DeleteCommand (IdTable& model, const std::string& id, QUndoCommand *parent) +: QUndoCommand (parent), mModel (model), mId (id), mOld (0) +{ + setText (("Delete record " + id).c_str()); + + mOld = model.getRecord (id).clone(); +} + +CSMWorld::DeleteCommand::~DeleteCommand() +{ + delete mOld; +} + +void CSMWorld::DeleteCommand::redo() +{ + QModelIndex index = mModel.getModelIndex (mId, 1); + RecordBase::State state = static_cast (mModel.data (index).toInt()); + + if (state==RecordBase::State_ModifiedOnly) + { + mModel.removeRows (index.row(), 1); + } + else + { + mModel.setData (index, static_cast (RecordBase::State_Deleted)); + } +} + +void CSMWorld::DeleteCommand::undo() { mModel.setRecord (*mOld); } \ No newline at end of file diff --git a/apps/opencs/model/world/commands.hpp b/apps/opencs/model/world/commands.hpp index e916d05c9..af419215d 100644 --- a/apps/opencs/model/world/commands.hpp +++ b/apps/opencs/model/world/commands.hpp @@ -69,6 +69,27 @@ namespace CSMWorld virtual void undo(); }; + + class DeleteCommand : public QUndoCommand + { + IdTable& mModel; + std::string mId; + RecordBase *mOld; + + // not implemented + DeleteCommand (const DeleteCommand&); + DeleteCommand& operator= (const DeleteCommand&); + + public: + + DeleteCommand (IdTable& model, const std::string& id, QUndoCommand *parent = 0); + + virtual ~DeleteCommand(); + + virtual void redo(); + + virtual void undo(); + }; } #endif \ No newline at end of file diff --git a/apps/opencs/model/world/record.hpp b/apps/opencs/model/world/record.hpp index 3b83836ab..53bb7ea2c 100644 --- a/apps/opencs/model/world/record.hpp +++ b/apps/opencs/model/world/record.hpp @@ -59,7 +59,7 @@ namespace CSMWorld template const ESXRecordT& Record::get() const { - if (isDeleted()) + if (mState==State_Erased) throw std::logic_error ("attempt to access a deleted record"); return mState==State_BaseOnly ? mBase : mModified; @@ -68,7 +68,7 @@ namespace CSMWorld template const ESXRecordT& Record::getBase() const { - if (isDeleted()) + if (mState==State_Erased) throw std::logic_error ("attempt to access a deleted record"); return mState==State_ModifiedOnly ? mModified : mBase; @@ -77,7 +77,7 @@ namespace CSMWorld template void Record::setModified (const ESXRecordT& modified) { - if (isDeleted()) + if (mState==State_Erased) throw std::logic_error ("attempt to modify a deleted record"); mModified = modified; diff --git a/apps/opencs/view/world/table.cpp b/apps/opencs/view/world/table.cpp index dafcb95c0..1595b6926 100644 --- a/apps/opencs/view/world/table.cpp +++ b/apps/opencs/view/world/table.cpp @@ -120,6 +120,9 @@ void CSVWorld::CommandDelegate::setEditLock (bool locked) if (selectedRows.size()>0) menu.addAction (mRevertAction); + if (selectedRows.size()>0) + menu.addAction (mDeleteAction); + menu.exec (event->globalPos()); } @@ -160,6 +163,10 @@ CSVWorld::Table::Table (const CSMWorld::UniversalId& id, CSMWorld::Data& data, Q mRevertAction = new QAction (tr ("Revert Record"), this); connect (mRevertAction, SIGNAL (triggered()), this, SLOT (revertRecord())); addAction (mRevertAction); + + mDeleteAction = new QAction (tr ("Delete Record"), this); + connect (mDeleteAction, SIGNAL (triggered()), this, SLOT (deleteRecord())); + addAction (mDeleteAction); } void CSVWorld::Table::setEditLock (bool locked) @@ -205,6 +212,36 @@ void CSVWorld::Table::revertRecord() for (std::vector::const_iterator iter (revertableIds.begin()); iter!=revertableIds.end(); ++iter) mUndoStack.push (new CSMWorld::RevertCommand (*mModel, *iter)); + if (revertableIds.size()>1) + mUndoStack.endMacro(); + } +} + +void CSVWorld::Table::deleteRecord() +{ + QModelIndexList selectedRows = selectionModel()->selectedRows(); + + std::vector revertableIds; + + for (QModelIndexList::const_iterator iter (selectedRows.begin()); iter!=selectedRows.end(); ++iter) + { + std::string id = mProxyModel->data (*iter).toString().toStdString(); + + CSMWorld::RecordBase::State state = + static_cast (mModel->data (mModel->getModelIndex (id, 1)).toInt()); + + if (state!=CSMWorld::RecordBase::State_Deleted) + revertableIds.push_back (id); + } + + if (revertableIds.size()>0) + { + if (revertableIds.size()>1) + mUndoStack.beginMacro (tr ("Delete multiple records")); + + for (std::vector::const_iterator iter (revertableIds.begin()); iter!=revertableIds.end(); ++iter) + mUndoStack.push (new CSMWorld::DeleteCommand (*mModel, *iter)); + if (revertableIds.size()>1) mUndoStack.endMacro(); } diff --git a/apps/opencs/view/world/table.hpp b/apps/opencs/view/world/table.hpp index 022d4f12a..555dc8e1f 100644 --- a/apps/opencs/view/world/table.hpp +++ b/apps/opencs/view/world/table.hpp @@ -29,6 +29,7 @@ namespace CSVWorld QUndoStack& mUndoStack; QAction *mCreateAction; QAction *mRevertAction; + QAction *mDeleteAction; CSMWorld::IdTableProxyModel *mProxyModel; CSMWorld::IdTable *mModel; @@ -48,6 +49,8 @@ namespace CSVWorld void createRecord(); void revertRecord(); + + void deleteRecord(); }; }