File indexing completed on 2024-05-05 17:33:57

0001 /*
0002  *   SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0003  *   SPDX-FileCopyrightText: 2008-2010 Matthias Fuchs <mat69@gmx.net>
0004  *   SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0005  *   SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
0006  *
0007  *   SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 
0010 #include "comicmodel.h"
0011 #include "engine/comicprovider.h"
0012 
0013 #include <QDebug>
0014 #include <QIcon>
0015 
0016 ComicModel::ComicModel(ComicEngine *engine, const QStringList &usedComics, QObject *parent)
0017     : QAbstractTableModel(parent)
0018     , mUsedComics(usedComics)
0019     , mEngine(engine)
0020 {
0021     Q_ASSERT(engine);
0022 
0023     load();
0024 }
0025 
0026 QHash<int, QByteArray> ComicModel::roleNames() const
0027 {
0028     QHash<int, QByteArray> roles;
0029     roles[Qt::DisplayRole] = "display";
0030     roles[Qt::DecorationRole] = "decoration";
0031     roles[Qt::UserRole] = "plugin";
0032     return roles;
0033 }
0034 
0035 int ComicModel::rowCount(const QModelIndex &index) const
0036 {
0037     if (!index.isValid()) {
0038         return mComics.count();
0039     }
0040 
0041     return 0;
0042 }
0043 
0044 int ComicModel::columnCount(const QModelIndex &index) const
0045 {
0046     Q_UNUSED(index)
0047     return 2;
0048 }
0049 
0050 QVariant ComicModel::data(const QModelIndex &index, int role) const
0051 {
0052     if (!index.isValid() || index.row() >= mComics.count()) {
0053         return QVariant();
0054     }
0055 
0056     const ComicProviderInfo &info = mComics.at(index.row());
0057     switch (role) {
0058     case Qt::DisplayRole:
0059         return info.name;
0060     case Qt::DecorationRole:
0061         return QIcon::fromTheme(info.icon);
0062     case Qt::UserRole:
0063         return info.pluginId;
0064     }
0065 
0066     return QVariant();
0067 }
0068 
0069 Qt::ItemFlags ComicModel::flags(const QModelIndex &index) const
0070 {
0071     if (index.isValid() && (index.column() == 0)) {
0072         return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
0073     }
0074 
0075     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0076 }
0077 
0078 void ComicModel::load()
0079 {
0080     beginResetModel();
0081     mComics = mEngine->loadProviders();
0082     endResetModel();
0083 }