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

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Reza Fatahilah Shah <rshah0385@kireihana.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "activecomicmodel.h"
0008 
0009 ActiveComicModel::ActiveComicModel(QObject *parent)
0010     : QStandardItemModel(0, 1, parent)
0011 {
0012     connect(this, &ActiveComicModel::modelReset, this, &ActiveComicModel::countChanged);
0013     connect(this, &ActiveComicModel::rowsInserted, this, &ActiveComicModel::countChanged);
0014     connect(this, &ActiveComicModel::rowsRemoved, this, &ActiveComicModel::countChanged);
0015 }
0016 
0017 QHash<int, QByteArray> ActiveComicModel::roleNames() const
0018 {
0019     auto roleNames = QStandardItemModel::roleNames();
0020     roleNames.insert(ComicKeyRole, "key");
0021     roleNames.insert(ComicTitleRole, "title");
0022     roleNames.insert(ComicIconRole, "icon");
0023     roleNames.insert(ComicHighlightRole, "highlight");
0024 
0025     return roleNames;
0026 }
0027 
0028 void ActiveComicModel::addComic(const QString &key, const QString &title, const QIcon &icon, bool highlight)
0029 {
0030     QList<QStandardItem *> newRow;
0031     QStandardItem *item = new QStandardItem(title);
0032 
0033     item->setData(key, ComicKeyRole);
0034     item->setData(title, ComicTitleRole);
0035     item->setData(icon, ComicIconRole);
0036     item->setData(highlight, ComicHighlightRole);
0037 
0038     newRow << item;
0039     appendRow(newRow);
0040 }
0041 
0042 QVariantHash ActiveComicModel::get(int row) const
0043 {
0044     QModelIndex idx = index(row, 0);
0045     QVariantHash hash;
0046 
0047     const auto roleNames = this->roleNames();
0048     hash.reserve(roleNames.size());
0049     for (auto end = roleNames.constEnd(), it = roleNames.constBegin(); it != end; ++it) {
0050         hash.insert(QString::fromUtf8(it.value()), data(idx, it.key()));
0051     }
0052 
0053     return hash;
0054 }