2018-12-25 15:16:24 +01:00
|
|
|
#include "headers/filtermodel.h"
|
|
|
|
|
|
|
|
FilterModel::FilterModel(QObject *parent ) : QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
m_filters.clear();
|
|
|
|
|
|
|
|
QList<QStringList> filtersList = pGlobalAppSettings->readFilters();
|
|
|
|
|
|
|
|
foreach(QStringList filterList, filtersList){
|
2018-12-28 11:37:06 +01:00
|
|
|
m_filters.append({filterList[0], filterList[1], filterList[2]});
|
2018-12-25 15:16:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-12-28 11:37:06 +01:00
|
|
|
case RoleRole: return m_filters.at(index.row()).role;
|
2018-12-25 15:16:24 +01:00
|
|
|
default: return QVariant();
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FilterModel::roleNames() const
|
|
|
|
{
|
|
|
|
static const QHash<int, QByteArray> roles {
|
|
|
|
{ GradeRole, "grade" },
|
|
|
|
{ ClassLetterRole, "classLetter" },
|
2018-12-28 11:37:06 +01:00
|
|
|
{ RoleRole, "role"}
|
2018-12-25 15:16:24 +01:00
|
|
|
};
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap FilterModel::get(int row) const
|
|
|
|
{
|
|
|
|
const Filter filter = m_filters.value(row);
|
2018-12-28 11:37:06 +01:00
|
|
|
return { {"grade", filter.grade}, {"classLetter", filter.classLetter}, {"role", filter.role} };
|
2018-12-25 15:16:24 +01:00
|
|
|
}
|
|
|
|
|
2018-12-28 11:37:06 +01:00
|
|
|
void FilterModel::append(const QString &grade, const QString &classLetter, const QString &role)
|
2018-12-25 15:16:24 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
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);
|
2018-12-28 11:37:06 +01:00
|
|
|
m_filters.insert(row, {grade, classLetter, role});
|
2018-12-25 15:16:24 +01:00
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
QList<QStringList> filtersList;
|
|
|
|
filtersList.clear();
|
|
|
|
|
|
|
|
foreach(Filter filter, this->m_filters){
|
2018-12-28 11:37:06 +01:00
|
|
|
filtersList.append({filter.grade, filter.classLetter, filter.role});
|
2018-12-25 15:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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<QStringList> filtersList;
|
|
|
|
filtersList.clear();
|
|
|
|
|
|
|
|
foreach(Filter filter, this->m_filters){
|
2018-12-28 11:37:06 +01:00
|
|
|
filtersList.append({filter.grade, filter.classLetter, filter.role});
|
2018-12-25 15:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pGlobalAppSettings->writeFilters(filtersList);
|
|
|
|
}
|