File indexing completed on 2024-05-12 16:01:30

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_document_undo_store.h"
0008 
0009 #include "KisDocument.h"
0010 #include <kundo2stack.h>
0011 
0012 
0013 /*****************************************************************/
0014 /*                KisDocumentUndoStore                           */
0015 /*****************************************************************/
0016 
0017 KisDocumentUndoStore::KisDocumentUndoStore(KisDocument *doc)
0018     : m_doc(doc)
0019 {
0020     /// The thread of the document should be the same as the store to
0021     /// avoid incorrect signal delivery
0022     KIS_SAFE_ASSERT_RECOVER_NOOP(doc->thread() == this->thread());
0023 
0024     // Use direct connection to avoid queueing the singal forwarding (BUG:447985)
0025     connect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SIGNAL(historyStateChanged()), Qt::DirectConnection);
0026 }
0027 
0028 const KUndo2Command* KisDocumentUndoStore::presentCommand()
0029 {
0030     return m_doc->undoStack()->command(m_doc->undoStack()->index() - 1);
0031 }
0032 
0033 void KisDocumentUndoStore::undoLastCommand()
0034 {
0035     /**
0036      * FIXME: Added as a workaround for being able to cancel creation
0037      * of the new adjustment mask (or any other mask whose
0038      * creation can be cancelled).
0039      *
0040      * Ideally, we should use "addToIndex-commit" technique like git does.
0041      * When a user presses Create button, we call command->redo()
0042      * and save this command in a cache. When the user confirms creation
0043      * of the layer with "OK" button, we "commit" the command to the undoStack.
0044      * If the user changes his mind and presses Cancel, we just call
0045      * command->undo() and remove the cache without committing it
0046      * to the undoStack
0047      */
0048     m_doc->undoStack()->undo();
0049 }
0050 
0051 void KisDocumentUndoStore::addCommand(KUndo2Command *command)
0052 {
0053     if(!command) return;
0054     m_doc->undoStack()->push(command);
0055 }
0056 
0057 void KisDocumentUndoStore::beginMacro(const KUndo2MagicString& macroName)
0058 {
0059     m_doc->undoStack()->beginMacro(macroName);
0060 }
0061 
0062 void KisDocumentUndoStore::endMacro()
0063 {
0064     m_doc->undoStack()->endMacro();
0065 }
0066 
0067 void KisDocumentUndoStore::purgeRedoState()
0068 {
0069     m_doc->undoStack()->purgeRedoState();
0070 }