Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_import_catcher.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8#include <kis_debug.h>
9
10#include <klocalizedstring.h>
11#include <QFileInfo>
12
14
15#include "kis_node_manager.h"
16#include "kis_count_visitor.h"
17#include "KisViewManager.h"
18#include "KisDocument.h"
19#include "kis_image.h"
20#include "kis_layer.h"
21#include "kis_painter.h"
22#include "kis_selection.h"
24#include "kis_group_layer.h"
25#include "kis_progress_widget.h"
26#include "kis_config.h"
27#include "KisPart.h"
28#include "kis_shape_layer.h"
29
31{
32public:
35 QString path;
36 QString layerType;
38
39 QString prettyLayerName(QString layerName) const;
40 void importAsPaintLayer(KisPaintDeviceSP device, QString layerName);
41 void importShapeLayer(KisShapeLayerSP shapeLayer);
42};
43
44QString KisImportCatcher::Private::prettyLayerName(QString layerName) const
45{
46 QString name = QFileInfo(path).fileName();
47 QString fileName = !name.isEmpty() ? name : path;
48 // The "Background" name here being matched on is dependent on the current language and so needs to be passed
49 // through i18n.
50 return (layerName.isEmpty() || layerName == i18nc("Name for the bottom-most layer in the layerstack", "Background"))
51 ? fileName
52 : layerName;
53}
54
56{
57 KisLayerSP newLayer = new KisPaintLayer(view->image(),
58 layerName,
60 device);
61
62 KisNodeSP parent = nullptr;
63 KisLayerSP currentActiveLayer = view->activeLayer();
64
65 if (currentActiveLayer) {
66 parent = currentActiveLayer->parent();
67 }
68
69 if (parent.isNull()) {
70 parent = view->image()->rootLayer();
71 }
72
73 KisNodeCommandsAdapter adapter(view);
74 adapter.addNode(newLayer, parent, currentActiveLayer);
75}
76
78{
79 KisNodeSP parent = nullptr;
80 KisLayerSP currentActiveLayer = view->activeLayer();
81
82 if (currentActiveLayer) {
83 parent = currentActiveLayer->parent();
84 }
85
86 if (parent.isNull()) {
87 parent = view->image()->rootLayer();
88 }
89
90 KisNodeCommandsAdapter adapter(view);
91 adapter.addNode(shapeLayer, parent, currentActiveLayer);
92}
93
94KisImportCatcher::KisImportCatcher(const QString &path, KisViewManager *view, const QString &layerType)
95 : m_d(new Private)
96{
98 m_d->view = view;
99 m_d->path = path;
100 m_d->layerType = layerType;
102
103 connect(m_d->doc, SIGNAL(sigLoadingFinished()), this, SLOT(slotLoadingFinished()));
104 bool result = m_d->doc->openPath(path, KisDocument::DontAddToRecent);
105
106 if (!result) {
107 deleteMyself();
108 }
109}
110
112{
113 KisImageWSP importedImage = m_d->doc->image();
114 importedImage->waitForDone();
115
116 if (importedImage && importedImage->bounds().isValid()) {
117 if (m_d->layerType == "KisPaintLayer") {
118 QStringList list;
119 list << "KisLayer";
120 KoProperties props;
121
122 const bool multilayerImport = importedImage->rootLayer()->childCount() > 1;
123
128 KisNodeSP node = importedImage->rootLayer()->lastChild();
129 while (node) {
130 // we need to pass a copied device to make sure it is not reset
131 // on image's destruction
132 KisPaintDeviceSP dev = new KisPaintDevice(*node->projection());
134 m_d->importAsPaintLayer(dev, multilayerImport ? node->name() : m_d->prettyLayerName(node->name()));
136 node = node->prevSibling();
137 }
138 }
139 else if (m_d->layerType == "KisShapeLayer") {
141 m_d->view->image().data(),
142 m_d->prettyLayerName(QString()),
144 KisShapeLayerSP imported = dynamic_cast<KisShapeLayer*>(importedImage->rootLayer()->firstChild().data());
145
146 const QTransform thisInvertedTransform = shapeLayer->absoluteTransformation().inverted();
147
148 Q_FOREACH (KoShape *shape, imported->shapes()) {
149 KoShape *clonedShape = shape->cloneShape();
150 clonedShape->setTransformation(shape->absoluteTransformation() * thisInvertedTransform);
151 shapeLayer->addShape(clonedShape);
152 }
153 m_d->importShapeLayer(shapeLayer);
155 }
156 else {
157 KisPaintDeviceSP dev = new KisPaintDevice(*importedImage->projection());
158 m_d->view->nodeManager()->createNode(m_d->layerType, false, dev);
160 }
161 }
162
163 deleteMyself();
164}
165
170
172{
173 m_d->doc->deleteLater();
174 deleteLater();
175}
176
181
183{
184 KisConfig cfg(true);
185 if (cfg.convertToImageColorspaceOnImport() && *dev->colorSpace() != *image->colorSpace()) {
187 dev->convertTo(image->colorSpace());
188 }
189}
190
const quint8 OPACITY_OPAQUE_U8
bool convertToImageColorspaceOnImport(bool defaultValue=false) const
KisImageSP image
KisShapeController * shapeController
bool openPath(const QString &path, OpenFlags flags=None)
openPath Open a Path
void waitForDone()
KisGroupLayerSP rootLayer() const
const KoColorSpace * colorSpace() const
KisPaintDeviceSP projection() const
QRect bounds() const override
static void adaptClipToImageColorSpace(KisPaintDeviceSP dev, KisImageSP image)
KisImportCatcher(const QString &url, KisViewManager *view, const QString &layerType)
int numLayersImported() const
void addNode(KisNodeSP node, KisNodeSP parent, KisNodeSP aboveThis, KisImageLayerAddCommand::Flags flags=KisImageLayerAddCommand::DoRedoUpdates|KisImageLayerAddCommand::DoUndoUpdates)
KisNodeSP createNode(const QString &nodeType, bool quiet=false, KisPaintDeviceSP copyFrom=0)
const KoColorSpace * colorSpace() const
void convertTo(const KoColorSpace *dstColorSpace, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags(), KUndo2Command *parentCommand=nullptr, KoUpdater *progressUpdater=nullptr)
static KisPart * instance()
Definition KisPart.cpp:131
KisDocument * createDocument() const
Definition KisPart.cpp:230
KisDocument * document() const
KisNodeManager * nodeManager() const
The node manager handles everything about nodes.
KisImageWSP image() const
Return the image this view is displaying.
void addShape(KoShape *shape)
QTransform absoluteTransformation() const
Definition KoShape.cpp:330
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:369
virtual KoShape * cloneShape() const
creates a deep copy of the shape or shape's subtree
Definition KoShape.cpp:173
virtual KisPaintDeviceSP projection() const =0
QString name() const
QString prettyLayerName(QString layerName) const
void importShapeLayer(KisShapeLayerSP shapeLayer)
void importAsPaintLayer(KisPaintDeviceSP device, QString layerName)
KisNodeSP prevSibling() const
Definition kis_node.cpp:402
KisNodeSP firstChild() const
Definition kis_node.cpp:361
quint32 childCount() const
Definition kis_node.cpp:414
KisNodeWSP parent
Definition kis_node.cpp:86
KisNodeSP lastChild() const
Definition kis_node.cpp:367