2014-01-27 12:36:01 +00:00
|
|
|
|
2012-11-29 15:05:28 +00:00
|
|
|
#include "commands.hpp"
|
2013-03-21 09:07:25 +00:00
|
|
|
#include <QAbstractItemModel>
|
2013-07-28 11:39:33 +00:00
|
|
|
#include "idtable.hpp"
|
2014-01-27 12:41:37 +00:00
|
|
|
#include <components/misc/stringops.hpp>
|
2012-12-03 20:44:16 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
CSMWorld::ModifyCommand::ModifyCommand(QAbstractItemModel& model, const QModelIndex& index,
|
|
|
|
const QVariant& new_, QUndoCommand* parent)
|
|
|
|
: QUndoCommand(parent), mModel(model), mIndex(index), mNew(new_)
|
2012-11-29 15:05:28 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mOld = mModel.data(mIndex, Qt::EditRole);
|
2012-11-29 15:05:28 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
setText("Modify " + mModel.headerData(mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString());
|
2012-11-29 15:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ModifyCommand::redo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setData(mIndex, mNew);
|
2012-11-29 15:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ModifyCommand::undo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setData(mIndex, mOld);
|
2012-12-03 20:44:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
CSMWorld::CloneCommand::CloneCommand(CSMWorld::IdTable& model,
|
|
|
|
const std::string& idOrigin,
|
|
|
|
const std::string& IdDestination,
|
|
|
|
const CSMWorld::UniversalId::Type type,
|
|
|
|
QUndoCommand* parent) :
|
|
|
|
QUndoCommand(parent),
|
|
|
|
mModel(model),
|
|
|
|
mIdOrigin(idOrigin),
|
2014-01-20 13:14:59 +00:00
|
|
|
mIdDestination(Misc::StringUtils::lowerCase(IdDestination)),
|
2014-01-19 15:49:39 +00:00
|
|
|
mType(type)
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
setText(("Clone record " + idOrigin + " to the " + IdDestination).c_str());
|
2014-01-19 15:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CSMWorld::CloneCommand::redo()
|
|
|
|
{
|
2014-01-27 12:08:14 +00:00
|
|
|
mModel.cloneRecord(mIdOrigin, mIdDestination, mType);
|
2014-01-20 13:29:47 +00:00
|
|
|
|
|
|
|
for (std::map<int, QVariant>::const_iterator iter(mValues.begin()); iter != mValues.end(); ++iter)
|
|
|
|
mModel.setData(mModel.getModelIndex(mIdDestination, iter->first), iter->second);
|
2014-01-19 15:49:39 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
void CSMWorld::CloneCommand::undo()
|
|
|
|
{
|
|
|
|
mModel.removeRow(mModel.getModelIndex(mIdDestination, 0).row());
|
|
|
|
}
|
2014-01-19 15:49:39 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
|
|
|
|
CSMWorld::CreateCommand::CreateCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
|
|
|
|
: QUndoCommand(parent), mModel(model), mId(id), mType(UniversalId::Type_None)
|
2012-12-03 20:44:16 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
setText(("Create record " + id).c_str());
|
2012-12-03 20:44:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
void CSMWorld::CreateCommand::addValue(int column, const QVariant& value)
|
2013-07-30 10:53:03 +00:00
|
|
|
{
|
|
|
|
mValues[column] = value;
|
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
void CSMWorld::CreateCommand::setType(UniversalId::Type type)
|
2013-07-30 08:27:17 +00:00
|
|
|
{
|
|
|
|
mType = type;
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:44:16 +00:00
|
|
|
void CSMWorld::CreateCommand::redo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.addRecord(mId, mType);
|
2013-07-30 10:53:03 +00:00
|
|
|
|
2014-01-20 12:59:00 +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()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.removeRow(mModel.getModelIndex(mId, 0).row());
|
2012-12-06 13:56:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
CSMWorld::RevertCommand::RevertCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
|
|
|
|
: QUndoCommand(parent), mModel(model), mId(id), mOld(0)
|
2012-12-06 13:56:04 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
setText(("Revert record " + id).c_str());
|
2012-12-06 13:56:04 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
mOld = model.getRecord(id).clone();
|
2012-12-06 13:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::RevertCommand::~RevertCommand()
|
|
|
|
{
|
|
|
|
delete mOld;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::RevertCommand::redo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
int column = mModel.findColumnIndex(Columns::ColumnId_Modification);
|
2013-08-26 10:25:52 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
QModelIndex index = mModel.getModelIndex(mId, column);
|
|
|
|
RecordBase::State state = static_cast<RecordBase::State>(mModel.data(index).toInt());
|
2012-12-06 13:56:04 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
if (state == RecordBase::State_ModifiedOnly)
|
2012-12-06 13:56:04 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.removeRows(index.row(), 1);
|
2012-12-06 13:56:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setData(index, static_cast<int>(RecordBase::State_BaseOnly));
|
2012-12-06 13:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::RevertCommand::undo()
|
2012-12-06 14:18:41 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setRecord(mId, *mOld);
|
2012-12-06 14:18:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
CSMWorld::DeleteCommand::DeleteCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
|
|
|
|
: QUndoCommand(parent), mModel(model), mId(id), mOld(0)
|
2012-12-06 14:18:41 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
setText(("Delete record " + id).c_str());
|
2012-12-06 14:18:41 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
mOld = model.getRecord(id).clone();
|
2012-12-06 14:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CSMWorld::DeleteCommand::~DeleteCommand()
|
|
|
|
{
|
|
|
|
delete mOld;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::DeleteCommand::redo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
int column = mModel.findColumnIndex(Columns::ColumnId_Modification);
|
2013-08-26 10:25:52 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
QModelIndex index = mModel.getModelIndex(mId, column);
|
|
|
|
RecordBase::State state = static_cast<RecordBase::State>(mModel.data(index).toInt());
|
2012-12-06 14:18:41 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
if (state == RecordBase::State_ModifiedOnly)
|
2012-12-06 14:18:41 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.removeRows(index.row(), 1);
|
2012-12-06 14:18:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setData(index, static_cast<int>(RecordBase::State_Deleted));
|
2012-12-06 14:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::DeleteCommand::undo()
|
2012-12-06 13:56:04 +00:00
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.setRecord(mId, *mOld);
|
2013-11-14 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
CSMWorld::ReorderRowsCommand::ReorderRowsCommand(IdTable& model, int baseIndex,
|
|
|
|
const std::vector<int>& newOrder)
|
|
|
|
: mModel(model), mBaseIndex(baseIndex), mNewOrder(newOrder)
|
2013-11-14 10:39:14 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void CSMWorld::ReorderRowsCommand::redo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.reorderRows(mBaseIndex, mNewOrder);
|
2013-11-14 10:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSMWorld::ReorderRowsCommand::undo()
|
|
|
|
{
|
2014-01-20 12:59:00 +00:00
|
|
|
int size = static_cast<int>(mNewOrder.size());
|
|
|
|
std::vector<int> reverse(size);
|
2013-11-14 10:39:14 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
reverse.at(mNewOrder[i]) = i;
|
2013-11-14 10:39:14 +00:00
|
|
|
|
2014-01-20 12:59:00 +00:00
|
|
|
mModel.reorderRows(mBaseIndex, reverse);
|
|
|
|
}
|
|
|
|
// kate: indent-mode cstyle; indent-width 4; replace-tabs on;
|