author | nemo |
Tue, 16 Dec 2014 14:32:53 -0500 | |
changeset 10683 | 31a0e7efa17a |
parent 10511 | c33b2f001730 |
child 10739 | d886f319e7b3 |
permissions | -rw-r--r-- |
6966 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
9998 | 3 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
6966 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
6966 | 17 |
*/ |
18 |
||
19 |
/** |
|
20 |
* @file |
|
21 |
* @brief RoomsListModel class implementation |
|
22 |
*/ |
|
23 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
24 |
#include <QBrush> |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
25 |
#include <QColor> |
6973 | 26 |
#include <QIcon> |
27 |
||
7258 | 28 |
#include "roomslistmodel.h" |
29 |
#include "MapModel.h" |
|
30 |
||
6732 | 31 |
RoomsListModel::RoomsListModel(QObject *parent) : |
6973 | 32 |
QAbstractTableModel(parent), |
9702 | 33 |
c_nColumns(9) |
6732 | 34 |
{ |
35 |
m_headerData = |
|
36 |
QStringList() |
|
6973 | 37 |
<< tr("In progress") |
6732 | 38 |
<< tr("Room Name") |
39 |
<< tr("C") |
|
40 |
<< tr("T") |
|
41 |
<< tr("Owner") |
|
42 |
<< tr("Map") |
|
9702 | 43 |
<< tr("Script") |
6732 | 44 |
<< tr("Rules") |
45 |
<< tr("Weapons"); |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
46 |
|
8377 | 47 |
m_staticMapModel = DataManager::instance().staticMapModel(); |
48 |
m_missionMapModel = DataManager::instance().missionMapModel(); |
|
6732 | 49 |
} |
50 |
||
6973 | 51 |
|
6732 | 52 |
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const |
53 |
{ |
|
54 |
if(orientation == Qt::Vertical || role != Qt::DisplayRole) |
|
55 |
return QVariant(); |
|
56 |
else |
|
57 |
return QVariant(m_headerData.at(section)); |
|
58 |
} |
|
59 |
||
6973 | 60 |
|
6732 | 61 |
int RoomsListModel::rowCount(const QModelIndex & parent) const |
62 |
{ |
|
63 |
if(parent.isValid()) |
|
64 |
return 0; |
|
65 |
else |
|
66 |
return m_data.size(); |
|
67 |
} |
|
68 |
||
6973 | 69 |
|
6732 | 70 |
int RoomsListModel::columnCount(const QModelIndex & parent) const |
71 |
{ |
|
72 |
if(parent.isValid()) |
|
73 |
return 0; |
|
74 |
else |
|
6973 | 75 |
return c_nColumns; |
6732 | 76 |
} |
77 |
||
6973 | 78 |
|
6732 | 79 |
QVariant RoomsListModel::data(const QModelIndex &index, int role) const |
80 |
{ |
|
6973 | 81 |
int column = index.column(); |
82 |
int row = index.row(); |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
83 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
84 |
// invalid index |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
85 |
if (!index.isValid()) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
86 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
87 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
88 |
// invalid row |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
89 |
if ((row < 0) || (row >= m_data.size())) |
6732 | 90 |
return QVariant(); |
91 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
92 |
// invalid column |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
93 |
if ((column < 0) || (column >= c_nColumns)) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
94 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
95 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
96 |
// not a role we have data for |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
97 |
if (role != Qt::DisplayRole) |
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
98 |
// only custom-align counters |
6993
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
99 |
if ((role != Qt::TextAlignmentRole) |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
100 |
|| ((column != PlayerCountColumn) && (column != TeamCountColumn))) |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
101 |
// only decorate name column |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
102 |
if ((role != Qt::DecorationRole) || (column != NameColumn)) |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
103 |
// only dye map column |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
104 |
if ((role != Qt::ForegroundRole) || (column != MapColumn)) |
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
105 |
return QVariant(); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
106 |
|
6973 | 107 |
// decorate room name based on room state |
108 |
if (role == Qt::DecorationRole) |
|
109 |
{ |
|
110 |
const QIcon roomBusyIcon(":/res/iconDamage.png"); |
|
111 |
const QIcon roomWaitingIcon(":/res/iconTime.png"); |
|
112 |
||
113 |
if (m_data.at(row).at(0).isEmpty()) |
|
114 |
return QVariant(roomWaitingIcon); |
|
115 |
else |
|
116 |
return QVariant(roomBusyIcon); |
|
117 |
} |
|
118 |
||
119 |
QString content = m_data.at(row).at(column); |
|
120 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
121 |
if (role == Qt::DisplayRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
122 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
123 |
// supply in progress flag as bool |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
124 |
if (column == 0) |
6995 | 125 |
return QVariant(QString(!content.isEmpty())); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
126 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
127 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
128 |
if (column == 5) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
129 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
130 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
131 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
132 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
133 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
134 |
if (content == "+maze+") return tr("Random Maze"); |
10391 | 135 |
if (content == "+perlin+") return tr("Random Perlin"); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
136 |
if (content == "+drawn+") return tr("Hand-drawn"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
137 |
} |
6973 | 138 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
139 |
// prefix ? if map not available |
8377 | 140 |
if (!m_staticMapModel->mapExists(content) && |
141 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
142 |
return QString ("? %1").arg(content); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
143 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
144 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
145 |
return content; |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
146 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
147 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
148 |
// dye map names red if map not available |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
149 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
150 |
{ |
8377 | 151 |
if (content == "+rnd+" || |
152 |
content == "+maze+" || |
|
10391 | 153 |
content == "+perlin+" || |
8377 | 154 |
content == "+drawn+" || |
155 |
m_staticMapModel->mapExists(content) || |
|
156 |
m_missionMapModel->mapExists(content)) |
|
157 |
return QVariant(); |
|
158 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
160 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
161 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
162 |
if (role == Qt::TextAlignmentRole) |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
163 |
{ |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
164 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
165 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
166 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
167 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
168 |
return QVariant(); |
6732 | 169 |
} |
6733 | 170 |
|
6973 | 171 |
|
6733 | 172 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
173 |
{ |
|
6973 | 174 |
beginResetModel(); |
175 |
||
176 |
m_data.clear(); |
|
6733 | 177 |
|
6973 | 178 |
int nRooms = rooms.size(); |
179 |
||
180 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 181 |
{ |
182 |
QStringList l; |
|
6973 | 183 |
|
184 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
185 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
186 |
#endif |
|
187 |
||
188 |
for (int t = 0; t < c_nColumns; t++) |
|
189 |
{ |
|
6733 | 190 |
l.append(rooms[i + t]); |
6973 | 191 |
} |
6733 | 192 |
|
193 |
m_data.append(roomInfo2RoomRecord(l)); |
|
194 |
} |
|
195 |
||
6973 | 196 |
endResetModel(); |
6733 | 197 |
} |
198 |
||
6973 | 199 |
|
6733 | 200 |
void RoomsListModel::addRoom(const QStringList & info) |
201 |
{ |
|
202 |
beginInsertRows(QModelIndex(), 0, 0); |
|
203 |
||
204 |
m_data.prepend(roomInfo2RoomRecord(info)); |
|
205 |
||
206 |
endInsertRows(); |
|
207 |
} |
|
208 |
||
6973 | 209 |
|
210 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
211 |
{ |
|
212 |
int size = m_data.size(); |
|
213 |
||
214 |
if (size < 1) |
|
215 |
return -1; |
|
216 |
||
217 |
int i = 0; |
|
218 |
||
219 |
// search for record with matching room name |
|
6993
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
220 |
while(m_data[i].at(NameColumn) != name) |
6973 | 221 |
{ |
222 |
i++; |
|
223 |
if(i >= size) |
|
224 |
return -1; |
|
225 |
} |
|
226 |
||
227 |
return i; |
|
228 |
} |
|
229 |
||
230 |
||
6733 | 231 |
void RoomsListModel::removeRoom(const QString & name) |
232 |
{ |
|
6973 | 233 |
int i = rowOfRoom(name); |
234 |
||
235 |
if (i < 0) |
|
6733 | 236 |
return; |
237 |
||
238 |
beginRemoveRows(QModelIndex(), i, i); |
|
239 |
||
240 |
m_data.removeAt(i); |
|
241 |
||
242 |
endRemoveRows(); |
|
243 |
} |
|
244 |
||
6973 | 245 |
|
6733 | 246 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
247 |
{ |
|
6973 | 248 |
int i = rowOfRoom(name); |
249 |
||
250 |
if (i < 0) |
|
6733 | 251 |
return; |
252 |
||
253 |
m_data[i] = roomInfo2RoomRecord(info); |
|
254 |
||
255 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
256 |
} |
|
257 |
||
6973 | 258 |
|
6733 | 259 |
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info) |
260 |
{ |
|
261 |
QStringList result; |
|
262 |
||
263 |
result = info; |
|
264 |
||
10511
c33b2f001730
This should work, can't test: room flags passed in room info message instead of just 'in-game' state, including 'in-game', 'restricted joins', 'registered only' and 'passworded' flags
unc0rr
parents:
10391
diff
changeset
|
265 |
QString flags = info[StateColumn]; |
6973 | 266 |
// for matters of less memory usage and quicker access store |
267 |
// the boolean string as either "t" or empty |
|
10511
c33b2f001730
This should work, can't test: room flags passed in room info message instead of just 'in-game' state, including 'in-game', 'restricted joins', 'registered only' and 'passworded' flags
unc0rr
parents:
10391
diff
changeset
|
268 |
if (flags.contains('g')) |
6993
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
269 |
result[StateColumn] = "t"; |
6973 | 270 |
else |
6993
47830cf50574
room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents:
6987
diff
changeset
|
271 |
result[StateColumn] = QString(); |
6973 | 272 |
|
6733 | 273 |
return result; |
274 |
} |