1 /* ColorCode, a free MasterMind clone with built in solver
2  * Copyright (C) 2009  Dirk Laebisch
3  * http://www.laebisch.com/
4  *
5  * ColorCode is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * ColorCode is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "highscoresmodel.h"
20 #include "gametableview.h"
21 
GetHighScoresModel()22 HighScoresModel* GetHighScoresModel()
23 {
24     static HighScoresModel* m = new HighScoresModel();
25     return m;
26 }
27 
HighScoresModel(QObject * parent)28 HighScoresModel::HighScoresModel(QObject* parent) : GamesListModel(parent)
29 {
30     mId = MODEL_ID_HIGH;
31     mEditIndex = QModelIndex();
32     mMaxSize = 20;
33 }
34 
IsValidHighScore(const int score) const35 bool HighScoresModel::IsValidHighScore(const int score) const
36 {
37     if (mGamesList.size() < mMaxSize || mGamesList.last().mScore < score)
38     {
39         return true;
40     }
41     return false;
42 }
43 
InsertRow(CCGame g)44 void HighScoresModel::InsertRow(CCGame g)
45 {
46     if (mEditIndex.isValid())
47     {
48         emit CloseEditorSignal(mEditIndex);
49     }
50     mView->clearSelection();
51 
52     beginInsertRows(QModelIndex(), rowCount(), rowCount());
53     mGamesList.push_back(g);
54     endInsertRows();
55 
56     QModelIndex mix = index(rowCount() - 1, GetColIx(COL_DELETE), QModelIndex());
57     mView->openPersistentEditor(mix);
58 
59     DoSort(mGamesList);
60     LimitGamesListSize();
61     int ix = mGamesList.indexOf(g);
62     emit layoutChanged();
63     if (ix > -1)
64     {
65         mEditIndex = createIndex(ix, GetColIx(COL_USERNAME));
66         EditField();
67     }
68 }
69 
GetColIx(const int ix) const70 int HighScoresModel::GetColIx(const int ix) const
71 {
72     int ret = -1;
73     if (ix == COL_RANKING)
74     {
75         ret = 0;
76     }
77     else if (ix == COL_SCORE)
78     {
79         ret = 1;
80     }
81     else if (ix == COL_USERNAME)
82     {
83         ret = 2;
84     }
85     else if (ix == COL_DELETE)
86     {
87         ret = 3;
88     }
89     return ret;
90 }
91 
GetMaxColCnt() const92 int HighScoresModel::GetMaxColCnt() const
93 {
94     return 4;
95 }
96 
DoSort(QList<CCGame> & list)97 void HighScoresModel::DoSort(QList<CCGame> &list)
98 {
99     qSort(list.begin(), list.end(), GamesListModel::SortListScore);
100 }
101