Krita

TransformMask.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "TransformMask.h"
7#include <kis_transform_mask.h>
8#include <kis_image.h>
9#include <kis_transform_mask_params_interface.h>
10#include <QDomDocument>
11
12#include "Node.h"
13#include <kis_transform_mask_params_factory_registry.h>
14#include <commands_new/KisSimpleModifyTransformMaskCommand.h>
15#include "commands_new/KisLazyCreateTransformMaskKeyframesCommand.h"
16#include <kis_processing_applicator.h>
17
18
19#include "kis_transform_mask_params_interface.h"
20
21
22TransformMask::TransformMask(KisImageSP image, QString name, QObject *parent) :
23 Node(image, new KisTransformMask(image, name), parent)
24{
25
26}
27
28TransformMask::TransformMask(KisImageSP image, KisTransformMaskSP mask, QObject *parent):
29 Node(image, mask, parent)
30{
31
32}
33
34TransformMask::~TransformMask()
35{
36
37}
38
40{
41 return "transformmask";
42}
43
44QTransform TransformMask::finalAffineTransform() const
45{
46 QTransform affineTransformation;
47 KisTransformMask *mask = dynamic_cast<KisTransformMask*>(this->node().data());
48 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, QTransform());
49
50 affineTransformation = mask->transformParams()->finalAffineTransform();
51
52 return affineTransformation;
53}
54
56{
57 KisTransformMaskSP mask = qobject_cast<KisTransformMask*>(this->node().data());
58 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, QString());
59
60 QDomDocument doc("transform_params");
61 QDomElement root = doc.createElement("transform_params");
62 QDomElement main = doc.createElement("main");
63 QDomElement data = doc.createElement("data");
64
65 main.setAttribute("id", mask->transformParams()->id());
66
67 mask->transformParams()->toXML(&data);
68 doc.appendChild(root);
69 root.appendChild(main);
70 root.appendChild(data);
71
72 return doc.toString();
73}
74
76{
77 QDomDocument doc;
78 KisTransformMaskSP mask = qobject_cast<KisTransformMask*>(this->node().data());
79 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, false);
80
81 doc.setContent(xml);
82
83 QDomElement rootElement = doc.documentElement();
84 QDomElement main = rootElement.firstChildElement("main");
85 QDomElement data = rootElement.firstChildElement("data");
86
87 if (!main.isElement() || !main.hasAttribute("id") || !data.isElement()) {
88 return false;
89 }
90
91 QString id = main.attribute("id", "not-valid");
92
93 // backward compatibility
94 if (id == "animatedtransformparams") {
95 id = "tooltransformparams";
96 }
97
98 KisTransformMaskParamsInterfaceSP params = KisTransformMaskParamsFactoryRegistry::instance()->createParams(id, data);
99
100 if (!params) {
101 return false;
102 }
103
104 QScopedPointer<KUndo2Command> cmd(new KUndo2Command);
105
106 if (KisLazyCreateTransformMaskKeyframesCommand::maskHasAnimation(mask)) {
107 new KisLazyCreateTransformMaskKeyframesCommand(mask, cmd.data());
108 }
109
110 new KisSimpleModifyTransformMaskCommand(mask, params, {}, cmd.data());
111
112 KisProcessingApplicator::runSingleCommandStroke(this->node()->image(), cmd.take());
113
114 return true;
115}
Node represents a layer or mask in a Krita image's Node hierarchy.
Definition Node.h:24
bool fromXML(const QString &xml)
QString toXML() const
toXML
virtual QString type() const override
type Krita has several types of nodes, split in layers and masks.
QString name(StandardAction id)
QDomElement createElement(const QString &tagName)
QDomElement documentElement() const const
ParseResult setContent(QAnyStringView text, ParseOptions options)
QString toString(int indent) const const
QString attribute(const QString &name, const QString &defValue) const const
bool hasAttribute(const QString &name) const const
void setAttribute(const QString &name, const QString &value)
QDomNode appendChild(const QDomNode &newChild)
QDomElement firstChildElement(const QString &tagName, const QString &namespaceURI) const const
bool isElement() const const
T qobject_cast(QObject *object)
T * data() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.