#include "headers/filtermodel.h" FilterModel::FilterModel(QObject *parent ) : QAbstractListModel(parent) { m_filters.clear(); QList filtersList = pGlobalAppSettings->readFilters(); foreach(QStringList filterList, filtersList){ m_filters.append({filterList[0], filterList[1]}); } } int FilterModel::rowCount(const QModelIndex &) const { return m_filters.count(); } QVariant FilterModel::data(const QModelIndex &index, int role) const { if (index.row() < rowCount()) switch (role) { case GradeRole: return m_filters.at(index.row()).grade; case ClassLetterRole: return m_filters.at(index.row()).classLetter; default: return QVariant(); } return QVariant(); } QHash FilterModel::roleNames() const { static const QHash roles { { GradeRole, "grade" }, { ClassLetterRole, "classLetter" }, }; return roles; } QVariantMap FilterModel::get(int row) const { const Filter filter = m_filters.value(row); return { {"grade", filter.grade}, {"classLetter", filter.classLetter} }; } void FilterModel::append(const QString &grade, const QString &classLetter) { foreach(Filter filter, this->m_filters){ if(filter.grade == grade && filter.classLetter == classLetter){ // dublicates aren't allowed return; } } int row = 0; while (row < m_filters.count() && grade.toInt() > m_filters.at(row).grade.toInt()){ row++; } while (row < m_filters.count() && classLetter > m_filters.at(row).classLetter && grade.toInt() == m_filters.at(row).grade.toInt()) { row++; } beginInsertRows(QModelIndex(), row, row); m_filters.insert(row, {grade, classLetter}); endInsertRows(); QList filtersList; filtersList.clear(); foreach(Filter filter, this->m_filters){ filtersList.append({filter.grade, filter.classLetter}); } pGlobalAppSettings->writeFilters(filtersList); } void FilterModel::remove(int row) { if (row < 0 || row >= m_filters.count()) return; beginRemoveRows(QModelIndex(), row, row); m_filters.removeAt(row); endRemoveRows(); QList filtersList; filtersList.clear(); foreach(Filter filter, this->m_filters){ filtersList.append({filter.grade, filter.classLetter}); } pGlobalAppSettings->writeFilters(filtersList); }