2012-11-29 15:05:28 +00:00
|
|
|
|
|
|
|
#include "commands.hpp"
|
|
|
|
|
2013-03-21 09:07:25 +00:00
|
|
|
#include <QAbstractItemModel>
|
2012-11-29 15:05:28 +00:00
|
|
|
|
2013-07-28 11:39:33 +00:00
|
|
|
#include "idtable.hpp"
|
2012-12-06 13:56:04 +00:00
|
|
|
#include "idtable.hpp"
|
2012-12-03 20:44:16 +00:00
|
|
|
|
2012-11-29 15:05:28 +00:00
|
|
|
CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelIndex& index,
|
|
|
|
const QVariant& new_, QUndoCommand *parent)
|
|
|
|
: QUndoCommand (parent), mModel (model), mIndex (index), mNew (new_)
|
|
|
|
{
|
|
|
|
mOld = mModel.data (mIndex, Qt::EditRole);
|
|
|
|
|
|
|
|
setText ("Modify " + mModel.headerData (mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ModifyCommand::redo()
|
|
|
|
{
|
|
|
|
mModel.setData (mIndex, mNew);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ModifyCommand::undo()
|
|
|
|
{
|
|
|
|
mModel.setData (mIndex, mOld);
|
2012-12-03 20:44:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 11:39:33 +00:00
|
|
|
CSMWorld::CreateCommand::CreateCommand (IdTable& model, const std::string& id, QUndoCommand *parent)
|
2013-07-30 08:27:17 +00:00
|
|
|
: QUndoCommand (parent), mModel (model), mId (id), mType (UniversalId::Type_None)
|
2012-12-03 20:44:16 +00:00
|
|
|
{
|
|
|
|
setText (("Create record " + id).c_str());
|
|
|
|
}
|
|
|
|
|
2013-07-30 10:53:03 +00:00
|
|
|
void CSMWorld::CreateCommand::addValue (int column, const QVariant& value)
|
|
|
|
{
|
|
|
|
mValues[column] = value;
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:27:17 +00:00
|
|
|
void CSMWorld::CreateCommand::setType (UniversalId::Type type)
|
|
|
|
{
|
|
|
|
mType = type;
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:44:16 +00:00
|
|
|
void CSMWorld::CreateCommand::redo()
|
|
|
|
{
|
2013-07-30 08:27:17 +00:00
|
|
|
mModel.addRecord (mId, mType);
|
2013-07-30 10:53:03 +00:00
|
|
|
|
|
|
|
for (std::map<int, QVariant>::const_iterator iter (mValues.begin()); iter!=mValues.end(); ++iter)
|
|
|
|
mModel.setData (mModel.getModelIndex (mId, iter->first), iter->second);
|
2012-12-03 20:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::CreateCommand::undo()
|
|
|
|
{
|
|
|
|
mModel.removeRow (mModel.getModelIndex (mId, 0).row());
|
2012-12-06 13:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::RevertCommand::RevertCommand (IdTable& model, const std::string& id, QUndoCommand *parent)
|
|
|
|
: QUndoCommand (parent), mModel (model), mId (id), mOld (0)
|
|
|
|
{
|
|
|
|
setText (("Revert record " + id).c_str());
|
|
|
|
|
|
|
|
mOld = model.getRecord (id).clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::RevertCommand::~RevertCommand()
|
|
|
|
{
|
|
|
|
delete mOld;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::RevertCommand::redo()
|
|
|
|
{
|
2013-08-26 10:25:52 +00:00
|
|
|
int column = mModel.findColumnIndex (Columns::ColumnId_Modification);
|
|
|
|
|
|
|
|
QModelIndex index = mModel.getModelIndex (mId, column);
|
2012-12-06 13:56:04 +00:00
|
|
|
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_BaseOnly));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::RevertCommand::undo()
|
2012-12-06 14:18:41 +00:00
|
|
|
{
|
2013-04-29 15:52:01 +00:00
|
|
|
mModel.setRecord (mId, *mOld);
|
2012-12-06 14:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2013-08-26 10:25:52 +00:00
|
|
|
int column = mModel.findColumnIndex (Columns::ColumnId_Modification);
|
|
|
|
|
|
|
|
QModelIndex index = mModel.getModelIndex (mId, column);
|
2012-12-06 14:18:41 +00:00
|
|
|
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()
|
2012-12-06 13:56:04 +00:00
|
|
|
{
|
2013-04-29 15:52:01 +00:00
|
|
|
mModel.setRecord (mId, *mOld);
|
2013-11-14 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CSMWorld::ReorderRowsCommand::ReorderRowsCommand (IdTable& model, int baseIndex,
|
|
|
|
const std::vector<int>& newOrder)
|
|
|
|
: mModel (model), mBaseIndex (baseIndex), mNewOrder (newOrder)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void CSMWorld::ReorderRowsCommand::redo()
|
|
|
|
{
|
|
|
|
mModel.reorderRows (mBaseIndex, mNewOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ReorderRowsCommand::undo()
|
|
|
|
{
|
|
|
|
int size = static_cast<int> (mNewOrder.size());
|
|
|
|
std::vector<int> reverse (size);
|
|
|
|
|
|
|
|
for (int i=0; i<size; ++i)
|
|
|
|
reverse.at (mNewOrder[i]) = i;
|
|
|
|
|
|
|
|
mModel.reorderRows (mBaseIndex, reverse);
|
2012-11-29 15:05:28 +00:00
|
|
|
}
|