7725
|
1 |
#include <QModelIndexList>
|
|
2 |
#include <QModelIndex>
|
|
3 |
#include <QPainter>
|
7727
|
4 |
#include <QDebug>
|
7725
|
5 |
|
7723
|
6 |
#include "playerslistmodel.h"
|
|
7 |
|
|
8 |
PlayersListModel::PlayersListModel(QObject *parent) :
|
7725
|
9 |
QAbstractListModel(parent)
|
7723
|
10 |
{
|
|
11 |
|
|
12 |
}
|
|
13 |
|
7725
|
14 |
|
|
15 |
int PlayersListModel::rowCount(const QModelIndex &parent) const
|
|
16 |
{
|
|
17 |
if(parent.isValid())
|
|
18 |
return 0;
|
|
19 |
else
|
|
20 |
return m_data.size();
|
|
21 |
}
|
|
22 |
|
|
23 |
|
|
24 |
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
|
|
25 |
{
|
7727
|
26 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
|
7725
|
27 |
return QVariant(QVariant::Invalid);
|
|
28 |
|
|
29 |
return m_data.at(index.row()).value(role);
|
|
30 |
}
|
|
31 |
|
|
32 |
|
|
33 |
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
34 |
{
|
|
35 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
|
|
36 |
return false;
|
|
37 |
|
|
38 |
m_data[index.row()].insert(role, value);
|
|
39 |
|
|
40 |
emit dataChanged(index, index);
|
|
41 |
|
|
42 |
return true;
|
|
43 |
}
|
|
44 |
|
|
45 |
|
|
46 |
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
|
|
47 |
{
|
|
48 |
return insertRows(row, 1, parent);
|
|
49 |
}
|
|
50 |
|
|
51 |
|
|
52 |
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
|
|
53 |
{
|
|
54 |
if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
|
|
55 |
return false;
|
|
56 |
|
|
57 |
beginInsertRows(parent, row, row + count - 1);
|
|
58 |
|
|
59 |
for(int i = 0; i < count; ++i)
|
|
60 |
m_data.insert(row, DataEntry());
|
|
61 |
|
|
62 |
endInsertRows();
|
|
63 |
|
|
64 |
return true;
|
|
65 |
}
|
|
66 |
|
|
67 |
|
|
68 |
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
69 |
{
|
|
70 |
if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
|
|
71 |
return false;
|
|
72 |
|
|
73 |
beginRemoveRows(parent, row, row + count - 1);
|
|
74 |
|
|
75 |
for(int i = 0; i < count; ++i)
|
|
76 |
m_data.removeAt(row);
|
|
77 |
|
|
78 |
endRemoveRows();
|
|
79 |
|
|
80 |
return true;
|
|
81 |
}
|
|
82 |
|
|
83 |
|
7723
|
84 |
void PlayersListModel::addPlayer(const QString & nickname)
|
|
85 |
{
|
7725
|
86 |
insertRow(rowCount());
|
7723
|
87 |
|
7728
|
88 |
QModelIndex mi = index(rowCount() - 1);
|
|
89 |
setData(mi, nickname);
|
7725
|
90 |
|
7728
|
91 |
updateSortData(mi);
|
|
92 |
updateIcon(mi);
|
7723
|
93 |
}
|
7725
|
94 |
|
|
95 |
|
|
96 |
void PlayersListModel::removePlayer(const QString & nickname)
|
|
97 |
{
|
7727
|
98 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
7725
|
99 |
|
|
100 |
if(mil.size())
|
|
101 |
removeRow(mil[0].row());
|
|
102 |
}
|
|
103 |
|
|
104 |
|
7731
|
105 |
void PlayersListModel::playerJoinedRoom(const QString & nickname)
|
|
106 |
{
|
|
107 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
|
108 |
|
|
109 |
if(mil.size())
|
|
110 |
setData(mil[0], "1", RoomFilterRole);
|
|
111 |
}
|
|
112 |
|
|
113 |
|
|
114 |
void PlayersListModel::playerLeftRoom(const QString & nickname)
|
|
115 |
{
|
|
116 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
|
117 |
|
|
118 |
if(mil.size())
|
|
119 |
setData(mil[0], "0", RoomFilterRole);
|
|
120 |
}
|
|
121 |
|
|
122 |
|
7725
|
123 |
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
|
|
124 |
{
|
|
125 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
|
126 |
|
|
127 |
if(mil.size())
|
|
128 |
{
|
7727
|
129 |
setData(mil[0], isSet, flagType);
|
7728
|
130 |
|
|
131 |
if(flagType == Friend || flagType == ServerAdmin
|
|
132 |
|| flagType == Ignore || flagType == RoomAdmin)
|
|
133 |
updateSortData(mil[0]);
|
|
134 |
|
7725
|
135 |
updateIcon(mil[0]);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
|
7731
|
140 |
void PlayersListModel::resetRoomFlags()
|
|
141 |
{
|
|
142 |
for(int i = rowCount() - 1; i >= 0; --i)
|
|
143 |
{
|
|
144 |
QModelIndex mi = index(i);
|
|
145 |
|
|
146 |
if(mi.data(RoomFilterRole).toString() == "1")
|
|
147 |
{
|
|
148 |
setData(mi, "0", RoomFilterRole);
|
|
149 |
setData(mi, false, RoomAdmin);
|
|
150 |
setData(mi, false, Ready);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
7725
|
155 |
void PlayersListModel::updateIcon(const QModelIndex & index)
|
|
156 |
{
|
|
157 |
quint32 iconNum = 0;
|
|
158 |
|
|
159 |
QList<bool> flags;
|
|
160 |
flags
|
|
161 |
<< index.data(Ready).toBool()
|
|
162 |
<< index.data(ServerAdmin).toBool()
|
|
163 |
<< index.data(RoomAdmin).toBool()
|
|
164 |
<< index.data(Registered).toBool()
|
|
165 |
<< index.data(Friend).toBool()
|
|
166 |
<< index.data(Ignore).toBool()
|
|
167 |
;
|
|
168 |
|
|
169 |
for(int i = flags.size() - 1; i >= 0; --i)
|
|
170 |
if(flags[i])
|
|
171 |
iconNum |= 1 << i;
|
|
172 |
|
|
173 |
if(m_icons().contains(iconNum))
|
|
174 |
{
|
|
175 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole);
|
|
176 |
}
|
|
177 |
else
|
|
178 |
{
|
|
179 |
QPixmap result(24, 16);
|
|
180 |
result.fill(Qt::transparent);
|
|
181 |
|
|
182 |
QPainter painter(&result);
|
|
183 |
|
|
184 |
if(index.data(Ready).toBool())
|
|
185 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
|
|
186 |
|
|
187 |
QString mainIconName(":/res/chat/");
|
|
188 |
|
|
189 |
if(index.data(RoomAdmin).toBool())
|
|
190 |
mainIconName += "roomadmin";
|
|
191 |
else if(index.data(ServerAdmin).toBool())
|
|
192 |
mainIconName += "serveradmin";
|
|
193 |
else
|
|
194 |
mainIconName += "hedgehog";
|
|
195 |
|
|
196 |
if(!index.data(Registered).toBool())
|
|
197 |
mainIconName += "_gray";
|
|
198 |
|
|
199 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
|
|
200 |
|
|
201 |
if(index.data(Ignore).toBool())
|
|
202 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
|
|
203 |
else
|
|
204 |
if(index.data(Friend).toBool())
|
|
205 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
|
|
206 |
|
|
207 |
painter.end();
|
|
208 |
|
|
209 |
QIcon icon(result);
|
|
210 |
|
|
211 |
setData(index, icon, Qt::DecorationRole);
|
|
212 |
m_icons().insert(iconNum, icon);
|
|
213 |
}
|
|
214 |
|
|
215 |
if(index.data(Ignore).toBool())
|
|
216 |
setData(index, Qt::gray, Qt::ForegroundRole);
|
|
217 |
else
|
|
218 |
if(index.data(Friend).toBool())
|
|
219 |
setData(index, Qt::green, Qt::ForegroundRole);
|
|
220 |
else
|
|
221 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
|
|
222 |
}
|
|
223 |
|
|
224 |
|
|
225 |
QHash<quint32, QIcon> & PlayersListModel::m_icons()
|
|
226 |
{
|
|
227 |
static QHash<quint32, QIcon> iconsCache;
|
|
228 |
|
|
229 |
return iconsCache;
|
|
230 |
}
|
7728
|
231 |
|
|
232 |
void PlayersListModel::updateSortData(const QModelIndex & index)
|
|
233 |
{
|
|
234 |
QString result = QString("%1%2%3%4%5")
|
|
235 |
.arg(1 - index.data(RoomAdmin).toInt())
|
|
236 |
.arg(1 - index.data(ServerAdmin).toInt())
|
|
237 |
.arg(1 - index.data(Friend).toInt())
|
|
238 |
.arg(index.data(Ignore).toInt())
|
|
239 |
.arg(index.data(Qt::DisplayRole).toString().toLower())
|
|
240 |
;
|
|
241 |
|
|
242 |
setData(index, result, SortRole);
|
|
243 |
}
|