File indexing completed on 2024-04-21 04:03:42

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 
0026 #include "highscore.h"
0027 
0028 HighScore::HighScore(QObject* parent) : QObject(parent), m_size(-1)
0029 {
0030 }
0031 
0032 QString HighScore::settingsStringForSize(int size) // static
0033 {
0034     return QStringLiteral("HighScore/size_%1").arg(size);
0035 }
0036 
0037 
0038 void HighScore::setSize(int size) {
0039     if (size != m_size) {
0040         m_size = size;
0041         emit highscoreChanged();
0042     }
0043 }
0044 
0045 int HighScore::size() const
0046 {
0047     return m_size;
0048 }
0049 
0050 int HighScore::highscore() const
0051 {
0052     if (m_size < 1 ) {
0053         return -1;
0054     }
0055     int highscore = m_settings.value(settingsStringForSize(m_size),-1).toInt();
0056     if (highscore < 1) {
0057         return -1;
0058     }
0059     return highscore;
0060 }
0061 
0062 void HighScore::setHighscore(int highscore)
0063 {
0064     if (m_size < 1 ) {
0065         return;
0066     }
0067     m_settings.setValue(settingsStringForSize(m_size), highscore);
0068     m_settings.sync();
0069     emit highscoreChanged();
0070 }
0071 
0072 QVariantList HighScore::allHighscores() const
0073 {
0074     QVariantList result;
0075     for(int i = 0; i < 100 ; i++ ) {
0076         auto string = settingsStringForSize(i);
0077         if (m_settings.contains(string)) {
0078             result << QVariant::fromValue(HighScoreElement(i,m_settings.value(string).toInt()));
0079         }
0080     }
0081     return result;
0082 
0083 }
0084 
0085 #include "moc_highscore.cpp"