git repos / SagoImageBrowser

blame: src/imagemodel.h

normal view · raw

14cf904f Poul Sander 2026-06-22 18:18 1
/*
14cf904f Poul Sander 2026-06-22 18:18 2
MIT License
14cf904f Poul Sander 2026-06-22 18:18 3
14cf904f Poul Sander 2026-06-22 18:18 4
Copyright (c) 2026 Poul Sander
14cf904f Poul Sander 2026-06-22 18:18 5
14cf904f Poul Sander 2026-06-22 18:18 6
Permission is hereby granted, free of charge, to any person obtaining a copy
14cf904f Poul Sander 2026-06-22 18:18 7
of this software and associated documentation files (the "Software"), to deal
14cf904f Poul Sander 2026-06-22 18:18 8
in the Software without restriction, including without limitation the rights
14cf904f Poul Sander 2026-06-22 18:18 9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14cf904f Poul Sander 2026-06-22 18:18 10
copies of the Software, and to permit persons to whom the Software is
14cf904f Poul Sander 2026-06-22 18:18 11
furnished to do so, subject to the following conditions:
14cf904f Poul Sander 2026-06-22 18:18 12
14cf904f Poul Sander 2026-06-22 18:18 13
The above copyright notice and this permission notice shall be included in all
14cf904f Poul Sander 2026-06-22 18:18 14
copies or substantial portions of the Software.
14cf904f Poul Sander 2026-06-22 18:18 15
14cf904f Poul Sander 2026-06-22 18:18 16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14cf904f Poul Sander 2026-06-22 18:18 17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14cf904f Poul Sander 2026-06-22 18:18 18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14cf904f Poul Sander 2026-06-22 18:18 19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14cf904f Poul Sander 2026-06-22 18:18 20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14cf904f Poul Sander 2026-06-22 18:18 21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
14cf904f Poul Sander 2026-06-22 18:18 22
SOFTWARE.
14cf904f Poul Sander 2026-06-22 18:18 23
14cf904f Poul Sander 2026-06-22 18:18 24
*/
14cf904f Poul Sander 2026-06-22 18:18 25
032aa1e1 Poul Sander 2026-02-19 18:55 26
#pragma once
032aa1e1 Poul Sander 2026-02-19 18:55 27
ac476ca8 Poul Sander 2026-05-18 16:47 28
#include "thumbnailcache.h"
ac476ca8 Poul Sander 2026-05-18 16:47 29
032aa1e1 Poul Sander 2026-02-19 18:55 30
#include <QAbstractListModel>
80a9926d Poul Sander 2026-05-19 20:28 31
#include <QByteArray>
032aa1e1 Poul Sander 2026-02-19 18:55 32
#include <QPixmap>
032aa1e1 Poul Sander 2026-02-19 18:55 33
#include <QVector>
cec3b5da Poul Sander 2026-02-19 19:17 34
#include <QThreadPool>
6fcd6c90 Poul Sander 2026-02-19 21:12 35
#include <QSet>
459bad0f Poul Sander 2026-05-17 14:11 36
#include <QHash>
459bad0f Poul Sander 2026-05-17 14:11 37
#include <QList>
6fcd6c90 Poul Sander 2026-02-19 21:12 38
#include <atomic>
09130ab9 Poul Sander 2026-02-22 22:19 39
#include <QIcon>
cec3b5da Poul Sander 2026-02-19 19:17 40
cec3b5da Poul Sander 2026-02-19 19:17 41
class ThumbnailWorker;
032aa1e1 Poul Sander 2026-02-19 18:55 42
032aa1e1 Poul Sander 2026-02-19 18:55 43
class ImageModel : public QAbstractListModel
032aa1e1 Poul Sander 2026-02-19 18:55 44
{
032aa1e1 Poul Sander 2026-02-19 18:55 45
    Q_OBJECT
032aa1e1 Poul Sander 2026-02-19 18:55 46
032aa1e1 Poul Sander 2026-02-19 18:55 47
public:
032aa1e1 Poul Sander 2026-02-19 18:55 48
    explicit ImageModel(QObject *parent = nullptr);
cec3b5da Poul Sander 2026-02-19 19:17 49
    ~ImageModel();
032aa1e1 Poul Sander 2026-02-19 18:55 50
80a9926d Poul Sander 2026-05-19 20:28 51
    // path is the raw native OS byte string (handles non-UTF-8 filenames)
80a9926d Poul Sander 2026-05-19 20:28 52
    void setDirectory(const QByteArray &path);
80a9926d Poul Sander 2026-05-19 20:28 53
    void setDirectory(const QString &path);   // convenience: converts via QFile::encodeName
80a9926d Poul Sander 2026-05-19 20:28 54
    QByteArray filePath(const QModelIndex &index) const;
09130ab9 Poul Sander 2026-02-22 22:19 55
    bool isFolder(const QModelIndex &index) const;
80a9926d Poul Sander 2026-05-19 20:28 56
    QByteArray currentDirectory() const;
032aa1e1 Poul Sander 2026-02-19 18:55 57
6fcd6c90 Poul Sander 2026-02-19 21:12 58
    void requestThumbnails(int firstRow, int lastRow);
48534775 Poul Sander 2026-06-08 18:57 59
    // Discard the cached thumbnail for a row and re-decode it from disk.
48534775 Poul Sander 2026-06-08 18:57 60
    void refreshThumbnail(const QModelIndex &index);
459bad0f Poul Sander 2026-05-17 14:11 61
    void setCacheMaxSize(int n);
ac476ca8 Poul Sander 2026-05-18 16:47 62
    void setThumbnailSize(ThumbnailCache::Size size);
6fcd6c90 Poul Sander 2026-02-19 21:12 63
032aa1e1 Poul Sander 2026-02-19 18:55 64
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
032aa1e1 Poul Sander 2026-02-19 18:55 65
    QVariant data(const QModelIndex &index, int role) const override;
032aa1e1 Poul Sander 2026-02-19 18:55 66
6fcd6c90 Poul Sander 2026-02-19 21:12 67
    void thumbnailReady(int row, const QImage &image);
cec3b5da Poul Sander 2026-02-19 19:17 68
032aa1e1 Poul Sander 2026-02-19 18:55 69
private:
032aa1e1 Poul Sander 2026-02-19 18:55 70
    struct Item
032aa1e1 Poul Sander 2026-02-19 18:55 71
    {
80a9926d Poul Sander 2026-05-19 20:28 72
        QByteArray path;      // raw native OS bytes — safe for non-UTF-8 filenames
09130ab9 Poul Sander 2026-02-22 22:19 73
        QString displayName;
032aa1e1 Poul Sander 2026-02-19 18:55 74
        QPixmap thumbnail;
cec3b5da Poul Sander 2026-02-19 19:17 75
        bool loaded = false;
09130ab9 Poul Sander 2026-02-22 22:19 76
        bool isFolder = false;
032aa1e1 Poul Sander 2026-02-19 18:55 77
    };
032aa1e1 Poul Sander 2026-02-19 18:55 78
6fcd6c90 Poul Sander 2026-02-19 21:12 79
    void queueRow(int row);
6fcd6c90 Poul Sander 2026-02-19 21:12 80
80a9926d Poul Sander 2026-05-19 20:28 81
    using ThumbnailMap = QHash<QByteArray, QPixmap>;
459bad0f Poul Sander 2026-05-17 14:11 82
032aa1e1 Poul Sander 2026-02-19 18:55 83
    QVector<Item> m_items;
cec3b5da Poul Sander 2026-02-19 19:17 84
    QThreadPool m_threadPool;
cec3b5da Poul Sander 2026-02-19 19:17 85
    QPixmap m_placeholder;
6fcd6c90 Poul Sander 2026-02-19 21:12 86
6fcd6c90 Poul Sander 2026-02-19 21:12 87
    QSet<int> m_pendingRows;
6fcd6c90 Poul Sander 2026-02-19 21:12 88
    std::atomic_bool m_cancelFlag{false};
09130ab9 Poul Sander 2026-02-22 22:19 89
    QIcon m_folderIcon;
80a9926d Poul Sander 2026-05-19 20:28 90
    QByteArray m_currentDir;
459bad0f Poul Sander 2026-05-17 14:11 91
459bad0f Poul Sander 2026-05-17 14:11 92
    // Folder thumbnail cache (last N visited directories)
80a9926d Poul Sander 2026-05-19 20:28 93
    QHash<QByteArray, ThumbnailMap> m_folderThumbnailCache;
80a9926d Poul Sander 2026-05-19 20:28 94
    QList<QByteArray> m_cacheOrder;
459bad0f Poul Sander 2026-05-17 14:11 95
    int m_cacheMaxSize = 4;
ac476ca8 Poul Sander 2026-05-18 16:47 96
ac476ca8 Poul Sander 2026-05-18 16:47 97
    ThumbnailCache::Size m_thumbnailSize = ThumbnailCache::Size::Normal;
032aa1e1 Poul Sander 2026-02-19 18:55 98
};
1970-01-01 00:00 99