author | unc0rr |
Fri, 02 Aug 2019 13:35:23 +0200 | |
changeset 15284 | ae8e14d14596 |
parent 15047 | 773beead236f |
child 15891 | d52f5d8e75e6 |
permissions | -rw-r--r-- |
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
1 |
#include <QDebug> |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
2 |
#include <QFile> |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
3 |
#include <QModelIndex> |
7725 | 4 |
#include <QModelIndexList> |
5 |
#include <QPainter> |
|
7732 | 6 |
#include <QTextStream> |
7725 | 7 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
8 |
#include "players_model.h" |
7723 | 9 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
10 |
PlayersListModel::PlayersListModel(QObject *parent) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
11 |
: QAbstractListModel(parent) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
12 |
m_fontInRoom = QFont(); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
13 |
m_fontInRoom.setItalic(true); |
7723 | 14 |
} |
15 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
16 |
int PlayersListModel::rowCount(const QModelIndex &parent) const { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
17 |
if (parent.isValid()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
18 |
return 0; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
19 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
20 |
return m_data.size(); |
7725 | 21 |
} |
22 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
23 |
QVariant PlayersListModel::data(const QModelIndex &index, int role) const { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
24 |
if (!index.isValid() || index.row() < 0 || index.row() >= rowCount() || |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
25 |
index.column() != 0) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
26 |
return QVariant(QVariant::Invalid); |
7725 | 27 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
28 |
return m_data.at(index.row()).value(role); |
7725 | 29 |
} |
30 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
31 |
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
32 |
int role) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
33 |
if (!index.isValid() || index.row() < 0 || index.row() >= rowCount() || |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
34 |
index.column() != 0) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
35 |
return false; |
7725 | 36 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
37 |
m_data[index.row()].insert(role, value); |
7725 | 38 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
39 |
emit dataChanged(index, index); |
7725 | 40 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
41 |
return true; |
7725 | 42 |
} |
43 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
44 |
bool PlayersListModel::insertRows(int row, int count, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
45 |
const QModelIndex &parent) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
46 |
if (parent.isValid() || row > rowCount() || row < 0 || count < 1) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
47 |
return false; |
7725 | 48 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
49 |
beginInsertRows(parent, row, row + count - 1); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
50 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
51 |
for (int i = 0; i < count; ++i) m_data.insert(row, DataEntry()); |
7725 | 52 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
53 |
endInsertRows(); |
7725 | 54 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
55 |
return true; |
7725 | 56 |
} |
57 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
58 |
bool PlayersListModel::removeRows(int row, int count, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
59 |
const QModelIndex &parent) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
60 |
if (parent.isValid() || row + count > rowCount() || row < 0 || count < 1) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
61 |
return false; |
7725 | 62 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
63 |
beginRemoveRows(parent, row, row + count - 1); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
64 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
65 |
for (int i = 0; i < count; ++i) m_data.removeAt(row); |
7725 | 66 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
67 |
endRemoveRows(); |
7725 | 68 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
69 |
return true; |
7725 | 70 |
} |
71 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
72 |
QModelIndex PlayersListModel::nicknameIndex(const QString &nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
73 |
QModelIndexList mil = |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
74 |
match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
9727 | 75 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
76 |
if (mil.size() > 0) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
77 |
return mil[0]; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
78 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
79 |
return QModelIndex(); |
9727 | 80 |
} |
7725 | 81 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
82 |
void PlayersListModel::addPlayer(const QString &nickname, bool notify) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
83 |
insertRow(rowCount()); |
7723 | 84 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
85 |
QModelIndex mi = index(rowCount() - 1); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
86 |
setData(mi, nickname); |
7725 | 87 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
88 |
checkFriendIgnore(mi); |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
89 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
90 |
emit nickAddedLobby(nickname, notify); |
7723 | 91 |
} |
7725 | 92 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
93 |
void PlayersListModel::removePlayer(const QString &nickname, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
94 |
const QString &msg) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
95 |
if (msg.isEmpty()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
96 |
emit nickRemovedLobby(nickname, QString()); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
97 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
98 |
emit nickRemovedLobby(nickname, msg); |
7725 | 99 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
100 |
QModelIndex mi = nicknameIndex(nickname); |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
101 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
102 |
if (mi.isValid()) removeRow(mi.row()); |
7725 | 103 |
} |
104 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
105 |
void PlayersListModel::playerJoinedRoom(const QString &nickname, bool notify) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
106 |
QModelIndex mi = nicknameIndex(nickname); |
7731 | 107 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
108 |
if (mi.isValid()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
109 |
setData(mi, true, RoomFilterRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
110 |
updateIcon(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
111 |
updateSortData(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
112 |
} |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
113 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
114 |
emit nickAdded(nickname, notify); |
7731 | 115 |
} |
116 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
117 |
void PlayersListModel::playerLeftRoom(const QString &nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
118 |
emit nickRemoved(nickname); |
7731 | 119 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
120 |
QModelIndex mi = nicknameIndex(nickname); |
7731 | 121 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
122 |
if (mi.isValid()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
123 |
setData(mi, false, RoomFilterRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
124 |
setData(mi, false, RoomAdmin); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
125 |
setData(mi, false, Ready); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
126 |
setData(mi, false, InGame); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
127 |
updateIcon(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
128 |
} |
7731 | 129 |
} |
130 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
131 |
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
132 |
bool isSet) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
133 |
if (flagType == Friend) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
134 |
if (isSet) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
135 |
m_friendsSet.insert(nickname.toLower()); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
136 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
137 |
m_friendsSet.remove(nickname.toLower()); |
7731 | 138 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
139 |
// FIXME: set proper file name |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
140 |
// saveSet(m_friendsSet, "friends"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
141 |
} else if (flagType == Ignore) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
142 |
if (isSet) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
143 |
m_ignoredSet.insert(nickname.toLower()); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
144 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
145 |
m_ignoredSet.remove(nickname.toLower()); |
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
146 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
147 |
// FIXME: set proper file name |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
148 |
// saveSet(m_ignoredSet, "ignore"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
149 |
} |
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
150 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
151 |
QModelIndex mi = nicknameIndex(nickname); |
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
152 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
153 |
if (mi.isValid()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
154 |
setData(mi, isSet, flagType); |
7728 | 155 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
156 |
if (flagType == Friend || flagType == ServerAdmin || flagType == Ignore || |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
157 |
flagType == RoomAdmin) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
158 |
updateSortData(mi); |
7728 | 159 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
160 |
updateIcon(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
161 |
} |
7725 | 162 |
} |
163 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
164 |
bool PlayersListModel::isFlagSet(const QString &nickname, StateFlag flagType) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
165 |
QModelIndex mi = nicknameIndex(nickname); |
7737 | 166 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
167 |
if (mi.isValid()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
168 |
return mi.data(flagType).toBool(); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
169 |
else if (flagType == Friend) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
170 |
return isFriend(nickname); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
171 |
else if (flagType == Ignore) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
172 |
return isIgnored(nickname); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
173 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
174 |
return false; |
7737 | 175 |
} |
176 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
177 |
void PlayersListModel::resetRoomFlags() { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
178 |
for (int i = rowCount() - 1; i >= 0; --i) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
179 |
QModelIndex mi = index(i); |
7731 | 180 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
181 |
if (mi.data(RoomFilterRole).toBool()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
182 |
setData(mi, false, RoomFilterRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
183 |
setData(mi, false, RoomAdmin); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
184 |
setData(mi, false, Ready); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
185 |
setData(mi, false, InGame); |
7737 | 186 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
187 |
updateSortData(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
188 |
updateIcon(mi); |
7731 | 189 |
} |
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
190 |
} |
7731 | 191 |
} |
192 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
193 |
void PlayersListModel::updateIcon(const QModelIndex &index) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
194 |
quint32 iconNum = 0; |
7725 | 195 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
196 |
QList<bool> flags; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
197 |
flags << index.data(Ready).toBool() << index.data(ServerAdmin).toBool() |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
198 |
<< index.data(RoomAdmin).toBool() << index.data(Registered).toBool() |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
199 |
<< index.data(Friend).toBool() << index.data(Ignore).toBool() |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
200 |
<< index.data(InGame).toBool() << index.data(RoomFilterRole).toBool() |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
201 |
<< index.data(InRoom).toBool() << index.data(Contributor).toBool(); |
7725 | 202 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
203 |
for (int i = flags.size() - 1; i >= 0; --i) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
204 |
if (flags[i]) iconNum |= 1 << i; |
7725 | 205 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
206 |
if (m_icons().contains(iconNum)) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
207 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
208 |
} else { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
209 |
QPixmap result(24, 16); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
210 |
result.fill(Qt::transparent); |
7725 | 211 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
212 |
QPainter painter(&result); |
7725 | 213 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
214 |
if (index.data(RoomFilterRole).toBool()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
215 |
if (index.data(InGame).toBool()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
216 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/ingame.png")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
217 |
} else { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
218 |
if (index.data(Ready).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
219 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png")); |
7725 | 220 |
else |
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
221 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
222 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
223 |
} else { // we're in lobby |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
224 |
if (!index.data(InRoom).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
225 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/Flake.png")); |
7725 | 226 |
} |
227 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
228 |
QString mainIconName(":/res/chat/"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
229 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
230 |
if (index.data(ServerAdmin).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
231 |
mainIconName += "serveradmin"; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
232 |
else { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
233 |
if (index.data(RoomAdmin).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
234 |
mainIconName += "roomadmin"; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
235 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
236 |
mainIconName += "hedgehog"; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
237 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
238 |
if (index.data(Contributor).toBool()) mainIconName += "contributor"; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
239 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
240 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
241 |
if (!index.data(Registered).toBool()) mainIconName += "_gray"; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
242 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
243 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
244 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
245 |
if (index.data(Ignore).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
246 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
247 |
else if (index.data(Friend).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
248 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png")); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
249 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
250 |
painter.end(); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
251 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
252 |
QIcon icon(result); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
253 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
254 |
setData(index, icon, Qt::DecorationRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
255 |
m_icons().insert(iconNum, icon); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
256 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
257 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
258 |
if (index.data(Ignore).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
259 |
setData(index, QColor(Qt::gray), Qt::ForegroundRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
260 |
else if (index.data(Friend).toBool()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
261 |
setData(index, QColor(Qt::green), Qt::ForegroundRole); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
262 |
else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
263 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole); |
7725 | 264 |
} |
265 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
266 |
QHash<quint32, QIcon> &PlayersListModel::m_icons() { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
267 |
static QHash<quint32, QIcon> iconsCache; |
7725 | 268 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
269 |
return iconsCache; |
7725 | 270 |
} |
7728 | 271 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
272 |
void PlayersListModel::updateSortData(const QModelIndex &index) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
273 |
QString result = |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
274 |
QString("%1%2%3%4%5%6") |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
275 |
// room admins go first, then server admins, then friends |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
276 |
.arg(1 - index.data(RoomAdmin).toInt()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
277 |
.arg(1 - index.data(ServerAdmin).toInt()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
278 |
.arg(1 - index.data(Friend).toInt()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
279 |
// ignored at bottom |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
280 |
.arg(index.data(Ignore).toInt()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
281 |
// keep nicknames starting from non-letter character at bottom within |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
282 |
// group assume there are no empty nicks in list |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
283 |
.arg(index.data(Qt::DisplayRole).toString().at(0).isLetter() ? 0 : 1) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
284 |
// sort ignoring case |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
285 |
.arg(index.data(Qt::DisplayRole).toString().toLower()); |
7732 | 286 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
287 |
setData(index, result, SortRole); |
7728 | 288 |
} |
7732 | 289 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
290 |
void PlayersListModel::setNickname(const QString &nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
291 |
m_nickname = nickname; |
7732 | 292 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
293 |
// FIXME: set proper file names |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
294 |
// loadSet(m_friendsSet, "friends"); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
295 |
// loadSet(m_ignoredSet, "ignore"); |
7732 | 296 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
297 |
for (int i = rowCount() - 1; i >= 0; --i) checkFriendIgnore(index(i)); |
7732 | 298 |
} |
299 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
300 |
bool PlayersListModel::isFriend(const QString &nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
301 |
return m_friendsSet.contains(nickname.toLower()); |
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
302 |
} |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
303 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
304 |
bool PlayersListModel::isIgnored(const QString &nickname) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
305 |
return m_ignoredSet.contains(nickname.toLower()); |
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
306 |
} |
7732 | 307 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
308 |
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
309 |
setData(mi, isFriend(mi.data().toString()), Friend); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
310 |
setData(mi, isIgnored(mi.data().toString()), Ignore); |
7732 | 311 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
312 |
updateIcon(mi); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
313 |
updateSortData(mi); |
7732 | 314 |
} |
315 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
316 |
void PlayersListModel::loadSet(QSet<QString> &set, const QString &fileName) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
317 |
set.clear(); |
7732 | 318 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
319 |
QFile txt(fileName); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
320 |
if (!txt.open(QIODevice::ReadOnly)) return; |
7732 | 321 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
322 |
QTextStream stream(&txt); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
323 |
stream.setCodec("UTF-8"); |
7732 | 324 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
325 |
while (!stream.atEnd()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
326 |
QString str = stream.readLine(); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
327 |
if (str.startsWith(";") || str.isEmpty()) continue; |
7732 | 328 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
329 |
set.insert(str.trimmed()); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
330 |
} |
7732 | 331 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
332 |
txt.close(); |
7732 | 333 |
} |
334 |
||
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
335 |
void PlayersListModel::saveSet(const QSet<QString> &set, |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
336 |
const QString &fileName) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
337 |
qDebug("saving set"); |
7737 | 338 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
339 |
QFile txt(fileName); |
7732 | 340 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
341 |
// list empty? => rather have no file for the list than an empty one |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
342 |
if (set.isEmpty()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
343 |
if (txt.exists()) { |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
344 |
// try to remove file, if successful we're done here. |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
345 |
if (txt.remove()) return; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
346 |
} else |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
347 |
// there is no file |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
348 |
return; |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
349 |
} |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
350 |
|
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
351 |
if (!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; |
7732 | 352 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
353 |
QTextStream stream(&txt); |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
354 |
stream.setCodec("UTF-8"); |
7732 | 355 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
356 |
stream << "; this list is used by Hedgewars - do not edit it unless you know " |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
357 |
"what you're doing!" |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
358 |
<< endl; |
7732 | 359 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
360 |
foreach (const QString &nick, set.values()) |
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
361 |
stream << nick << endl; |
7732 | 362 |
|
15047
773beead236f
Add handling of some messages, reuse models from the old frontend
unc0rr
parents:
12897
diff
changeset
|
363 |
txt.close(); |
7732 | 364 |
} |