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