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