Krita Source Code Documentation
Loading...
Searching...
No Matches
KisImportExportFilter.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2001 Werner Trobin <trobin@kde.org>
5 SPDX-FileCopyrightText: 2002 Werner Trobin <trobin@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
11
12#include <QFile>
13#include <QFileInfo>
14#include <kis_debug.h>
15#include <QStack>
19#include <KisExportCheckBase.h>
21#include "KoUpdater.h"
22#include <klocalizedstring.h>
23#include "kis_config.h"
24#include <KoStore.h>
25#include <KisDocument.h>
26
27const QString KisImportExportFilter::ImageContainsTransparencyTag = "ImageContainsTransparency";
28const QString KisImportExportFilter::ColorModelIDTag = "ColorModelID";
29const QString KisImportExportFilter::ColorDepthIDTag = "ColorDepthID";
30const QString KisImportExportFilter::sRGBTag = "sRGB";
31const QString KisImportExportFilter::CICPPrimariesTag = "CICPCompatiblePrimaries";
32const QString KisImportExportFilter::CICPTransferCharacteristicsTag = "CICPCompatibleTransferFunction";
33const QString KisImportExportFilter::HDRTag = "HDRSupported";
34
35class Q_DECL_HIDDEN KisImportExportFilter::Private
36{
37public:
39 QByteArray mime;
40 QString filename;
41 QString realFilename;
43 KisImportUserFeedbackInterface *importUserFeedBackInterface {nullptr};
44
45 QMap<QString, KisExportCheckBase*> capabilities;
46
48 : updater(0), mime("")
49 , batchmode(false)
50 {}
51
53 {
54 qDeleteAll(capabilities);
55 }
56
57};
58
59
61 : QObject(parent)
62 , d(new Private)
63{
64}
65
67{
68 if (d->updater) {
69 d->updater->setProgress(100);
70 }
71 delete d;
72}
73
75{
76 return d->filename;
77}
78
80{
81 return d->realFilename;
82}
83
85{
86 return d->batchmode;
87}
88
90{
91 return d->importUserFeedBackInterface;
92}
93
95{
96 d->batchmode = batchmode;
97}
98
100{
101 d->importUserFeedBackInterface = interface;
102}
103
104void KisImportExportFilter::setFilename(const QString &filename)
105{
106 d->filename = filename;
107}
108
109void KisImportExportFilter::setRealFilename(const QString &filename)
110{
111 d->realFilename = filename;
112}
113
114
115void KisImportExportFilter::setMimeType(const QString &mime)
116{
117 d->mime = mime.toLatin1();
118}
119
121{
122 return d->mime;
123}
124
125KisPropertiesConfigurationSP KisImportExportFilter::defaultConfiguration(const QByteArray &from, const QByteArray &to) const
126{
127 Q_UNUSED(from);
128 Q_UNUSED(to);
129 return 0;
130}
131
132KisPropertiesConfigurationSP KisImportExportFilter::lastSavedConfiguration(const QByteArray &from, const QByteArray &to) const
133{
135 const QString filterConfig = KisConfig(true).exportConfigurationXML(to);
136 if (cfg && !filterConfig.isEmpty()) {
137 cfg->fromXML(filterConfig, false);
138 }
139 return cfg;
140}
141
142KisConfigWidget *KisImportExportFilter::createConfigurationWidget(QWidget *, const QByteArray &from, const QByteArray &to) const
143{
144 Q_UNUSED(from);
145 Q_UNUSED(to);
146 return 0;
147}
148
149QMap<QString, KisExportCheckBase *> KisImportExportFilter::exportChecks()
150{
151 qDeleteAll(d->capabilities);
153 return d->capabilities;
154}
155
157{
158 return false;
159}
160
161QString KisImportExportFilter::verify(const QString &fileName) const
162{
163 QFileInfo fi(fileName);
164
165 if (!fi.exists()) {
166 return i18n("%1 does not exist after writing. Try saving again under a different name, in another location.", fileName);
167 }
168
169 if (!fi.isReadable()) {
170 return i18n("%1 is not readable", fileName);
171 }
172
173 if (fi.size() < 10) {
174 return i18n("%1 is smaller than 10 bytes, it must be corrupt. Try saving again under a different name, in another location.", fileName);
175 }
176
177 QFile f(fileName);
178 f.open(QFile::ReadOnly);
179 QByteArray ba = f.read(std::min(f.size(), (qint64)1000));
180 bool found = false;
181 for(int i = 0; i < ba.size(); ++i) {
182 if (ba.at(i) > 0) {
183 found = true;
184 break;
185 }
186 }
187
188 if (!found) {
189 return i18n("%1 has only zero bytes in the first 1000 bytes, it's probably corrupt. Try saving again under a different name, in another location.", fileName);
190 }
191
192 return QString();
193}
194
196{
197 d->updater = updater;
198}
199
201{
202 return d->updater;
203}
204
206{
207 if (d->updater) {
208 d->updater->setValue(value);
209 }
210}
211
213{
214 // XXX: Initialize everything to fully supported?
215}
216
218{
219 d->capabilities[capability->id()] = capability;
220}
221
222
223
224void KisImportExportFilter::addSupportedColorModels(QList<QPair<KoID, KoID> > supportedColorModels, const QString &name, KisExportCheckBase::Level level)
225{
226 Q_ASSERT(level != KisExportCheckBase::SUPPORTED);
227 QString layerMessage;
228 QString imageMessage;
230 Q_FOREACH(const KoID &colorModelID, allColorModels) {
232 Q_FOREACH(const KoID &colorDepthID, allColorDepths) {
233
234 KisExportCheckFactory *colorModelCheckFactory =
235 KisExportCheckRegistry::instance()->get("ColorModelCheck/" + colorModelID.id() + "/" + colorDepthID.id());
236 KisExportCheckFactory *colorModelPerLayerCheckFactory =
237 KisExportCheckRegistry::instance()->get("ColorModelPerLayerCheck/" + colorModelID.id() + "/" + colorDepthID.id());
238
239 if(!colorModelCheckFactory || !colorModelPerLayerCheckFactory) {
240 qWarning() << "No factory for" << colorModelID << colorDepthID;
241 continue;
242 }
243
244 if (supportedColorModels.contains(QPair<KoID, KoID>(colorModelID, colorDepthID))) {
245 addCapability(colorModelCheckFactory->create(KisExportCheckBase::SUPPORTED));
246 addCapability(colorModelPerLayerCheckFactory->create(KisExportCheckBase::SUPPORTED));
247 }
248 else {
249
250
251 if (level == KisExportCheckBase::PARTIALLY) {
252 imageMessage = i18nc("image conversion warning",
253 "%1 cannot save images with color model <b>%2</b> and depth <b>%3</b>. The image will be converted."
254 ,name, colorModelID.name(), colorDepthID.name());
255
256 layerMessage =
257 i18nc("image conversion warning",
258 "%1 cannot save layers with color model <b>%2</b> and depth <b>%3</b>. The layers will be converted or skipped."
259 ,name, colorModelID.name(), colorDepthID.name());
260 }
261 else {
262 imageMessage = i18nc("image conversion warning",
263 "%1 cannot save images with color model <b>%2</b> and depth <b>%3</b>. The image will not be saved."
264 ,name, colorModelID.name(), colorDepthID.name());
265
266 layerMessage =
267 i18nc("image conversion warning",
268 "%1 cannot save layers with color model <b>%2</b> and depth <b>%3</b>. The layers will be skipped."
269 , name, colorModelID.name(), colorDepthID.name());
270 }
271
272
273
274 addCapability(colorModelCheckFactory->create(level, imageMessage));
275 addCapability(colorModelPerLayerCheckFactory->create(level, layerMessage));
276 }
277 }
278 }
279}
280
281QString KisImportExportFilter::verifyZiPBasedFiles(const QString &fileName, const QStringList &filesToCheck) const
282{
283 QScopedPointer<KoStore> store(KoStore::createStore(fileName, KoStore::Read, KIS_MIME_TYPE, KoStore::Zip));
284
285 if (!store || store->bad()) {
286 return i18n("Could not open the saved file %1. Please try to save again in a different location.", fileName);
287 }
288
289 Q_FOREACH(const QString &file, filesToCheck) {
290 if (!store->hasFile(file)) {
291 return i18n("Component %1 is missing in %2. Please try to save again in a different location.", file, fileName);
292 }
293 }
294
295 return QString();
296
297}
float value(const T *src, size_t ch)
#define KIS_MIME_TYPE
Definition KisDocument.h:56
QString exportConfigurationXML(const QString &filterId, bool defaultValue=false) const
The KisExportCheckBase class defines the interface of the individual checks of an export filter's cap...
virtual QString id() const
virtual KisExportCheckBase * create(KisExportCheckBase::Level level, const QString &customWarning=QString())=0
static KisExportCheckRegistry * instance()
The base class for import and export filters.
void setImportUserFeedBackInterface(KisImportUserFeedbackInterface *interface)
virtual KisConfigWidget * createConfigurationWidget(QWidget *parent, const QByteArray &from="", const QByteArray &to="") const
createConfigurationWidget creates a widget that can be used to define the settings for a given import...
virtual KisPropertiesConfigurationSP defaultConfiguration(const QByteArray &from="", const QByteArray &to="") const
defaultConfiguration defines the default settings for the given import export filter
KisImportExportFilter(QObject *parent=0)
void setMimeType(const QString &mime)
void setUpdater(QPointer< KoUpdater > updater)
virtual QMap< QString, KisExportCheckBase * > exportChecks()
generate and return the list of capabilities of this export filter. The list
QPointer< KoUpdater > updater
KisPropertiesConfigurationSP lastSavedConfiguration(const QByteArray &from="", const QByteArray &to="") const
lastSavedConfiguration return the last saved configuration for this filter
static const QString ColorDepthIDTag
static const QString ColorModelIDTag
void setBatchMode(bool batchmode)
static const QString CICPTransferCharacteristicsTag
static const QString CICPPrimariesTag
KisImportUserFeedbackInterface * importUserFeedBackInterface
void addSupportedColorModels(QList< QPair< KoID, KoID > > supportedColorModels, const QString &name, KisExportCheckBase::Level level=KisExportCheckBase::PARTIALLY)
static const QString ImageContainsTransparencyTag
virtual QString verify(const QString &fileName) const
Verify whether the given file is correct and readable.
QMap< QString, KisExportCheckBase * > capabilities
static const QString sRGBTag
QString verifyZiPBasedFiles(const QString &fileName, const QStringList &filesToCheck) const
void setFilename(const QString &filename)
void setRealFilename(const QString &filename)
static const QString HDRTag
void addCapability(KisExportCheckBase *capability)
virtual bool exportSupportsGuides() const
exportSupportsGuides Because guides are in the document and not the image, checking for guides cannot...
T get(const QString &id) const
Definition KoID.h:30
QString name() const
Definition KoID.cpp:68
QString id() const
Definition KoID.cpp:63
@ Read
Definition KoStore.h:29
@ Zip
Definition KoStore.h:30
static KoStore * createStore(const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
Definition KoStore.cpp:39
QList< KoID > colorDepthList(const KoID &colorModelId, ColorSpaceListVisibility option) const
static KoColorSpaceRegistry * instance()
QList< KoID > colorModelsList(ColorSpaceListVisibility option) const
@ AllColorSpaces
All color space even those not visible to the user.