#include "edititemcommand.h" #include #include "../metadata.h" #include "../tablemodel.h" EditItemCommand::EditItemCommand(TableModel* model, const QModelIndex& index, QMap& 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 NameRole: case DescriptionRole: case InfoRole: case AmountRole: case FactorRole: commandText = QString("Setting '%1' of item '%2' to '%3'") .arg(roleName) .arg(index.data(NameRole).toString()) .arg(value.toString()); break; default: commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString()); break; } } else { qDebug() << "More than one value to change. Using a generic command text..."; commandText = QString("Edit item '%1'").arg(index.data(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 EditItemCommand::getOldValues( const QModelIndex& index, const QMap& changedValues) const { QMap result; QMap::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; }