author | koda |
Sun, 11 Nov 2012 16:44:40 +0100 | |
branch | webgl |
changeset 8020 | 00b1facf2805 |
parent 7842 | d1c0e4341165 |
child 8891 | bf67b4d7d7b4 |
permissions | -rw-r--r-- |
7725 | 1 |
#include <QModelIndexList> |
2 |
#include <QModelIndex> |
|
3 |
#include <QPainter> |
|
7732 | 4 |
#include <QFile> |
5 |
#include <QTextStream> |
|
7727 | 6 |
#include <QDebug> |
7725 | 7 |
|
7723 | 8 |
#include "playerslistmodel.h" |
7732 | 9 |
#include "hwconsts.h" |
7723 | 10 |
|
11 |
PlayersListModel::PlayersListModel(QObject *parent) : |
|
7725 | 12 |
QAbstractListModel(parent) |
7723 | 13 |
{ |
14 |
||
15 |
} |
|
16 |
||
7725 | 17 |
|
18 |
int PlayersListModel::rowCount(const QModelIndex &parent) const |
|
19 |
{ |
|
20 |
if(parent.isValid()) |
|
21 |
return 0; |
|
22 |
else |
|
23 |
return m_data.size(); |
|
24 |
} |
|
25 |
||
26 |
||
27 |
QVariant PlayersListModel::data(const QModelIndex &index, int role) const |
|
28 |
{ |
|
7727 | 29 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0) |
7725 | 30 |
return QVariant(QVariant::Invalid); |
31 |
||
32 |
return m_data.at(index.row()).value(role); |
|
33 |
} |
|
34 |
||
35 |
||
36 |
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
37 |
{ |
|
38 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0) |
|
39 |
return false; |
|
40 |
||
41 |
m_data[index.row()].insert(role, value); |
|
42 |
||
43 |
emit dataChanged(index, index); |
|
44 |
||
45 |
return true; |
|
46 |
} |
|
47 |
||
48 |
||
49 |
bool PlayersListModel::insertRow(int row, const QModelIndex &parent) |
|
50 |
{ |
|
51 |
return insertRows(row, 1, parent); |
|
52 |
} |
|
53 |
||
54 |
||
55 |
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent) |
|
56 |
{ |
|
57 |
if(parent.isValid() || row > rowCount() || row < 0 || count < 1) |
|
58 |
return false; |
|
59 |
||
60 |
beginInsertRows(parent, row, row + count - 1); |
|
61 |
||
62 |
for(int i = 0; i < count; ++i) |
|
63 |
m_data.insert(row, DataEntry()); |
|
64 |
||
65 |
endInsertRows(); |
|
66 |
||
67 |
return true; |
|
68 |
} |
|
69 |
||
70 |
||
71 |
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent) |
|
72 |
{ |
|
73 |
if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1) |
|
74 |
return false; |
|
75 |
||
76 |
beginRemoveRows(parent, row, row + count - 1); |
|
77 |
||
78 |
for(int i = 0; i < count; ++i) |
|
79 |
m_data.removeAt(row); |
|
80 |
||
81 |
endRemoveRows(); |
|
82 |
||
83 |
return true; |
|
84 |
} |
|
85 |
||
86 |
||
7723 | 87 |
void PlayersListModel::addPlayer(const QString & nickname) |
88 |
{ |
|
7725 | 89 |
insertRow(rowCount()); |
7723 | 90 |
|
7728 | 91 |
QModelIndex mi = index(rowCount() - 1); |
92 |
setData(mi, nickname); |
|
7725 | 93 |
|
7732 | 94 |
checkFriendIgnore(mi); |
7723 | 95 |
} |
7725 | 96 |
|
97 |
||
98 |
void PlayersListModel::removePlayer(const QString & nickname) |
|
99 |
{ |
|
7764 | 100 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
7725 | 101 |
|
102 |
if(mil.size()) |
|
103 |
removeRow(mil[0].row()); |
|
104 |
} |
|
105 |
||
106 |
||
7731 | 107 |
void PlayersListModel::playerJoinedRoom(const QString & nickname) |
108 |
{ |
|
7764 | 109 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
7731 | 110 |
|
111 |
if(mil.size()) |
|
7764 | 112 |
{ |
7833 | 113 |
setData(mil[0], true, RoomFilterRole); |
7764 | 114 |
updateIcon(mil[0]); |
7833 | 115 |
updateSortData(mil[0]); |
7764 | 116 |
} |
7731 | 117 |
} |
118 |
||
119 |
||
120 |
void PlayersListModel::playerLeftRoom(const QString & nickname) |
|
121 |
{ |
|
7764 | 122 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
7731 | 123 |
|
124 |
if(mil.size()) |
|
7764 | 125 |
{ |
7833 | 126 |
setData(mil[0], false, RoomFilterRole); |
127 |
setData(mil[0], false, RoomAdmin); |
|
128 |
setData(mil[0], false, Ready); |
|
129 |
setData(mil[0], false, InGame); |
|
7764 | 130 |
updateIcon(mil[0]); |
131 |
} |
|
7731 | 132 |
} |
133 |
||
134 |
||
7725 | 135 |
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet) |
136 |
{ |
|
7764 | 137 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
7725 | 138 |
|
139 |
if(mil.size()) |
|
140 |
{ |
|
7727 | 141 |
setData(mil[0], isSet, flagType); |
7728 | 142 |
|
143 |
if(flagType == Friend || flagType == ServerAdmin |
|
144 |
|| flagType == Ignore || flagType == RoomAdmin) |
|
145 |
updateSortData(mil[0]); |
|
146 |
||
7737 | 147 |
if(flagType == Friend) |
148 |
{ |
|
149 |
if(isSet) |
|
150 |
m_friendsSet.insert(nickname.toLower()); |
|
151 |
else |
|
152 |
m_friendsSet.remove(nickname.toLower()); |
|
153 |
||
154 |
saveSet(m_friendsSet, "friends"); |
|
155 |
} |
|
156 |
||
157 |
if(flagType == Ignore) |
|
158 |
{ |
|
159 |
if(isSet) |
|
160 |
m_ignoredSet.insert(nickname.toLower()); |
|
161 |
else |
|
162 |
m_ignoredSet.remove(nickname.toLower()); |
|
163 |
||
164 |
saveSet(m_ignoredSet, "ignore"); |
|
165 |
} |
|
166 |
||
7725 | 167 |
updateIcon(mil[0]); |
168 |
} |
|
169 |
} |
|
170 |
||
171 |
||
7737 | 172 |
bool PlayersListModel::isFlagSet(const QString & nickname, StateFlag flagType) |
173 |
{ |
|
7764 | 174 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
7737 | 175 |
|
176 |
if(mil.size()) |
|
177 |
return mil[0].data(flagType).toBool(); |
|
178 |
else |
|
179 |
return false; |
|
180 |
} |
|
181 |
||
7731 | 182 |
void PlayersListModel::resetRoomFlags() |
183 |
{ |
|
184 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
185 |
{ |
|
186 |
QModelIndex mi = index(i); |
|
187 |
||
7833 | 188 |
if(mi.data(RoomFilterRole).toBool()) |
7731 | 189 |
{ |
7833 | 190 |
setData(mi, false, RoomFilterRole); |
7731 | 191 |
setData(mi, false, RoomAdmin); |
192 |
setData(mi, false, Ready); |
|
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
193 |
setData(mi, false, InGame); |
7737 | 194 |
|
195 |
updateSortData(mi); |
|
196 |
updateIcon(mi); |
|
7731 | 197 |
} |
198 |
} |
|
199 |
} |
|
200 |
||
7725 | 201 |
void PlayersListModel::updateIcon(const QModelIndex & index) |
202 |
{ |
|
203 |
quint32 iconNum = 0; |
|
204 |
||
205 |
QList<bool> flags; |
|
206 |
flags |
|
207 |
<< index.data(Ready).toBool() |
|
208 |
<< index.data(ServerAdmin).toBool() |
|
209 |
<< index.data(RoomAdmin).toBool() |
|
210 |
<< index.data(Registered).toBool() |
|
211 |
<< index.data(Friend).toBool() |
|
212 |
<< index.data(Ignore).toBool() |
|
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
213 |
<< index.data(InGame).toBool() |
7833 | 214 |
<< index.data(RoomFilterRole).toBool() |
7725 | 215 |
; |
216 |
||
217 |
for(int i = flags.size() - 1; i >= 0; --i) |
|
218 |
if(flags[i]) |
|
219 |
iconNum |= 1 << i; |
|
220 |
||
221 |
if(m_icons().contains(iconNum)) |
|
222 |
{ |
|
223 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole); |
|
224 |
} |
|
225 |
else |
|
226 |
{ |
|
227 |
QPixmap result(24, 16); |
|
228 |
result.fill(Qt::transparent); |
|
229 |
||
230 |
QPainter painter(&result); |
|
231 |
||
7833 | 232 |
if(index.data(RoomFilterRole).toBool()) |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
233 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
234 |
if(index.data(InGame).toBool()) |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
235 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
236 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/ingame.png")); |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
237 |
} |
7764 | 238 |
else |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
239 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
240 |
if(index.data(Ready).toBool()) |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
241 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png")); |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
242 |
else |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
243 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png")); |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
244 |
} |
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
245 |
} |
7725 | 246 |
|
247 |
QString mainIconName(":/res/chat/"); |
|
248 |
||
249 |
if(index.data(RoomAdmin).toBool()) |
|
250 |
mainIconName += "roomadmin"; |
|
251 |
else if(index.data(ServerAdmin).toBool()) |
|
252 |
mainIconName += "serveradmin"; |
|
253 |
else |
|
254 |
mainIconName += "hedgehog"; |
|
255 |
||
256 |
if(!index.data(Registered).toBool()) |
|
257 |
mainIconName += "_gray"; |
|
258 |
||
259 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png")); |
|
260 |
||
261 |
if(index.data(Ignore).toBool()) |
|
262 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png")); |
|
263 |
else |
|
264 |
if(index.data(Friend).toBool()) |
|
265 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png")); |
|
266 |
||
267 |
painter.end(); |
|
268 |
||
269 |
QIcon icon(result); |
|
270 |
||
271 |
setData(index, icon, Qt::DecorationRole); |
|
272 |
m_icons().insert(iconNum, icon); |
|
273 |
} |
|
274 |
||
275 |
if(index.data(Ignore).toBool()) |
|
276 |
setData(index, Qt::gray, Qt::ForegroundRole); |
|
277 |
else |
|
278 |
if(index.data(Friend).toBool()) |
|
279 |
setData(index, Qt::green, Qt::ForegroundRole); |
|
280 |
else |
|
281 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole); |
|
282 |
} |
|
283 |
||
284 |
||
285 |
QHash<quint32, QIcon> & PlayersListModel::m_icons() |
|
286 |
{ |
|
287 |
static QHash<quint32, QIcon> iconsCache; |
|
288 |
||
289 |
return iconsCache; |
|
290 |
} |
|
7728 | 291 |
|
7732 | 292 |
|
7728 | 293 |
void PlayersListModel::updateSortData(const QModelIndex & index) |
294 |
{ |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
295 |
QString result = QString("%1%2%3%4%5%6") |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
296 |
// room admins go first, then server admins, then friends |
7728 | 297 |
.arg(1 - index.data(RoomAdmin).toInt()) |
298 |
.arg(1 - index.data(ServerAdmin).toInt()) |
|
299 |
.arg(1 - index.data(Friend).toInt()) |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
300 |
// ignored at bottom |
7728 | 301 |
.arg(index.data(Ignore).toInt()) |
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
302 |
// keep nicknames starting from non-letter character at bottom within group |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
303 |
// assume there are no empty nicks in list |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
304 |
.arg(index.data(Qt::DisplayRole).toString().at(0).isLetter() ? 0 : 1) |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
305 |
// sort ignoring case |
7728 | 306 |
.arg(index.data(Qt::DisplayRole).toString().toLower()) |
307 |
; |
|
308 |
||
309 |
setData(index, result, SortRole); |
|
310 |
} |
|
7732 | 311 |
|
312 |
||
313 |
void PlayersListModel::setNickname(const QString &nickname) |
|
314 |
{ |
|
315 |
m_nickname = nickname; |
|
316 |
||
317 |
loadSet(m_friendsSet, "friends"); |
|
318 |
loadSet(m_ignoredSet, "ignore"); |
|
319 |
||
320 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
321 |
checkFriendIgnore(index(i)); |
|
322 |
} |
|
323 |
||
324 |
||
325 |
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) |
|
326 |
{ |
|
327 |
setData(mi, m_friendsSet.contains(mi.data().toString().toLower()), Friend); |
|
328 |
setData(mi, m_ignoredSet.contains(mi.data().toString().toLower()), Ignore); |
|
329 |
||
330 |
updateIcon(mi); |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
331 |
updateSortData(mi); |
7732 | 332 |
} |
333 |
||
334 |
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix) |
|
335 |
{ |
|
336 |
set.clear(); |
|
337 |
||
338 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
339 |
||
340 |
QFile txt(fileName); |
|
341 |
if(!txt.open(QIODevice::ReadOnly)) |
|
342 |
return; |
|
343 |
||
344 |
QTextStream stream(&txt); |
|
345 |
stream.setCodec("UTF-8"); |
|
346 |
||
347 |
while(!stream.atEnd()) |
|
348 |
{ |
|
349 |
QString str = stream.readLine(); |
|
350 |
if(str.startsWith(";") || str.isEmpty()) |
|
351 |
continue; |
|
352 |
||
353 |
set.insert(str.trimmed()); |
|
354 |
} |
|
355 |
||
356 |
txt.close(); |
|
357 |
} |
|
358 |
||
359 |
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix) |
|
360 |
{ |
|
7737 | 361 |
qDebug("saving set"); |
362 |
||
7732 | 363 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
364 |
||
365 |
QFile txt(fileName); |
|
366 |
||
367 |
// list empty? => rather have no file for the list than an empty one |
|
368 |
if (set.isEmpty()) |
|
369 |
{ |
|
370 |
if (txt.exists()) |
|
371 |
{ |
|
372 |
// try to remove file, if successful we're done here. |
|
373 |
if (txt.remove()) |
|
374 |
return; |
|
375 |
} |
|
376 |
else |
|
377 |
// there is no file |
|
378 |
return; |
|
379 |
} |
|
380 |
||
381 |
if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
|
382 |
return; |
|
383 |
||
384 |
QTextStream stream(&txt); |
|
385 |
stream.setCodec("UTF-8"); |
|
386 |
||
387 |
stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl; |
|
388 |
||
389 |
foreach(const QString & nick, set.values()) |
|
390 |
stream << nick << endl; |
|
391 |
||
392 |
txt.close(); |
|
393 |
} |