File indexing completed on 2024-03-24 05:53:47
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 Library 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 0021 #include "data.h" 0022 #include "availablegradesmodel.h" 0023 0024 #include <QFile> 0025 #include <QDebug> 0026 #include <QTextStream> 0027 #include <QTimer> 0028 #include <QCoreApplication> 0029 #include <KConfigGroup> 0030 0031 Data::Data(QObject *parent) 0032 : QObject(parent) 0033 { 0034 m_configSyncTimer = new QTimer(this); 0035 m_configSyncTimer->setSingleShot(false); 0036 connect(m_configSyncTimer, &QTimer::timeout, 0037 this, [this]() { 0038 config()->sync(); 0039 }); 0040 0041 KConfigGroup cg(config(), "General"); 0042 m_currentTab = cg.readEntry("currentTab", 0); 0043 m_leadAndBoulderLinked = cg.readEntry("leadAndBoulderLinked", false); 0044 0045 m_availableLeadModel = new AvailableGradesModel(this); 0046 m_availableLeadModel->load("lead"); 0047 m_availableBoulderModel = new AvailableGradesModel(this); 0048 m_availableBoulderModel->load("boulder"); 0049 0050 QFile file(":/data.csv"); 0051 0052 QString rawData; 0053 if (file.open(QIODevice::ReadOnly)) { 0054 QTextStream in(&file); 0055 rawData = in.readAll(); 0056 file.close(); 0057 } 0058 const QStringList lines = rawData.split('\n'); 0059 bool first = true; 0060 int line = 0; 0061 foreach (const QString &string, lines) { 0062 if (first) { 0063 first = false; 0064 foreach (const QString &scale, string.split(',')) { 0065 m_data[scale] = QVector<QString>(lines.count()); 0066 m_scales << scale; 0067 } 0068 } else { 0069 int i = 0; 0070 foreach (const QString &grade, string.split(',')) { 0071 if (i > m_data.size()) { 0072 break; 0073 } 0074 m_data[m_scales[i]][line] = grade; 0075 ++i; 0076 } 0077 ++line; 0078 } 0079 //qWarning()<<m_data; 0080 } 0081 } 0082 0083 Data::~Data() 0084 { 0085 config()->sync(); 0086 } 0087 0088 void Data::configNeedsSaving() 0089 { 0090 m_configSyncTimer->start(1000); 0091 } 0092 0093 KSharedConfigPtr Data::config() 0094 { 0095 if (!m_config) { 0096 m_config = KSharedConfig::openConfig("climbinggradesrc", KConfig::SimpleConfig); 0097 } 0098 0099 return m_config; 0100 } 0101 0102 AvailableGradesModel *Data::availableLeadModel() 0103 { 0104 return m_availableLeadModel; 0105 } 0106 0107 AvailableGradesModel *Data::availableBoulderModel() 0108 { 0109 return m_availableBoulderModel; 0110 } 0111 0112 0113 QString Data::gradeName(const QString &scale, int decimalGrade) const 0114 { 0115 const int position = qRound((qreal)decimalGrade/(qreal)2.0); 0116 0117 if (position < 0 || !m_data.contains(scale) || m_data[scale].size() <= position+1) { 0118 return QString(); 0119 } 0120 0121 return m_data[scale][position]; 0122 } 0123 0124 int Data::currentTab() const 0125 { 0126 return m_currentTab; 0127 } 0128 0129 void Data::setCurrentTab(int tab) 0130 { 0131 if (tab == m_currentTab) { 0132 return; 0133 } 0134 0135 m_currentTab = tab; 0136 0137 KConfigGroup cg(config(), "General"); 0138 cg.writeEntry("currentTab", tab); 0139 configNeedsSaving(); 0140 0141 emit currentTabChanged(); 0142 } 0143 0144 int Data::isLeadAndBoulderLinked() const 0145 { 0146 return m_leadAndBoulderLinked; 0147 } 0148 0149 void Data::setLeadAndBoulderLinked(bool linked) 0150 { 0151 if (linked == m_leadAndBoulderLinked) { 0152 return; 0153 } 0154 0155 m_leadAndBoulderLinked = linked; 0156 0157 KConfigGroup cg(config(), "General"); 0158 cg.writeEntry("leadAndBoulderLinked", linked); 0159 configNeedsSaving(); 0160 0161 emit leadAndBoulderLinkedChanged(); 0162 } 0163 0164 #include "moc_data.cpp" 0165