author | sheepluva |
Sun, 23 Apr 2017 18:23:59 +0200 | |
changeset 12313 | c214668c9569 |
parent 11752 | 3c66845947a8 |
child 12474 | 42da9d8d82ab |
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"); |
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
|
153 |
if (content == "+forts+") return tr("Forts"); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
154 |
} |
6973 | 155 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
156 |
// prefix ? if map not available |
8377 | 157 |
if (!m_staticMapModel->mapExists(content) && |
158 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
return QString ("? %1").arg(content); |
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 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
162 |
return content; |
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 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
165 |
// 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
|
166 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
167 |
{ |
8377 | 168 |
if (content == "+rnd+" || |
169 |
content == "+maze+" || |
|
10391 | 170 |
content == "+perlin+" || |
8377 | 171 |
content == "+drawn+" || |
11752
3c66845947a8
rooms-list: fix "Forts" map type being displayed in dark red
sheepluva
parents:
11744
diff
changeset
|
172 |
content == "+forts+" || |
8377 | 173 |
m_staticMapModel->mapExists(content) || |
174 |
m_missionMapModel->mapExists(content)) |
|
175 |
return QVariant(); |
|
176 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
177 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
178 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
179 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
180 |
if (role == Qt::TextAlignmentRole) |
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 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
183 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
184 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
185 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
186 |
return QVariant(); |
6732 | 187 |
} |
6733 | 188 |
|
6973 | 189 |
|
6733 | 190 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
191 |
{ |
|
6973 | 192 |
beginResetModel(); |
193 |
||
194 |
m_data.clear(); |
|
6733 | 195 |
|
6973 | 196 |
int nRooms = rooms.size(); |
197 |
||
198 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 199 |
{ |
200 |
QStringList l; |
|
6973 | 201 |
|
202 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
203 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
204 |
#endif |
|
205 |
||
206 |
for (int t = 0; t < c_nColumns; t++) |
|
207 |
{ |
|
6733 | 208 |
l.append(rooms[i + t]); |
6973 | 209 |
} |
6733 | 210 |
|
10739 | 211 |
m_data.append(l); |
6733 | 212 |
} |
213 |
||
6973 | 214 |
endResetModel(); |
6733 | 215 |
} |
216 |
||
6973 | 217 |
|
6733 | 218 |
void RoomsListModel::addRoom(const QStringList & info) |
219 |
{ |
|
220 |
beginInsertRows(QModelIndex(), 0, 0); |
|
221 |
||
10739 | 222 |
m_data.prepend(info); |
6733 | 223 |
|
224 |
endInsertRows(); |
|
225 |
} |
|
226 |
||
6973 | 227 |
|
228 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
229 |
{ |
|
230 |
int size = m_data.size(); |
|
231 |
||
232 |
if (size < 1) |
|
233 |
return -1; |
|
234 |
||
235 |
int i = 0; |
|
236 |
||
237 |
// 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
|
238 |
while(m_data[i].at(NameColumn) != name) |
6973 | 239 |
{ |
240 |
i++; |
|
241 |
if(i >= size) |
|
242 |
return -1; |
|
243 |
} |
|
244 |
||
245 |
return i; |
|
246 |
} |
|
247 |
||
248 |
||
6733 | 249 |
void RoomsListModel::removeRoom(const QString & name) |
250 |
{ |
|
6973 | 251 |
int i = rowOfRoom(name); |
252 |
||
253 |
if (i < 0) |
|
6733 | 254 |
return; |
255 |
||
256 |
beginRemoveRows(QModelIndex(), i, i); |
|
257 |
||
258 |
m_data.removeAt(i); |
|
259 |
||
260 |
endRemoveRows(); |
|
261 |
} |
|
262 |
||
6973 | 263 |
|
6733 | 264 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
265 |
{ |
|
6973 | 266 |
int i = rowOfRoom(name); |
267 |
||
268 |
if (i < 0) |
|
6733 | 269 |
return; |
270 |
||
10739 | 271 |
m_data[i] = info; |
6733 | 272 |
|
273 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
274 |
} |