C
Qt Quick Ultralite swipe_game Demo
/****************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/#include "HighscoreModel.h" #include <algorithm> #include <cassert> HighscoreModel::HighscoreModel() { m_highscores.reserve(10); } int HighscoreModel::count() const { return m_highscores.size(); } Highscore HighscoreModel::data(int index) const { assert(index >= 0 && index < m_highscores.size()); return (index >= 0 && index < m_highscores.size()) ? m_highscores[index] : Highscore(); } static bool reverseOrder(const Highscore &lhs, const Highscore &rhs) { return lhs > rhs; } void HighscoreModel::addEntry(int time, int tries, int score) { Highscore newScore(time, tries, score); if (m_highscores.size() < HighscoreModel::maxSize) m_highscores.push_back(newScore); else if (m_highscores.back() < newScore) m_highscores.back() = newScore; std::sort(m_highscores.begin(), m_highscores.end(), &reverseOrder); modelReset(); }