File indexing completed on 2024-12-22 04:13:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisToolChangesTracker.h"
0008 
0009 #include "kis_global.h"
0010 #include <QSharedPointer>
0011 
0012 struct KisToolChangesTracker::Private {
0013     QList<KisToolChangesTrackerDataSP> undoStack;
0014     QList<KisToolChangesTrackerDataSP> redoStack;
0015 };
0016 
0017 
0018 KisToolChangesTracker::KisToolChangesTracker()
0019     : m_d(new Private)
0020 {
0021 }
0022 
0023 KisToolChangesTracker::~KisToolChangesTracker()
0024 {
0025 }
0026 
0027 void KisToolChangesTracker::commitConfig(KisToolChangesTrackerDataSP state)
0028 {
0029     m_d->undoStack.append(state);
0030     m_d->redoStack.clear();
0031 }
0032 
0033 void KisToolChangesTracker::requestUndo()
0034 {
0035     if (m_d->undoStack.isEmpty()) return;
0036 
0037     m_d->redoStack.append(m_d->undoStack.last());
0038     m_d->undoStack.removeLast();
0039     if (!m_d->undoStack.isEmpty()) {
0040         emit sigConfigChanged(m_d->undoStack.last());
0041     }
0042 }
0043 
0044 void KisToolChangesTracker::requestRedo()
0045 {
0046     if (m_d->redoStack.isEmpty()) return;
0047 
0048     m_d->undoStack.append(m_d->redoStack.last());
0049     m_d->redoStack.removeLast();
0050     if (!m_d->undoStack.isEmpty()) {
0051         emit sigConfigChanged(m_d->undoStack.last());
0052     }
0053 }
0054 
0055 KisToolChangesTrackerDataSP KisToolChangesTracker::lastState() const
0056 {
0057     return !m_d->undoStack.isEmpty() ? m_d->undoStack.last() : static_cast<QSharedPointer<KisToolChangesTrackerData>>(0);
0058 }
0059 
0060 void KisToolChangesTracker::reset()
0061 {
0062     m_d->undoStack.clear();
0063     m_d->redoStack.clear();
0064 }
0065 
0066 bool KisToolChangesTracker::isEmpty() const
0067 {
0068     return m_d->undoStack.isEmpty() && m_d->redoStack.isEmpty();
0069 }
0070 
0071 bool KisToolChangesTracker::canUndo() const
0072 {
0073     return m_d->undoStack.size() > 1;
0074 }
0075 
0076 bool KisToolChangesTracker::canRedo() const
0077 {
0078     return !m_d->redoStack.isEmpty();
0079 }