forked from t-mon/monsterwars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelmodel.cpp
137 lines (121 loc) · 4.21 KB
/
levelmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <[email protected]> *
* *
* This file is part of Monster Wars. *
* *
* Monster Wars is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 3 of the License. *
* *
* Monster Wars is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Monster Wars. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "levelmodel.h"
LevelModel::LevelModel(QObject *parent) :
QAbstractListModel(parent)
{
}
QList<Level *> LevelModel::levels()
{
return m_levels;
}
Level *LevelModel::level(int levelId)
{
foreach (Level *level, m_levels) {
if (level->levelId() == levelId)
return level;
}
return NULL;
}
int LevelModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_levels.count();
}
QVariant LevelModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_levels.count())
return QVariant();
Level *level= m_levels.at(index.row());
if (role == IdRole) {
return level->levelId();
} else if (role == TimeRole) {
return level->bestTime();
} else if (role == NameRole) {
return level->name();
} else if (role == UnlockedRole) {
return level->unlocked();
}
return QVariant();
}
void LevelModel::addLevel(Level *level)
{
beginInsertRows(QModelIndex(), m_levels.count(), m_levels.count());
m_levels.append(level);
connect(level, &Level::unlockedChanged, this, &LevelModel::unlockedChanged);
connect(level, &Level::bestTimeChanged, this, &LevelModel::bestTimeChanged);
endInsertRows();
QModelIndex i = index(m_levels.indexOf(level));
emit dataChanged(i, i);
}
void LevelModel::resetLevelSettings()
{
beginResetModel();
QSettings settings;
foreach (Level *level, m_levels) {
settings.beginGroup(level->name());
settings.setValue("timeStamp", 0);
level->setTimeStamp(0);
if (level->levelId() == 1) {
settings.setValue("unlocked", true);
level->setUnlocked(true);
} else {
settings.setValue("unlocked", false);
level->setUnlocked(false);
}
settings.endGroup();
}
endResetModel();
}
void LevelModel::sortLevels()
{
beginResetModel();
qSort(m_levels.begin(), m_levels.end(), compareLevel);
endResetModel();
}
QHash<int, QByteArray> LevelModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "levelId";
roles[TimeRole] = "bestTime";
roles[NameRole] = "levelName";
roles[UnlockedRole] = "unlocked";
return roles;
}
int LevelModel::indexOf(Level *level)
{
return m_levels.indexOf(level);
}
void LevelModel::unlockedChanged()
{
Level *level = qobject_cast<Level *>(sender());
QModelIndex i = index(m_levels.indexOf(level));
emit dataChanged(i, i, {UnlockedRole});
}
void LevelModel::bestTimeChanged()
{
Level *level = qobject_cast<Level *>(sender());
QModelIndex i = index(m_levels.indexOf(level));
emit dataChanged(i, i, {TimeRole});
}
bool compareLevel(Level *level1, Level *level2)
{
return level1->levelId() < level2->levelId();
}