File indexing completed on 2025-04-27 04:08:16

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "RecentImageImageProvider.h"
0008 #include <QFile>
0009 #include <QImage>
0010 
0011 #include <KoStore.h>
0012 #include <KisDocument.h>
0013 
0014 RecentImageImageProvider::RecentImageImageProvider()
0015     : QQuickImageProvider(QQuickImageProvider::Image)
0016 {
0017 }
0018 
0019 QImage RecentImageImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
0020 {
0021     int width = 38;
0022     int height = 38;
0023 
0024     if (size) {
0025         *size = QSize(width, height);
0026     }
0027 
0028     QSize sz(requestedSize.width() > 0 ? requestedSize.width() : width,
0029              requestedSize.height() > 0 ? requestedSize.height() : height);
0030 
0031     QFile f(id);
0032     QImage thumbnail;
0033 
0034     if (f.exists()) {
0035         if (f.fileName().endsWith(".kra", Qt::CaseInsensitive)) {
0036             // try to use any embedded thumbnail
0037             KoStore *store = KoStore::createStore(id, KoStore::Read);
0038             KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(store, QImage());
0039 
0040             QString thumbnailPath = QLatin1String("Thumbnails/thumbnail.png");
0041             QString previewPath = QLatin1String("preview.png");
0042             bool thumbnailExists = store->hasFile(thumbnailPath);
0043             bool previewExists = store->hasFile(previewPath);
0044             QString pathToUse = thumbnailExists ? thumbnailPath : (previewExists ? previewPath : "");
0045 
0046             if (!pathToUse.isEmpty() && store->open(pathToUse)) {
0047                 // Hooray! No long delay for the user...
0048                 const QByteArray thumbnailData = store->read(store->size());
0049 
0050                 if (thumbnail.loadFromData(thumbnailData) &&
0051                         (thumbnail.width() >= width || thumbnail.height() >= height)) {
0052                     thumbnail = thumbnail.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0053                 }
0054             }
0055             delete store;
0056 
0057         }
0058         else {
0059             QImage img(id);
0060             if (img.width() >= sz.width() || img.height() >= sz.height()) {
0061                 thumbnail = img.scaled(sz, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0062             }
0063         }
0064     }
0065     return thumbnail;
0066 }