author | unc0rr |
Wed, 10 Oct 2012 00:21:18 +0400 | |
changeset 7737 | ff63da8a3202 |
parent 7258 | 722e8a0d89dc |
child 8377 | 869f80966a77 |
permissions | -rw-r--r-- |
6966 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
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), |
33 |
c_nColumns(8) |
|
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") |
|
43 |
<< tr("Rules") |
|
44 |
<< tr("Weapons"); |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
45 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
46 |
m_mapModel = DataManager::instance().mapModel(); |
6732 | 47 |
} |
48 |
||
6973 | 49 |
|
6732 | 50 |
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const |
51 |
{ |
|
52 |
if(orientation == Qt::Vertical || role != Qt::DisplayRole) |
|
53 |
return QVariant(); |
|
54 |
else |
|
55 |
return QVariant(m_headerData.at(section)); |
|
56 |
} |
|
57 |
||
6973 | 58 |
|
6732 | 59 |
int RoomsListModel::rowCount(const QModelIndex & parent) const |
60 |
{ |
|
61 |
if(parent.isValid()) |
|
62 |
return 0; |
|
63 |
else |
|
64 |
return m_data.size(); |
|
65 |
} |
|
66 |
||
6973 | 67 |
|
6732 | 68 |
int RoomsListModel::columnCount(const QModelIndex & parent) const |
69 |
{ |
|
70 |
if(parent.isValid()) |
|
71 |
return 0; |
|
72 |
else |
|
6973 | 73 |
return c_nColumns; |
6732 | 74 |
} |
75 |
||
6973 | 76 |
|
6732 | 77 |
QVariant RoomsListModel::data(const QModelIndex &index, int role) const |
78 |
{ |
|
6973 | 79 |
int column = index.column(); |
80 |
int row = index.row(); |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
81 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
82 |
// invalid index |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
83 |
if (!index.isValid()) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
84 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
85 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
86 |
// invalid row |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
87 |
if ((row < 0) || (row >= m_data.size())) |
6732 | 88 |
return QVariant(); |
89 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
90 |
// invalid column |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
91 |
if ((column < 0) || (column >= c_nColumns)) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
92 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
93 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
94 |
// not a role we have data for |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
95 |
if (role != Qt::DisplayRole) |
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
96 |
// 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
|
97 |
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
|
98 |
|| ((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
|
99 |
// 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
|
100 |
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
|
101 |
// 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
|
102 |
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
|
103 |
return QVariant(); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
104 |
|
6973 | 105 |
// decorate room name based on room state |
106 |
if (role == Qt::DecorationRole) |
|
107 |
{ |
|
108 |
const QIcon roomBusyIcon(":/res/iconDamage.png"); |
|
109 |
const QIcon roomWaitingIcon(":/res/iconTime.png"); |
|
110 |
||
111 |
if (m_data.at(row).at(0).isEmpty()) |
|
112 |
return QVariant(roomWaitingIcon); |
|
113 |
else |
|
114 |
return QVariant(roomBusyIcon); |
|
115 |
} |
|
116 |
||
117 |
QString content = m_data.at(row).at(column); |
|
118 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
119 |
if (role == Qt::DisplayRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
120 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
121 |
// supply in progress flag as bool |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
122 |
if (column == 0) |
6995 | 123 |
return QVariant(QString(!content.isEmpty())); |
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
124 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
125 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
126 |
if (column == 5) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
127 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
128 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
129 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
130 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
131 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
132 |
if (content == "+maze+") return tr("Random Maze"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
133 |
if (content == "+drawn+") return tr("Hand-drawn"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
134 |
} |
6973 | 135 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
136 |
// prefix ? if map not available |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
137 |
if ((m_mapModel->indexOf(content) < 0)) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
138 |
return QString ("? %1").arg(content); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
139 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
140 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
141 |
return content; |
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 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
144 |
// 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
|
145 |
if (role == Qt::ForegroundRole) |
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 |
if ((m_mapModel->indexOf(content) < 0)) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
148 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
149 |
else |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
150 |
return QVariant(); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
151 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
152 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
153 |
if (role == Qt::TextAlignmentRole) |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
154 |
{ |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
155 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
156 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
157 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
158 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
return QVariant(); |
6732 | 160 |
} |
6733 | 161 |
|
6973 | 162 |
|
6733 | 163 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
164 |
{ |
|
6973 | 165 |
beginResetModel(); |
166 |
||
167 |
m_data.clear(); |
|
6733 | 168 |
|
6973 | 169 |
int nRooms = rooms.size(); |
170 |
||
171 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 172 |
{ |
173 |
QStringList l; |
|
6973 | 174 |
|
175 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
176 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
177 |
#endif |
|
178 |
||
179 |
for (int t = 0; t < c_nColumns; t++) |
|
180 |
{ |
|
6733 | 181 |
l.append(rooms[i + t]); |
6973 | 182 |
} |
6733 | 183 |
|
184 |
m_data.append(roomInfo2RoomRecord(l)); |
|
185 |
} |
|
186 |
||
6973 | 187 |
endResetModel(); |
6733 | 188 |
} |
189 |
||
6973 | 190 |
|
6733 | 191 |
void RoomsListModel::addRoom(const QStringList & info) |
192 |
{ |
|
193 |
beginInsertRows(QModelIndex(), 0, 0); |
|
194 |
||
195 |
m_data.prepend(roomInfo2RoomRecord(info)); |
|
196 |
||
197 |
endInsertRows(); |
|
198 |
} |
|
199 |
||
6973 | 200 |
|
201 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
202 |
{ |
|
203 |
int size = m_data.size(); |
|
204 |
||
205 |
if (size < 1) |
|
206 |
return -1; |
|
207 |
||
208 |
int i = 0; |
|
209 |
||
210 |
// 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
|
211 |
while(m_data[i].at(NameColumn) != name) |
6973 | 212 |
{ |
213 |
i++; |
|
214 |
if(i >= size) |
|
215 |
return -1; |
|
216 |
} |
|
217 |
||
218 |
return i; |
|
219 |
} |
|
220 |
||
221 |
||
6733 | 222 |
void RoomsListModel::removeRoom(const QString & name) |
223 |
{ |
|
6973 | 224 |
int i = rowOfRoom(name); |
225 |
||
226 |
if (i < 0) |
|
6733 | 227 |
return; |
228 |
||
229 |
beginRemoveRows(QModelIndex(), i, i); |
|
230 |
||
231 |
m_data.removeAt(i); |
|
232 |
||
233 |
endRemoveRows(); |
|
234 |
} |
|
235 |
||
6973 | 236 |
|
6733 | 237 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
238 |
{ |
|
6973 | 239 |
int i = rowOfRoom(name); |
240 |
||
241 |
if (i < 0) |
|
6733 | 242 |
return; |
243 |
||
244 |
m_data[i] = roomInfo2RoomRecord(info); |
|
245 |
||
246 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
247 |
} |
|
248 |
||
6973 | 249 |
|
6733 | 250 |
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info) |
251 |
{ |
|
252 |
QStringList result; |
|
253 |
||
254 |
result = info; |
|
255 |
||
6973 | 256 |
// for matters of less memory usage and quicker access store |
257 |
// the boolean string as either "t" or empty |
|
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
|
258 |
if (info[StateColumn].toLower() == "true") |
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
|
259 |
result[StateColumn] = "t"; |
6973 | 260 |
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
|
261 |
result[StateColumn] = QString(); |
6973 | 262 |
|
6733 | 263 |
return result; |
264 |
} |