Editing an item is now undo/redo-able.
This commit is contained in:
@ -27,6 +27,7 @@ add_library(${TARGET_APP} STATIC
|
|||||||
formats/jsonparser.h formats/jsonparser.cpp
|
formats/jsonparser.h formats/jsonparser.cpp
|
||||||
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
|
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
|
||||||
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
|
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
|
||||||
|
model/commands/edititemcommand.h model/commands/edititemcommand.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
30
model/commands/edititemcommand.h
Normal file
30
model/commands/edititemcommand.h
Normal 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
|
||||||
@ -16,3 +16,25 @@ bool ModelItem::setData(const QVariant& value, int role) {
|
|||||||
|
|
||||||
return valueChanged;
|
return valueChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
|
||||||
|
bool valueChanged = false;
|
||||||
|
|
||||||
|
QMap<int, QVariant>::const_iterator citer = changedValues.constBegin();
|
||||||
|
|
||||||
|
while (citer != changedValues.constEnd()) {
|
||||||
|
const int role = citer.key();
|
||||||
|
const QVariant value = citer.value();
|
||||||
|
|
||||||
|
if (m_values.contains(role)) {
|
||||||
|
if (m_values.value(role) != value) {
|
||||||
|
valueChanged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_values[role] = value;
|
||||||
|
|
||||||
|
citer++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return valueChanged;
|
||||||
|
}
|
||||||
|
|||||||
@ -8,8 +8,9 @@ class ModelItem {
|
|||||||
ModelItem(const QHash<int, QVariant> values);
|
ModelItem(const QHash<int, QVariant> values);
|
||||||
|
|
||||||
QVariant data(int role) const;
|
QVariant data(int role) const;
|
||||||
|
|
||||||
bool setData(const QVariant& value, int role);
|
bool setData(const QVariant& value, int role);
|
||||||
|
// TODO change return value to list of changed roles
|
||||||
|
bool setItemData(const QMap<int, QVariant>& changedValues);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<int, QVariant> m_values;
|
QHash<int, QVariant> m_values;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include "tablemodel.h"
|
#include "tablemodel.h"
|
||||||
|
|
||||||
#include "../formats/jsonparser.h"
|
#include "../formats/jsonparser.h"
|
||||||
|
#include "commands/edititemcommand.h"
|
||||||
#include "commands/insertrowscommand.h"
|
#include "commands/insertrowscommand.h"
|
||||||
#include "commands/removerowscommand.h"
|
#include "commands/removerowscommand.h"
|
||||||
#include "modelitem.h"
|
#include "modelitem.h"
|
||||||
@ -10,6 +11,7 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
|||||||
{InfoRole, "Info"},
|
{InfoRole, "Info"},
|
||||||
{AmountRole, "Amount"},
|
{AmountRole, "Amount"},
|
||||||
{FactorRole, "Factor"}};
|
{FactorRole, "Factor"}};
|
||||||
|
QList<QString> TableModel::intColumns = {"Amount", "Factor"};
|
||||||
|
|
||||||
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
||||||
: QAbstractTableModel{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) {
|
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||||
if (role == Qt::EditRole) {
|
if (role == Qt::EditRole && checkIndex(index)) {
|
||||||
if (!checkIndex(index)) {
|
const int column = index.column();
|
||||||
return false;
|
const int roleForColumn = getRoleForColumn(column);
|
||||||
}
|
return setItemData(index, {{roleForColumn, value}});
|
||||||
int columnRole = getRoleForColumn(index.column());
|
|
||||||
shared_ptr<ModelItem> item = m_items.at(index.row());
|
|
||||||
return item->setData(value, columnRole);
|
|
||||||
}
|
}
|
||||||
return false;
|
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) {
|
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||||
if (parentIndex != QModelIndex()) {
|
if (parentIndex != QModelIndex()) {
|
||||||
@ -156,6 +170,22 @@ void TableModel::execRemoveItems(const int firstRow, const int nRows) {
|
|||||||
endRemoveRows();
|
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 {
|
int TableModel::getRoleForColumn(const int column) const {
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -178,3 +208,36 @@ int TableModel::getRoleForColumn(const int column) const {
|
|||||||
break;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -13,10 +13,12 @@ class TableModel : public QAbstractTableModel {
|
|||||||
|
|
||||||
friend class InsertRowsCommand;
|
friend class InsertRowsCommand;
|
||||||
friend class RemoveRowsCommand;
|
friend class RemoveRowsCommand;
|
||||||
|
friend class EditItemCommand;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||||
static QHash<int, QByteArray> ROLE_NAMES;
|
static QHash<int, QByteArray> ROLE_NAMES;
|
||||||
|
static QList<QString> intColumns;
|
||||||
|
|
||||||
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
||||||
|
|
||||||
@ -30,7 +32,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||||
|
|
||||||
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
||||||
// bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||||
@ -48,9 +50,13 @@ class TableModel : public QAbstractTableModel {
|
|||||||
/// undo/redo functions
|
/// undo/redo functions
|
||||||
void execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList);
|
void execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList);
|
||||||
void execRemoveItems(const int firstRow, const int nRows);
|
void execRemoveItems(const int firstRow, const int nRows);
|
||||||
|
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
||||||
|
|
||||||
/// misc functions
|
/// misc functions
|
||||||
int getRoleForColumn(const int column) const;
|
int getRoleForColumn(const int column) const;
|
||||||
|
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||||
|
const QMap<int, QVariant>& roleValueMap) const;
|
||||||
|
bool isEmptyValueEqualToZero(const int role) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TABLEMODEL_H
|
#endif // TABLEMODEL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user