7725
|
1 |
#include <QModelIndexList>
|
|
2 |
#include <QModelIndex>
|
|
3 |
#include <QPainter>
|
7727
|
4 |
#include <QDebug>
|
7725
|
5 |
|
7723
|
6 |
#include "playerslistmodel.h"
|
|
7 |
|
|
8 |
PlayersListModel::PlayersListModel(QObject *parent) :
|
7725
|
9 |
QAbstractListModel(parent)
|
7723
|
10 |
{
|
|
11 |
|
|
12 |
}
|
|
13 |
|
7725
|
14 |
|
|
15 |
int PlayersListModel::rowCount(const QModelIndex &parent) const
|
|
16 |
{
|
|
17 |
if(parent.isValid())
|
|
18 |
return 0;
|
|
19 |
else
|
|
20 |
return m_data.size();
|
|
21 |
}
|
|
22 |
|
|
23 |
|
|
24 |
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
|
|
25 |
{
|
7727
|
26 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
|
7725
|
27 |
return QVariant(QVariant::Invalid);
|
|
28 |
|
|
29 |
return m_data.at(index.row()).value(role);
|
|
30 |
}
|
|
31 |
|
|
32 |
|
|
33 |
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
34 |
{
|
|
35 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
|
|
36 |
return false;
|
|
37 |
|
|
38 |
m_data[index.row()].insert(role, value);
|
|
39 |
|
|
40 |
emit dataChanged(index, index);
|
|
41 |
|
|
42 |
return true;
|
|
43 |
}
|
|
44 |
|
|
45 |
|
|
46 |
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
|
|
47 |
{
|
|
48 |
return insertRows(row, 1, parent);
|
|
49 |
}
|
|
50 |
|
|
51 |
|
|
52 |
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
|
|
53 |
{
|
|
54 |
if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
|
|
55 |
return false;
|
|
56 |
|
|
57 |
beginInsertRows(parent, row, row + count - 1);
|
|
58 |
|
|
59 |
for(int i = 0; i < count; ++i)
|
|
60 |
m_data.insert(row, DataEntry());
|
|
61 |
|
|
62 |
endInsertRows();
|
|
63 |
|
|
64 |
return true;
|
|
65 |
}
|
|
66 |
|
|
67 |
|
|
68 |
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
69 |
{
|
|
70 |
if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
|
|
71 |
return false;
|
|
72 |
|
|
73 |
beginRemoveRows(parent, row, row + count - 1);
|
|
74 |
|
|
75 |
for(int i = 0; i < count; ++i)
|
|
76 |
m_data.removeAt(row);
|
|
77 |
|
|
78 |
endRemoveRows();
|
|
79 |
|
|
80 |
return true;
|
|
81 |
}
|
|
82 |
|
|
83 |
|
7723
|
84 |
void PlayersListModel::addPlayer(const QString & nickname)
|
|
85 |
{
|
7725
|
86 |
insertRow(rowCount());
|
7723
|
87 |
|
7728
|
88 |
QModelIndex mi = index(rowCount() - 1);
|
|
89 |
setData(mi, nickname);
|
7725
|
90 |
|
7728
|
91 |
updateSortData(mi);
|
|
92 |
updateIcon(mi);
|
7723
|
93 |
}
|
7725
|
94 |
|
|
95 |
|
|
96 |
void PlayersListModel::removePlayer(const QString & nickname)
|
|
97 |
{
|
7727
|
98 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
7725
|
99 |
|
|
100 |
if(mil.size())
|
|
101 |
removeRow(mil[0].row());
|
|
102 |
}
|
|
103 |
|
|
104 |
|
|
105 |
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
|
|
106 |
{
|
|
107 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
|
|
108 |
|
|
109 |
if(mil.size())
|
|
110 |
{
|
7727
|
111 |
setData(mil[0], isSet, flagType);
|
7728
|
112 |
|
|
113 |
if(flagType == Friend || flagType == ServerAdmin
|
|
114 |
|| flagType == Ignore || flagType == RoomAdmin)
|
|
115 |
updateSortData(mil[0]);
|
|
116 |
|
7725
|
117 |
updateIcon(mil[0]);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
|
|
122 |
void PlayersListModel::updateIcon(const QModelIndex & index)
|
|
123 |
{
|
|
124 |
quint32 iconNum = 0;
|
|
125 |
|
|
126 |
QList<bool> flags;
|
|
127 |
flags
|
|
128 |
<< index.data(Ready).toBool()
|
|
129 |
<< index.data(ServerAdmin).toBool()
|
|
130 |
<< index.data(RoomAdmin).toBool()
|
|
131 |
<< index.data(Registered).toBool()
|
|
132 |
<< index.data(Friend).toBool()
|
|
133 |
<< index.data(Ignore).toBool()
|
|
134 |
;
|
|
135 |
|
|
136 |
for(int i = flags.size() - 1; i >= 0; --i)
|
|
137 |
if(flags[i])
|
|
138 |
iconNum |= 1 << i;
|
|
139 |
|
|
140 |
if(m_icons().contains(iconNum))
|
|
141 |
{
|
|
142 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole);
|
|
143 |
}
|
|
144 |
else
|
|
145 |
{
|
|
146 |
QPixmap result(24, 16);
|
|
147 |
result.fill(Qt::transparent);
|
|
148 |
|
|
149 |
QPainter painter(&result);
|
|
150 |
|
|
151 |
if(index.data(Ready).toBool())
|
|
152 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
|
|
153 |
|
|
154 |
QString mainIconName(":/res/chat/");
|
|
155 |
|
|
156 |
if(index.data(RoomAdmin).toBool())
|
|
157 |
mainIconName += "roomadmin";
|
|
158 |
else if(index.data(ServerAdmin).toBool())
|
|
159 |
mainIconName += "serveradmin";
|
|
160 |
else
|
|
161 |
mainIconName += "hedgehog";
|
|
162 |
|
|
163 |
if(!index.data(Registered).toBool())
|
|
164 |
mainIconName += "_gray";
|
|
165 |
|
|
166 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
|
|
167 |
|
|
168 |
if(index.data(Ignore).toBool())
|
|
169 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
|
|
170 |
else
|
|
171 |
if(index.data(Friend).toBool())
|
|
172 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
|
|
173 |
|
|
174 |
painter.end();
|
|
175 |
|
|
176 |
QIcon icon(result);
|
|
177 |
|
|
178 |
setData(index, icon, Qt::DecorationRole);
|
|
179 |
m_icons().insert(iconNum, icon);
|
|
180 |
}
|
|
181 |
|
|
182 |
if(index.data(Ignore).toBool())
|
|
183 |
setData(index, Qt::gray, Qt::ForegroundRole);
|
|
184 |
else
|
|
185 |
if(index.data(Friend).toBool())
|
|
186 |
setData(index, Qt::green, Qt::ForegroundRole);
|
|
187 |
else
|
|
188 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
|
|
189 |
}
|
|
190 |
|
|
191 |
|
|
192 |
QHash<quint32, QIcon> & PlayersListModel::m_icons()
|
|
193 |
{
|
|
194 |
static QHash<quint32, QIcon> iconsCache;
|
|
195 |
|
|
196 |
return iconsCache;
|
|
197 |
}
|
7728
|
198 |
|
|
199 |
void PlayersListModel::updateSortData(const QModelIndex & index)
|
|
200 |
{
|
|
201 |
QString result = QString("%1%2%3%4%5")
|
|
202 |
.arg(1 - index.data(RoomAdmin).toInt())
|
|
203 |
.arg(1 - index.data(ServerAdmin).toInt())
|
|
204 |
.arg(1 - index.data(Friend).toInt())
|
|
205 |
.arg(index.data(Ignore).toInt())
|
|
206 |
.arg(index.data(Qt::DisplayRole).toString().toLower())
|
|
207 |
;
|
|
208 |
|
|
209 |
setData(index, result, SortRole);
|
|
210 |
}
|