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