author | antonc27 <antonc27@mail.ru> |
Fri, 06 Nov 2015 03:21:00 +0100 | |
branch | ios-revival |
changeset 11313 | 4701700276f5 |
parent 11046 | 47a8c19ecb60 |
child 11749 | ac58a063d26a |
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 |
{ |
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"); |
|
10739 | 111 |
const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png"); |
112 |
const QIcon roomBusyIconRed(":/res/iconDamageLockR.png"); |
|
6973 | 113 |
const QIcon roomWaitingIcon(":/res/iconTime.png"); |
10739 | 114 |
const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png"); |
115 |
const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png"); |
|
116 |
||
10745 | 117 |
QString flags = m_data.at(row).at(StateColumn); |
6973 | 118 |
|
10739 | 119 |
if (flags.contains("g")) |
120 |
{ |
|
121 |
if (flags.contains("j")) |
|
10741 | 122 |
return QVariant(roomBusyIconRed); |
123 |
else if (flags.contains("p")) |
|
124 |
return QVariant(roomBusyIconGreen); |
|
125 |
else |
|
126 |
return QVariant(roomBusyIcon); |
|
127 |
} |
|
128 |
else |
|
129 |
{ |
|
130 |
if (flags.contains("j")) |
|
10739 | 131 |
return QVariant(roomWaitingIconRed); |
132 |
else if (flags.contains("p")) |
|
133 |
return QVariant(roomWaitingIconGreen); |
|
134 |
else |
|
135 |
return QVariant(roomWaitingIcon); |
|
136 |
} |
|
6973 | 137 |
} |
138 |
||
139 |
QString content = m_data.at(row).at(column); |
|
140 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
141 |
if (role == Qt::DisplayRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
142 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
143 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
144 |
if (column == 5) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
145 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
146 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
147 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
148 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
149 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
150 |
if (content == "+maze+") return tr("Random Maze"); |
10391 | 151 |
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
|
152 |
if (content == "+drawn+") return tr("Hand-drawn"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
153 |
} |
6973 | 154 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
155 |
// prefix ? if map not available |
8377 | 156 |
if (!m_staticMapModel->mapExists(content) && |
157 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
158 |
return QString ("? %1").arg(content); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
} |
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 |
return content; |
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 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
164 |
// 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
|
165 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
166 |
{ |
8377 | 167 |
if (content == "+rnd+" || |
168 |
content == "+maze+" || |
|
10391 | 169 |
content == "+perlin+" || |
8377 | 170 |
content == "+drawn+" || |
171 |
m_staticMapModel->mapExists(content) || |
|
172 |
m_missionMapModel->mapExists(content)) |
|
173 |
return QVariant(); |
|
174 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
175 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
176 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
177 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
178 |
if (role == Qt::TextAlignmentRole) |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
179 |
{ |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
180 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
181 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
182 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
183 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
184 |
return QVariant(); |
6732 | 185 |
} |
6733 | 186 |
|
6973 | 187 |
|
6733 | 188 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
189 |
{ |
|
6973 | 190 |
beginResetModel(); |
191 |
||
192 |
m_data.clear(); |
|
6733 | 193 |
|
6973 | 194 |
int nRooms = rooms.size(); |
195 |
||
196 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 197 |
{ |
198 |
QStringList l; |
|
6973 | 199 |
|
200 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
201 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
202 |
#endif |
|
203 |
||
204 |
for (int t = 0; t < c_nColumns; t++) |
|
205 |
{ |
|
6733 | 206 |
l.append(rooms[i + t]); |
6973 | 207 |
} |
6733 | 208 |
|
10739 | 209 |
m_data.append(l); |
6733 | 210 |
} |
211 |
||
6973 | 212 |
endResetModel(); |
6733 | 213 |
} |
214 |
||
6973 | 215 |
|
6733 | 216 |
void RoomsListModel::addRoom(const QStringList & info) |
217 |
{ |
|
218 |
beginInsertRows(QModelIndex(), 0, 0); |
|
219 |
||
10739 | 220 |
m_data.prepend(info); |
6733 | 221 |
|
222 |
endInsertRows(); |
|
223 |
} |
|
224 |
||
6973 | 225 |
|
226 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
227 |
{ |
|
228 |
int size = m_data.size(); |
|
229 |
||
230 |
if (size < 1) |
|
231 |
return -1; |
|
232 |
||
233 |
int i = 0; |
|
234 |
||
235 |
// 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
|
236 |
while(m_data[i].at(NameColumn) != name) |
6973 | 237 |
{ |
238 |
i++; |
|
239 |
if(i >= size) |
|
240 |
return -1; |
|
241 |
} |
|
242 |
||
243 |
return i; |
|
244 |
} |
|
245 |
||
246 |
||
6733 | 247 |
void RoomsListModel::removeRoom(const QString & name) |
248 |
{ |
|
6973 | 249 |
int i = rowOfRoom(name); |
250 |
||
251 |
if (i < 0) |
|
6733 | 252 |
return; |
253 |
||
254 |
beginRemoveRows(QModelIndex(), i, i); |
|
255 |
||
256 |
m_data.removeAt(i); |
|
257 |
||
258 |
endRemoveRows(); |
|
259 |
} |
|
260 |
||
6973 | 261 |
|
6733 | 262 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
263 |
{ |
|
6973 | 264 |
int i = rowOfRoom(name); |
265 |
||
266 |
if (i < 0) |
|
6733 | 267 |
return; |
268 |
||
10739 | 269 |
m_data[i] = info; |
6733 | 270 |
|
271 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
272 |
} |