-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelmodel.cpp
More file actions
83 lines (66 loc) · 1.48 KB
/
levelmodel.cpp
File metadata and controls
83 lines (66 loc) · 1.48 KB
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
#include "levelmodel.h"
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lvlm, "app.models.levelmodel")
LevelModel::LevelModel(QObject *parent)
: QAbstractListModel { parent }
{
}
int LevelModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data.count();
}
QVariant LevelModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_data.count())
return QVariant();
const LevelDescription &data = m_data[index.row()];
switch (role) {
case Name:
return data.name;
case Preview:
return data.preview;
case Path:
return data.path;
}
return QVariant();
}
QHash<int, QByteArray> LevelModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Name] = "name";
roles[Preview] = "preview";
roles[Path] = "path";
return roles;
}
void LevelModel::addLevel(const LevelDescription &level)
{
beginInsertRows(QModelIndex(), m_data.count(), m_data.count());
m_data << level;
endInsertRows();
}
void LevelModel::clear()
{
beginResetModel();
m_data.clear();
endResetModel();
}
LevelDescription LevelModel::levelAtIndex(int index) const
{
return m_data.at(index);
}
int LevelModel::indexOf(const QString &name) const
{
for (int index = 0; index < m_data.count(); index++) {
if (m_data.at(index).name == name) {
return index;
}
}
return -1;
}
void LevelModel::removeLevel(int index)
{
beginRemoveRows(QModelIndex(), index, index);
m_data.remove(index);
endRemoveRows();
}