git repos / SagoImageBrowser

blame: imagemodel.cpp

normal view · raw

032aa1e1 Poul Sander 2026-02-19 18:55 1
#include "imagemodel.h"
cec3b5da Poul Sander 2026-02-19 19:17 2
#include "thumbnailworker.h"
032aa1e1 Poul Sander 2026-02-19 18:55 3
032aa1e1 Poul Sander 2026-02-19 18:55 4
#include <QDir>
032aa1e1 Poul Sander 2026-02-19 18:55 5
#include <QImageReader>
032aa1e1 Poul Sander 2026-02-19 18:55 6
#include <QFileInfo>
032aa1e1 Poul Sander 2026-02-19 18:55 7
032aa1e1 Poul Sander 2026-02-19 18:55 8
ImageModel::ImageModel(QObject *parent)
032aa1e1 Poul Sander 2026-02-19 18:55 9
    : QAbstractListModel(parent)
032aa1e1 Poul Sander 2026-02-19 18:55 10
{
cec3b5da Poul Sander 2026-02-19 19:17 11
    m_threadPool.setMaxThreadCount(QThread::idealThreadCount());
cec3b5da Poul Sander 2026-02-19 19:17 12
cec3b5da Poul Sander 2026-02-19 19:17 13
    // Simple gray placeholder
cec3b5da Poul Sander 2026-02-19 19:17 14
    m_placeholder = QPixmap(128, 128);
cec3b5da Poul Sander 2026-02-19 19:17 15
    m_placeholder.fill(Qt::lightGray);
cec3b5da Poul Sander 2026-02-19 19:17 16
}
cec3b5da Poul Sander 2026-02-19 19:17 17
cec3b5da Poul Sander 2026-02-19 19:17 18
ImageModel::~ImageModel()
cec3b5da Poul Sander 2026-02-19 19:17 19
{
cec3b5da Poul Sander 2026-02-19 19:17 20
    m_threadPool.waitForDone();
032aa1e1 Poul Sander 2026-02-19 18:55 21
}
032aa1e1 Poul Sander 2026-02-19 18:55 22
032aa1e1 Poul Sander 2026-02-19 18:55 23
void ImageModel::setDirectory(const QString &path)
032aa1e1 Poul Sander 2026-02-19 18:55 24
{
032aa1e1 Poul Sander 2026-02-19 18:55 25
    beginResetModel();
032aa1e1 Poul Sander 2026-02-19 18:55 26
    m_items.clear();
032aa1e1 Poul Sander 2026-02-19 18:55 27
032aa1e1 Poul Sander 2026-02-19 18:55 28
    QDir dir(path);
032aa1e1 Poul Sander 2026-02-19 18:55 29
    QStringList filters;
032aa1e1 Poul Sander 2026-02-19 18:55 30
032aa1e1 Poul Sander 2026-02-19 18:55 31
    const auto formats = QImageReader::supportedImageFormats();
032aa1e1 Poul Sander 2026-02-19 18:55 32
    for (const QByteArray &fmt : formats)
032aa1e1 Poul Sander 2026-02-19 18:55 33
        filters << "*." + fmt;
032aa1e1 Poul Sander 2026-02-19 18:55 34
032aa1e1 Poul Sander 2026-02-19 18:55 35
    dir.setNameFilters(filters);
032aa1e1 Poul Sander 2026-02-19 18:55 36
    dir.setFilter(QDir::Files);
032aa1e1 Poul Sander 2026-02-19 18:55 37
cec3b5da Poul Sander 2026-02-19 19:17 38
    const auto files = dir.entryInfoList();
cec3b5da Poul Sander 2026-02-19 19:17 39
cec3b5da Poul Sander 2026-02-19 19:17 40
    int row = 0;
cec3b5da Poul Sander 2026-02-19 19:17 41
    for (const QFileInfo &file : files)
032aa1e1 Poul Sander 2026-02-19 18:55 42
    {
cec3b5da Poul Sander 2026-02-19 19:17 43
        m_items.append({file.absoluteFilePath(), m_placeholder, false});
cec3b5da Poul Sander 2026-02-19 19:17 44
cec3b5da Poul Sander 2026-02-19 19:17 45
        // Create worker
cec3b5da Poul Sander 2026-02-19 19:17 46
        auto *worker = new ThumbnailWorker(file.absoluteFilePath(), row);
032aa1e1 Poul Sander 2026-02-19 18:55 47
cec3b5da Poul Sander 2026-02-19 19:17 48
        connect(worker, &ThumbnailWorker::finished,
cec3b5da Poul Sander 2026-02-19 19:17 49
                this, &ImageModel::thumbnailReady,
cec3b5da Poul Sander 2026-02-19 19:17 50
                Qt::QueuedConnection);
032aa1e1 Poul Sander 2026-02-19 18:55 51
cec3b5da Poul Sander 2026-02-19 19:17 52
        m_threadPool.start(worker);
cec3b5da Poul Sander 2026-02-19 19:17 53
cec3b5da Poul Sander 2026-02-19 19:17 54
        row++;
032aa1e1 Poul Sander 2026-02-19 18:55 55
    }
032aa1e1 Poul Sander 2026-02-19 18:55 56
032aa1e1 Poul Sander 2026-02-19 18:55 57
    endResetModel();
032aa1e1 Poul Sander 2026-02-19 18:55 58
}
032aa1e1 Poul Sander 2026-02-19 18:55 59
032aa1e1 Poul Sander 2026-02-19 18:55 60
QString ImageModel::filePath(const QModelIndex &index) const
032aa1e1 Poul Sander 2026-02-19 18:55 61
{
032aa1e1 Poul Sander 2026-02-19 18:55 62
    if (!index.isValid())
032aa1e1 Poul Sander 2026-02-19 18:55 63
        return {};
032aa1e1 Poul Sander 2026-02-19 18:55 64
032aa1e1 Poul Sander 2026-02-19 18:55 65
    return m_items[index.row()].path;
032aa1e1 Poul Sander 2026-02-19 18:55 66
}
032aa1e1 Poul Sander 2026-02-19 18:55 67
032aa1e1 Poul Sander 2026-02-19 18:55 68
int ImageModel::rowCount(const QModelIndex &) const
032aa1e1 Poul Sander 2026-02-19 18:55 69
{
032aa1e1 Poul Sander 2026-02-19 18:55 70
    return m_items.size();
032aa1e1 Poul Sander 2026-02-19 18:55 71
}
032aa1e1 Poul Sander 2026-02-19 18:55 72
032aa1e1 Poul Sander 2026-02-19 18:55 73
QVariant ImageModel::data(const QModelIndex &index, int role) const
032aa1e1 Poul Sander 2026-02-19 18:55 74
{
032aa1e1 Poul Sander 2026-02-19 18:55 75
    if (!index.isValid())
032aa1e1 Poul Sander 2026-02-19 18:55 76
        return {};
032aa1e1 Poul Sander 2026-02-19 18:55 77
032aa1e1 Poul Sander 2026-02-19 18:55 78
    const Item &item = m_items[index.row()];
032aa1e1 Poul Sander 2026-02-19 18:55 79
032aa1e1 Poul Sander 2026-02-19 18:55 80
    if (role == Qt::DecorationRole)
032aa1e1 Poul Sander 2026-02-19 18:55 81
        return item.thumbnail;
032aa1e1 Poul Sander 2026-02-19 18:55 82
032aa1e1 Poul Sander 2026-02-19 18:55 83
    if (role == Qt::DisplayRole)
032aa1e1 Poul Sander 2026-02-19 18:55 84
        return QFileInfo(item.path).fileName();
032aa1e1 Poul Sander 2026-02-19 18:55 85
032aa1e1 Poul Sander 2026-02-19 18:55 86
    return {};
032aa1e1 Poul Sander 2026-02-19 18:55 87
}
cec3b5da Poul Sander 2026-02-19 19:17 88
cec3b5da Poul Sander 2026-02-19 19:17 89
void ImageModel::thumbnailReady(int row, const QPixmap &pixmap)
cec3b5da Poul Sander 2026-02-19 19:17 90
{
cec3b5da Poul Sander 2026-02-19 19:17 91
    if (row < 0 || row >= m_items.size())
cec3b5da Poul Sander 2026-02-19 19:17 92
        return;
cec3b5da Poul Sander 2026-02-19 19:17 93
cec3b5da Poul Sander 2026-02-19 19:17 94
    m_items[row].thumbnail = pixmap;
cec3b5da Poul Sander 2026-02-19 19:17 95
    m_items[row].loaded = true;
cec3b5da Poul Sander 2026-02-19 19:17 96
cec3b5da Poul Sander 2026-02-19 19:17 97
    QModelIndex idx = index(row);
cec3b5da Poul Sander 2026-02-19 19:17 98
    emit dataChanged(idx, idx, {Qt::DecorationRole});
cec3b5da Poul Sander 2026-02-19 19:17 99
}
1970-01-01 00:00 100