mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-25 06:35:30 +00:00
added delete command
This commit is contained in:
parent
b41cc5e9e9
commit
c12ee129f7
@ -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<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();
|
||||
};
|
||||
|
||||
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
|
@ -59,7 +59,7 @@ namespace CSMWorld
|
||||
template <typename ESXRecordT>
|
||||
const ESXRecordT& Record<ESXRecordT>::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 <typename ESXRecordT>
|
||||
const ESXRecordT& Record<ESXRecordT>::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 <typename ESXRecordT>
|
||||
void Record<ESXRecordT>::setModified (const ESXRecordT& modified)
|
||||
{
|
||||
if (isDeleted())
|
||||
if (mState==State_Erased)
|
||||
throw std::logic_error ("attempt to modify a deleted record");
|
||||
|
||||
mModified = modified;
|
||||
|
@ -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<std::string>::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<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;
|
||||
QAction *mCreateAction;
|
||||
QAction *mRevertAction;
|
||||
QAction *mDeleteAction;
|
||||
CSMWorld::IdTableProxyModel *mProxyModel;
|
||||
CSMWorld::IdTable *mModel;
|
||||
|
||||
@ -48,6 +49,8 @@ namespace CSVWorld
|
||||
void createRecord();
|
||||
|
||||
void revertRecord();
|
||||
|
||||
void deleteRecord();
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user