-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonstermodel.cpp
132 lines (117 loc) · 4.46 KB
/
monstermodel.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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 "monstermodel.h"
MonsterModel::MonsterModel(QObject *parent) :
QAbstractListModel(parent)
{
}
QList<Monster *> MonsterModel::monsters()
{
return m_monsters;
}
Monster *MonsterModel::monsterWithId(int id)
{
foreach (Monster *monster, m_monsters) {
if (monster->id() == id)
return monster;
}
return NULL;
}
void MonsterModel::impact(Monster *monster, AttackPillow *pillow)
{
monster->impact(pillow);
}
int MonsterModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_monsters.count();
}
QVariant MonsterModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_monsters.count())
return QVariant();
Monster *monster= m_monsters.at(index.row());
if (role == IdRole) {
return monster->id();
} else if (role == ValueRole) {
return monster->value();
} else if (role == ColorStringRole) {
return monster->colorString();
} else if (role == MonsterTypeRole) {
return monster->monsterTypeString();
} else if (role == SizeRole) {
return monster->size();
} else if (role == PositionXRole) {
return monster->position().x();
} else if (role == PositionYRole) {
return monster->position().y();
} else if (role == SelectedRole) {
return monster->selected();
}
return QVariant();
}
void MonsterModel::addMonster(Monster *monster)
{
beginInsertRows(QModelIndex(), m_monsters.count(), m_monsters.count());
m_monsters.append(monster);
connect(monster, &Monster::valueChanged, this, &MonsterModel::monsterValueChanged);
connect(monster, &Monster::selectedChanged, this, &MonsterModel::monsterSelectedChanged);
connect(monster, &Monster::colorStringChanged, this, &MonsterModel::monsterColorChanged);
endInsertRows();
QModelIndex i = index(m_monsters.indexOf(monster));
emit dataChanged(i, i);
}
void MonsterModel::clearModel()
{
beginResetModel();
m_monsters.clear();
endResetModel();
}
QHash<int, QByteArray> MonsterModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "monsterId";
roles[ValueRole] = "monsterValue";
roles[ColorStringRole] = "monsterColor";
roles[MonsterTypeRole] = "monsterType";
roles[SizeRole] = "monsterSize";
roles[PositionXRole] = "positionX";
roles[PositionYRole] = "positionY";
roles[SelectedRole] = "selected";
return roles;
}
void MonsterModel::monsterValueChanged()
{
Monster *monster = qobject_cast<Monster *>(sender());
QModelIndex i = index(m_monsters.indexOf(monster));
emit dataChanged(i, i, {ValueRole});
}
void MonsterModel::monsterColorChanged()
{
Monster *monster = qobject_cast<Monster *>(sender());
QModelIndex i = index(m_monsters.indexOf(monster));
emit dataChanged(i, i, {ColorStringRole});
}
void MonsterModel::monsterSelectedChanged()
{
Monster *monster = qobject_cast<Monster *>(sender());
QModelIndex i = index(m_monsters.indexOf(monster));
emit dataChanged(i, i, {SelectedRole});
}