author | Wuzzy <almikes@aol.com> |
Sat, 23 Sep 2017 07:38:41 +0200 | |
changeset 12499 | d97050a7c074 |
parent 12474 | 42da9d8d82ab |
child 15900 | fc3cb23fd26f |
permissions | -rw-r--r-- |
6966 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 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 |
{ |
12474
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
35 |
m_headerData = QStringList(); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
36 |
m_headerData << tr("In progress"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
37 |
m_headerData << tr("Room Name"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
38 |
//: Caption of the column for the number of connected clients in the list of rooms |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
39 |
m_headerData << tr("C"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
40 |
//: Caption of the column for the number of teams in the list of rooms |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
41 |
m_headerData << tr("T"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
42 |
m_headerData << tr("Owner"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
43 |
m_headerData << tr("Map"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
44 |
m_headerData << tr("Script"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
45 |
m_headerData << tr("Rules"); |
42da9d8d82ab
Provide translator context for a few less obvious engine strings
Wuzzy <almikes@aol.com>
parents:
11752
diff
changeset
|
46 |
m_headerData << tr("Weapons"); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
47 |
|
8377 | 48 |
m_staticMapModel = DataManager::instance().staticMapModel(); |
49 |
m_missionMapModel = DataManager::instance().missionMapModel(); |
|
6732 | 50 |
} |
51 |
||
6973 | 52 |
|
6732 | 53 |
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const |
54 |
{ |
|
55 |
if(orientation == Qt::Vertical || role != Qt::DisplayRole) |
|
56 |
return QVariant(); |
|
57 |
else |
|
58 |
return QVariant(m_headerData.at(section)); |
|
59 |
} |
|
60 |
||
6973 | 61 |
|
6732 | 62 |
int RoomsListModel::rowCount(const QModelIndex & parent) const |
63 |
{ |
|
64 |
if(parent.isValid()) |
|
65 |
return 0; |
|
66 |
else |
|
67 |
return m_data.size(); |
|
68 |
} |
|
69 |
||
6973 | 70 |
|
6732 | 71 |
int RoomsListModel::columnCount(const QModelIndex & parent) const |
72 |
{ |
|
73 |
if(parent.isValid()) |
|
74 |
return 0; |
|
75 |
else |
|
6973 | 76 |
return c_nColumns; |
6732 | 77 |
} |
78 |
||
6973 | 79 |
|
6732 | 80 |
QVariant RoomsListModel::data(const QModelIndex &index, int role) const |
81 |
{ |
|
6973 | 82 |
int column = index.column(); |
83 |
int row = index.row(); |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
84 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
85 |
// invalid index |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
86 |
if (!index.isValid()) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
87 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
88 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
89 |
// invalid row |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
90 |
if ((row < 0) || (row >= m_data.size())) |
6732 | 91 |
return QVariant(); |
92 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
93 |
// invalid column |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
94 |
if ((column < 0) || (column >= c_nColumns)) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
95 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
96 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
97 |
// not a role we have data for |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
98 |
if (role != Qt::DisplayRole) |
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
99 |
// 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
|
100 |
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
|
101 |
|| ((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
|
102 |
// 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
|
103 |
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
|
104 |
// 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
|
105 |
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
|
106 |
return QVariant(); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
107 |
|
6973 | 108 |
// decorate room name based on room state |
109 |
if (role == Qt::DecorationRole) |
|
110 |
{ |
|
111 |
const QIcon roomBusyIcon(":/res/iconDamage.png"); |
|
10739 | 112 |
const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png"); |
113 |
const QIcon roomBusyIconRed(":/res/iconDamageLockR.png"); |
|
6973 | 114 |
const QIcon roomWaitingIcon(":/res/iconTime.png"); |
10739 | 115 |
const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png"); |
116 |
const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png"); |
|
117 |
||
10745 | 118 |
QString flags = m_data.at(row).at(StateColumn); |
6973 | 119 |
|
10739 | 120 |
if (flags.contains("g")) |
121 |
{ |
|
122 |
if (flags.contains("j")) |
|
10741 | 123 |
return QVariant(roomBusyIconRed); |
124 |
else if (flags.contains("p")) |
|
125 |
return QVariant(roomBusyIconGreen); |
|
126 |
else |
|
127 |
return QVariant(roomBusyIcon); |
|
128 |
} |
|
129 |
else |
|
130 |
{ |
|
131 |
if (flags.contains("j")) |
|
10739 | 132 |
return QVariant(roomWaitingIconRed); |
133 |
else if (flags.contains("p")) |
|
134 |
return QVariant(roomWaitingIconGreen); |
|
135 |
else |
|
136 |
return QVariant(roomWaitingIcon); |
|
137 |
} |
|
6973 | 138 |
} |
139 |
||
140 |
QString content = m_data.at(row).at(column); |
|
141 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
142 |
if (role == Qt::DisplayRole) |
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 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
145 |
if (column == 5) |
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 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
148 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
149 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
150 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
151 |
if (content == "+maze+") return tr("Random Maze"); |
10391 | 152 |
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
|
153 |
if (content == "+drawn+") return tr("Hand-drawn"); |
11744
ac58a063d26a
Added "Forts" to map type selection. This makes the mode easier selectable/discoverable. Also the slider can be used to adjust the distance between forts.
sheepluva
parents:
11046
diff
changeset
|
154 |
if (content == "+forts+") return tr("Forts"); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
155 |
} |
6973 | 156 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
157 |
// prefix ? if map not available |
8377 | 158 |
if (!m_staticMapModel->mapExists(content) && |
159 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
160 |
return QString ("? %1").arg(content); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
161 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
162 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
163 |
return content; |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
164 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
165 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
166 |
// 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
|
167 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
168 |
{ |
8377 | 169 |
if (content == "+rnd+" || |
170 |
content == "+maze+" || |
|
10391 | 171 |
content == "+perlin+" || |
8377 | 172 |
content == "+drawn+" || |
11752
3c66845947a8
rooms-list: fix "Forts" map type being displayed in dark red
sheepluva
parents:
11744
diff
changeset
|
173 |
content == "+forts+" || |
8377 | 174 |
m_staticMapModel->mapExists(content) || |
175 |
m_missionMapModel->mapExists(content)) |
|
176 |
return QVariant(); |
|
177 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
178 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
179 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
180 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
181 |
if (role == Qt::TextAlignmentRole) |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
182 |
{ |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
183 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
184 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
185 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
186 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
187 |
return QVariant(); |
6732 | 188 |
} |
6733 | 189 |
|
6973 | 190 |
|
6733 | 191 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
192 |
{ |
|
6973 | 193 |
beginResetModel(); |
194 |
||
195 |
m_data.clear(); |
|
6733 | 196 |
|
6973 | 197 |
int nRooms = rooms.size(); |
198 |
||
199 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 200 |
{ |
201 |
QStringList l; |
|
6973 | 202 |
|
203 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
204 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
205 |
#endif |
|
206 |
||
207 |
for (int t = 0; t < c_nColumns; t++) |
|
208 |
{ |
|
6733 | 209 |
l.append(rooms[i + t]); |
6973 | 210 |
} |
6733 | 211 |
|
10739 | 212 |
m_data.append(l); |
6733 | 213 |
} |
214 |
||
6973 | 215 |
endResetModel(); |
6733 | 216 |
} |
217 |
||
6973 | 218 |
|
6733 | 219 |
void RoomsListModel::addRoom(const QStringList & info) |
220 |
{ |
|
221 |
beginInsertRows(QModelIndex(), 0, 0); |
|
222 |
||
10739 | 223 |
m_data.prepend(info); |
6733 | 224 |
|
225 |
endInsertRows(); |
|
226 |
} |
|
227 |
||
6973 | 228 |
|
229 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
230 |
{ |
|
231 |
int size = m_data.size(); |
|
232 |
||
233 |
if (size < 1) |
|
234 |
return -1; |
|
235 |
||
236 |
int i = 0; |
|
237 |
||
238 |
// 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
|
239 |
while(m_data[i].at(NameColumn) != name) |
6973 | 240 |
{ |
241 |
i++; |
|
242 |
if(i >= size) |
|
243 |
return -1; |
|
244 |
} |
|
245 |
||
246 |
return i; |
|
247 |
} |
|
248 |
||
249 |
||
6733 | 250 |
void RoomsListModel::removeRoom(const QString & name) |
251 |
{ |
|
6973 | 252 |
int i = rowOfRoom(name); |
253 |
||
254 |
if (i < 0) |
|
6733 | 255 |
return; |
256 |
||
257 |
beginRemoveRows(QModelIndex(), i, i); |
|
258 |
||
259 |
m_data.removeAt(i); |
|
260 |
||
261 |
endRemoveRows(); |
|
262 |
} |
|
263 |
||
6973 | 264 |
|
6733 | 265 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
266 |
{ |
|
6973 | 267 |
int i = rowOfRoom(name); |
268 |
||
269 |
if (i < 0) |
|
6733 | 270 |
return; |
271 |
||
10739 | 272 |
m_data[i] = info; |
6733 | 273 |
|
274 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
275 |
} |