Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_node_property_list_command.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <klocalizedstring.h>
8#include "kis_node.h"
9#include "kis_layer.h"
10#include "kis_image.h"
11#include "kis_selection_mask.h"
12#include "kis_paint_layer.h"
14#include "kis_undo_adapter.h"
16#include "kis_command_ids.h"
17
18#include <memory>
19
20// HACK! please refactor out!
23
24namespace {
25
26QSet<QString> changedProperties(const KisBaseNode::PropertyList &before,
27 const KisBaseNode::PropertyList &after)
28{
29 QSet<QString> changedIds;
30
31 auto valueForId = [] (const QString &id, const KisBaseNode::PropertyList &list) {
32 QVariant value;
33 Q_FOREACH (const KisBaseNode::Property &prop, list) {
34 if (prop.id == id) {
35 value = prop.state;
36 break;
37 }
38
39 }
40 return value;
41 };
42
45 const KisBaseNode::PropertyList &list1 = before.size() >= after.size() ? before : after;
46 const KisBaseNode::PropertyList &list2 = before.size() >= after.size() ? after : before;
47
48 Q_FOREACH (const KisBaseNode::Property &prop, list1) {
49 if (prop.state != valueForId(prop.id, list2)) {
50 changedIds.insert(prop.id);
51 }
52 }
53
54 return changedIds;
55}
56
57}
58
59
61 : KisNodeCommand(kundo2_i18n("Property Changes"), node),
62 m_newPropertyList(newPropertyList),
63 m_oldPropertyList(node->sectionModelProperties())
69{
70}
71
73{
75 const QSet<QString> changed = changedProperties(propsBefore, m_newPropertyList);
76 if (changed.isEmpty()) return;
77
78 const QRect oldExtent = m_node->projectionPlane()->tightUserVisibleBounds();
80
81 if (!propsWithNoUpdates().contains(changed)) {
82 doUpdate(propsBefore, m_node->sectionModelProperties(), oldExtent | m_node->projectionPlane()->tightUserVisibleBounds());
83 }
84}
85
87{
89 const QSet<QString> changed = changedProperties(propsBefore, m_oldPropertyList);
90 if (changed.isEmpty()) return;
91
92 const QRect oldExtent = m_node->projectionPlane()->tightUserVisibleBounds();
94
95 if (!propsWithNoUpdates().contains(changed)) {
96 doUpdate(propsBefore, m_node->sectionModelProperties(), oldExtent | m_node->projectionPlane()->tightUserVisibleBounds());
97 }
98}
99
104
106{
107 const KisNodePropertyListCommand *other =
108 dynamic_cast<const KisNodePropertyListCommand*>(command);
109
110 if (other && other->m_node == m_node &&
111 (changedProperties(m_oldPropertyList, m_newPropertyList).isEmpty() ||
112 changedProperties(m_oldPropertyList, m_newPropertyList) ==
113 changedProperties(other->m_oldPropertyList, other->m_newPropertyList))) {
114
115 const QSet<QString> changedInTheMeantime =
116 changedProperties(m_newPropertyList, other->m_oldPropertyList);
117
118 // stateless properties change without an undo command, so they may differ between two mergeable commands
119 bool onlyStatelessChanged = true;
120 Q_FOREACH (const QString &id, changedInTheMeantime) {
121 onlyStatelessChanged &= KisLayerPropertiesIcons::isStatelessProperty(id);
122 }
123 KIS_SAFE_ASSERT_RECOVER_NOOP(onlyStatelessChanged);
124
126 return true;
127 }
128
129 return false;
130}
131
133{
134 const KisNodePropertyListCommand *other =
135 dynamic_cast<const KisNodePropertyListCommand*>(command);
136
137 return other && other->m_node == m_node &&
138 (changedProperties(m_oldPropertyList, m_newPropertyList).isEmpty() ||
139 changedProperties(m_oldPropertyList, m_newPropertyList) ==
140 changedProperties(other->m_oldPropertyList, other->m_newPropertyList));
141}
142
144{
145 const KisNodePropertyListCommand *other =
146 dynamic_cast<const KisNodePropertyListCommand*>(command);
147
148 return other && other->m_node == m_node &&
149 changedProperties(m_oldPropertyList, other->m_newPropertyList).isEmpty();
150}
151
153 const KisBaseNode::PropertyList &newPropertyList)
154{
155 return changedProperties(oldPropertyList, newPropertyList).contains(KisLayerPropertiesIcons::onionSkins.id());
156}
157
158
160 const KisBaseNode::PropertyList &newPropertyList,
161 const QRect &totalUpdateExtent)
162{
167 if (oldPropertyList == newPropertyList) {
168 return;
169 }
170
171 bool oldPassThroughValue = false;
172 bool newPassThroughValue = false;
173
174 bool oldVisibilityValue = false;
175 bool newVisibilityValue = false;
176
177 Q_FOREACH (const KisBaseNode::Property &prop, oldPropertyList) {
179 oldPassThroughValue = prop.state.toBool();
180 }
181 if (prop.id == KisLayerPropertiesIcons::visible.id()) {
182 oldVisibilityValue = prop.state.toBool();
183 }
184 }
185
186 Q_FOREACH (const KisBaseNode::Property &prop, newPropertyList) {
188 newPassThroughValue = prop.state.toBool();
189 }
190 if (prop.id == KisLayerPropertiesIcons::visible.id()) {
191 newVisibilityValue = prop.state.toBool();
192 }
193 }
194
195 if (oldPassThroughValue && !newPassThroughValue) {
196 KisLayerSP layer(qobject_cast<KisLayer*>(m_node.data()));
197 KisImageSP image = layer->image().toStrongRef();
198 if (image) {
199 image->refreshGraphAsync(layer);
200 }
201 } else if ((m_node->parent() && !oldPassThroughValue && newPassThroughValue) ||
202 (oldPassThroughValue && newPassThroughValue &&
203 !oldVisibilityValue && newVisibilityValue)) {
204
205 KisLayerSP layer(qobject_cast<KisLayer*>(m_node->parent().data()));
206 KisImageSP image = layer->image().toStrongRef();
207 if (image) {
208 image->refreshGraphAsync(layer);
209 }
210 } else if (checkOnionSkinChanged(oldPropertyList, newPropertyList)) {
211 m_node->setDirtyDontResetAnimationCache(totalUpdateExtent);
212 } else {
213 m_node->setDirty(totalUpdateExtent); // TODO check if visibility was actually changed or not
214 }
215}
216
230
232{
233 const QSet<QString> properties = changedProperties(node->sectionModelProperties(), proplist);
234
235 const bool undo = !properties.isEmpty() &&
236 (properties.size() != 1 ||
238
239 std::unique_ptr<KUndo2Command> cmd(new KisNodePropertyListCommand(node, proplist));
240
241 if (undo) {
242 image->undoAdapter()->addCommand(cmd.release());
243 }
244 else {
256 struct SimpleLodResettingStroke : public KisSimpleStrokeStrategy {
257 SimpleLodResettingStroke(KUndo2Command *cmd)
258 : KisSimpleStrokeStrategy(QLatin1String("SimpleLodResettingStroke")),
259 m_cmd(cmd)
260 {
261 setClearsRedoOnStart(false);
262 this->enableJob(JOB_INIT, true);
263 }
264
265 void initStrokeCallback() override {
266 m_cmd->redo();
267 // NOTE: we don't Q_EMIT imageModified signal here because this
268 // branch is only taken for the stasis changes, that do not
269 // change actual image representation.
270 }
271
272 private:
273 QScopedPointer<KUndo2Command> m_cmd;
274 };
275
276 KisStrokeId strokeId = image->startStroke(new SimpleLodResettingStroke(cmd.release()));
277 image->endStroke(strokeId);
278 }
279
280}
float value(const T *src, size_t ch)
KisUndoAdapter * undoAdapter() const
void refreshGraphAsync(KisNodeSP root, const QVector< QRect > &rects, const QRect &cropRect, KisProjectionUpdateFlags flags=KisProjectionUpdateFlag::None) override
KisStrokeId startStroke(KisStrokeStrategy *strokeStrategy) override
void endStroke(KisStrokeId id) override
static bool isStatelessProperty(const QString &id)
the base command for commands altering a node
The command for changing the property list of a layer.
bool mergeWith(const KUndo2Command *command) override
KisBaseNode::PropertyList m_newPropertyList
KisBaseNode::PropertyList m_oldPropertyList
static void setNodePropertiesAutoUndo(KisNodeSP node, KisImageSP image, PropertyList proplist)
KisNodePropertyListCommand(KisNodeSP node, KisBaseNode::PropertyList newPropertyList)
bool canAnnihilateWith(const KUndo2Command *other) const override
bool canMergeWith(const KUndo2Command *command) const override
void doUpdate(const KisBaseNode::PropertyList &oldPropertyList, const KisBaseNode::PropertyList &newPropertyList, const QRect &totalUpdateExtent)
static const QSet< QString > & propsWithNoUpdates()
virtual void addCommand(KUndo2Command *cmd)=0
KisSharedPtr< T > toStrongRef() const
toStrongRef returns a KisSharedPtr which may be dereferenced.
QString id() const
Definition KoID.cpp:63
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
bool checkOnionSkinChanged(const KisBaseNode::PropertyList &oldPropertyList, const KisBaseNode::PropertyList &newPropertyList)
KUndo2MagicString kundo2_i18n(const char *text)
KisImageWSP image
virtual PropertyList sectionModelProperties() const
virtual void setSectionModelProperties(const PropertyList &properties)
void setDirtyDontResetAnimationCache()
Definition kis_node.cpp:599
virtual KisAbstractProjectionPlaneSP projectionPlane() const
Definition kis_node.cpp:240
KisNodeWSP parent
Definition kis_node.cpp:86
virtual void setDirty()
Definition kis_node.cpp:577