Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_command_utils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_command_utils.h"
8#include "kis_assert.h"
9
10namespace KisCommandUtils
11{
13 : KUndo2Command(parent),
14 m_firstRedo(true) {}
15
17 : KUndo2Command(text, parent),
18 m_firstRedo(true) {}
19
21 {
22 if (m_firstRedo) {
23 m_firstRedo = false;
24
26 }
27
29 }
30
32 {
34 }
35
37 {
38 if (!cmd) return;
40 }
41
42 LambdaCommand::LambdaCommand(std::function<KUndo2Command*()> createCommandFunc)
43 : m_createCommandFunc(createCommandFunc)
44 {
45
46 }
47
49 std::function<KUndo2Command*()> createCommandFunc)
50 : AggregateCommand(text),
51 m_createCommandFunc(createCommandFunc)
52 {
53
54 }
55
57 KUndo2Command *parent,
58 std::function<KUndo2Command*()> createCommandFunc)
59 : AggregateCommand(text, parent),
60 m_createCommandFunc(createCommandFunc)
61 {
62 }
63
65 std::function<KUndo2Command*()> createCommandFunc)
66 : AggregateCommand(parent),
67 m_createCommandFunc(createCommandFunc)
68 {
69 }
70
83
85 : KUndo2Command(child ? child->text() : kundo2_noi18n("<bug: unnamed command>"), parent), m_firstRedo(true), m_child(child) {}
86
88 {
89 if (m_firstRedo) {
90 m_firstRedo = false;
91 } else {
92 if (m_child) {
93 m_child->redo();
94 }
96 }
97 }
98
100 {
102 if (m_child) {
103 m_child->undo();
104 }
105 }
106
108 : KUndo2Command(parent),
109 m_firstRedo(skipFirstRedo)
110 {
111 }
112
114 : KUndo2Command(text, parent),
115 m_firstRedo(skipFirstRedo)
116 {
117 }
118
120 {
121 if (m_firstRedo) {
122 m_firstRedo = false;
123 } else {
124 redoImpl();
126 }
127 }
128
130 {
132 undoImpl();
133 }
134
139
141 : KUndo2Command(parent)
142 {
144 }
145
147 : KUndo2Command(parent),
148 m_currentState(initialState)
149 {}
150
152 {
154 partA();
155 } else {
156 partB();
157 }
158
159 m_firstRedo = false;
160 }
161
163 {
165 partA();
166 } else {
167 partB();
168 }
169 }
170
173
176
180
182 if (cmd) {
183 m_commands << cmd;
184 }
185 }
186
189 Q_FOREACH (KUndo2Command *cmd, m_commands) {
190 cmd->redo();
191 }
192 }
193
195 for (auto it = m_commands.rbegin(); it != m_commands.rend(); ++it) {
196 (*it)->undo();
197 }
199 }
200
203 cmd = new KUndo2Command(kundo2_noi18n("failed"));
204 }
205
207 KIS_SAFE_ASSERT_RECOVER_NOOP(!parent || !parent->hasParent());
208
209 if (!parent) return cmd;
210
211 if (CompositeCommand *compositeParent = dynamic_cast<CompositeCommand*>(parent)) {
212 compositeParent->addCommand(cmd);
213 return parent;
214 }
215
216 CompositeCommand *newCompositeParent = new CompositeCommand();
217 newCompositeParent->setText(parent->text());
218 newCompositeParent->addCommand(parent);
219 newCompositeParent->addCommand(cmd);
220
221 return newCompositeParent;
222 }
223
224 void redoAndMergeIntoAccumulatingCommand(KUndo2Command *cmd, QScopedPointer<KUndo2Command> &accumulatingCommand)
225 {
226 cmd->redo();
227 if (accumulatingCommand) {
228 const bool isMerged = accumulatingCommand->mergeWith(cmd);
230 delete cmd;
231 } else {
232 accumulatingCommand.reset(cmd);
233 }
234 }
235
236
237}
float value(const T *src, size_t ch)
virtual void undo()
void setText(const KUndo2MagicString &text)
virtual void redo()
bool hasParent() const
void addCommand(KUndo2Command *cmd) override
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
KUndo2MagicString kundo2_noi18n(const QString &text)
void redoAndMergeIntoAccumulatingCommand(KUndo2Command *cmd, QScopedPointer< KUndo2Command > &accumulatingCommand)
KUndo2Command * composeCommands(KUndo2Command *parent, KUndo2Command *cmd)
The AggregateCommand struct is a command with delayed initialization. On first redo() populateChildCo...
AggregateCommand(KUndo2Command *parent=0)
virtual void populateChildCommands()=0
void addCommand(KUndo2Command *cmd)
QVector< KUndo2Command * > m_commands
CompositeCommand(KUndo2Command *parent=0)
FlipFlopCommand(State initialState, KUndo2Command *parent=0)
std::function< KUndo2Command *()> m_createCommandFunc
LambdaCommand(std::function< KUndo2Command *()> createCommandFunc)
SkipFirstRedoBase(bool skipFirstRedo, KUndo2Command *parent=0)
QScopedPointer< KUndo2Command > m_child
SkipFirstRedoWrapper(KUndo2Command *child=0, KUndo2Command *parent=0)