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
  • interface
undoredocommand.cpp
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
19#include <QDebug>
20
21#include "layermanager.h"
22#include "selectionmanager.h"
23
24#include "layersound.h"
25#include "layerbitmap.h"
26#include "layervector.h"
27
28#include "editor.h"
29#include "undoredocommand.h"
30
31UndoRedoCommand::UndoRedoCommand(Editor* editor, QUndoCommand* parent) : QUndoCommand(parent)
32{
33 qDebug() << "backupElement created";
34 mEditor = editor;
35}
36
37UndoRedoCommand::~UndoRedoCommand()
38{
39}
40
41BitmapReplaceCommand::BitmapReplaceCommand(const BitmapImage* undoBitmap,
42 const int undoLayerId,
43 const QString& description,
44 Editor *editor,
45 QUndoCommand *parent) : UndoRedoCommand(editor, parent)
46{
47
48 this->undoBitmap = *undoBitmap;
49 this->undoLayerId = undoLayerId;
50
51 Layer* layer = editor->layers()->currentLayer();
52 redoLayerId = layer->id();
53 redoBitmap = *static_cast<LayerBitmap*>(layer)->
54 getBitmapImageAtFrame(editor->currentFrame());
55
56 setText(description);
57}
58
59void BitmapReplaceCommand::undo()
60{
61 QUndoCommand::undo();
62
63 Layer* layer = editor()->layers()->findLayerById(undoLayerId);
64 static_cast<LayerBitmap*>(layer)->replaceKeyFrame(&undoBitmap);
65
66 editor()->scrubTo(undoBitmap.pos());
67}
68
69void BitmapReplaceCommand::redo()
70{
71 QUndoCommand::redo();
72
73 // Ignore automatic redo when added to undo stack
74 if (isFirstRedo()) { setFirstRedo(false); return; }
75
76 Layer* layer = editor()->layers()->findLayerById(redoLayerId);
77 static_cast<LayerBitmap*>(layer)->replaceKeyFrame(&redoBitmap);
78
79 editor()->scrubTo(redoBitmap.pos());
80}
81
82VectorReplaceCommand::VectorReplaceCommand(const VectorImage* undoVector,
83 const int undoLayerId,
84 const QString& description,
85 Editor* editor,
86 QUndoCommand* parent) : UndoRedoCommand(editor, parent)
87{
88
89 this->undoVector = *undoVector;
90 this->undoLayerId = undoLayerId;
91 Layer* layer = editor->layers()->currentLayer();
92 redoLayerId = layer->id();
93 redoVector = *static_cast<LayerVector*>(layer)->
94 getVectorImageAtFrame(editor->currentFrame());
95
96 setText(description);
97}
98
99void VectorReplaceCommand::undo()
100{
101 QUndoCommand::undo();
102
103 Layer* layer = editor()->layers()->findLayerById(undoLayerId);
104
105 static_cast<LayerVector*>(layer)->replaceKeyFrame(&undoVector);
106
107 editor()->scrubTo(undoVector.pos());
108}
109
110void VectorReplaceCommand::redo()
111{
112 QUndoCommand::redo();
113
114 // Ignore automatic redo when added to undo stack
115 if (isFirstRedo()) { setFirstRedo(false); return; }
116
117 Layer* layer = editor()->layers()->findLayerById(redoLayerId);
118
119 static_cast<LayerVector*>(layer)->replaceKeyFrame(&redoVector);
120
121 editor()->scrubTo(redoVector.pos());
122}
123
124TransformCommand::TransformCommand(const QRectF& undoSelectionRect,
125 const QPointF& undoTranslation,
126 const qreal undoRotationAngle,
127 const qreal undoScaleX,
128 const qreal undoScaleY,
129 const QPointF& undoTransformAnchor,
130 const bool roundPixels,
131 const QString& description,
132 Editor* editor,
133 QUndoCommand *parent) : UndoRedoCommand(editor, parent)
134{
135 this->roundPixels = roundPixels;
136
137 this->undoSelectionRect = undoSelectionRect;
138 this->undoAnchor = undoTransformAnchor;
139 this->undoTranslation = undoTranslation;
140 this->undoRotationAngle = undoRotationAngle;
141 this->undoScaleX = undoScaleX;
142 this->undoScaleY = undoScaleY;
143
144 auto selectMan = editor->select();
145 redoSelectionRect = selectMan->mySelectionRect();
146 redoAnchor = selectMan->currentTransformAnchor();
147 redoTranslation = selectMan->myTranslation();
148 redoRotationAngle = selectMan->myRotation();
149 redoScaleX = selectMan->myScaleX();
150 redoScaleY = selectMan->myScaleY();
151
152 setText(description);
153}
154
155void TransformCommand::undo()
156{
157 apply(undoSelectionRect,
158 undoTranslation,
159 undoRotationAngle,
160 undoScaleX,
161 undoScaleY,
162 undoAnchor,
163 roundPixels);
164}
165
166void TransformCommand::redo()
167{
168 // Ignore automatic redo when added to undo stack
169 if (isFirstRedo()) { setFirstRedo(false); return; }
170
171 apply(redoSelectionRect,
172 redoTranslation,
173 redoRotationAngle,
174 redoScaleX,
175 redoScaleY,
176 redoAnchor,
177 roundPixels);
178}
179
180void TransformCommand::apply(const QRectF& selectionRect,
181 const QPointF& translation,
182 const qreal rotationAngle,
183 const qreal scaleX,
184 const qreal scaleY,
185 const QPointF& selectionAnchor,
186 const bool roundPixels)
187{
188 auto selectMan = editor()->select();
189 selectMan->setSelection(selectionRect, roundPixels);
190 selectMan->setTransformAnchor(selectionAnchor);
191 selectMan->setTranslation(translation);
192 selectMan->setRotation(rotationAngle);
193 selectMan->setScale(scaleX, scaleY);
194
195 selectMan->calculateSelectionTransformation();
196}
BitmapImage
Definition: bitmapimage.h:28
Editor
Definition: editor.h:71
LayerBitmap
Definition: layerbitmap.h:26
Layer
Definition: layer.h:33
LayerVector
Definition: layervector.h:26
UndoRedoCommand
Definition: undoredocommand.h:38
VectorImage
Definition: vectorimage.h:32
QPointF
QRectF
QString
QUndoCommand
QUndoCommand::redo
virtual void redo()
QUndoCommand::undo
virtual void undo()
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39