Editing an item is now undo/redo-able.

This commit is contained in:
2025-12-15 18:04:14 +01:00
parent 772ab6b2ff
commit e54204e394
7 changed files with 212 additions and 10 deletions

View 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;
}

View File

@ -0,0 +1,30 @@
#ifndef EDITITEMCOMMAND_H
#define EDITITEMCOMMAND_H
#include <QMap>
#include <QUndoCommand>
class TableModel;
class EditItemCommand : public QUndoCommand {
public:
EditItemCommand(TableModel* model,
const QModelIndex& index,
QMap<int, QVariant>& changedValues,
QUndoCommand* parent = nullptr);
/// QUndoCommand interface
void undo();
void redo();
private:
TableModel* m_model = nullptr;
const int m_row;
QMap<int, QVariant> m_oldValues;
QMap<int, QVariant> m_newValues;
/// private functions
const QMap<int, QVariant> getOldValues(const QModelIndex& index,
const QMap<int, QVariant>& changedValues) const;
};
#endif // EDITITEMCOMMAND_H