Krita

TransformMask.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <[email protected]>
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 <kis_processing_applicator.h>
16 
17 
18 #include "kis_transform_mask_params_interface.h"
19 
20 
21 TransformMask::TransformMask(KisImageSP image, QString name, QObject *parent) :
22  Node(image, new KisTransformMask(image, name), parent)
23 {
24 
25 }
26 
27 TransformMask::TransformMask(KisImageSP image, KisTransformMaskSP mask, QObject *parent):
28  Node(image, mask, parent)
29 {
30 
31 }
32 
33 TransformMask::~TransformMask()
34 {
35 
36 }
37 
39 {
40  return "transformmask";
41 }
42 
43 QTransform TransformMask::finalAffineTransform() const
44 {
45  QTransform affineTransformation;
46  KisTransformMask *mask = dynamic_cast<KisTransformMask*>(this->node().data());
47  KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, QTransform());
48 
49  affineTransformation = mask->transformParams()->finalAffineTransform();
50 
51  return affineTransformation;
52 }
53 
55 {
56  KisTransformMaskSP mask = qobject_cast<KisTransformMask*>(this->node().data());
57  KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, QString());
58 
59  QDomDocument doc("transform_params");
60  QDomElement root = doc.createElement("transform_params");
61  QDomElement main = doc.createElement("main");
62  QDomElement data = doc.createElement("data");
63 
64  main.setAttribute("id", mask->transformParams()->id());
65 
66  mask->transformParams()->toXML(&data);
67  doc.appendChild(root);
68  root.appendChild(main);
69  root.appendChild(data);
70 
71  return doc.toString();
72 }
73 
75 {
76  QDomDocument doc;
77  KisTransformMaskSP mask = qobject_cast<KisTransformMask*>(this->node().data());
78  KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, false);
79 
80  doc.setContent(xml);
81 
82  QDomElement rootElement = doc.documentElement();
83  QDomElement main = rootElement.firstChildElement("main");
84  QDomElement data = rootElement.firstChildElement("data");
85 
86  if (!main.isElement() || !main.hasAttribute("id") || !data.isElement()) {
87  return false;
88  }
89 
90  KisTransformMaskParamsInterfaceSP params = KisTransformMaskParamsFactoryRegistry::instance()->createParams(main.attribute("id"), data);
91 
92  if (!params) {
93  return false;
94  }
95 
96  KUndo2Command *cmd = new KUndo2Command();
97 
98  if (mask->isAnimated()) {
99  KisAnimatedTransformParamsInterface* animInterface = dynamic_cast<KisAnimatedTransformParamsInterface*>(mask->transformParams().data());
100  KIS_ASSERT(animInterface);
101  animInterface->initializeKeyframes(mask, params, cmd);
102  } else {
103  cmd = new KisSimpleModifyTransformMaskCommand(mask, mask->transformParams(), params);
104  }
105 
106  KisProcessingApplicator::runSingleCommandStroke(this->node()->image(), cmd);
107 
108  return true;
109 }
QString toString(int indent) const const
virtual QString type() const override
type Krita has several types of nodes, split in layers and masks.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
bool fromXML(const QString &xml)
QDomElement documentElement() const const
QDomElement firstChildElement(const QString &tagName) const const
bool hasAttribute(const QString &name) const const
Node represents a layer or mask in a Krita image's Node hierarchy.
Definition: Node.h:21
QDomNode appendChild(const QDomNode &newChild)
QString name(StandardShortcut id)
QString attribute(const QString &name, const QString &defValue) const const
bool isElement() const const
QString toXML() const
toXML
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:09:30 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.