diff --git a/src/qtquick/PDFCoverImageProvider.cpp b/src/qtquick/PDFCoverImageProvider.cpp index 0ba0861..fccb4da 100644 --- a/src/qtquick/PDFCoverImageProvider.cpp +++ b/src/qtquick/PDFCoverImageProvider.cpp @@ -1,109 +1,109 @@ /* * Copyright (C) 2016 Dan Leinir Turthra Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "PDFCoverImageProvider.h" #include #include #include #include #include #include #include #include class PDFCoverImageProvider::Private { public: Private() { QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); thumbDir = QDir(path); QString subpath("thumbcache"); if(!thumbDir.exists(subpath)) { thumbDir.mkpath(subpath); } thumbDir.cd(subpath); } QDir thumbDir; }; PDFCoverImageProvider::PDFCoverImageProvider() - : QQuickImageProvider(QQuickImageProvider::Image) + : QQuickImageProvider(QQuickImageProvider::Pixmap, QQmlImageProviderBase::ForceAsynchronousImageLoading) , d(new Private) { } PDFCoverImageProvider::~PDFCoverImageProvider() { delete d; } -QImage PDFCoverImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize) +QPixmap PDFCoverImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) { Q_UNUSED(size) Q_UNUSED(requestedSize) - QImage img; + QPixmap img; QMimeDatabase db; db.mimeTypeForFile(id, QMimeDatabase::MatchContent); const QMimeType mime = db.mimeTypeForFile(id, QMimeDatabase::MatchContent); if(mime.inherits("application/pdf")) { //-sOutputFile=FILENAME.png FILENAME QString outFile = QString("%1/%2.png").arg(d->thumbDir.absolutePath()).arg(QUrl(id).toString().replace("/", "-").replace(":", "-")); if(!QFile::exists(outFile)) { // then we've not already generated a thumbnail, try to make one... QProcess thumbnailer; QStringList args; - args << "-sPageList=1" << "-dLastPage=1" << "-dSAFER" << "-dBATCH" << "-dNOPAUSE" << "-dQUIET" << "-sDEVICE=png16m" << "-dGraphicsAlphaBits=4" << "-r150"; + args << "-sPageList=1" << "-dLastPage=1" << "-dSAFER" << "-dBATCH" << "-dNOPAUSE" << "-dQUIET" << "-sDEVICE=png16m" << "-dGraphicsAlphaBits=4" << "-r300"; args << QString("-sOutputFile=%1").arg(outFile) << id; QString gsApp; #ifdef Q_OS_WIN #ifdef __MINGW32__ gsApp = qApp->applicationDirPath() + "/gsc.exe"; #else gsApp = qApp->applicationDirPath(); #ifdef Q_OS_WIN64 gsApp += "/gswin64c.exe"; #else gsApp += "/gswin32c.exe"; #endif #endif #else gsApp = "gs"; #endif thumbnailer.start(gsApp, args); thumbnailer.waitForFinished(); } bool success = false; // Now, does it exist this time? if(QFile::exists(outFile)) { success = img.load(outFile); } if(!success) { QIcon oops = QIcon::fromTheme("unknown"); - img = oops.pixmap(oops.availableSizes().last()).toImage(); + img = oops.pixmap(oops.availableSizes().last()); qCDebug(QTQUICK_LOG) << "Failed to load image with id" << id << "from thumbnail file" << outFile; } } return img; } diff --git a/src/qtquick/PDFCoverImageProvider.h b/src/qtquick/PDFCoverImageProvider.h index aaa2556..0bb6a57 100644 --- a/src/qtquick/PDFCoverImageProvider.h +++ b/src/qtquick/PDFCoverImageProvider.h @@ -1,53 +1,53 @@ /* * Copyright (C) 2016 Dan Leinir Turthra Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #ifndef PDFCOVERIMAGEPROVIDER_H #define PDFCOVERIMAGEPROVIDER_H #include /** * \brief Get file previews of PDF files, where the thumbnailer isn't available... * * NOTE: As this task is potentially heavy, make sure to mark any Image using this provider asynchronous */ class PDFCoverImageProvider : public QQuickImageProvider { public: explicit PDFCoverImageProvider(); virtual ~PDFCoverImageProvider(); /** * \brief Get an image. * * @param id The source of the image. * @param size The size of the original image, unused. * @param requestedSize The required size of the final image, unused. * - * @return a QImage. + * @return a QPixmap. */ - virtual QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override; + virtual QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override; private: class Private; Private* d; }; #endif//PDFCOVERIMAGEPROVIDER_H diff --git a/src/qtquick/PreviewImageProvider.cpp b/src/qtquick/PreviewImageProvider.cpp index 9fa4e7b..b38261b 100644 --- a/src/qtquick/PreviewImageProvider.cpp +++ b/src/qtquick/PreviewImageProvider.cpp @@ -1,145 +1,145 @@ /* * Copyright (C) 2015 Dan Leinir Turthra Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "PreviewImageProvider.h" #include #include #include #include #include #include #include class PreviewImageProvider::Private { public: Private() {}; // Yes, we might use the KFileItem here, but we have a one-to-one equivalence between jobs and previews here anyway, so... QHash previews; QHash jobCompletion; }; PreviewImageProvider::PreviewImageProvider(QObject* parent) : QObject(parent) - , QQuickImageProvider(QQuickImageProvider::Image, QQmlImageProviderBase::ForceAsynchronousImageLoading) + , QQuickImageProvider(QQuickImageProvider::Pixmap, QQmlImageProviderBase::ForceAsynchronousImageLoading) , d(new Private) { qRegisterMetaType("KFileItem"); } PreviewImageProvider::~PreviewImageProvider() { delete d; } -QImage PreviewImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize) +QPixmap PreviewImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) { - QImage image; + QPixmap image; QSize ourSize(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous); if(requestedSize.width() > 0 && requestedSize.height() > 0) { ourSize = requestedSize; } if(QFile(id).exists()) { QMimeDatabase db; QList mimetypes = db.mimeTypesForFileName(id); QString mimetype; if(mimetypes.count() > 0) { mimetype = mimetypes.first().name(); } const QStringList* allPlugins = new QStringList(KIO::PreviewJob::availablePlugins()); KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << KFileItem(QUrl::fromLocalFile(id), mimetype, 0), ourSize, allPlugins); job->setIgnoreMaximumSize(true); job->setScaleType(KIO::PreviewJob::ScaledAndCached); connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)), SLOT(updatePreview(KFileItem,QPixmap))); connect(job, SIGNAL(failed(KFileItem)), SLOT(fallbackPreview(KFileItem))); connect(job, SIGNAL(finished(KJob*)), SLOT(finishedPreview(KJob*))); connect(job, &QObject::destroyed, [job,this](){d->jobCompletion.remove(job);}); d->jobCompletion[job] = false; if(job->exec()) { // Do not access the job after this point! As we are requesting that // it be deleted in finishedPreview(), don't expect it to be around. while(!d->jobCompletion[job]) { // Let's let the job do its thing and whatnot... qApp->processEvents(); } if(!d->previews[job].isNull()) { if(requestedSize.width() > 0 && requestedSize.height() > 0) { - image = d->previews[job].scaled(requestedSize).toImage(); + image = d->previews[job].scaled(requestedSize); } else { - image = d->previews[job].toImage(); + image = d->previews[job]; } } } d->previews.remove(job); delete allPlugins; } else { - image = QImage(ourSize, QImage::Format_ARGB32); + image = QPixmap(ourSize); } if(size) { *size = ourSize; } return image; } void PreviewImageProvider::fallbackPreview(const KFileItem& item) { KIO::PreviewJob* previewJob = qobject_cast(sender()); if(previewJob) { QMimeDatabase db; QPixmap preview = QIcon::fromTheme(db.mimeTypeForName(item.mimetype()).iconName()).pixmap(128); d->previews[previewJob] = preview; d->jobCompletion[previewJob] = true; } } void PreviewImageProvider::updatePreview(const KFileItem&, const QPixmap& p) { KIO::PreviewJob* previewJob = qobject_cast(sender()); if(previewJob) { d->previews[previewJob] = p; } } void PreviewImageProvider::finishedPreview(KJob* job) { d->jobCompletion[job] = true; } diff --git a/src/qtquick/PreviewImageProvider.h b/src/qtquick/PreviewImageProvider.h index 3f47625..63dfa9b 100644 --- a/src/qtquick/PreviewImageProvider.h +++ b/src/qtquick/PreviewImageProvider.h @@ -1,75 +1,75 @@ /* * Copyright (C) 2015 Dan Leinir Turthra Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #ifndef PREVIEWIMAGEPROVIDER_H #define PREVIEWIMAGEPROVIDER_H #include /** * \brief Get file previews using KIO::PreviewJob * * NOTE: As this task is potentially heavy, make sure to mark any Image using this provider asynchronous */ class KFileItem; class KJob; class PreviewImageProvider : public QObject, public QQuickImageProvider { Q_OBJECT public: explicit PreviewImageProvider(QObject* parent = nullptr); ~PreviewImageProvider() override; /** * \brief Get an image. * * @param id The source of the image. * @param size The size of the original image. * @param requestedSize The required size of the final image. * - * @return a QImage. + * @return a QPixmap. */ - QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override; + QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override; /** *\brief Get an icon associated with the mimetype of the image as a fallback. * * @param p Pointer to pixmap to write this fallback into. */ Q_SLOT void updatePreview(const KFileItem&, const QPixmap& p); /** *\brief Get an icon associated with the mimetype of the image as a fallback. * * @param item The image to write a fallback for. */ Q_SLOT void fallbackPreview(const KFileItem& item); /** * \brief Set whether the preview generation is finished. * * @param job The job to mark finished. */ Q_SLOT void finishedPreview(KJob* job); private: class Private; Private* d; }; #endif//PREVIEWIMAGEPROVIDER_H