File indexing completed on 2025-01-26 04:15:00

0001 /*
0002  * Copyright (C) 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 #ifndef PREVIEWIMAGEPROVIDER_H
0023 #define PREVIEWIMAGEPROVIDER_H
0024 
0025 #include <QQuickAsyncImageProvider>
0026 #include <QRunnable>
0027 
0028 /**
0029  * \brief Get file previews using KIO::PreviewJob
0030  *
0031  * NOTE: As this task is potentially heavy, make sure to mark any Image using this provider asynchronous
0032  */
0033 class KFileItem;
0034 class KJob;
0035 class PreviewImageProvider : public QQuickAsyncImageProvider
0036 {
0037 public:
0038     explicit PreviewImageProvider();
0039     ~PreviewImageProvider() override;
0040 
0041     /**
0042      * \brief Get an image.
0043      * 
0044      * @param id The source of the image.
0045      * @param requestedSize The required size of the final image.
0046      * 
0047      * @return an asynchronous image response
0048      */
0049     QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
0050 private:
0051     class Private;
0052     Private* d;
0053 };
0054 
0055 /**
0056  * \brief A worker class which does the bulk of the work for PreviewImageProvider
0057  */
0058 class PreviewRunnable : public QObject, public QRunnable {
0059     Q_OBJECT;
0060 public:
0061     explicit PreviewRunnable(const QString &id, const QSize &requestedSize);
0062     ~PreviewRunnable() override;
0063 
0064     void run() override;
0065 
0066     /**
0067      * Request that the preview worker abort what it's doing
0068      */
0069     Q_SLOT void abort();
0070 
0071     /**
0072      * \brief Emitted once the preview has been retrieved (successfully or not)
0073      * @param image The preview image in the requested size (possibly a placeholder)
0074      */
0075     Q_SIGNAL void done(QImage image);
0076     /**
0077      *\brief Get an icon associated with the mimetype of the image as a fallback.
0078      * 
0079      * @param p Pointer to pixmap to write this fallback into.
0080      */
0081     Q_SLOT void updatePreview(const KFileItem&, const QPixmap& p);
0082     /**
0083      *\brief Get an icon associated with the mimetype of the image as a fallback.
0084      * 
0085      * @param item The image to write a fallback for.
0086      */
0087     Q_SLOT void fallbackPreview(const KFileItem& item);
0088     /**
0089      * \brief Set whether the preview generation is finished.
0090      * 
0091      * @param job The job to mark finished.
0092      */
0093     Q_SLOT void finishedPreview(KJob* job);
0094 private:
0095     class Private;
0096     Private* d;
0097 };
0098 
0099 #endif//PREVIEWIMAGEPROVIDER_H