File indexing completed on 2024-12-22 05:15:23
0001 /* SPDX-FileCopyrightText: 2023 Noah Davis <noahadvs@gmail.com> 0002 * SPDX-FileCopyrightText: 2023 Tanbir Jishan <tantalising007@gmail.com> 0003 * SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "sectionsmodel.h" 0007 0008 SectionsModel::SectionsModel(QObject *parent) 0009 : QAbstractListModel(parent) 0010 { 0011 m_roleNames[Qt::DisplayRole] = QByteArrayLiteral("section"); 0012 m_roleNames[FirstIndexRole] = QByteArrayLiteral("firstIndex"); 0013 } 0014 0015 QHash<int, QByteArray> SectionsModel::roleNames() const 0016 { 0017 return m_roleNames; 0018 } 0019 0020 QVariant SectionsModel::data(const QModelIndex &index, int role) const 0021 { 0022 int row = index.row(); 0023 QVariant ret; 0024 if (!checkIndex(index, CheckIndexOption::IndexIsValid)) { 0025 return ret; 0026 } 0027 if (role == Qt::DisplayRole) { 0028 ret = m_data.at(row).section; 0029 } else if (role == FirstIndexRole) { 0030 ret = m_data.at(row).firstIndex; 0031 } 0032 return ret; 0033 } 0034 0035 int SectionsModel::rowCount(const QModelIndex &parent) const 0036 { 0037 Q_UNUSED(parent) 0038 return m_data.size(); 0039 } 0040 0041 void SectionsModel::clear() 0042 { 0043 m_data.clear(); 0044 } 0045 0046 void SectionsModel::append(const QString §ion, int firstIndex) 0047 { 0048 m_data.append({section, firstIndex}); 0049 } 0050 0051 QString SectionsModel::lastSection() const 0052 { 0053 Q_ASSERT(!m_data.empty()); 0054 return m_data.constLast().section; 0055 }