76 lines
2.6 KiB
C++
76 lines
2.6 KiB
C++
#include "edititemcommand.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include "../metadata.h"
|
|
#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);
|
|
|
|
if (USER_FACING_ROLES.contains(role)) {
|
|
commandText = QString("Setting '%1' of item '%2' to '%3'")
|
|
.arg(roleName)
|
|
.arg(index.data(DEFAULT_ROLE).toString())
|
|
.arg(value.toString());
|
|
} else {
|
|
qWarning() << "Role didn't match! Using a generic command text...";
|
|
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
|
}
|
|
} else {
|
|
qDebug() << "More than one value to change. Using a generic command text...";
|
|
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).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;
|
|
}
|