Krita

Filter.cpp
1/*
2 * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "Filter.h"
7
8#include <KoCanvasResourceProvider.h>
9
10#include <kis_canvas_resource_provider.h>
11#include <kis_filter.h>
12#include <kis_properties_configuration.h>
13#include <kis_filter_configuration.h>
14#include <kis_filter_manager.h>
15#include <kis_filter_registry.h>
16#include <KisDocument.h>
17#include <kis_paint_device.h>
18#include <kis_paint_device_frames_interface.h>
19#include <KisPart.h>
20#include <KisView.h>
21
22#include <strokes/kis_filter_stroke_strategy.h>
23#include <krita_utils.h>
24#include <KisGlobalResourcesInterface.h>
25
26#include "Krita.h"
27#include "Document.h"
28#include "InfoObject.h"
29#include "Node.h"
30
31struct Filter::Private {
32 Private() {}
33 QString name;
34 InfoObject *configuration {0};
35};
36
38 : QObject(0)
39 , d(new Private)
40{
41}
42
43Filter::~Filter()
44{
45 delete d->configuration;
46 delete d;
47}
48
49bool Filter::operator==(const Filter &other) const
50{
51 return (d->name == other.d->name
52 && d->configuration == other.d->configuration);
53}
54
55bool Filter::operator!=(const Filter &other) const
56{
57 return !(operator==(other));
58}
59
60
62{
63 return d->name;
64}
65
66void Filter::setName(const QString &name)
67{
68 d->name = name;
69 delete d->configuration;
70
71 KisFilterSP filter = KisFilterRegistry::instance()->value(d->name);
72 d->configuration = new InfoObject(filter->defaultConfiguration(KisGlobalResourcesInterface::instance()));
73}
74
76{
77 return d->configuration;
78}
79
81{
82 d->configuration = value;
83}
84
85bool Filter::apply(Node *node, int x, int y, int w, int h)
86{
87 if (node->locked()) return false;
88
89 KisFilterSP filter = KisFilterRegistry::instance()->value(d->name);
90 if (!filter) return false;
91
92 KisPaintDeviceSP dev = node->paintDevice();
93 if (!dev) return false;
94
95 QRect applyRect = QRect(x, y, w, h);
96 KisFilterConfigurationSP config = static_cast<KisFilterConfiguration*>(d->configuration->configuration().data());
97 filter->process(dev, applyRect, config->cloneWithResourcesSnapshot());
98 return true;
99}
100
101bool Filter::startFilter(Node *node, int x, int y, int w, int h)
102{
103 if (node->locked()) return false;
104
105 KisFilterSP filter = KisFilterRegistry::instance()->value(d->name);
106 if (!filter) return false;
107
108 KisImageWSP image = node->image();
109 if (!image) return false;
110
111 KisFilterConfigurationSP filterConfig = static_cast<KisFilterConfiguration*>(d->configuration->configuration().data());
112
113 image->waitForDone();
114 QRect initialApplyRect = QRect(x, y, w, h);
115
116 QRect applyRect = initialApplyRect;
117
118 KisPaintDeviceSP paintDevice = node->paintDevice();
119 if (paintDevice && filter->needsTransparentPixels(filterConfig.data(), paintDevice->colorSpace())) {
120 applyRect |= image->bounds();
121 }
122
123 KisResourcesSnapshotSP resources = new KisResourcesSnapshot(image, node->node());
124
125 Document *document = Krita::instance()->activeDocument();
126 if (document && KisPart::instance()->viewCount(document->document()) > 0) {
127 Q_FOREACH (QPointer<KisView> view, KisPart::instance()->views()) {
128 if (view && view->document() == document->document()) {
129 resources = new KisResourcesSnapshot(image, node->node(), view->resourceProvider()->resourceManager());
130 break;
131 }
132 }
133 }
134 delete document;
135
136 KisStrokeId currentStrokeId = image->startStroke(new KisFilterStrokeStrategy(filter,
137 KisFilterConfigurationSP(filterConfig),
138 resources));
139
140 QRect processRect = filter->changedRect(applyRect, filterConfig.data(), 0);
141 processRect &= image->bounds();
142
143 image->addJob(currentStrokeId, new KisFilterStrokeStrategy::FilterJobData());
144
145
146 image->endStroke(currentStrokeId);
147 image->waitForDone();
148
149 return true;
150}
151
152KisFilterConfigurationSP Filter::filterConfig()
153{
154 KisFilterConfigurationSP config = KisFilterRegistry::instance()->get(d->name)->factoryConfiguration(KisGlobalResourcesInterface::instance());
155 Q_FOREACH(const QString property, d->configuration->properties().keys()) {
156 config->setProperty(property, d->configuration->property(property));
157 }
158 return config;
159}
The Document class encapsulates a Krita Document/Image.
Definition Document.h:37
Filter: represents a filter and its configuration.
Definition Filter.h:31
bool startFilter(Node *node, int x, int y, int w, int h)
startFilter starts the given filter on the given node.
Definition Filter.cpp:101
QString name() const
name the internal name of this filter.
Definition Filter.cpp:61
void setConfiguration(InfoObject *value)
setConfiguration set the configuration object for the filter
Definition Filter.cpp:80
InfoObject * configuration() const
Definition Filter.cpp:75
Filter()
Filter: create an empty filter object.
Definition Filter.cpp:37
void setName(const QString &name)
setName set the filter's name to the given name.
Definition Filter.cpp:66
bool apply(Node *node, int x, int y, int w, int h)
Apply the filter to the given node.
Definition Filter.cpp:85
InfoObject wrap a properties map.
Definition InfoObject.h:20
QVariant property(const QString &key)
return the value for the property identified by key, or None if there is no such key.
QMap< QString, QVariant > properties() const
Return all properties this InfoObject manages.
Document * activeDocument() const
Definition Krita.cpp:104
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:402
Node represents a layer or mask in a Krita image's Node hierarchy.
Definition Node.h:24
bool locked() const
locked checks whether the Node is locked.
Definition Node.cpp:411
QVariant property(const char *name) 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.