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
30class QAction;
31class QUndoCommand;
32
33class BitmapImage;
34class VectorImage;
35class Camera;
36class SoundClip;
37class KeyFrame;
38class LegacyBackupElement;
39class UndoRedoCommand;
40
42enum class UndoRedoRecordType {
43 KEYFRAME_MODIFY, // Any modification that involve a keyframe
44
45 // Possible future actions
46 // KEYFRAME_REMOVE, // Removing a keyframe
47 // KEYFRAME_ADD, // Adding a keyframe
48 // SCRUB_LAYER, // Scrubbing layer
49 // SCRUB_KEYFRAME, // Scrubbing keyframe
50 INVALID
51};
52
53struct SelectionSaveState {
54
55 SelectionSaveState(const QRectF& rect,
56 const qreal rotationAngle,
57 const qreal scaleX,
58 const qreal scaleY,
59 const QPointF& translation,
60 const QPointF& anchor)
61 {
62 this->bounds = rect;
63 this->rotationAngle = rotationAngle;
64 this->scaleX = scaleX;
65 this->scaleY = scaleY;
66 this->translation = translation;
67 this->anchor = anchor;
68 }
69
70 QRectF bounds;
71 qreal rotationAngle = 0.0;
72 qreal scaleX = 0.0;
73 qreal scaleY = 0.0;
74 QPointF translation;
75 QPointF anchor;
76};
77
80struct UndoSaveState {
81 int layerId = 0;
82 Layer::LAYER_TYPE layerType = Layer::UNDEFINED;
83
84 std::unique_ptr<KeyFrame> keyframe = nullptr;
85 std::unique_ptr<SelectionSaveState> selectionState = nullptr;
86
87 UndoRedoRecordType recordType = UndoRedoRecordType::INVALID;
88};
89
90class UndoRedoManager : public BaseManager
91{
92 Q_OBJECT
93
94public:
95 explicit UndoRedoManager(Editor* editor);
96 ~UndoRedoManager() override;
97
98 bool init() override;
99 Status load(Object*) override;
100 Status save(Object*) override;
101
107 void record(const UndoSaveState*& undoState, const QString& description);
108
109
112 bool hasUnsavedChanges() const;
113
116 const UndoSaveState* state(UndoRedoRecordType recordType) const;
117
118 QAction* createUndoAction(QObject* parent, const QIcon& icon);
119 QAction* createRedoAction(QObject* parent, const QIcon& icon);
120
121 void updateUndoAction(QAction* undoAction);
122 void updateRedoAction(QAction* redoAction);
123
125 void clearStack();
126
127 // Developer note:
128 // Our legacy undo/redo system is not meant to be build upon anymore.
129 // The implementation should however be kept until the new undo/redo system takes over.
130
131 void legacyBackup(const QString& undoText);
132 bool legacyBackup(int backupLayer, int backupFrame, const QString& undoText);
143 void sanitizeLegacyBackupElementsAfterLayerDeletion(int layerIndex);
144 void restoreLegacyKey();
145
146 void rememberLastModifiedFrame(int layerNumber, int frameNumber);
147
148 void onSettingChanged(SETTING setting);
149
150signals:
151 void didUpdateUndoStack();
152
153private:
154
155 void replaceKeyFrame(const UndoSaveState& undoState, const QString& description);
156 void replaceBitmap(const UndoSaveState& undoState, const QString& description);
157 void replaceVector(const UndoSaveState& undoState, const QString& description);
158
159 const UndoSaveState* savedKeyFrameState() const;
160
161 void pushCommand(QUndoCommand* command);
162
163 void legacyUndo();
164 void legacyRedo();
165
166 QUndoStack mUndoStack;
167
168 // Legacy system
169 int mLegacyBackupIndex = -1;
170 LegacyBackupElement* mLegacyBackupAtSave = nullptr;
171 QList<LegacyBackupElement*> mLegacyBackupList;
172
173 int mLegacyLastModifiedLayer = -1;
174 int mLegacyLastModifiedFrame = -1;
175
176 bool mNewBackupSystemEnabled = false;
177};
178
179#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:38
UndoRedoManager
Definition: undoredomanager.h:91
UndoRedoManager::record
void record(const UndoSaveState *&undoState, const QString &description)
Records the given save state.
Definition: undoredomanager.cpp:95
UndoRedoManager::hasUnsavedChanges
bool hasUnsavedChanges() const
Checks whether there are unsaved changes.
Definition: undoredomanager.cpp:125
UndoRedoManager::sanitizeLegacyBackupElementsAfterLayerDeletion
void sanitizeLegacyBackupElementsAfterLayerDeletion(int layerIndex)
Restores integrity of the backup elements after a layer has been deleted.
Definition: undoredomanager.cpp:509
UndoRedoManager::clearStack
void clearStack()
Clears the undo stack.
Definition: undoredomanager.cpp:336
UndoRedoManager::state
const UndoSaveState * state(UndoRedoRecordType recordType) const
Prepares and returns a save state with the given scope.
Definition: undoredomanager.cpp:199
VectorImage
Definition: vectorimage.h:32
QAction
QIcon
QList
QObject
QObject::Q_OBJECT
Q_OBJECTQ_OBJECT
QObject::parent
QObject * parent() const const
QPointF
QRectF
QString
QUndoCommand
QUndoStack
SelectionSaveState
Definition: undoredomanager.h:53
UndoSaveState
This is the main undo/redo state structure which is meant to populate whatever states that needs to b...
Definition: undoredomanager.h:80
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39