author | nemo |
Sat, 03 Jan 2015 17:27:33 -0500 | |
branch | 0.9.21 |
changeset 10739 | d886f319e7b3 |
parent 10511 | c33b2f001730 |
child 10741 | 7fababf10b60 |
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"); |
|
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 |
||
117 |
QString flags = m_data.at(row).at(0); |
|
6973 | 118 |
|
10739 | 119 |
if (flags.contains("g")) |
120 |
{ |
|
121 |
if (flags.contains("j")) |
|
122 |
return QVariant(roomWaitingIconRed); |
|
123 |
else if (flags.contains("p")) |
|
124 |
return QVariant(roomWaitingIconGreen); |
|
125 |
else |
|
126 |
return QVariant(roomWaitingIcon); |
|
127 |
} |
|
6973 | 128 |
else |
10739 | 129 |
{ |
130 |
if (flags.contains("j")) |
|
131 |
return QVariant(roomBusyIconRed); |
|
132 |
else if (flags.contains("p")) |
|
133 |
return QVariant(roomBusyIconGreen); |
|
134 |
else |
|
135 |
return QVariant(roomBusyIcon); |
|
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 |
// supply in progress flag as bool |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
144 |
if (column == 0) |
6995 | 145 |
return QVariant(QString(!content.isEmpty())); |
6983
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 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
148 |
if (column == 5) |
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 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
151 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
152 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
153 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
154 |
if (content == "+maze+") return tr("Random Maze"); |
10391 | 155 |
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
|
156 |
if (content == "+drawn+") return tr("Hand-drawn"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
157 |
} |
6973 | 158 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
// prefix ? if map not available |
8377 | 160 |
if (!m_staticMapModel->mapExists(content) && |
161 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
162 |
return QString ("? %1").arg(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 |
return content; |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
166 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
167 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
168 |
// 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
|
169 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
170 |
{ |
8377 | 171 |
if (content == "+rnd+" || |
172 |
content == "+maze+" || |
|
10391 | 173 |
content == "+perlin+" || |
8377 | 174 |
content == "+drawn+" || |
175 |
m_staticMapModel->mapExists(content) || |
|
176 |
m_missionMapModel->mapExists(content)) |
|
177 |
return QVariant(); |
|
178 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
179 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
180 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
181 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
182 |
if (role == Qt::TextAlignmentRole) |
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 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
185 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
186 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
187 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
188 |
return QVariant(); |
6732 | 189 |
} |
6733 | 190 |
|
6973 | 191 |
|
6733 | 192 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
193 |
{ |
|
6973 | 194 |
beginResetModel(); |
195 |
||
196 |
m_data.clear(); |
|
6733 | 197 |
|
6973 | 198 |
int nRooms = rooms.size(); |
199 |
||
200 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 201 |
{ |
202 |
QStringList l; |
|
6973 | 203 |
|
204 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
205 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
206 |
#endif |
|
207 |
||
208 |
for (int t = 0; t < c_nColumns; t++) |
|
209 |
{ |
|
6733 | 210 |
l.append(rooms[i + t]); |
6973 | 211 |
} |
6733 | 212 |
|
10739 | 213 |
m_data.append(l); |
6733 | 214 |
} |
215 |
||
6973 | 216 |
endResetModel(); |
6733 | 217 |
} |
218 |
||
6973 | 219 |
|
6733 | 220 |
void RoomsListModel::addRoom(const QStringList & info) |
221 |
{ |
|
222 |
beginInsertRows(QModelIndex(), 0, 0); |
|
223 |
||
10739 | 224 |
m_data.prepend(info); |
6733 | 225 |
|
226 |
endInsertRows(); |
|
227 |
} |
|
228 |
||
6973 | 229 |
|
230 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
231 |
{ |
|
232 |
int size = m_data.size(); |
|
233 |
||
234 |
if (size < 1) |
|
235 |
return -1; |
|
236 |
||
237 |
int i = 0; |
|
238 |
||
239 |
// 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
|
240 |
while(m_data[i].at(NameColumn) != name) |
6973 | 241 |
{ |
242 |
i++; |
|
243 |
if(i >= size) |
|
244 |
return -1; |
|
245 |
} |
|
246 |
||
247 |
return i; |
|
248 |
} |
|
249 |
||
250 |
||
6733 | 251 |
void RoomsListModel::removeRoom(const QString & name) |
252 |
{ |
|
6973 | 253 |
int i = rowOfRoom(name); |
254 |
||
255 |
if (i < 0) |
|
6733 | 256 |
return; |
257 |
||
258 |
beginRemoveRows(QModelIndex(), i, i); |
|
259 |
||
260 |
m_data.removeAt(i); |
|
261 |
||
262 |
endRemoveRows(); |
|
263 |
} |
|
264 |
||
6973 | 265 |
|
6733 | 266 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
267 |
{ |
|
6973 | 268 |
int i = rowOfRoom(name); |
269 |
||
270 |
if (i < 0) |
|
6733 | 271 |
return; |
272 |
||
10739 | 273 |
m_data[i] = info; |
6733 | 274 |
|
275 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
276 |
} |