File indexing completed on 2024-11-10 05:02:46

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "slidemodel.h"
0008 
0009 #include <QDir>
0010 
0011 #include "model/imageproxymodel.h"
0012 
0013 SlideModel::SlideModel(const QBindable<QSize> &bindableTargetSize, const QBindable<bool> &bindableUsedInConfig, QObject *parent)
0014     : QConcatenateTablesProxyModel(parent)
0015     , m_targetSize(bindableTargetSize.makeBinding())
0016     , m_usedInConfig(bindableUsedInConfig.makeBinding())
0017 {
0018 }
0019 
0020 QHash<int, QByteArray> SlideModel::roleNames() const
0021 {
0022     const auto models = sourceModels();
0023 
0024     if (!models.empty()) {
0025         return models.at(0)->roleNames();
0026     }
0027 
0028     return QConcatenateTablesProxyModel::roleNames();
0029 }
0030 
0031 QVariant SlideModel::data(const QModelIndex &index, int role) const
0032 {
0033     if (!index.isValid()) {
0034         return QVariant();
0035     }
0036 
0037     if (role == ToggleRole) {
0038         return m_checkedTable.value(index.data(PackageNameRole).toString(), true);
0039     }
0040 
0041     return QConcatenateTablesProxyModel::data(index, role);
0042 }
0043 
0044 bool SlideModel::setData(const QModelIndex &index, const QVariant &value, int role)
0045 {
0046     if (!index.isValid()) {
0047         return false;
0048     }
0049 
0050     if (role == ToggleRole) {
0051         m_checkedTable[index.data(PackageNameRole).toString()] = value.toBool();
0052 
0053         Q_EMIT dataChanged(index, index, {ToggleRole});
0054         return true;
0055     }
0056 
0057     return QConcatenateTablesProxyModel::setData(index, value, role);
0058 }
0059 
0060 int SlideModel::indexOf(const QString &packagePath) const
0061 {
0062     int idx = -1;
0063 
0064     for (const auto models{sourceModels()}; auto m : models) {
0065         idx = static_cast<ImageProxyModel *>(m)->indexOf(packagePath);
0066 
0067         if (idx >= 0) {
0068             return mapFromSource(m->index(idx, 0)).row();
0069         }
0070     }
0071 
0072     return idx;
0073 }
0074 
0075 QStringList SlideModel::addDirs(const QStringList &dirs)
0076 {
0077     QStringList added;
0078 
0079     for (const QString &_d : dirs) {
0080         if (!QFileInfo(_d).isDir()) {
0081             continue;
0082         }
0083 
0084         const QString d = _d.endsWith(QDir::separator()) ? _d : _d + QDir::separator();
0085 
0086         if (!m_models.contains(d)) {
0087             auto m = new ImageProxyModel({d}, QBindable<QSize>(&m_targetSize), QBindable<bool>(&m_usedInConfig), this);
0088             m_models.insert(d, m);
0089             added.append(d);
0090 
0091             if (m->loading().value()) {
0092                 connect(m, &ImageProxyModel::loadingChanged, this, &SlideModel::slotSourceModelLoadingChanged);
0093             } else {
0094                 // In case it loads immediately
0095                 ++m_loaded;
0096                 addSourceModel(m);
0097             }
0098         }
0099     }
0100 
0101     if (!added.empty()) {
0102         m_loading = m_loaded != m_models.size();
0103         if (!m_loading) {
0104             Q_EMIT done();
0105         }
0106     }
0107 
0108     return added;
0109 }
0110 
0111 QString SlideModel::removeDir(const QString &_dir)
0112 {
0113     const QString dir = _dir.endsWith(QDir::separator()) ? _dir : _dir + QDir::separator();
0114 
0115     if (!m_models.contains(dir)) {
0116         return QString();
0117     }
0118 
0119     auto *m = m_models.take(dir);
0120 
0121     m_loaded--;
0122     removeSourceModel(m);
0123     m->deleteLater();
0124 
0125     return dir;
0126 }
0127 
0128 void SlideModel::setSlidePaths(const QStringList &slidePaths)
0129 {
0130     const auto models = sourceModels();
0131 
0132     for (const auto k : std::as_const(m_models)) {
0133         if (models.contains(k)) {
0134             removeSourceModel(k);
0135         } else {
0136             // Abort loading
0137             disconnect(k, nullptr, this, nullptr);
0138         }
0139         delete k;
0140     }
0141 
0142     m_models.clear();
0143     m_loaded = 0;
0144 
0145     addDirs(slidePaths);
0146 }
0147 
0148 void SlideModel::setUncheckedSlides(const QStringList &uncheckedSlides)
0149 {
0150     m_checkedTable.clear();
0151 
0152     for (const QString &p : uncheckedSlides) {
0153         m_checkedTable[p] = false;
0154     }
0155 }
0156 
0157 QBindable<bool> SlideModel::loading() const
0158 {
0159     return &m_loading;
0160 }
0161 
0162 void SlideModel::slotSourceModelLoadingChanged()
0163 {
0164     auto m = static_cast<ImageProxyModel *>(sender());
0165     disconnect(m, &ImageProxyModel::loadingChanged, this, nullptr);
0166     addSourceModel(m);
0167 
0168     if (++m_loaded == m_models.size()) {
0169         m_loading = false;
0170         Q_EMIT done();
0171     }
0172 }