File indexing completed on 2024-09-08 03:45:45
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 #pragma once 0026 #include <QObject> 0027 #include <QSettings> 0028 #include <QVariantMap> 0029 0030 /** 0031 * Helper class representing a element in the high score list 0032 */ 0033 class HighScoreElement { 0034 Q_GADGET 0035 Q_PROPERTY(int size READ size) 0036 Q_PROPERTY(int highscore READ highscore) 0037 int m_size; 0038 int m_score; 0039 public: 0040 HighScoreElement(int size, int score) : m_size(size), m_score(score) {} 0041 HighScoreElement() : m_size(-1), m_score(-1) {} 0042 int size() const { 0043 return m_size; 0044 } 0045 int highscore() const { 0046 return m_score; 0047 } 0048 }; 0049 Q_DECLARE_METATYPE(HighScoreElement) 0050 0051 /** 0052 * High score class 0053 * 0054 * Uses QSettings as a backend and stores the best result for each 0055 * board size. 0056 */ 0057 class HighScore : public QObject 0058 { 0059 Q_OBJECT 0060 /** The current board size that the highscore is handled for */ 0061 Q_PROPERTY(int size READ size WRITE setSize NOTIFY highscoreChanged) 0062 /** The highscore for the selected board size, or -1 if there is no high score*/ 0063 Q_PROPERTY(int highscore READ highscore WRITE setHighscore NOTIFY highscoreChanged) 0064 /** All highscores as a list of HighScoreElement*/ 0065 Q_PROPERTY(QVariantList allHighscores READ allHighscores NOTIFY highscoreChanged) 0066 int m_size; 0067 QSettings m_settings; 0068 static QString settingsStringForSize(int size); 0069 public: 0070 HighScore(QObject* parent = nullptr); 0071 0072 int size() const; 0073 void setSize(int size); 0074 int highscore() const; 0075 void setHighscore(int highscore); 0076 /** 0077 * List of high score elements 0078 */ 0079 QVariantList allHighscores() const; 0080 0081 Q_SIGNALS: 0082 void highscoreChanged(); 0083 0084 };