author | koda |
Sat, 04 Jan 2014 02:18:57 +0100 | |
branch | 0.9.20 |
changeset 9911 | 9a8fe10c1ecf |
parent 9702 | 27006953d901 |
child 9998 | 736015b847e3 |
permissions | -rw-r--r-- |
6966 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
9080 | 3 |
* Copyright (c) 2004-2013 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 |
|
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), |
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"); |
|
111 |
const QIcon roomWaitingIcon(":/res/iconTime.png"); |
|
112 |
||
113 |
if (m_data.at(row).at(0).isEmpty()) |
|
114 |
return QVariant(roomWaitingIcon); |
|
115 |
else |
|
116 |
return QVariant(roomBusyIcon); |
|
117 |
} |
|
118 |
||
119 |
QString content = m_data.at(row).at(column); |
|
120 |
||
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
121 |
if (role == Qt::DisplayRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
122 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
123 |
// supply in progress flag as bool |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
124 |
if (column == 0) |
6995 | 125 |
return QVariant(QString(!content.isEmpty())); |
6983
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 |
// display room names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
128 |
if (column == 5) |
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 |
// special names |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
131 |
if (content[0] == '+') |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
132 |
{ |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
133 |
if (content == "+rnd+") return tr("Random Map"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
134 |
if (content == "+maze+") return tr("Random Maze"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
135 |
if (content == "+drawn+") return tr("Hand-drawn"); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
136 |
} |
6973 | 137 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
138 |
// prefix ? if map not available |
8377 | 139 |
if (!m_staticMapModel->mapExists(content) && |
140 |
!m_missionMapModel->mapExists(content)) |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
141 |
return QString ("? %1").arg(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 |
return content; |
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 |
|
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
147 |
// 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
|
148 |
if (role == Qt::ForegroundRole) |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
149 |
{ |
8377 | 150 |
if (content == "+rnd+" || |
151 |
content == "+maze+" || |
|
152 |
content == "+drawn+" || |
|
153 |
m_staticMapModel->mapExists(content) || |
|
154 |
m_missionMapModel->mapExists(content)) |
|
155 |
return QVariant(); |
|
156 |
else |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
157 |
return QBrush(QColor("darkred")); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
158 |
} |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
159 |
|
6987
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
160 |
if (role == Qt::TextAlignmentRole) |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
161 |
{ |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
162 |
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
163 |
} |
e34415c77342
allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents:
6983
diff
changeset
|
164 |
|
6983
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
165 |
Q_ASSERT(false); |
ede55af89e78
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents:
6973
diff
changeset
|
166 |
return QVariant(); |
6732 | 167 |
} |
6733 | 168 |
|
6973 | 169 |
|
6733 | 170 |
void RoomsListModel::setRoomsList(const QStringList & rooms) |
171 |
{ |
|
6973 | 172 |
beginResetModel(); |
173 |
||
174 |
m_data.clear(); |
|
6733 | 175 |
|
6973 | 176 |
int nRooms = rooms.size(); |
177 |
||
178 |
for (int i = 0; i < nRooms; i += c_nColumns) |
|
6733 | 179 |
{ |
180 |
QStringList l; |
|
6973 | 181 |
|
182 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
183 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt |
|
184 |
#endif |
|
185 |
||
186 |
for (int t = 0; t < c_nColumns; t++) |
|
187 |
{ |
|
6733 | 188 |
l.append(rooms[i + t]); |
6973 | 189 |
} |
6733 | 190 |
|
191 |
m_data.append(roomInfo2RoomRecord(l)); |
|
192 |
} |
|
193 |
||
6973 | 194 |
endResetModel(); |
6733 | 195 |
} |
196 |
||
6973 | 197 |
|
6733 | 198 |
void RoomsListModel::addRoom(const QStringList & info) |
199 |
{ |
|
200 |
beginInsertRows(QModelIndex(), 0, 0); |
|
201 |
||
202 |
m_data.prepend(roomInfo2RoomRecord(info)); |
|
203 |
||
204 |
endInsertRows(); |
|
205 |
} |
|
206 |
||
6973 | 207 |
|
208 |
int RoomsListModel::rowOfRoom(const QString & name) |
|
209 |
{ |
|
210 |
int size = m_data.size(); |
|
211 |
||
212 |
if (size < 1) |
|
213 |
return -1; |
|
214 |
||
215 |
int i = 0; |
|
216 |
||
217 |
// 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
|
218 |
while(m_data[i].at(NameColumn) != name) |
6973 | 219 |
{ |
220 |
i++; |
|
221 |
if(i >= size) |
|
222 |
return -1; |
|
223 |
} |
|
224 |
||
225 |
return i; |
|
226 |
} |
|
227 |
||
228 |
||
6733 | 229 |
void RoomsListModel::removeRoom(const QString & name) |
230 |
{ |
|
6973 | 231 |
int i = rowOfRoom(name); |
232 |
||
233 |
if (i < 0) |
|
6733 | 234 |
return; |
235 |
||
236 |
beginRemoveRows(QModelIndex(), i, i); |
|
237 |
||
238 |
m_data.removeAt(i); |
|
239 |
||
240 |
endRemoveRows(); |
|
241 |
} |
|
242 |
||
6973 | 243 |
|
6733 | 244 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info) |
245 |
{ |
|
6973 | 246 |
int i = rowOfRoom(name); |
247 |
||
248 |
if (i < 0) |
|
6733 | 249 |
return; |
250 |
||
251 |
m_data[i] = roomInfo2RoomRecord(info); |
|
252 |
||
253 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1)); |
|
254 |
} |
|
255 |
||
6973 | 256 |
|
6733 | 257 |
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info) |
258 |
{ |
|
259 |
QStringList result; |
|
260 |
||
261 |
result = info; |
|
262 |
||
6973 | 263 |
// for matters of less memory usage and quicker access store |
264 |
// 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
|
265 |
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
|
266 |
result[StateColumn] = "t"; |
6973 | 267 |
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
|
268 |
result[StateColumn] = QString(); |
6973 | 269 |
|
6733 | 270 |
return result; |
271 |
} |