#include "editing/tkrEditHistory.h" #include 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 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().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