Editing an item is now undo/redo-able.
This commit is contained in:
79
model/commands/edititemcommand.cpp
Normal file
79
model/commands/edititemcommand.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "edititemcommand.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "../tablemodel.h"
|
||||
|
||||
EditItemCommand::EditItemCommand(TableModel* model,
|
||||
const QModelIndex& index,
|
||||
QMap<int, QVariant>& changedValues,
|
||||
QUndoCommand* parent)
|
||||
: QUndoCommand(parent)
|
||||
, m_model(model)
|
||||
, m_row(index.row()) {
|
||||
qInfo() << "New EditCommand...";
|
||||
QString commandText;
|
||||
|
||||
if (changedValues.size() == 1) {
|
||||
qDebug() << "Only one value to change. Using more specific command text...";
|
||||
const int role = changedValues.firstKey();
|
||||
const QVariant value = changedValues.first();
|
||||
QString roleName = model->roleNames().value(role);
|
||||
switch (role) {
|
||||
case TableModel::NameRole:
|
||||
case TableModel::DescriptionRole:
|
||||
case TableModel::InfoRole:
|
||||
case TableModel::AmountRole:
|
||||
case TableModel::FactorRole:
|
||||
commandText = QString("Setting '%1' of item '%2' to '%3'")
|
||||
.arg(roleName)
|
||||
.arg(index.data(TableModel::NameRole).toString())
|
||||
.arg(value.toString());
|
||||
break;
|
||||
default:
|
||||
commandText = QString("Edit item '%1'").arg(index.data(TableModel::NameRole).toString());
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "More than one value to change. Using a generic command text...";
|
||||
commandText = QString("Edit item '%1'").arg(index.data(TableModel::NameRole).toString());
|
||||
}
|
||||
setText(commandText);
|
||||
|
||||
m_newValues = changedValues;
|
||||
|
||||
/// storing old values for undo step
|
||||
m_oldValues = getOldValues(index, changedValues);
|
||||
}
|
||||
|
||||
void EditItemCommand::undo() {
|
||||
qDebug() << "Undoing the EditCommand...";
|
||||
m_model->execEditItemData(m_row, m_oldValues);
|
||||
}
|
||||
|
||||
void EditItemCommand::redo() {
|
||||
qDebug() << "(Re-)doing the EditCommand...";
|
||||
m_model->execEditItemData(m_row, m_newValues);
|
||||
}
|
||||
|
||||
const QMap<int, QVariant> EditItemCommand::getOldValues(
|
||||
const QModelIndex& index,
|
||||
const QMap<int, QVariant>& changedValues) const {
|
||||
QMap<int, QVariant> result;
|
||||
QMap<int, QVariant>::const_iterator i;
|
||||
for (i = changedValues.constBegin(); i != changedValues.constEnd(); ++i) {
|
||||
const int role = i.key();
|
||||
const QVariant newValue = i.value();
|
||||
const QVariant oldValue = index.data(role);
|
||||
// TODO check if role is a editable role?
|
||||
if (oldValue != newValue) {
|
||||
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
|
||||
result.insert(role, oldValue);
|
||||
} else {
|
||||
qInfo() << "oldValue is already the same as newValue:" << oldValue;
|
||||
}
|
||||
}
|
||||
// QVariant oldModifiedDate = index.data(ModifiedDateUTCRole);
|
||||
// result.insert(ModifiedDateUTCRole, oldModifiedDate);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user