PDF4QT/Pdf4QtViewer/pdfundoredomanager.cpp

112 lines
3.0 KiB
C++
Raw Normal View History

2020-04-25 18:15:12 +02:00
// Copyright (C) 2020 Jakub Melka
//
2020-12-20 19:03:58 +01:00
// This file is part of Pdf4Qt.
2020-04-25 18:15:12 +02:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is free software: you can redistribute it and/or modify
2020-04-25 18:15:12 +02:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is distributed in the hope that it will be useful,
2020-04-25 18:15:12 +02:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2020-12-20 19:03:58 +01:00
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
2020-04-25 18:15:12 +02:00
#include "pdfundoredomanager.h"
namespace pdfviewer
{
PDFUndoRedoManager::PDFUndoRedoManager(QObject* parent) :
BaseClass(parent)
{
}
PDFUndoRedoManager::~PDFUndoRedoManager()
{
}
void PDFUndoRedoManager::doUndo()
{
if (!canUndo())
{
// Undo operation can't be performed
return;
}
UndoRedoItem item = m_undoSteps.back();
m_undoSteps.pop_back();
m_redoSteps.insert(m_redoSteps.begin(), item);
clampUndoRedoSteps();
emit undoRedoStateChanged();
2020-05-03 18:52:22 +02:00
emit documentChangeRequest(pdf::PDFModifiedDocument(item.oldDocument, nullptr, item.flags));
2020-04-25 18:15:12 +02:00
}
void PDFUndoRedoManager::doRedo()
{
if (!canRedo())
{
// Redo operation can't be performed
return;
}
UndoRedoItem item = m_redoSteps.front();
m_redoSteps.erase(m_redoSteps.begin());
m_undoSteps.push_back(item);
clampUndoRedoSteps();
emit undoRedoStateChanged();
2020-05-03 18:52:22 +02:00
emit documentChangeRequest(pdf::PDFModifiedDocument(item.newDocument, nullptr, item.flags));
2020-04-25 18:15:12 +02:00
}
void PDFUndoRedoManager::clear()
{
if (canUndo() || canRedo())
{
m_undoSteps.clear();
m_redoSteps.clear();
emit undoRedoStateChanged();
}
}
2020-05-03 18:52:22 +02:00
void PDFUndoRedoManager::createUndo(pdf::PDFModifiedDocument document, pdf::PDFDocumentPointer oldDocument)
{
m_undoSteps.emplace_back(oldDocument, document, document.getFlags());
m_redoSteps.clear();
clampUndoRedoSteps();
emit undoRedoStateChanged();
}
void PDFUndoRedoManager::setMaximumSteps(size_t undoLimit, size_t redoLimit)
{
if (m_undoLimit != undoLimit || m_redoLimit != redoLimit)
{
m_undoLimit = undoLimit;
m_redoLimit = redoLimit;
clampUndoRedoSteps();
emit undoRedoStateChanged();
}
}
2020-04-25 18:15:12 +02:00
void PDFUndoRedoManager::clampUndoRedoSteps()
{
if (m_undoSteps.size() > m_undoLimit)
{
// We erase from oldest steps to newest
m_undoSteps.erase(m_undoSteps.begin(), std::next(m_undoSteps.begin(), m_undoSteps.size() - m_undoLimit));
}
if (m_redoSteps.size() > m_redoLimit)
{
// Newest steps are erased
m_redoSteps.resize(m_redoLimit);
}
}
} // namespace pdfviewer