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