Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • managers
undoredomanager.h
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2008-2009 Mj Mendoza IV
6Copyright (C) 2012-2020 Matthew Chiawen Chang
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; version 2 of the License.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17*/
18#ifndef UNDOREDOMANAGER_H
19#define UNDOREDOMANAGER_H
20
21#include "basemanager.h"
22#include "layer.h"
23#include "keyframe.h"
24
25#include "preferencesdef.h"
26
27#include <QUndoStack>
28#include <QRectF>
29#include <QMap>
30
31class QAction;
32class QUndoCommand;
33
34class BitmapImage;
35class VectorImage;
36class Camera;
37class SoundClip;
38class KeyFrame;
39class LegacyBackupElement;
40class UndoRedoCommand;
41
42using SAVESTATE_ID = int;
43
45enum class UndoRedoRecordType {
46 KEYFRAME_MODIFY, // Any modification that involve a keyframe
47 KEYFRAME_REMOVE, // Removing a keyframe
48 KEYFRAME_ADD, // Adding a keyframe
49 KEYFRAME_MOVE,
50 // SCRUB_LAYER, // Scrubbing layer
51 // SCRUB_KEYFRAME, // Scrubbing keyframe
52 INVALID
53};
54
55struct SelectionSaveState {
56
57 SelectionSaveState() = default;
58 SelectionSaveState(const QRectF& rect,
59 const qreal rotationAngle,
60 const qreal scaleX,
61 const qreal scaleY,
62 const QPointF& translation,
63 const QPointF& anchor)
64 {
65 this->bounds = rect;
66 this->rotationAngle = rotationAngle;
67 this->scaleX = scaleX;
68 this->scaleY = scaleY;
69 this->translation = translation;
70 this->anchor = anchor;
71 }
72
73 QRectF bounds;
74 qreal rotationAngle = 0.0;
75 qreal scaleX = 0.0;
76 qreal scaleY = 0.0;
77 QPointF translation;
78 QPointF anchor;
79};
80
81struct MoveFramesSaveState {
82
83 MoveFramesSaveState() = default;
84 MoveFramesSaveState(int offset,
85 const QList<int>& positions)
86 {
87 this->offset = offset;
88 this->positions = positions;
89 }
90
91 int offset = 0;
92 QList<int> positions;
93};
94
100struct UserSaveState {
101 MoveFramesSaveState moveFramesState = {};
102};
103
106struct UndoSaveState {
107 // Common data
108 UndoRedoRecordType recordType = UndoRedoRecordType::INVALID;
109 int layerId = 0;
110 int currentFrameIndex = 0;
111 Layer::LAYER_TYPE layerType = Layer::UNDEFINED;
112 std::unique_ptr<KeyFrame> keyframe;
113 SelectionSaveState selectionState = {};
114
115 UserSaveState userState = {};
116};
117
118class UndoRedoManager : public BaseManager
119{
120 Q_OBJECT
121
122public:
123 explicit UndoRedoManager(Editor* editor);
124 ~UndoRedoManager() override;
125
126 bool init() override;
127 Status load(Object*) override;
128 Status save(Object*) override;
129
135 void record(SAVESTATE_ID SaveStateId, const QString& description);
136
137
140 bool hasUnsavedChanges() const;
141
144 SAVESTATE_ID createState(UndoRedoRecordType recordType);
145
151 void addUserState(SAVESTATE_ID SaveStateId, const UserSaveState& userState);
152
153 QAction* createUndoAction(QObject* parent, const QIcon& icon);
154 QAction* createRedoAction(QObject* parent, const QIcon& icon);
155
156 void updateUndoAction(QAction* undoAction);
157 void updateRedoAction(QAction* redoAction);
158
160 void clearStack();
161
162 // Developer note:
163 // Our legacy undo/redo system is not meant to be build upon anymore.
164 // The implementation should however be kept until the new undo/redo system takes over.
165
166 void legacyBackup(const QString& undoText);
167 bool legacyBackup(int backupLayer, int backupFrame, const QString& undoText);
178 void sanitizeLegacyBackupElementsAfterLayerDeletion(int layerIndex);
179 void restoreLegacyKey();
180
181 void rememberLastModifiedFrame(int layerNumber, int frameNumber);
182
183 void onSettingChanged(SETTING setting);
184
185signals:
186 void didUpdateUndoStack();
187
188private:
189
190 void replaceKeyFrame(const UndoSaveState& undoState, const QString& description);
191 void replaceBitmap(const UndoSaveState& undoState, const QString& description);
192 void replaceVector(const UndoSaveState& undoState, const QString& description);
193
194 void addKeyFrame(const UndoSaveState& undoState, const QString& description);
195 void removeKeyFrame(const UndoSaveState& undoState, const QString& description);
196 void moveKeyFrames(const UndoSaveState& undoState, const QString& description);
197
198 void initCommonKeyFrameState(UndoSaveState* undoSaveState) const;
199
200 void pushCommand(QUndoCommand* command);
201
202 void clearState(UndoSaveState*& state);
203 void clearSaveStates();
204
205 void legacyUndo();
206 void legacyRedo();
207
208 QUndoStack mUndoStack;
209
210 QMap<SAVESTATE_ID, UndoSaveState*> mSaveStates;
211
212 // Legacy system
213 int mLegacyBackupIndex = -1;
214 LegacyBackupElement* mLegacyBackupAtSave = nullptr;
215 QList<LegacyBackupElement*> mLegacyBackupList;
216
217 int mLegacyLastModifiedLayer = -1;
218 int mLegacyLastModifiedFrame = -1;
219
220 SAVESTATE_ID mSaveStateId = 1;
221
222 bool mNewBackupSystemEnabled = false;
223};
224
225#endif // UNDOREDOMANAGER_H
BaseManager
Definition: basemanager.h:29
BitmapImage
Definition: bitmapimage.h:28
Camera
Definition: camera.h:25
Editor
Definition: editor.h:71
KeyFrame
Definition: keyframe.h:30
LegacyBackupElement
Definition: legacybackupelement.h:29
Object
Definition: object.h:42
SoundClip
Definition: soundclip.h:27
Status
Definition: pencilerror.h:40
UndoRedoCommand
Definition: undoredocommand.h:40
UndoRedoManager
Definition: undoredomanager.h:119
UndoRedoManager::hasUnsavedChanges
bool hasUnsavedChanges() const
Checks whether there are unsaved changes.
Definition: undoredomanager.cpp:159
UndoRedoManager::sanitizeLegacyBackupElementsAfterLayerDeletion
void sanitizeLegacyBackupElementsAfterLayerDeletion(int layerIndex)
Restores integrity of the backup elements after a layer has been deleted.
Definition: undoredomanager.cpp:570
UndoRedoManager::addUserState
void addUserState(SAVESTATE_ID SaveStateId, const UserSaveState &userState)
Adds userState to the saveState found at SaveStateId If no record is found matching the id,...
Definition: undoredomanager.cpp:274
UndoRedoManager::record
void record(SAVESTATE_ID SaveStateId, const QString &description)
Records the given save state.
Definition: undoredomanager.cpp:96
UndoRedoManager::createState
SAVESTATE_ID createState(UndoRedoRecordType recordType)
Prepares and returns an save state with common data.
Definition: undoredomanager.cpp:261
UndoRedoManager::clearStack
void clearStack()
Clears the undo stack.
Definition: undoredomanager.cpp:397
VectorImage
Definition: vectorimage.h:32
QAction
QIcon
QList
QMap
QObject
QObject::Q_OBJECT
Q_OBJECTQ_OBJECT
QObject::parent
QObject * parent() const const
QPointF
QRectF
QString
QUndoCommand
QUndoStack
MoveFramesSaveState
Definition: undoredomanager.h:81
SelectionSaveState
Definition: undoredomanager.h:55
UndoSaveState
This is the main undo/redo state structure which is meant to populate whatever states that needs to b...
Definition: undoredomanager.h:106
UserSaveState
Use this struct to store user related data that will later be added to the backup This struct is mean...
Definition: undoredomanager.h:100
Generated on Wed Apr 29 2026 06:05:08 for Pencil2D by doxygen 1.9.6 based on revision 5841f625b8680b86111af64be6a1235440cd8ff2