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