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