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