File indexing completed on 2024-06-30 05:32:58

0001 /*
0002     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 #include <QBindable>
0011 #include <QCache>
0012 #include <QPixmap>
0013 #include <QSize>
0014 
0015 #include "imageroles.h"
0016 
0017 class KFileItem;
0018 
0019 struct MediaMetadata;
0020 
0021 /**
0022  * Base class for image list model.
0023  */
0024 class AbstractImageListModel : public QAbstractListModel, public ImageRoles
0025 {
0026     Q_OBJECT
0027 
0028     Q_PROPERTY(int count READ count NOTIFY countChanged)
0029 
0030 public:
0031     explicit AbstractImageListModel(const QBindable<QSize> &bindableTargetSize, const QBindable<bool> &bindableUsedInConfig, QObject *parent = nullptr);
0032 
0033     QHash<int, QByteArray> roleNames() const override;
0034 
0035     int count() const;
0036     virtual int indexOf(const QString &path) const = 0;
0037 
0038     virtual void load(const QStringList &customPaths = {});
0039     /**
0040      * Reload when target size changes or a new package is installed
0041      */
0042     void reload();
0043 
0044 public Q_SLOTS:
0045     virtual QStringList addBackground(const QString &path) = 0;
0046     /**
0047      * @return removed files that should be removed from \KDirWatch
0048      */
0049     virtual QStringList removeBackground(const QString &path) = 0;
0050 
0051 Q_SIGNALS:
0052     void countChanged();
0053     void loaded(AbstractImageListModel *model);
0054 
0055 protected:
0056     /**
0057      * Asynchronously generates a preview.
0058      * Multiple images are displayed side by side following the order in @c paths
0059      *
0060      * @note @c paths should have no duplicate urls.
0061      */
0062     void asyncGetPreview(const QStringList &paths, const QPersistentModelIndex &index) const;
0063 
0064     /**
0065      * Asynchronously extracts metadata from an image or a video file.
0066      */
0067     void asyncGetMediaMetadata(const QString &path, const QPersistentModelIndex &index) const;
0068 
0069     /**
0070      * Clears all cached records.
0071      */
0072     void clearCache();
0073 
0074     bool m_loading = false;
0075 
0076     Q_OBJECT_BINDABLE_PROPERTY(AbstractImageListModel, QSize, m_screenshotSize)
0077     Q_OBJECT_BINDABLE_PROPERTY(AbstractImageListModel, QSize, m_targetSize)
0078     QPropertyNotifier m_targetSizeChangeNotifier;
0079 
0080     QCache<QStringList, QPixmap> m_imageCache;
0081     // Store side-by-side images
0082     QHash<QStringList, QPixmap> m_imageTempCache;
0083     QCache<QString, QString /* title */> m_backgroundTitleCache;
0084     QCache<QString, QString /* author */> m_backgroundAuthorCache;
0085     QCache<QString, QSize> m_imageSizeCache;
0086 
0087     mutable QHash<QPersistentModelIndex, QStringList> m_previewJobsUrls;
0088     mutable QHash<QString, QPersistentModelIndex> m_sizeJobsUrls;
0089 
0090     QHash<QString, bool> m_pendingDeletion;
0091     QStringList m_removableWallpapers;
0092     QStringList m_customPaths;
0093 
0094     Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(AbstractImageListModel, bool, m_usedInConfig, true)
0095 
0096     friend class ImageProxyModel; // For m_removableWallpapers
0097 
0098 private Q_SLOTS:
0099     void slotMediaMetadataFound(const QString &path, const MediaMetadata &metadata);
0100     void slotHandlePreview(const KFileItem &item, const QPixmap &preview);
0101     void slotHandlePreviewFailed(const KFileItem &item);
0102 };