File indexing completed on 2024-04-28 04:58:04

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2000 Malte Starostik <malte@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef _THUMBNAIL_H_
0008 #define _THUMBNAIL_H_
0009 
0010 #include <QHash>
0011 #include <QPainter>
0012 #include <QRandomGenerator>
0013 #include <QSet>
0014 
0015 #include <KIO/ThumbnailCreator>
0016 #include <KIO/WorkerBase>
0017 #include <KPluginMetaData>
0018 
0019 #include <kio_version.h>
0020 
0021 class ThumbCreator;
0022 class QImage;
0023 
0024 struct ThumbCreatorWithMetadata {
0025     std::unique_ptr<KIO::ThumbnailCreator> creator;
0026     bool cacheThumbnail = true;
0027     bool devicePixelRatioDependent = false;
0028     bool handleSequences = false;
0029 };
0030 
0031 class ThumbnailProtocol : public KIO::WorkerBase
0032 {
0033 public:
0034     ThumbnailProtocol(const QByteArray &pool, const QByteArray &app);
0035     ~ThumbnailProtocol() override;
0036 
0037     KIO::WorkerResult get(const QUrl &url) override;
0038 
0039 protected:
0040     ThumbCreatorWithMetadata *getThumbCreator(const QString &plugin);
0041     bool isOpaque(const QImage &image) const;
0042     void drawPictureFrame(QPainter *painter, const QPoint &pos, const QImage &image, int frameWidth, QSize imageTargetSize, int rotationAngle) const;
0043     QImage thumbForDirectory(const QString &directory);
0044     KPluginMetaData pluginForMimeType(const QString &mimeType);
0045 
0046     float sequenceIndex() const;
0047 
0048 private:
0049     /**
0050      * Creates a sub thumbnail for the directory thumbnail. If a cached
0051      * version of the sub thumbnail is available, the cached version will be used.
0052      * If no cached version is available, the created sub thumbnail will be
0053      * added to the cache for later use.
0054      */
0055     bool createSubThumbnail(QImage &thumbnail, const QString &filePath, int segmentWidth, int segmentHeight);
0056 
0057     /**
0058      * Draw the SubThumbnail
0059      **/
0060     void drawSubThumbnail(QPainter &p, QImage subThumbnail, int width, int height, int xPos, int yPos, int borderStrokeWidth);
0061 
0062 private:
0063     void ensureDirsCreated();
0064     bool createThumbnail(ThumbCreatorWithMetadata *subCreator, const QString &filePath, int width, int height, QImage &thumbnail);
0065 
0066     QString m_mimeType;
0067     int m_width;
0068     int m_height;
0069     qreal m_devicePixelRatio;
0070     // Thumbnail creators
0071     QHash<QString, ThumbCreatorWithMetadata *> m_creators;
0072     QStringList m_enabledPlugins;
0073     QSet<QString> m_propagationDirectories;
0074     QString m_thumbBasePath;
0075     qint64 m_maxFileSize;
0076     QRandomGenerator m_randomGenerator;
0077     float m_sequenceIndexWrapAroundPoint = -1;
0078 };
0079 
0080 #endif