Krita Source Code Documentation
Loading...
Searching...
No Matches
KisInterstrokeDataTransactionWrapperFactory.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <kundo2command.h>
10
11#include "KisInterstrokeData.h"
13
14#include <kis_paint_device.h>
15#include <kis_pointer_utils.h>
16#include <kis_command_utils.h>
17
18
19namespace {
20struct BeginInterstrokeDataTransactionCommand : public KUndo2Command
21{
22 BeginInterstrokeDataTransactionCommand(KisPaintDeviceSP device, KisInterstrokeDataSP newData)
23 : m_device(device),
24 m_interstrokeData(newData)
25 {
26 }
27
28 void redo() override {
29 if (m_firstRedo) {
30 if (m_device->interstrokeData() != m_interstrokeData) {
31 m_dataSwapCommand.reset(m_device->createChangeInterstrokeDataCommand(m_interstrokeData));
32 m_dataSwapCommand->redo();
33 }
34
35 if (m_interstrokeData) {
36 m_interstrokeData->beginTransaction();
37 }
38
39 m_firstRedo = false;
40 } else if (m_dataSwapCommand) {
41 m_dataSwapCommand->redo();
42
43 }
44 }
45
46 void undo() override {
47 if (m_dataSwapCommand) {
48 m_dataSwapCommand->undo();
49 }
50 }
51
52private:
53 bool m_firstRedo {true};
54 KisPaintDeviceSP m_device;
55 KisInterstrokeDataSP m_interstrokeData;
56 QScopedPointer<KUndo2Command> m_dataSwapCommand;
57};
58
59struct EndInterstrokeDataTransactionCommand : public KUndo2Command
60{
61 EndInterstrokeDataTransactionCommand(KisPaintDeviceSP device)
62 : m_device(device)
63 {
64 }
65
66 void redo() override {
67 KisInterstrokeDataSP data = m_device->interstrokeData();
68
69 if (!m_transactionCommand && data) {
70 m_transactionCommand.reset(data->endTransaction());
71 }
72
73 if (m_transactionCommand) {
74 m_transactionCommand->redo();
75 }
76 }
77
78 void undo() override {
79 if (m_transactionCommand) {
80 m_transactionCommand->undo();
81 }
82 }
83
84private:
85 KisPaintDeviceSP m_device;
86 QScopedPointer<KUndo2Command> m_transactionCommand;
87};
88
89}
90
91
98
100 : m_d(new Private())
101{
102 m_d->factory.reset(factory);
103 m_d->supportsContinuedInterstrokeData = supportsContinuedInterstrokeData;
104}
105
109
111{
112 KisInterstrokeDataSP data = device->interstrokeData();
113 if (m_d->factory) {
114 if (!m_d->supportsContinuedInterstrokeData ||
115 !data ||
116 !data->isStillCompatible() ||
117 !m_d->factory->isCompatible(data.data())) {
118
119 data = toQShared(m_d->factory->create(device));
120 }
121 } else {
122 data.clear();
123 }
124
125 KUndo2Command *cmd = 0;
126
127 if (device->interstrokeData() || data) {
128 m_d->device = device;
129 cmd = new BeginInterstrokeDataTransactionCommand(device, data);
130 }
131
132 return cmd;
133}
134
136{
137 KUndo2Command *result = 0;
138
139 if (m_d->device) {
140 if (m_d->supportsContinuedInterstrokeData) {
141 result = new EndInterstrokeDataTransactionCommand(m_d->device);
142 } else {
145
146 composite->addCommand(new EndInterstrokeDataTransactionCommand(m_d->device));
147 composite->addCommand(new BeginInterstrokeDataTransactionCommand(m_d->device, 0));
148
149 result = composite;
150 }
151 }
152
153 return result;
154}
KUndo2Command * createBeginTransactionCommand(KisPaintDeviceSP device) override
KisInterstrokeDataTransactionWrapperFactory(KisInterstrokeDataFactory *factory, bool supportsContinuedInterstrokeData=true)
KisInterstrokeDataSP interstrokeData() const
QSharedPointer< T > toQShared(T *ptr)
QAction * redo(const QObject *recvr, const char *slot, QObject *parent)
QAction * undo(const QObject *recvr, const char *slot, QObject *parent)