File indexing completed on 2022-09-20 16:26:43

0001 /*
0002  *   Copyright 2016 Marco Martin <mart@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU Library General Public License as
0006  *   published by the Free Software Foundation; either version 2, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details
0013  *
0014  *   You should have received a copy of the GNU Library General Public
0015  *   License along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 
0020 #include "availablegradesmodel.h"
0021 #include "data.h"
0022 
0023 #include <QCoreApplication>
0024 #include <QDebug>
0025 #include <QByteArray>
0026 #include <QDir>
0027 #include <QDirIterator>
0028 #include <QFile>
0029 #include <QStandardPaths>
0030 #include <QSettings>
0031 
0032 #include <KConfigGroup>
0033 
0034 
0035 AvailableGradesModel::AvailableGradesModel(Data *parent)
0036     : QAbstractListModel(parent),
0037       m_data(parent),
0038       m_personalRecord(0)
0039 {
0040     m_roleNames.insert(NameRole, "nameRole");
0041     m_roleNames.insert(EnabledRole, "enabledRole");
0042     m_roleNames.insert(DescriptionRole, "descriptionRole");
0043     m_roleNames.insert(UrlRole, "urlRole");
0044 }
0045 
0046 AvailableGradesModel::~AvailableGradesModel()
0047 {
0048 }
0049 
0050 QHash<int, QByteArray> AvailableGradesModel::roleNames() const
0051 {
0052     return m_roleNames;
0053 }
0054 
0055 int AvailableGradesModel::rowCount(const QModelIndex &parent) const
0056 {
0057     Q_UNUSED(parent)
0058     return m_jsonDoc.array().size();
0059 }
0060 
0061 void AvailableGradesModel::setScaleEnabled(int row, bool enabled)
0062 {
0063     if (row < 0 || row > m_jsonDoc.array().size()) {
0064         return;
0065     }
0066 
0067     KConfigGroup cg(m_data->config(), "General");
0068     const QVariantMap value = m_jsonDoc.array().at(row).toObject().toVariantMap();
0069     cg.writeEntry(value.value("name").toString() + "-enabled", enabled);
0070     m_data->configNeedsSaving();
0071 
0072     emit dataChanged(index(row), index(row));
0073 }
0074 
0075 QVariant AvailableGradesModel::data(const QModelIndex &index, int role) const
0076 {
0077     if (!index.isValid() || index.row() < 0 || index.row() > m_jsonDoc.array().size()) {
0078         return QVariant();
0079     }
0080 
0081     const QVariantMap value = m_jsonDoc.array().at(index.row()).toObject().toVariantMap();
0082 
0083     switch (role) {
0084     case NameRole:
0085         return value.value("name");
0086     case EnabledRole: {
0087         KConfigGroup cg(m_data->config(), "General");
0088         return cg.readEntry(value.value("name").toString() + "-enabled", true);
0089     }
0090     case DescriptionRole:
0091         return value.value("description");
0092     case UrlRole:
0093         return value.value("url");
0094     }
0095 
0096     return QVariant();
0097 }
0098 
0099 
0100 void AvailableGradesModel::load(const QString &dataName)
0101 {
0102     beginResetModel();
0103 
0104     m_dataName = dataName;
0105     QFile jsonFile(":/" + dataName + "scales.json");
0106     jsonFile.open(QIODevice::ReadOnly);
0107 
0108     QJsonParseError error;
0109     m_jsonDoc = QJsonDocument::fromJson(jsonFile.readAll(), &error);
0110 
0111     if (error.error != QJsonParseError::NoError) {
0112         qWarning() << "Error parsing Json" << error.errorString();
0113     }
0114 
0115     endResetModel();
0116 
0117     KConfigGroup cg(m_data->config(), m_dataName);
0118     m_personalRecord = cg.readEntry("personalRecord", 0);
0119     m_currentGrade = cg.readEntry("currentGrade", 50);
0120 
0121     emit personalRecordChanged();
0122 }
0123 
0124 int AvailableGradesModel::personalRecord() const
0125 {
0126     return m_personalRecord;
0127 }
0128 
0129 void AvailableGradesModel::setPersonalRecord(int record)
0130 {
0131     if (record == m_personalRecord) {
0132         return;
0133     }
0134 
0135     m_personalRecord = record;
0136 
0137     KConfigGroup cg(m_data->config(), m_dataName);
0138     cg.writeEntry("personalRecord", record);
0139     m_data->configNeedsSaving();
0140 
0141     emit personalRecordChanged();
0142 }
0143 
0144 int AvailableGradesModel::currentGrade() const
0145 {
0146     return m_currentGrade;
0147 }
0148 
0149 void AvailableGradesModel::setCurrentGrade(int tab)
0150 {
0151     if (tab == m_currentGrade) {
0152         return;
0153     }
0154 
0155     m_currentGrade = tab;
0156 
0157     KConfigGroup cg(m_data->config(), m_dataName);
0158     cg.writeEntry("currentGrade", tab);
0159     m_data->configNeedsSaving();
0160 
0161     emit currentGradeChanged();
0162 }
0163 
0164 #include "moc_availablegradesmodel.cpp"