git repos / SagoImageBrowser

blame: src/imagemodel.h

normal view · raw

032aa1e1 Poul Sander 2026-02-19 18:55 1
#pragma once
032aa1e1 Poul Sander 2026-02-19 18:55 2
032aa1e1 Poul Sander 2026-02-19 18:55 3
#include <QAbstractListModel>
032aa1e1 Poul Sander 2026-02-19 18:55 4
#include <QPixmap>
032aa1e1 Poul Sander 2026-02-19 18:55 5
#include <QVector>
cec3b5da Poul Sander 2026-02-19 19:17 6
#include <QThreadPool>
6fcd6c90 Poul Sander 2026-02-19 21:12 7
#include <QSet>
459bad0f Poul Sander 2026-05-17 14:11 8
#include <QHash>
459bad0f Poul Sander 2026-05-17 14:11 9
#include <QList>
6fcd6c90 Poul Sander 2026-02-19 21:12 10
#include <atomic>
09130ab9 Poul Sander 2026-02-22 22:19 11
#include <QIcon>
cec3b5da Poul Sander 2026-02-19 19:17 12
cec3b5da Poul Sander 2026-02-19 19:17 13
class ThumbnailWorker;
032aa1e1 Poul Sander 2026-02-19 18:55 14
032aa1e1 Poul Sander 2026-02-19 18:55 15
class ImageModel : public QAbstractListModel
032aa1e1 Poul Sander 2026-02-19 18:55 16
{
032aa1e1 Poul Sander 2026-02-19 18:55 17
    Q_OBJECT
032aa1e1 Poul Sander 2026-02-19 18:55 18
032aa1e1 Poul Sander 2026-02-19 18:55 19
public:
032aa1e1 Poul Sander 2026-02-19 18:55 20
    explicit ImageModel(QObject *parent = nullptr);
cec3b5da Poul Sander 2026-02-19 19:17 21
    ~ImageModel();
032aa1e1 Poul Sander 2026-02-19 18:55 22
032aa1e1 Poul Sander 2026-02-19 18:55 23
    void setDirectory(const QString &path);
032aa1e1 Poul Sander 2026-02-19 18:55 24
    QString filePath(const QModelIndex &index) const;
09130ab9 Poul Sander 2026-02-22 22:19 25
    bool isFolder(const QModelIndex &index) const;
09130ab9 Poul Sander 2026-02-22 22:19 26
    QString currentDirectory() const;
032aa1e1 Poul Sander 2026-02-19 18:55 27
6fcd6c90 Poul Sander 2026-02-19 21:12 28
    void requestThumbnails(int firstRow, int lastRow);
459bad0f Poul Sander 2026-05-17 14:11 29
    void setCacheMaxSize(int n);
6fcd6c90 Poul Sander 2026-02-19 21:12 30
032aa1e1 Poul Sander 2026-02-19 18:55 31
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
032aa1e1 Poul Sander 2026-02-19 18:55 32
    QVariant data(const QModelIndex &index, int role) const override;
032aa1e1 Poul Sander 2026-02-19 18:55 33
6fcd6c90 Poul Sander 2026-02-19 21:12 34
    void thumbnailReady(int row, const QImage &image);
cec3b5da Poul Sander 2026-02-19 19:17 35
032aa1e1 Poul Sander 2026-02-19 18:55 36
private:
032aa1e1 Poul Sander 2026-02-19 18:55 37
    struct Item
032aa1e1 Poul Sander 2026-02-19 18:55 38
    {
032aa1e1 Poul Sander 2026-02-19 18:55 39
        QString path;
09130ab9 Poul Sander 2026-02-22 22:19 40
        QString displayName;
032aa1e1 Poul Sander 2026-02-19 18:55 41
        QPixmap thumbnail;
cec3b5da Poul Sander 2026-02-19 19:17 42
        bool loaded = false;
09130ab9 Poul Sander 2026-02-22 22:19 43
        bool isFolder = false;
032aa1e1 Poul Sander 2026-02-19 18:55 44
    };
032aa1e1 Poul Sander 2026-02-19 18:55 45
6fcd6c90 Poul Sander 2026-02-19 21:12 46
    void queueRow(int row);
6fcd6c90 Poul Sander 2026-02-19 21:12 47
459bad0f Poul Sander 2026-05-17 14:11 48
    using ThumbnailMap = QHash<QString, QPixmap>;
459bad0f Poul Sander 2026-05-17 14:11 49
032aa1e1 Poul Sander 2026-02-19 18:55 50
    QVector<Item> m_items;
cec3b5da Poul Sander 2026-02-19 19:17 51
    QThreadPool m_threadPool;
cec3b5da Poul Sander 2026-02-19 19:17 52
    QPixmap m_placeholder;
6fcd6c90 Poul Sander 2026-02-19 21:12 53
6fcd6c90 Poul Sander 2026-02-19 21:12 54
    QSet<int> m_pendingRows;
6fcd6c90 Poul Sander 2026-02-19 21:12 55
    std::atomic_bool m_cancelFlag{false};
09130ab9 Poul Sander 2026-02-22 22:19 56
    QIcon m_folderIcon;
09130ab9 Poul Sander 2026-02-22 22:19 57
    QString m_currentDir;
459bad0f Poul Sander 2026-05-17 14:11 58
459bad0f Poul Sander 2026-05-17 14:11 59
    // Folder thumbnail cache (last N visited directories)
459bad0f Poul Sander 2026-05-17 14:11 60
    QHash<QString, ThumbnailMap> m_folderThumbnailCache;
459bad0f Poul Sander 2026-05-17 14:11 61
    QList<QString> m_cacheOrder;
459bad0f Poul Sander 2026-05-17 14:11 62
    int m_cacheMaxSize = 4;
032aa1e1 Poul Sander 2026-02-19 18:55 63
};
1970-01-01 00:00 64