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