mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-03-29 19:36:43 +00:00
added delete command
This commit is contained in:
parent
b41cc5e9e9
commit
c12ee129f7
5 changed files with 97 additions and 3 deletions
|
@ -73,3 +73,36 @@ void CSMWorld::RevertCommand::undo()
|
||||||
{
|
{
|
||||||
mModel.setRecord (*mOld);
|
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<RecordBase::State> (mModel.data (index).toInt());
|
||||||
|
|
||||||
|
if (state==RecordBase::State_ModifiedOnly)
|
||||||
|
{
|
||||||
|
mModel.removeRows (index.row(), 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mModel.setData (index, static_cast<int> (RecordBase::State_Deleted));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CSMWorld::DeleteCommand::undo()
|
||||||
|
{
|
||||||
|
mModel.setRecord (*mOld);
|
||||||
|
}
|
|
@ -69,6 +69,27 @@ namespace CSMWorld
|
||||||
|
|
||||||
virtual void undo();
|
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
|
#endif
|
|
@ -59,7 +59,7 @@ namespace CSMWorld
|
||||||
template <typename ESXRecordT>
|
template <typename ESXRecordT>
|
||||||
const ESXRecordT& Record<ESXRecordT>::get() const
|
const ESXRecordT& Record<ESXRecordT>::get() const
|
||||||
{
|
{
|
||||||
if (isDeleted())
|
if (mState==State_Erased)
|
||||||
throw std::logic_error ("attempt to access a deleted record");
|
throw std::logic_error ("attempt to access a deleted record");
|
||||||
|
|
||||||
return mState==State_BaseOnly ? mBase : mModified;
|
return mState==State_BaseOnly ? mBase : mModified;
|
||||||
|
@ -68,7 +68,7 @@ namespace CSMWorld
|
||||||
template <typename ESXRecordT>
|
template <typename ESXRecordT>
|
||||||
const ESXRecordT& Record<ESXRecordT>::getBase() const
|
const ESXRecordT& Record<ESXRecordT>::getBase() const
|
||||||
{
|
{
|
||||||
if (isDeleted())
|
if (mState==State_Erased)
|
||||||
throw std::logic_error ("attempt to access a deleted record");
|
throw std::logic_error ("attempt to access a deleted record");
|
||||||
|
|
||||||
return mState==State_ModifiedOnly ? mModified : mBase;
|
return mState==State_ModifiedOnly ? mModified : mBase;
|
||||||
|
@ -77,7 +77,7 @@ namespace CSMWorld
|
||||||
template <typename ESXRecordT>
|
template <typename ESXRecordT>
|
||||||
void Record<ESXRecordT>::setModified (const ESXRecordT& modified)
|
void Record<ESXRecordT>::setModified (const ESXRecordT& modified)
|
||||||
{
|
{
|
||||||
if (isDeleted())
|
if (mState==State_Erased)
|
||||||
throw std::logic_error ("attempt to modify a deleted record");
|
throw std::logic_error ("attempt to modify a deleted record");
|
||||||
|
|
||||||
mModified = modified;
|
mModified = modified;
|
||||||
|
|
|
@ -120,6 +120,9 @@ void CSVWorld::CommandDelegate::setEditLock (bool locked)
|
||||||
if (selectedRows.size()>0)
|
if (selectedRows.size()>0)
|
||||||
menu.addAction (mRevertAction);
|
menu.addAction (mRevertAction);
|
||||||
|
|
||||||
|
if (selectedRows.size()>0)
|
||||||
|
menu.addAction (mDeleteAction);
|
||||||
|
|
||||||
menu.exec (event->globalPos());
|
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);
|
mRevertAction = new QAction (tr ("Revert Record"), this);
|
||||||
connect (mRevertAction, SIGNAL (triggered()), this, SLOT (revertRecord()));
|
connect (mRevertAction, SIGNAL (triggered()), this, SLOT (revertRecord()));
|
||||||
addAction (mRevertAction);
|
addAction (mRevertAction);
|
||||||
|
|
||||||
|
mDeleteAction = new QAction (tr ("Delete Record"), this);
|
||||||
|
connect (mDeleteAction, SIGNAL (triggered()), this, SLOT (deleteRecord()));
|
||||||
|
addAction (mDeleteAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSVWorld::Table::setEditLock (bool locked)
|
void CSVWorld::Table::setEditLock (bool locked)
|
||||||
|
@ -209,3 +216,33 @@ void CSVWorld::Table::revertRecord()
|
||||||
mUndoStack.endMacro();
|
mUndoStack.endMacro();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSVWorld::Table::deleteRecord()
|
||||||
|
{
|
||||||
|
QModelIndexList selectedRows = selectionModel()->selectedRows();
|
||||||
|
|
||||||
|
std::vector<std::string> 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<CSMWorld::RecordBase::State> (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<std::string>::const_iterator iter (revertableIds.begin()); iter!=revertableIds.end(); ++iter)
|
||||||
|
mUndoStack.push (new CSMWorld::DeleteCommand (*mModel, *iter));
|
||||||
|
|
||||||
|
if (revertableIds.size()>1)
|
||||||
|
mUndoStack.endMacro();
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ namespace CSVWorld
|
||||||
QUndoStack& mUndoStack;
|
QUndoStack& mUndoStack;
|
||||||
QAction *mCreateAction;
|
QAction *mCreateAction;
|
||||||
QAction *mRevertAction;
|
QAction *mRevertAction;
|
||||||
|
QAction *mDeleteAction;
|
||||||
CSMWorld::IdTableProxyModel *mProxyModel;
|
CSMWorld::IdTableProxyModel *mProxyModel;
|
||||||
CSMWorld::IdTable *mModel;
|
CSMWorld::IdTable *mModel;
|
||||||
|
|
||||||
|
@ -48,6 +49,8 @@ namespace CSVWorld
|
||||||
void createRecord();
|
void createRecord();
|
||||||
|
|
||||||
void revertRecord();
|
void revertRecord();
|
||||||
|
|
||||||
|
void deleteRecord();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue