Krita Source Code Documentation
Loading...
Searching...
No Matches
KisFileIconCreator Class Reference

The KisFileIconCreator class creates a thumbnail from a file on disk. More...

#include <KisFileIconCreator.h>

+ Inheritance diagram for KisFileIconCreator:

Public Member Functions

bool createFileIcon (QString path, QIcon &icon, qreal devicePixelRatioF, QSize iconSize) override
 createFileIcon creates an icon from the file on disk
 
- Public Member Functions inherited from KisAbstractFileIconCreator
 KisAbstractFileIconCreator ()
 
virtual ~KisAbstractFileIconCreator ()
 

Detailed Description

The KisFileIconCreator class creates a thumbnail from a file on disk.

On Welcome Page and possibly other places there might be a need to show the user a thumbnail of a file. This class tries to open a file and create a thumbnail out of it.

In theory creating the object is not needed, so if you, dear future reader, want to convert the function inside to a static one, go ahead.

Definition at line 25 of file KisFileIconCreator.h.

Member Function Documentation

◆ createFileIcon()

bool KisFileIconCreator::createFileIcon ( QString path,
QIcon & icon,
qreal devicePixelRatioF,
QSize iconSize )
overridevirtual

createFileIcon creates an icon from the file on disk

Parameters
pathpath to the file
iconcreated icon
devicePixelRatioFa result from devicePixelRatioF() called in a widget
iconSizesize of the icon
Returns
true if icon was created successfully, false if not (for example the file doesn't exist)

Implements KisAbstractFileIconCreator.

Definition at line 86 of file KisFileIconCreator.cpp.

87{
88 iconSize *= devicePixelRatioF;
89 QFileInfo fi(path);
90 if (fi.exists()) {
91 QString mimeType = KisMimeDatabase::mimeTypeForFile(path);
92 if (mimeType == KisDocument::nativeFormatMimeType()
93 || mimeType == "application/x-krita-archive"
94 || mimeType == "image/openraster") {
95
96 QScopedPointer<KoStore> store(KoStore::createStore(path, KoStore::Read));
97 if (store) {
98 QString thumbnailpath;
99 if (store->hasFile(QString("Thumbnails/thumbnail.png"))){
100 thumbnailpath = QString("Thumbnails/thumbnail.png");
101 }
102 else if (store->hasFile(QString("mergedimage.png"))) {
103 thumbnailpath = QString("mergedimage.png");
104 }
105 else if (store->hasFile(QString("preview.png"))) {
106 thumbnailpath = QString("preview.png");
107 }
108 if (!thumbnailpath.isEmpty() && store->open(thumbnailpath)) {
109
110 QByteArray bytes = store->read(store->size());
111 store->close();
112 QImage img;
113 img.loadFromData(bytes);
114
115 icon = createIcon(img, iconSize);
116 return true;
117
118 } else {
119 return false;
120 }
121 } else {
122 return false;
123 }
124 } else if (mimeType == "image/tiff" || mimeType == "image/x-tiff" || mimeType == "image/jxl") {
125 // Workaround for a bug in Qt tiff QImageIO plugin
126 // XXX: Also for JPEG-XL if KImageFormats haven't updated to accommodate libjxl >= v0.9.0 API changes.
127 QScopedPointer<KisDocument> doc(KisPart::instance()->createTemporaryDocument());
128 doc->setFileBatchMode(true);
129 bool r = doc->openPath(path, KisDocument::DontAddToRecent);
130 if (r) {
131 KisPaintDeviceSP projection = doc->image()->projection();
132 const QRect bounds = projection->exactBounds();
133 QSize imageSize = bounds.size();
134 if (imageSize.width() > iconSize.width() || imageSize.height() > iconSize.height()) {
135 imageSize.scale(iconSize, Qt::KeepAspectRatio);
136 }
137 const QImage &thumbnail = projection->createThumbnail(imageSize.width(), imageSize.height(), bounds);
138 icon = createIcon(thumbnail, iconSize);
139 return true;
140 } else {
141 return false;
142 }
143 } else {
144 QImage img;
145 img.load(path);
146 if (!img.isNull()) {
147 icon = createIcon(img, iconSize);
148 return true;
149 } else {
150 return false;
151 }
152 }
153 } else {
154 return false;
155 }
156}
int iconSize(qreal width, qreal height)
static QByteArray nativeFormatMimeType()
static QString mimeTypeForFile(const QString &file, bool checkExistingFiles=true)
Find the mimetype for the given filename. The filename must include a suffix.
QImage createThumbnail(qint32 maxw, qint32 maxh, QRect rect, qreal oversample=1, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags())
QRect exactBounds() const
static KisPart * instance()
Definition KisPart.cpp:131
@ Read
Definition KoStore.h:29
static KoStore * createStore(const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
Definition KoStore.cpp:39
#define bounds(x, a, b)

References bounds, KoStore::createStore(), KisPaintDevice::createThumbnail(), KisDocument::DontAddToRecent, KisPaintDevice::exactBounds(), iconSize(), KisPart::instance(), KisMimeDatabase::mimeTypeForFile(), KisDocument::nativeFormatMimeType(), and KoStore::Read.


The documentation for this class was generated from the following files: