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

@ -1,6 +1,7 @@
#include "tablemodel.h"
#include "../formats/jsonparser.h"
#include "commands/edititemcommand.h"
#include "commands/insertrowscommand.h"
#include "commands/removerowscommand.h"
#include "modelitem.h"
@ -10,6 +11,7 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
{InfoRole, "Info"},
{AmountRole, "Amount"},
{FactorRole, "Factor"}};
QList<QString> TableModel::intColumns = {"Amount", "Factor"};
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
: QAbstractTableModel{parent}
@ -84,18 +86,30 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
}
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::EditRole) {
if (!checkIndex(index)) {
return false;
}
int columnRole = getRoleForColumn(index.column());
shared_ptr<ModelItem> item = m_items.at(index.row());
return item->setData(value, columnRole);
if (role == Qt::EditRole && checkIndex(index)) {
const int column = index.column();
const int roleForColumn = getRoleForColumn(column);
return setItemData(index, {{roleForColumn, value}});
}
return false;
}
// bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {}
bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {
if (!checkIndex(index)) {
return false;
}
// if (isRoleReadOnly(roleForColumn)) {
// return false;
// }
QMap<int, QVariant> changedValues = onlyChangedValues(index, roles);
if (changedValues.size() > 0) {
EditItemCommand* editCommand = new EditItemCommand(this, index, changedValues);
m_undoStack->push(editCommand);
return true;
}
return false;
}
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
if (parentIndex != QModelIndex()) {
@ -156,6 +170,22 @@ void TableModel::execRemoveItems(const int firstRow, const int nRows) {
endRemoveRows();
}
void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& changedValues) {
shared_ptr<ModelItem> item = m_items.at(row);
bool isDataChanged = item->setItemData(changedValues);
if (isDataChanged) {
/// FIXME due to the mapping from roles to the DisplayRole of different columns the complete row
/// is getting notified about (potential) data changes; dataChanged should be called only for
/// the affected columns
const QModelIndex firstIndex = this->index(row, 0);
const QModelIndex lastIndex = this->index(row, ROLE_NAMES.size() - 1);
QList<int> roles = changedValues.keys();
roles.insert(0, Qt::DisplayRole);
emit dataChanged(firstIndex, lastIndex, roles.toVector());
}
}
int TableModel::getRoleForColumn(const int column) const {
switch (column) {
case 0:
@ -178,3 +208,36 @@ int TableModel::getRoleForColumn(const int column) const {
break;
}
}
QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
const QMap<int, QVariant>& roleValueMap) const {
QMap<int, QVariant> result;
QMap<int, QVariant>::const_iterator i;
for (i = roleValueMap.constBegin(); i != roleValueMap.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) {
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
<< "mean the same. Ignoring...";
continue;
}
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
result.insert(role, newValue);
} else {
qInfo() << "oldValue is already the same as newValue:" << oldValue << "-> ignoring...";
}
}
return result;
}
bool TableModel::isEmptyValueEqualToZero(const int role) const {
const QString roleName = ROLE_NAMES.value(role);
if (intColumns.contains(roleName)) {
return true;
}
return false;
}