Fix thumbnail memory leak
This commit is contained in:
parent
3fd826ec4a
commit
411f013de6
9 changed files with 258 additions and 294 deletions
|
@ -35,11 +35,12 @@ add_executable(cutefish-filemanager
|
||||||
desktop/desktopsettings.cpp
|
desktop/desktopsettings.cpp
|
||||||
|
|
||||||
helper/datehelper.cpp
|
helper/datehelper.cpp
|
||||||
helper/thumbnailer.cpp
|
|
||||||
helper/pathhistory.cpp
|
helper/pathhistory.cpp
|
||||||
helper/fm.cpp
|
helper/fm.cpp
|
||||||
helper/shortcut.cpp
|
helper/shortcut.cpp
|
||||||
helper/thumbnailerjob.cpp
|
|
||||||
|
thumbnailer/thumbnailprovider.cpp
|
||||||
|
thumbnailer/thumbnailcache.cpp
|
||||||
|
|
||||||
desktopiconprovider.cpp
|
desktopiconprovider.cpp
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "desktopview.h"
|
#include "desktopview.h"
|
||||||
#include "helper/thumbnailer.h"
|
#include "thumbnailer/thumbnailprovider.h"
|
||||||
|
|
||||||
#include <QQmlEngine>
|
#include <QQmlEngine>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
|
@ -39,7 +39,7 @@ DesktopView::DesktopView(QQuickView *parent)
|
||||||
KWindowSystem::setState(winId(), NET::KeepBelow);
|
KWindowSystem::setState(winId(), NET::KeepBelow);
|
||||||
|
|
||||||
engine()->rootContext()->setContextProperty("desktopView", this);
|
engine()->rootContext()->setContextProperty("desktopView", this);
|
||||||
engine()->addImageProvider("thumbnailer", new Thumbnailer());
|
engine()->addImageProvider("thumbnailer", new ThumbnailProvider());
|
||||||
|
|
||||||
setTitle(tr("Desktop"));
|
setTitle(tr("Desktop"));
|
||||||
setScreen(qApp->primaryScreen());
|
setScreen(qApp->primaryScreen());
|
||||||
|
|
|
@ -1,73 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 CutefishOS Team.
|
|
||||||
*
|
|
||||||
* Author: revenmartin <revenmartin@gmail.com>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "thumbnailer.h"
|
|
||||||
|
|
||||||
#include <KIO/PreviewJob>
|
|
||||||
|
|
||||||
#include "thumbnailerjob.h"
|
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QImage>
|
|
||||||
|
|
||||||
QQuickImageResponse *Thumbnailer::requestImageResponse(const QString &id, const QSize &requestedSize)
|
|
||||||
{
|
|
||||||
AsyncImageResponse *response = new AsyncImageResponse(id, requestedSize);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncImageResponse::AsyncImageResponse(const QString &id, const QSize &requestedSize)
|
|
||||||
: m_id(id)
|
|
||||||
, m_requestedSize(requestedSize)
|
|
||||||
{
|
|
||||||
auto job_ = new ThumbnailerJob(QUrl::fromUserInput(id).toLocalFile(), requestedSize);
|
|
||||||
connect(job_, &ThumbnailerJob::gotPreview, [this] (QPixmap pixmap) {
|
|
||||||
m_image = pixmap.toImage();
|
|
||||||
emit this->finished();
|
|
||||||
});
|
|
||||||
|
|
||||||
connect(job_, &ThumbnailerJob::failed, [this] {
|
|
||||||
emit this->cancel();
|
|
||||||
emit this->finished();
|
|
||||||
});
|
|
||||||
|
|
||||||
job_->start();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// QStringList plugins = KIO::PreviewJob::defaultPlugins();
|
|
||||||
// auto job = new KIO::PreviewJob(KFileItemList() << KFileItem(QUrl::fromUserInput(id)), requestedSize, &plugins);
|
|
||||||
|
|
||||||
// connect(job, &KIO::PreviewJob::gotPreview, [this](KFileItem, QPixmap pixmap) {
|
|
||||||
// m_image = pixmap.toImage();
|
|
||||||
// emit this->finished();
|
|
||||||
// });
|
|
||||||
|
|
||||||
// connect(job, &KIO::PreviewJob::failed, [this](KFileItem) {
|
|
||||||
// emit this->cancel();
|
|
||||||
// emit this->finished();
|
|
||||||
// });
|
|
||||||
|
|
||||||
// job->start();
|
|
||||||
}
|
|
||||||
|
|
||||||
QQuickTextureFactory *AsyncImageResponse::textureFactory() const
|
|
||||||
{
|
|
||||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
|
||||||
}
|
|
|
@ -1,170 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2021 CutefishOS Team.
|
|
||||||
*
|
|
||||||
* Author: Reion Wong <reionwong@gmail.com>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "thumbnailerjob.h"
|
|
||||||
#include <QStandardPaths>
|
|
||||||
#include <QCryptographicHash>
|
|
||||||
#include <QFile>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QImage>
|
|
||||||
#include <QPixmap>
|
|
||||||
#include <QSaveFile>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QImageReader>
|
|
||||||
|
|
||||||
#include <sys/ipc.h>
|
|
||||||
#include <sys/shm.h>
|
|
||||||
|
|
||||||
ThumbnailerJob::ThumbnailerJob(const QString &fileName, const QSize &size, QObject *parent)
|
|
||||||
: QThread(parent)
|
|
||||||
, m_url(QUrl::fromUserInput(fileName))
|
|
||||||
, m_size(size)
|
|
||||||
, shmaddr(nullptr)
|
|
||||||
, shmid(-1)
|
|
||||||
{
|
|
||||||
// http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html#DIRECTORY
|
|
||||||
m_thumbnailsDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/thumbnails/");
|
|
||||||
}
|
|
||||||
|
|
||||||
ThumbnailerJob::~ThumbnailerJob()
|
|
||||||
{
|
|
||||||
if (shmaddr) {
|
|
||||||
shmdt((char *)shmaddr);
|
|
||||||
shmctl(shmid, IPC_RMID, nullptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ThumbnailerJob::run()
|
|
||||||
{
|
|
||||||
if (!QFile::exists(m_url.toLocalFile())) {
|
|
||||||
emit failed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int width = m_size.width();
|
|
||||||
int height = m_size.height();
|
|
||||||
int cacheSize = 0;
|
|
||||||
bool needCache = false;
|
|
||||||
|
|
||||||
// 首先需要找到目录
|
|
||||||
if (width <= 128 && height <= 128)
|
|
||||||
cacheSize = 128;
|
|
||||||
else if (width <= 256 && height <= 256)
|
|
||||||
cacheSize = 256;
|
|
||||||
else
|
|
||||||
cacheSize = 512;
|
|
||||||
|
|
||||||
struct CachePool {
|
|
||||||
QString path;
|
|
||||||
int minSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
const static auto pools = {
|
|
||||||
CachePool{QStringLiteral("/normal/"), 128},
|
|
||||||
CachePool{QStringLiteral("/large/"), 256},
|
|
||||||
CachePool{QStringLiteral("/x-large/"), 512},
|
|
||||||
CachePool{QStringLiteral("/xx-large/"), 1024},
|
|
||||||
};
|
|
||||||
|
|
||||||
QString thumbDir;
|
|
||||||
int wants = /*devicePixelRatio **/ cacheSize;
|
|
||||||
for (const auto &p : pools) {
|
|
||||||
if (p.minSize < wants) {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
thumbDir = p.path;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_thumbnailsPath = m_thumbnailsDir + thumbDir;
|
|
||||||
|
|
||||||
// 不存在需要创建路径
|
|
||||||
if (!QDir(m_thumbnailsPath).exists()) {
|
|
||||||
if (QDir().mkpath(m_thumbnailsPath)) {
|
|
||||||
QFile f(m_thumbnailsPath);
|
|
||||||
f.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser); // 0700
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 求出文件的 md5
|
|
||||||
QByteArray origName;
|
|
||||||
const QFileInfo info(m_url.toLocalFile());
|
|
||||||
const QString canonicalPath = info.canonicalFilePath();
|
|
||||||
origName = QUrl::fromLocalFile(canonicalPath).toEncoded(QUrl::RemovePassword | QUrl::FullyEncoded);
|
|
||||||
|
|
||||||
if (origName.isEmpty()) {
|
|
||||||
emit failed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QCryptographicHash md5(QCryptographicHash::Md5);
|
|
||||||
md5.addData(origName);
|
|
||||||
// Image size
|
|
||||||
md5.addData(QString::number(m_size.width()).toStdString().c_str());
|
|
||||||
md5.addData(QString::number(m_size.height()).toStdString().c_str());
|
|
||||||
// Time
|
|
||||||
md5.addData(QString::number(info.lastModified().toTime_t()).toStdString().c_str());
|
|
||||||
|
|
||||||
m_thumbnailsName = QString::fromLatin1(md5.result().toHex()) + QLatin1String(".png");
|
|
||||||
|
|
||||||
// 是否需要生成缓存
|
|
||||||
needCache = !QFile::exists(m_thumbnailsPath + m_thumbnailsName);
|
|
||||||
|
|
||||||
if (needCache) {
|
|
||||||
QFile f(m_url.toLocalFile());
|
|
||||||
if (f.open(QIODevice::ReadOnly)) {
|
|
||||||
QByteArray data = f.readAll();
|
|
||||||
QImage thumb;
|
|
||||||
thumb.loadFromData(data);
|
|
||||||
thumb = thumb.scaled(m_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
||||||
|
|
||||||
// 保存 cache 文件
|
|
||||||
QSaveFile saveFile(m_thumbnailsPath + m_thumbnailsName);
|
|
||||||
if (saveFile.open(QIODevice::WriteOnly)) {
|
|
||||||
if (thumb.save(&saveFile, "PNG")) {
|
|
||||||
saveFile.commit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
emitPreview(thumb);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QFile f(m_thumbnailsPath + m_thumbnailsName);
|
|
||||||
if (f.open(QIODevice::ReadOnly)) {
|
|
||||||
QByteArray data = f.readAll();
|
|
||||||
QImage thumb;
|
|
||||||
thumb.loadFromData(data);
|
|
||||||
emitPreview(thumb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ThumbnailerJob::emitPreview(const QImage &image)
|
|
||||||
{
|
|
||||||
QPixmap pixmap;
|
|
||||||
|
|
||||||
if (image.width() > m_size.width() || image.height() > m_size.height()) {
|
|
||||||
pixmap = QPixmap::fromImage(image.scaled(m_size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|
||||||
} else {
|
|
||||||
pixmap = QPixmap::fromImage(image);
|
|
||||||
}
|
|
||||||
|
|
||||||
emit gotPreview(pixmap);
|
|
||||||
}
|
|
7
main.cpp
7
main.cpp
|
@ -33,11 +33,12 @@
|
||||||
#include "widgets/itemviewadapter.h"
|
#include "widgets/itemviewadapter.h"
|
||||||
#include "desktop/desktopsettings.h"
|
#include "desktop/desktopsettings.h"
|
||||||
#include "desktop/desktopview.h"
|
#include "desktop/desktopview.h"
|
||||||
#include "helper/thumbnailer.h"
|
|
||||||
#include "helper/datehelper.h"
|
#include "helper/datehelper.h"
|
||||||
#include "helper/fm.h"
|
#include "helper/fm.h"
|
||||||
#include "helper/shortcut.h"
|
#include "helper/shortcut.h"
|
||||||
|
|
||||||
|
#include "thumbnailer/thumbnailprovider.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||||
|
@ -68,7 +69,7 @@ int main(int argc, char *argv[])
|
||||||
qmlRegisterType<DesktopSettings>(uri, 1, 0, "DesktopSettings");
|
qmlRegisterType<DesktopSettings>(uri, 1, 0, "DesktopSettings");
|
||||||
qmlRegisterType<Fm>(uri, 1, 0, "Fm");
|
qmlRegisterType<Fm>(uri, 1, 0, "Fm");
|
||||||
qmlRegisterType<ShortCut>(uri, 1, 0, "ShortCut");
|
qmlRegisterType<ShortCut>(uri, 1, 0, "ShortCut");
|
||||||
// qmlRegisterAnonymousType<QAction>(uri, 1);
|
qmlRegisterAnonymousType<QAction>(uri, 1);
|
||||||
|
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
parser.setApplicationDescription(QStringLiteral("File Manager"));
|
parser.setApplicationDescription(QStringLiteral("File Manager"));
|
||||||
|
@ -121,7 +122,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.load(url);
|
engine.load(url);
|
||||||
engine.addImageProvider("thumbnailer", new Thumbnailer());
|
engine.addImageProvider("thumbnailer", new ThumbnailProvider());
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
186
thumbnailer/thumbnailcache.cpp
Normal file
186
thumbnailer/thumbnailcache.cpp
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2021 CutefishOS Team.
|
||||||
|
*
|
||||||
|
* Author: Reion Wong <reionwong@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "thumbnailcache.h"
|
||||||
|
#include <QCryptographicHash>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
#include <QImageReader>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QSize>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
|
static ThumbnailCache *SELF = nullptr;
|
||||||
|
|
||||||
|
QImage scaleImage(const QImage &image, const QSize &requestedSize, bool crop, Qt::TransformationMode mode)
|
||||||
|
{
|
||||||
|
const QImage scaledImage(image.size() != requestedSize
|
||||||
|
? image.scaled(requestedSize, crop ? Qt::KeepAspectRatioByExpanding : Qt::KeepAspectRatio, mode)
|
||||||
|
: image);
|
||||||
|
|
||||||
|
if (crop && scaledImage.size() != requestedSize) {
|
||||||
|
QRect cropRect(0, 0, requestedSize.width(), requestedSize.height());
|
||||||
|
cropRect.moveCenter(QPoint(scaledImage.width() / 2, scaledImage.height() / 2));
|
||||||
|
|
||||||
|
return scaledImage.copy(cropRect);
|
||||||
|
} else {
|
||||||
|
return scaledImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ThumbnailCache *ThumbnailCache::self()
|
||||||
|
{
|
||||||
|
if (!SELF) {
|
||||||
|
SELF = new ThumbnailCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SELF;
|
||||||
|
}
|
||||||
|
|
||||||
|
ThumbnailCache::ThumbnailCache(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{
|
||||||
|
m_thumbnailsDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/thumbnails/");
|
||||||
|
|
||||||
|
QDir dir(m_thumbnailsDir);
|
||||||
|
if (!dir.exists())
|
||||||
|
dir.mkdir(m_thumbnailsDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ThumbnailCache::requestThumbnail(const QString &filePath, const QSize &requestedSize)
|
||||||
|
{
|
||||||
|
QString path(filePath);
|
||||||
|
if (path.startsWith("file://")) {
|
||||||
|
path = path.mid(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!QFile::exists(path)) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = requestedSize.width();
|
||||||
|
int height = requestedSize.height();
|
||||||
|
int cacheSize = 0;
|
||||||
|
|
||||||
|
// 首先需要找到目录
|
||||||
|
if (width <= 128 && height <= 128)
|
||||||
|
cacheSize = 128;
|
||||||
|
else if (width <= 256 && height <= 256)
|
||||||
|
cacheSize = 256;
|
||||||
|
else
|
||||||
|
cacheSize = 512;
|
||||||
|
|
||||||
|
struct CachePool {
|
||||||
|
QString path;
|
||||||
|
int minSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
const static auto pools = {
|
||||||
|
CachePool{ QStringLiteral("/normal/"), 128 },
|
||||||
|
CachePool{ QStringLiteral("/large/"), 256 },
|
||||||
|
CachePool{ QStringLiteral("/x-large/"), 512 },
|
||||||
|
CachePool{ QStringLiteral("/xx-large/"), 1024 },
|
||||||
|
};
|
||||||
|
|
||||||
|
QString thumbDir;
|
||||||
|
int wants = /*devicePixelRatio **/ cacheSize;
|
||||||
|
for (const auto &p : pools) {
|
||||||
|
if (p.minSize < wants) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
thumbDir = p.path;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尺寸文件夹
|
||||||
|
QString sizeDir = m_thumbnailsDir + thumbDir;
|
||||||
|
|
||||||
|
// 不存在需要创建路径
|
||||||
|
if (!QDir(sizeDir).exists()) {
|
||||||
|
if (QDir().mkpath(sizeDir)) {
|
||||||
|
QFile f(sizeDir);
|
||||||
|
f.setPermissions(QFile::ReadUser | QFile::WriteUser | QFile::ExeUser); // 0700
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Md5
|
||||||
|
QByteArray origName;
|
||||||
|
const QFileInfo info(path);
|
||||||
|
const QString canonicalPath = info.canonicalFilePath();
|
||||||
|
origName = QUrl::fromLocalFile(canonicalPath).toEncoded(QUrl::RemovePassword | QUrl::FullyEncoded);
|
||||||
|
|
||||||
|
if (origName.isEmpty()) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QCryptographicHash md5(QCryptographicHash::Md5);
|
||||||
|
md5.addData(origName);
|
||||||
|
// Image size
|
||||||
|
md5.addData(QString::number(width).toStdString().c_str());
|
||||||
|
md5.addData(QString::number(height).toStdString().c_str());
|
||||||
|
// Time
|
||||||
|
md5.addData(QString::number(info.lastModified().toTime_t()).toStdString().c_str());
|
||||||
|
|
||||||
|
QString thumbnailsName = QString::fromLatin1(md5.result().toHex()) + QLatin1String(".png");
|
||||||
|
QString thumbnailsPath = m_thumbnailsDir + thumbnailsName;
|
||||||
|
|
||||||
|
if (!QFile::exists(thumbnailsPath)) {
|
||||||
|
return generateThumbnail(path, thumbnailsPath, requestedSize);
|
||||||
|
} else {
|
||||||
|
return thumbnailsPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ThumbnailCache::generateThumbnail(const QString &source, const QString &target, const QSize &requestedSize)
|
||||||
|
{
|
||||||
|
QImageReader reader(source);
|
||||||
|
if (reader.canRead()) {
|
||||||
|
// Quality in the jpeg reader is binary. >= 50: high quality, < 50 fast
|
||||||
|
reader.setQuality(49);
|
||||||
|
|
||||||
|
reader.setScaledSize(reader.size().scaled(requestedSize.width(),
|
||||||
|
requestedSize.height(), Qt::KeepAspectRatio));
|
||||||
|
reader.setAutoTransform(true);
|
||||||
|
|
||||||
|
QImage image(reader.read());
|
||||||
|
return writeCacheFile(target, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ThumbnailCache::writeCacheFile(const QString &path, const QImage &image)
|
||||||
|
{
|
||||||
|
const QString thumbnailPath(path);
|
||||||
|
QFile thumbnailFile(path);
|
||||||
|
|
||||||
|
if (!thumbnailFile.open(QIODevice::WriteOnly)) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
image.save(&thumbnailFile, image.hasAlphaChannel() ? "PNG" : "JPG");
|
||||||
|
thumbnailFile.flush();
|
||||||
|
thumbnailFile.close();
|
||||||
|
|
||||||
|
return thumbnailPath;
|
||||||
|
}
|
|
@ -17,43 +17,27 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef THUMBNAILERJOB_H
|
#ifndef THUMBNAILCACHE_H
|
||||||
#define THUMBNAILERJOB_H
|
#define THUMBNAILCACHE_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QThread>
|
|
||||||
#include <QSize>
|
|
||||||
#include <QUrl>
|
|
||||||
|
|
||||||
class ThumbnailerJob : public QThread
|
class QImageReader;
|
||||||
|
class ThumbnailCache : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ThumbnailerJob(const QString &fileName, const QSize &size, QObject *parent = nullptr);
|
static ThumbnailCache *self();
|
||||||
~ThumbnailerJob();
|
explicit ThumbnailCache(QObject *parent = nullptr);
|
||||||
|
|
||||||
void run() override;
|
QString requestThumbnail(const QString &filePath, const QSize &requestedSize);
|
||||||
|
QString generateThumbnail(const QString &source, const QString &target, const QSize &requestedSize);
|
||||||
signals:
|
QString writeCacheFile(const QString &path, const QImage &image);
|
||||||
void gotPreview(const QPixmap &pixmap);
|
|
||||||
void failed();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void emitPreview(const QImage &image);
|
|
||||||
|
|
||||||
private:
|
|
||||||
QUrl m_url;
|
|
||||||
QSize m_size;
|
|
||||||
|
|
||||||
// thumbnail cache folder
|
// thumbnail cache folder
|
||||||
QString m_thumbnailsDir;
|
QString m_thumbnailsDir;
|
||||||
QString m_thumbnailsPath;
|
|
||||||
QString m_thumbnailsName;
|
|
||||||
|
|
||||||
uchar *shmaddr;
|
|
||||||
size_t shmsize;
|
|
||||||
int shmid;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // THUMBNAILERJOB_H
|
#endif // THUMBNAILCACHE_H
|
43
thumbnailer/thumbnailprovider.cpp
Normal file
43
thumbnailer/thumbnailprovider.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2021 CutefishOS Team.
|
||||||
|
*
|
||||||
|
* Author: Reion Wong <reionwong@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "thumbnailprovider.h"
|
||||||
|
#include "thumbnailcache.h"
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
QImage ThumbnailProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
|
||||||
|
{
|
||||||
|
if (!requestedSize.isValid()) {
|
||||||
|
return QImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
*size = requestedSize;
|
||||||
|
|
||||||
|
QString thumbnail = ThumbnailCache::self()->requestThumbnail(id, requestedSize);
|
||||||
|
|
||||||
|
if (!thumbnail.isEmpty()) {
|
||||||
|
return QImage(thumbnail);
|
||||||
|
}
|
||||||
|
|
||||||
|
return QImage();
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2021 CutefishOS Team.
|
* Copyright (C) 2021 CutefishOS Team.
|
||||||
*
|
*
|
||||||
* Author: revenmartin <revenmartin@gmail.com>
|
* Author: Reion Wong <reionwong@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -17,28 +17,20 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef THUMBNAILER_H
|
#ifndef THUMBNAILPROVIDER_H
|
||||||
#define THUMBNAILER_H
|
#define THUMBNAILPROVIDER_H
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QQuickImageProvider>
|
#include <QQuickImageProvider>
|
||||||
|
|
||||||
class AsyncImageResponse : public QQuickImageResponse
|
class ThumbnailProvider : public QQuickImageProvider
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AsyncImageResponse(const QString &id, const QSize &requestedSize);
|
ThumbnailProvider()
|
||||||
QQuickTextureFactory *textureFactory() const override;
|
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
|
||||||
QString m_id;
|
|
||||||
QSize m_requestedSize;
|
|
||||||
QImage m_image;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Thumbnailer : public QQuickAsyncImageProvider
|
#endif // THUMBNAILPROVIDER_H
|
||||||
{
|
|
||||||
public:
|
|
||||||
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // THUMBNAILER_H
|
|
Loading…
Reference in a new issue