137 lines
6.1 KiB
C++
137 lines
6.1 KiB
C++
#include "model/tkrTikzDocument.h"
|
|
|
|
#include <algorithm>
|
|
#include <new>
|
|
|
|
namespace TikzRefinery {
|
|
|
|
void tkrTikzDocument::loadSource(const QByteArray &baSource, const QString &strFilePath)
|
|
{
|
|
m_baSource = baSource;
|
|
m_baSavedSource = baSource;
|
|
m_strFilePath = strFilePath;
|
|
QVector<ElementRecord>().swap(m_vecElements);
|
|
m_history.clear();
|
|
m_documentId = QUuid::createUuid();
|
|
++m_uRevision; // Opening another file also invalidates old edits.
|
|
m_bIsLoaded = true;
|
|
}
|
|
|
|
const QByteArray &tkrTikzDocument::sourceBytes() const { return m_baSource; }
|
|
const QString &tkrTikzDocument::filePath() const { return m_strFilePath; }
|
|
const QVector<ElementRecord> &tkrTikzDocument::elements() const { return m_vecElements; }
|
|
quint64 tkrTikzDocument::revision() const { return m_uRevision; }
|
|
const QUuid &tkrTikzDocument::identity() const { return m_documentId; }
|
|
bool tkrTikzDocument::isLoaded() const { return m_bIsLoaded; }
|
|
bool tkrTikzDocument::hasUnsavedChanges() const { return m_baSource != m_baSavedSource; }
|
|
void tkrTikzDocument::markSaved() { m_baSavedSource = m_baSource; }
|
|
|
|
OperationResult tkrTikzDocument::setParsedElements(const QVector<ElementRecord> &vecElements,
|
|
quint64 uExpectedRevision)
|
|
{
|
|
if (!m_bIsLoaded || uExpectedRevision != m_uRevision) {
|
|
return {false, QStringLiteral("Parsed elements belong to an outdated document.")};
|
|
}
|
|
for (const ElementRecord &elementRecord : vecElements) {
|
|
if (elementRecord.uDocumentRevision != m_uRevision) {
|
|
return {false, QStringLiteral("An element belongs to an outdated document.")};
|
|
}
|
|
}
|
|
m_vecElements = vecElements;
|
|
for (ElementRecord &element : m_vecElements) element.documentId = m_documentId;
|
|
return {true, {}};
|
|
}
|
|
|
|
OperationResult tkrTikzDocument::applyCoordinateEdits(const QVector<CoordinateEdit> &vecEdits,
|
|
const QString &strDescription) try
|
|
{
|
|
if (!m_bIsLoaded) {
|
|
return {false, QStringLiteral("No document is open.")};
|
|
}
|
|
QVector<CoordinateEdit> vecSortedEdits = vecEdits;
|
|
std::sort(vecSortedEdits.begin(), vecSortedEdits.end(),
|
|
[](const CoordinateEdit &editLeft, const CoordinateEdit &editRight) {
|
|
return editLeft.rangeSource.nStart < editRight.rangeSource.nStart;
|
|
});
|
|
|
|
qsizetype nPreviousEnd = 0;
|
|
for (const CoordinateEdit &coordinateEdit : vecSortedEdits) {
|
|
const SourceRange &rangeSource = coordinateEdit.rangeSource;
|
|
if (coordinateEdit.uDocumentRevision != m_uRevision) {
|
|
return {false, QStringLiteral("The document changed after this edit was prepared.")};
|
|
}
|
|
// Subtract only after validating the start, avoiding integer overflow.
|
|
if (rangeSource.nStart < 0 || rangeSource.nLength <= 0
|
|
|| rangeSource.nStart > m_baSource.size()
|
|
|| rangeSource.nLength > m_baSource.size() - rangeSource.nStart) {
|
|
return {false, QStringLiteral("A coordinate source range is invalid.")};
|
|
}
|
|
if (rangeSource.nStart < nPreviousEnd) {
|
|
return {false, QStringLiteral("Coordinate replacement ranges overlap.")};
|
|
}
|
|
if (m_baSource.mid(rangeSource.nStart, rangeSource.nLength)
|
|
!= coordinateEdit.baExpectedText) {
|
|
return {false, QStringLiteral("The expected coordinate text no longer matches.")};
|
|
}
|
|
nPreviousEnd = rangeSource.nStart + rangeSource.nLength;
|
|
}
|
|
|
|
// Validate the complete batch first, then replace backwards to preserve offsets.
|
|
QByteArray baUpdatedSource = m_baSource;
|
|
for (auto iterEdit = vecSortedEdits.crbegin(); iterEdit != vecSortedEdits.crend(); ++iterEdit) {
|
|
baUpdatedSource.replace(iterEdit->rangeSource.nStart, iterEdit->rangeSource.nLength,
|
|
iterEdit->baReplacementText);
|
|
}
|
|
if (baUpdatedSource != m_baSource) {
|
|
m_history.record(m_baSource, baUpdatedSource, strDescription);
|
|
// From here, publication consists only of non-allocating changes.
|
|
m_baSource.swap(baUpdatedSource);
|
|
// Qt clear() may allocate on a shared container; an empty swap cannot.
|
|
QVector<ElementRecord>().swap(m_vecElements); // Reparse before another edit.
|
|
++m_uRevision;
|
|
}
|
|
return {true, {}};
|
|
}
|
|
catch (const std::bad_alloc &)
|
|
{
|
|
return {false, QStringLiteral("Insufficient memory to record this edit. Source unchanged.")};
|
|
}
|
|
|
|
bool tkrTikzDocument::canUndo() const { return m_history.canUndo(); }
|
|
OperationResult tkrTikzDocument::acceptPreparedState(tkrTikzDocument &documentCandidate,
|
|
quint64 uExpectedRevision)
|
|
{
|
|
constexpr quint64 uRevisionStep = 1;
|
|
if (m_uRevision != uExpectedRevision || documentCandidate.m_documentId != m_documentId
|
|
|| documentCandidate.m_uRevision != m_uRevision + uRevisionStep) {
|
|
return {false, QStringLiteral("The document changed during preparation.")};
|
|
}
|
|
// Source, parsed records and history were all prepared before publication.
|
|
m_baSource.swap(documentCandidate.m_baSource);
|
|
m_vecElements.swap(documentCandidate.m_vecElements);
|
|
m_history.swap(documentCandidate.m_history);
|
|
m_uRevision = documentCandidate.m_uRevision;
|
|
return {true, {}};
|
|
}
|
|
bool tkrTikzDocument::canRedo() const { return m_history.canRedo(); }
|
|
QString tkrTikzDocument::undoDescription() const { return m_history.undoDescription(); }
|
|
QString tkrTikzDocument::redoDescription() const { return m_history.redoDescription(); }
|
|
OperationResult tkrTikzDocument::restoreHistory(bool bRedo) try
|
|
{
|
|
if (bRedo ? !canRedo() : !canUndo()) {
|
|
return {false, QStringLiteral("No history entry is available.")};
|
|
}
|
|
QByteArray baRestored = m_history.restorationSource(bRedo);
|
|
m_baSource.swap(baRestored);
|
|
m_history.advance(bRedo);
|
|
QVector<ElementRecord>().swap(m_vecElements);
|
|
++m_uRevision; // Revisions are fresh even when earlier bytes return.
|
|
return {true, {}};
|
|
}
|
|
catch (const std::bad_alloc &)
|
|
{
|
|
return {false, QStringLiteral("Insufficient memory to restore history. Source unchanged.")};
|
|
}
|
|
|
|
} // namespace TikzRefinery
|