Warning, file /utilities/telly-skout/src/groupsmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2022 Plata Hill <plata.hill@kdemail.net>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "groupsmodel.h"
0005 
0006 #include "database.h"
0007 #include "group.h"
0008 #include "types.h"
0009 
0010 #include <QDebug>
0011 
0012 #include <limits>
0013 
0014 GroupsModel::GroupsModel(QObject *parent)
0015     : QAbstractListModel(parent)
0016 {
0017     connect(&Database::instance(), &Database::groupAdded, this, [this]() {
0018         m_groupFactory.load();
0019         beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1);
0020         endInsertRows();
0021     });
0022 }
0023 
0024 QHash<int, QByteArray> GroupsModel::roleNames() const
0025 {
0026     QHash<int, QByteArray> roleNames;
0027     roleNames[0] = "group";
0028     return roleNames;
0029 }
0030 
0031 int GroupsModel::rowCount(const QModelIndex &parent) const
0032 {
0033     Q_UNUSED(parent)
0034     Q_ASSERT(m_groupFactory.count() <= std::numeric_limits<int>::max());
0035     return static_cast<int>(m_groupFactory.count());
0036 }
0037 
0038 QVariant GroupsModel::data(const QModelIndex &index, int role) const
0039 {
0040     if (role != 0) {
0041         return QVariant();
0042     }
0043     if (m_groups.length() <= index.row()) {
0044         loadGroup(index.row());
0045     }
0046     return QVariant::fromValue(m_groups[index.row()]);
0047 }
0048 
0049 void GroupsModel::loadGroup(int index) const
0050 {
0051     m_groups += m_groupFactory.create(index);
0052 }