File indexing completed on 2024-04-21 03:55:30

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Malte Starostik <malte@kde.org>
0004     SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef _THUMBNAILCREATOR_H_
0010 #define _THUMBNAILCREATOR_H_
0011 
0012 #include "kiogui_export.h"
0013 
0014 #include <QObject>
0015 #include <QUrl>
0016 
0017 #include <memory>
0018 
0019 class QString;
0020 class QImage;
0021 
0022 namespace KIO
0023 {
0024 
0025 class ThumbnailCreatorPrivate;
0026 class ThumbnailRequestPrivate;
0027 class ThumbnailResultPrivate;
0028 
0029 /*
0030  * Encapsulates the input data for a thumbnail request.
0031  * This includes the URL of the target file as well as additional
0032  * data such as the target size
0033  *
0034  * @since 5.100
0035  *
0036  */
0037 class KIOGUI_EXPORT ThumbnailRequest
0038 {
0039 public:
0040     /**
0041      * Contruct a new ThumbnailRequest for a given file.
0042      *
0043      * @param url URL of the relevant file.
0044      * @param targetSize A size hint for the result image.
0045      * The actual result size may be different. This already
0046      * accounts for highdpi scaling, i.e. if a 500x500px thumbnail
0047      * with a DPR of 2 is requested 1000x1000 is passed here.
0048      * @param mimeType The MIME type of the target file.
0049      * @param dpr The device pixle ratio for this request. This can
0050      * be used to adjust the level of detail rendered. For example
0051      * a thumbnail for text of size 1000x1000 and DPR 1 should have
0052      * the name number of text lines as for a request of size 2000x2000
0053      * and DPR 2.
0054      * @param sequenceIndex If the thumbnailer supports sequences this
0055      * determines which sequence frame is used. Pass 0 otherwise.
0056      *
0057      */
0058     explicit ThumbnailRequest(const QUrl &url, const QSize &targetSize, const QString &mimeType, qreal dpr, float sequenceIndex);
0059     ThumbnailRequest(const ThumbnailRequest &);
0060     ThumbnailRequest &operator=(const ThumbnailRequest &);
0061     ~ThumbnailRequest();
0062 
0063     /**
0064      * URL of the relevant file
0065      */
0066     QUrl url() const;
0067 
0068     /**
0069      * The target thumbnail size
0070      */
0071     QSize targetSize() const;
0072 
0073     /**
0074      * The target file's MIME type
0075      */
0076     QString mimeType() const;
0077 
0078     /**
0079      * The device Pixel Ratio used for thumbnail creation
0080      */
0081     qreal devicePixelRatio() const;
0082 
0083     /**
0084      * If the thumb-creator can create a sequence of thumbnails,
0085      * it should use this to decide what sequence item to use.
0086      *
0087      * If the value is zero, the standard thumbnail should be created.
0088      *
0089      * This can be used for example to create thumbnails for different
0090      * timeframes in videos(For example 0m, 10m, 20m, ...).
0091      *
0092      * If the thumb-creator supports a high granularity, like a video,
0093      * the sub-integer precision coming from the float should be respected.
0094      *
0095      * If the end of the sequence is reached, the sequence should start
0096      * from the beginning.
0097      */
0098     float sequenceIndex() const;
0099 
0100 private:
0101     std::unique_ptr<ThumbnailRequestPrivate> d;
0102 };
0103 
0104 /**
0105  * Encapsulates the output of a thumbnail request.
0106  * It contains information on whether the request was successful and,
0107  * if successful, the requested thumbnail
0108  *
0109  * To create a result use KIO::ThumbnailResult::pass(image) or KIO::ThumbnailResult::fail()
0110  *
0111  * @since 5.100
0112  */
0113 class KIOGUI_EXPORT ThumbnailResult
0114 {
0115 public:
0116     ThumbnailResult(const ThumbnailResult &);
0117     ThumbnailResult &operator=(const ThumbnailResult &);
0118     ~ThumbnailResult();
0119 
0120     /**
0121      * The requested thumbnail.
0122      *
0123      * If the request failed the image is null
0124      */
0125     QImage image() const;
0126 
0127     /**
0128      * Whether the request was successful.
0129      */
0130     bool isValid() const;
0131 
0132     /**
0133      * Returns the point at which this thumb-creator's sequence indices
0134      * will wrap around (loop).
0135      *
0136      * Usually, the frontend will call setSequenceIndex() with indices
0137      * that increase indefinitely with time, e.g. as long as the user
0138      * keeps hovering a video file. Most thumb-creators however only
0139      * want to display a finite sequence of thumbs, after which their
0140      * sequence repeats.
0141      *
0142      * This method can return the sequence index at which this
0143      * thumb-creator's sequence starts wrapping around to the start
0144      * again ("looping"). The frontend may use this to generate only
0145      * thumbs up to this index, and then use cached versions for the
0146      * repeating sequence instead.
0147      *
0148      * Like sequenceIndex(), fractional values can be used if the
0149      * wraparound does not happen at an integer position, but
0150      * frontends handling only integer sequence indices may choose
0151      * to round it down.
0152      *
0153      * By default, this method returns a negative index, which signals
0154      * the frontend that it can't rely on this fixed-length sequence.
0155      *
0156      */
0157     float sequenceIndexWraparoundPoint() const;
0158 
0159     /**
0160      * Sets the point at which this thumb-creator's sequence indices
0161      * will wrap around.
0162      *
0163      * @see sequenceIndexWraparoundPoint()
0164      */
0165     void setSequenceIndexWraparoundPoint(float wraparoundPoint);
0166 
0167     /**
0168      * Create a successful result with a given image
0169      */
0170     static ThumbnailResult pass(const QImage &image);
0171 
0172     /**
0173      * Create an error result, i.e. the thumbnail creation failed
0174      */
0175     static ThumbnailResult fail();
0176 
0177 private:
0178     KIOGUI_NO_EXPORT ThumbnailResult();
0179     std::unique_ptr<ThumbnailResultPrivate> d;
0180 };
0181 
0182 /**
0183  * @class ThumbnailCreator thumbnailcreator.h <KIO/ThumbnailCreator>
0184  *
0185  * Base class for thumbnail generator plugins.
0186  *
0187  * KIO::PreviewJob, via the "thumbnail" KIO worker, uses instances of this class
0188  * to generate the thumbnail previews.
0189  *
0190  * To add support for a new document type, subclass KIO::ThumbnailCreator and implement
0191  * create() to generate a thumbnail for a given path.
0192  *
0193  * Compile your ThumbCreator as a plugin; for example, the relevant CMake code
0194  * for a thumbnailer for the "foo" filetype might look like
0195  * \code
0196  * kcoreaddons_add_plugin(foothumbnail SOURCES foothumbnail.cpp INSTALL_NAMESPACE "kf6/thumbcreator")
0197  * target_link_libraries(foothumbnail PRIVATE KF5::KIOGui)
0198  * \endcode
0199  *
0200  * You also need a JSON file containing the metadata:
0201  * \code
0202  * {
0203  *   "CacheThumbnail": true,
0204  *   "KPlugin": {
0205  *       "MimeTypes": [
0206  *           "image/x-foo"
0207  *       ],
0208  *       "Name": "Foo Documents"
0209  *   }
0210  * }
0211  * \endcode
0212  *
0213  * MIME types can also use
0214  * simple wildcards, like
0215  * \htmlonly "text/&#42;".\endhtmlonly\latexonly text/$\ast$.\endlatexonly
0216  *
0217  * If the thumbnail creation is cheap (such as text previews), you can set
0218  * \code
0219  * "CacheThumbnail": false
0220  * \endcode
0221  * in metadata to prevent your thumbnails from being cached on disk.
0222  *
0223  * You can also use the "ThumbnailerVersion" optional property in the .desktop
0224  * file, like
0225  * \code
0226  * "ThumbnailerVersion": 5
0227  * \endcode
0228  * When this is incremented (or defined when it previously was not), all the
0229  * previously-cached thumbnails for this creator will be discarded.  You should
0230  * increase the version if and only if old thumbnails need to be regenerated.
0231  *
0232  * @since 5.100
0233  */
0234 class KIOGUI_EXPORT ThumbnailCreator : public QObject
0235 {
0236     Q_OBJECT
0237 public:
0238     explicit ThumbnailCreator(QObject *parent, const QVariantList &args);
0239     virtual ~ThumbnailCreator();
0240 
0241     /**
0242      * Creates a thumbnail for a given request
0243      */
0244     virtual ThumbnailResult create(const ThumbnailRequest &request) = 0;
0245 
0246 private:
0247     void *d; // Placeholder
0248 };
0249 }
0250 #endif