Files
vpn-dns-gateway/demo/tikzrefinery/code/src/editing/tkrEditHistory.cpp
T
vsun1 27b82c169e
vpn-dns-gateway/pipeline/head This commit looks good
vpn-dns-gateway/pipeline/pr-main This commit looks good
Add frozen TikZRefinery demo baseline and build pipeline
2026-11-25 22:46:33 -05:00

35 lines
1.5 KiB
C++

#include "editing/tkrEditHistory.h"
#include <utility>
namespace TikzRefinery {
namespace { constexpr qsizetype nEntryStep = 1; }
bool tkrEditHistory::canUndo() const { return m_nCursor > 0; }
bool tkrEditHistory::canRedo() const { return m_nCursor < m_vecEntries.size(); }
QString tkrEditHistory::undoDescription() const
{ return canUndo() ? m_vecEntries.at(m_nCursor - nEntryStep).strDescription : QString(); }
QString tkrEditHistory::redoDescription() const
{ return canRedo() ? m_vecEntries.at(m_nCursor).strDescription : QString(); }
void tkrEditHistory::record(const QByteArray &baBefore, const QByteArray &baAfter,
const QString &strDescription)
{
// Prepare detached storage first. Allocation failure cannot discard the redo branch.
QVector<Entry> vecPrepared = m_vecEntries;
vecPrepared.resize(m_nCursor);
vecPrepared.append({baBefore, baAfter, strDescription});
m_vecEntries.swap(vecPrepared);
m_nCursor = m_vecEntries.size();
}
void tkrEditHistory::clear() { QVector<Entry>().swap(m_vecEntries); m_nCursor = 0; }
QByteArray tkrEditHistory::restorationSource(bool bRedo) const
{
return bRedo ? m_vecEntries.at(m_nCursor).baAfter
: m_vecEntries.at(m_nCursor - nEntryStep).baBefore;
}
void tkrEditHistory::advance(bool bRedo) { m_nCursor += bRedo ? nEntryStep : -nEntryStep; }
void tkrEditHistory::swap(tkrEditHistory &historyOther) noexcept
{
m_vecEntries.swap(historyOther.m_vecEntries);
std::swap(m_nCursor, historyOther.m_nCursor);
}
} // namespace TikzRefinery