XPlor/app/fieldeditcommand.cpp

68 lines
1.8 KiB
C++
Raw Permalink Normal View History

#include "fieldeditcommand.h"
// Command ID for merging consecutive edits to the same field
static const int FIELD_EDIT_COMMAND_ID = 1001;
FieldEditCommand::FieldEditCommand(int journalId,
const QString& fieldName,
const QVariant& oldValue,
const QVariant& newValue,
ApplyCallback applyCallback,
QUndoCommand* parent)
: QUndoCommand(parent)
, m_journalId(journalId)
, m_fieldName(fieldName)
, m_oldValue(oldValue)
, m_newValue(newValue)
, m_applyCallback(applyCallback)
{
setText(QString("Edit %1").arg(fieldName));
}
void FieldEditCommand::undo()
{
if (m_applyCallback) {
m_applyCallback(m_fieldName, m_oldValue);
}
}
void FieldEditCommand::redo()
{
// Skip the first redo because the value was already applied
// when the user made the edit (before command was pushed)
if (m_firstRedo) {
m_firstRedo = false;
return;
}
if (m_applyCallback) {
m_applyCallback(m_fieldName, m_newValue);
}
}
int FieldEditCommand::id() const
{
return FIELD_EDIT_COMMAND_ID;
}
bool FieldEditCommand::mergeWith(const QUndoCommand* other)
{
// Merge consecutive edits to the same field in the same journal
// This prevents creating a new undo entry for every keystroke
if (other->id() != id()) {
return false;
}
const FieldEditCommand* otherEdit = static_cast<const FieldEditCommand*>(other);
// Only merge if same journal and field
if (otherEdit->m_journalId != m_journalId ||
otherEdit->m_fieldName != m_fieldName) {
return false;
}
// Keep original old value, take new value from other command
m_newValue = otherEdit->m_newValue;
return true;
}