Krita Source Code Documentation
Loading...
Searching...
No Matches
KisAslStorage.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "KisAslStorage.h"
10
11#include <QFileInfo>
13
16}
17
19{
20public:
21
22 AslTagIterator(const QString &location, const QString &resourceType)
23 : m_location(location)
24 , m_resourceType(resourceType)
25 {}
26
27 bool hasNext() const override {return false;}
28 void next() override {}
29
30 KisTagSP tag() const override { return nullptr; }
31
32private:
33
34 QString m_location;
36
37};
38
40{
41
42private:
43
44 QString m_filename;
47 QHash<QString, KoPatternSP> m_patterns;
49 QScopedPointer<QHashIterator<QString, KoPatternSP>> m_patternsIterator;
50 QScopedPointer<QVectorIterator<KisPSDLayerStyleSP>> m_stylesIterator;
55
56public:
57
58 AslIterator(QSharedPointer<KisAslLayerStyleSerializer> aslSerializer, const QString& filename, const QString& resourceType)
59 : m_filename(filename)
60 , m_aslSerializer(aslSerializer)
61 , m_isLoaded(false)
62 , m_resourceType(resourceType)
63 {
64 }
65
66 bool hasNext() const override
67 {
69 if (!m_aslSerializer->isInitialized()) {
70 m_aslSerializer->readFromFile(m_filename);
71 }
72
73 const_cast<AslIterator*>(this)->m_isLoaded = true;
74 const_cast<AslIterator*>(this)->m_patterns = m_aslSerializer->patterns();
75 const_cast<AslIterator*>(this)->m_styles = m_aslSerializer->styles();
76
77 const_cast<AslIterator*>(this)->m_patternsIterator.reset(new QHashIterator<QString, KoPatternSP>(m_patterns));
78 const_cast<AslIterator*>(this)->m_stylesIterator.reset(new QVectorIterator<KisPSDLayerStyleSP>(m_styles));
79 }
80 if (!m_aslSerializer->isValid()) {
81 return false;
82 }
83
87 return m_stylesIterator->hasNext();
88 }
89 return false;
90 }
91 void next() override
92 {
94 if (m_patternsIterator->hasNext()) {
96 m_patternsIterator->next();
97 KoPatternSP currentPattern = m_patternsIterator->value();
98 m_currentResource = currentPattern;
99 KIS_ASSERT(currentPattern);
100 m_currentUuid = currentPattern->filename();
101 }
103 if (m_stylesIterator->hasNext()) {
105 KisPSDLayerStyleSP currentLayerStyle = m_stylesIterator->next();
106 m_currentResource = currentLayerStyle;
107 KIS_ASSERT(currentLayerStyle);
108 m_currentUuid = currentLayerStyle->filename();
109 }
110 }
111 }
112
113 QString url() const override
114 {
115 if (m_currentResource.isNull()) {
116 return QString();
117 }
118 return m_currentUuid;
119 }
120
121 QString type() const override
122 {
123 return m_currentResource.isNull() ? QString() : m_currentType;
124 }
125
126 QDateTime lastModified() const override {
127 QFileInfo fi(m_filename);
128 return fi.lastModified();
129 }
130
131
133 KoResourceSP resourceImpl() const override
134 {
135 return m_currentResource;
136 }
137};
138
139KisAslStorage::KisAslStorage(const QString &location)
140 : KisStoragePlugin(location)
141 , m_aslSerializer(new KisAslLayerStyleSerializer())
142{
143}
144
149
151{
153 item.url = url;
154 item.folder = location();
155 item.resourceType = url.contains("pattern") ? ResourceType::Patterns : ResourceType::LayerStyles;
156 item.lastModified = QFileInfo(location()).lastModified();
157 return item;
158}
159
161{
162 if (!m_aslSerializer->isInitialized()) {
163 m_aslSerializer->readFromFile(location());
164 }
165 int indexOfUnderscore = url.lastIndexOf("_");
166 QString realUuid = url;
167 if (indexOfUnderscore >= 0) {
168 realUuid.remove(indexOfUnderscore, url.length() - indexOfUnderscore); // remove _pattern or _style added in iterator
169 }
170 // TODO: RESOURCES: Since we do get a resource type at the beginning of the path now
171 // maybe we could skip adding the _[resourcetype] at the end of the path as well?
172 realUuid = QFileInfo(realUuid).baseName(); // remove patterns/ at the beginning, if there are any
173
174 if (url.contains("pattern") || url.contains(".pat")) {
175 QHash<QString, KoPatternSP> patterns = m_aslSerializer->patterns();
176
177 if (patterns.contains(realUuid)) {
178 return patterns[realUuid];
179 }
180 }
181 else {
182 QHash<QString, KisPSDLayerStyleSP> styles = m_aslSerializer->stylesHash();
183 if (styles.contains(realUuid)) {
184 return styles[realUuid];
185 } else {
186 // can be {realUuid} or {realUuid}
187 if (realUuid.startsWith("{")) {
188 realUuid = realUuid.right(realUuid.length() - 1);
189 }
190 if (realUuid.endsWith("}")) {
191 realUuid = realUuid.left(realUuid.length() - 1);
192 }
193
194 if (styles.contains(realUuid)) {
195 return styles[realUuid];
196 } else {
197 Q_FOREACH(QString ke, styles.keys()) {
198 }
199 }
200
201 }
202 }
203 return 0;
204}
205
207{
208 return false;
209}
210
212{
213 return false;
214}
215
220
225
226bool KisAslStorage::saveAsNewVersion(const QString &/*resourceType*/, KoResourceSP /*resource*/)
227{
228 // not implemented yet
229 warnKrita << "KisAslStorage::saveAsNewVersion is not implemented yet";
230 return false;
231}
232
233bool KisAslStorage::addResource(const QString &/*resourceType*/, KoResourceSP resource)
234{
235 if (!resource) {
236 warnKrita << "Trying to add a null resource to KisAslStorage";
237 return false;
238 }
239 KisPSDLayerStyleSP layerStyle = resource.dynamicCast<KisPSDLayerStyle>();
240 if (!layerStyle) {
241 warnKrita << "Trying to add a resource that is not a layer style to KisAslStorage";
242 return false;
243 }
244
246 styles << layerStyle;
247 m_aslSerializer->setStyles(styles);
248 return m_aslSerializer->saveToFile(location());
249}
250
252{
253 if (!m_aslSerializer->isInitialized()) {
254 m_aslSerializer->readFromFile(location());
255 }
256 return m_aslSerializer->isValid();
257}
KIS_DECLARE_STATIC_INITIALIZER
QVector< KisPSDLayerStyleSP > m_styles
QScopedPointer< QHashIterator< QString, KoPatternSP > > m_patternsIterator
QString m_currentType
QHash< QString, KoPatternSP > m_patterns
KoResourceSP resourceImpl() const override
This only loads the resource when called (but not in case of asl...)
QScopedPointer< QVectorIterator< KisPSDLayerStyleSP > > m_stylesIterator
QString type() const override
QString url() const override
AslIterator(QSharedPointer< KisAslLayerStyleSerializer > aslSerializer, const QString &filename, const QString &resourceType)
QSharedPointer< KisAslLayerStyleSerializer > m_aslSerializer
KoResourceSP m_currentResource
QString m_resourceType
QDateTime lastModified() const override
void next() override
The iterator is only valid if next() has been called at least once.
QString m_filename
QString m_currentUuid
bool hasNext() const override
void next() override
The iterator is only valid if next() has been called at least once.
AslTagIterator(const QString &location, const QString &resourceType)
bool hasNext() const override
KisTagSP tag() const override
A tag object on which we can set properties and which we can save.
QSharedPointer< KisResourceStorage::ResourceIterator > resources(const QString &resourceType) override
QSharedPointer< KisResourceStorage::TagIterator > tags(const QString &resourceType) override
virtual ~KisAslStorage()
bool addResource(const QString &resourceType, KoResourceSP resource) override
bool loadVersionedResource(KoResourceSP resource) override
KisResourceStorage::ResourceItem resourceItem(const QString &url) override
KisAslStorage(const QString &location)
bool saveAsNewVersion(const QString &resourceType, KoResourceSP resource) override
bool supportsVersioning() const override
QSharedPointer< KisAslLayerStyleSerializer > m_aslSerializer
KoResourceSP resource(const QString &url) override
bool isValid() const override
void addStoragePluginFactory(KisResourceStorage::StorageType storageType, KisStoragePluginFactoryBase *factory)
static KisStoragePluginRegistry * instance()
QString location() const
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
#define warnKrita
Definition kis_debug.h:87
const QString LayerStyles
const QString Patterns
The KisPSDLayerStyle class implements loading, saving and applying the PSD layer effects.
A resource item is simply an entry in the storage,.