author | unc0rr |
Sat, 27 Oct 2012 00:39:27 +0400 | |
changeset 7833 | 20e3bd929f07 |
parent 7765 | 1e162c1d6dc7 |
child 7842 | d1c0e4341165 |
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()) |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
233 |
if(index.data(InGame).toBool()) |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
234 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/ingame.png")); |
7764 | 235 |
else |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
236 |
if(index.data(Ready).toBool()) |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
237 |
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
|
238 |
else |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
239 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png")); |
7725 | 240 |
|
241 |
QString mainIconName(":/res/chat/"); |
|
242 |
||
243 |
if(index.data(RoomAdmin).toBool()) |
|
244 |
mainIconName += "roomadmin"; |
|
245 |
else if(index.data(ServerAdmin).toBool()) |
|
246 |
mainIconName += "serveradmin"; |
|
247 |
else |
|
248 |
mainIconName += "hedgehog"; |
|
249 |
||
250 |
if(!index.data(Registered).toBool()) |
|
251 |
mainIconName += "_gray"; |
|
252 |
||
253 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png")); |
|
254 |
||
255 |
if(index.data(Ignore).toBool()) |
|
256 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png")); |
|
257 |
else |
|
258 |
if(index.data(Friend).toBool()) |
|
259 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png")); |
|
260 |
||
261 |
painter.end(); |
|
262 |
||
263 |
QIcon icon(result); |
|
264 |
||
265 |
setData(index, icon, Qt::DecorationRole); |
|
266 |
m_icons().insert(iconNum, icon); |
|
267 |
} |
|
268 |
||
269 |
if(index.data(Ignore).toBool()) |
|
270 |
setData(index, Qt::gray, Qt::ForegroundRole); |
|
271 |
else |
|
272 |
if(index.data(Friend).toBool()) |
|
273 |
setData(index, Qt::green, Qt::ForegroundRole); |
|
274 |
else |
|
275 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole); |
|
276 |
} |
|
277 |
||
278 |
||
279 |
QHash<quint32, QIcon> & PlayersListModel::m_icons() |
|
280 |
{ |
|
281 |
static QHash<quint32, QIcon> iconsCache; |
|
282 |
||
283 |
return iconsCache; |
|
284 |
} |
|
7728 | 285 |
|
7732 | 286 |
|
7728 | 287 |
void PlayersListModel::updateSortData(const QModelIndex & index) |
288 |
{ |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
289 |
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
|
290 |
// room admins go first, then server admins, then friends |
7728 | 291 |
.arg(1 - index.data(RoomAdmin).toInt()) |
292 |
.arg(1 - index.data(ServerAdmin).toInt()) |
|
293 |
.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
|
294 |
// ignored at bottom |
7728 | 295 |
.arg(index.data(Ignore).toInt()) |
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
296 |
// 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
|
297 |
// 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
|
298 |
.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
|
299 |
// sort ignoring case |
7728 | 300 |
.arg(index.data(Qt::DisplayRole).toString().toLower()) |
301 |
; |
|
302 |
||
303 |
setData(index, result, SortRole); |
|
304 |
} |
|
7732 | 305 |
|
306 |
||
307 |
void PlayersListModel::setNickname(const QString &nickname) |
|
308 |
{ |
|
309 |
m_nickname = nickname; |
|
310 |
||
311 |
loadSet(m_friendsSet, "friends"); |
|
312 |
loadSet(m_ignoredSet, "ignore"); |
|
313 |
||
314 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
315 |
checkFriendIgnore(index(i)); |
|
316 |
} |
|
317 |
||
318 |
||
319 |
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) |
|
320 |
{ |
|
321 |
setData(mi, m_friendsSet.contains(mi.data().toString().toLower()), Friend); |
|
322 |
setData(mi, m_ignoredSet.contains(mi.data().toString().toLower()), Ignore); |
|
323 |
||
324 |
updateIcon(mi); |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
325 |
updateSortData(mi); |
7732 | 326 |
} |
327 |
||
328 |
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix) |
|
329 |
{ |
|
330 |
set.clear(); |
|
331 |
||
332 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
333 |
||
334 |
QFile txt(fileName); |
|
335 |
if(!txt.open(QIODevice::ReadOnly)) |
|
336 |
return; |
|
337 |
||
338 |
QTextStream stream(&txt); |
|
339 |
stream.setCodec("UTF-8"); |
|
340 |
||
341 |
while(!stream.atEnd()) |
|
342 |
{ |
|
343 |
QString str = stream.readLine(); |
|
344 |
if(str.startsWith(";") || str.isEmpty()) |
|
345 |
continue; |
|
346 |
||
347 |
set.insert(str.trimmed()); |
|
348 |
} |
|
349 |
||
350 |
txt.close(); |
|
351 |
} |
|
352 |
||
353 |
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix) |
|
354 |
{ |
|
7737 | 355 |
qDebug("saving set"); |
356 |
||
7732 | 357 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
358 |
||
359 |
QFile txt(fileName); |
|
360 |
||
361 |
// list empty? => rather have no file for the list than an empty one |
|
362 |
if (set.isEmpty()) |
|
363 |
{ |
|
364 |
if (txt.exists()) |
|
365 |
{ |
|
366 |
// try to remove file, if successful we're done here. |
|
367 |
if (txt.remove()) |
|
368 |
return; |
|
369 |
} |
|
370 |
else |
|
371 |
// there is no file |
|
372 |
return; |
|
373 |
} |
|
374 |
||
375 |
if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
|
376 |
return; |
|
377 |
||
378 |
QTextStream stream(&txt); |
|
379 |
stream.setCodec("UTF-8"); |
|
380 |
||
381 |
stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl; |
|
382 |
||
383 |
foreach(const QString & nick, set.values()) |
|
384 |
stream << nick << endl; |
|
385 |
||
386 |
txt.close(); |
|
387 |
} |