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 Q_FOREACH(KisNodeSP node, importedImage->rootLayer()->childNodes(list, props)) {
123 // we need to pass a copied device to make sure it is not reset
124 // on image's destruction
125 KisPaintDeviceSP dev = new KisPaintDevice(*node->projection());
129 }
130 }
131 else if (m_d->layerType == "KisShapeLayer") {
133 m_d->view->image().data(),
134 m_d->prettyLayerName(QString()),
136 KisShapeLayerSP imported = dynamic_cast<KisShapeLayer*>(importedImage->rootLayer()->firstChild().data());
137
138 const QTransform thisInvertedTransform = shapeLayer->absoluteTransformation().inverted();
139
140 Q_FOREACH (KoShape *shape, imported->shapes()) {
141 KoShape *clonedShape = shape->cloneShape();
142 clonedShape->setTransformation(shape->absoluteTransformation() * thisInvertedTransform);
143 shapeLayer->addShape(clonedShape);
144 }
145 m_d->importShapeLayer(shapeLayer);
147 }
148 else {
149 KisPaintDeviceSP dev = new KisPaintDevice(*importedImage->projection());
150 m_d->view->nodeManager()->createNode(m_d->layerType, false, dev);
152 }
153 }
154
155 deleteMyself();
156}
157
162
164{
165 m_d->doc->deleteLater();
166 deleteLater();
167}
168
173
175{
176 KisConfig cfg(true);
177 if (cfg.convertToImageColorspaceOnImport() && *dev->colorSpace() != *image->colorSpace()) {
179 dev->convertTo(image->colorSpace());
180 }
181}
182
const quint8 OPACITY_OPAQUE_U8
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
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:382
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:417
virtual KoShape * cloneShape() const
creates a deep copy of the shape or shape's subtree
Definition KoShape.cpp:200
virtual KisPaintDeviceSP projection() const =0
QString name() const
QString prettyLayerName(QString layerName) const
void importShapeLayer(KisShapeLayerSP shapeLayer)
void importAsPaintLayer(KisPaintDeviceSP device, QString layerName)
KisNodeSP firstChild() const
Definition kis_node.cpp:361
QList< KisNodeSP > childNodes(const QStringList &nodeTypes, const KoProperties &properties) const
Definition kis_node.cpp:439
KisNodeWSP parent
Definition kis_node.cpp:86