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 |
|
6973
|
26 |
#include <QIcon>
|
|
27 |
|
6732
|
28 |
RoomsListModel::RoomsListModel(QObject *parent) :
|
6973
|
29 |
QAbstractTableModel(parent),
|
|
30 |
c_nColumns(8)
|
6732
|
31 |
{
|
|
32 |
m_headerData =
|
|
33 |
QStringList()
|
6973
|
34 |
<< tr("In progress")
|
6732
|
35 |
<< tr("Room Name")
|
|
36 |
<< tr("C")
|
|
37 |
<< tr("T")
|
|
38 |
<< tr("Owner")
|
|
39 |
<< tr("Map")
|
|
40 |
<< tr("Rules")
|
|
41 |
<< tr("Weapons");
|
|
42 |
}
|
|
43 |
|
6973
|
44 |
|
6732
|
45 |
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
46 |
{
|
|
47 |
if(orientation == Qt::Vertical || role != Qt::DisplayRole)
|
|
48 |
return QVariant();
|
|
49 |
else
|
|
50 |
return QVariant(m_headerData.at(section));
|
|
51 |
}
|
|
52 |
|
6973
|
53 |
|
6732
|
54 |
int RoomsListModel::rowCount(const QModelIndex & parent) const
|
|
55 |
{
|
|
56 |
if(parent.isValid())
|
|
57 |
return 0;
|
|
58 |
else
|
|
59 |
return m_data.size();
|
|
60 |
}
|
|
61 |
|
6973
|
62 |
|
6732
|
63 |
int RoomsListModel::columnCount(const QModelIndex & parent) const
|
|
64 |
{
|
|
65 |
if(parent.isValid())
|
|
66 |
return 0;
|
|
67 |
else
|
6973
|
68 |
return c_nColumns;
|
6732
|
69 |
}
|
|
70 |
|
6973
|
71 |
|
6732
|
72 |
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
|
|
73 |
{
|
6973
|
74 |
int column = index.column();
|
|
75 |
int row = index.row();
|
|
76 |
|
|
77 |
if (!index.isValid() || (row < 0)
|
|
78 |
|| (row >= m_data.size())
|
|
79 |
|| (column >= c_nColumns)
|
|
80 |
|| ((role != Qt::DecorationRole) && (role != Qt::DisplayRole))
|
6732
|
81 |
)
|
|
82 |
return QVariant();
|
|
83 |
|
6973
|
84 |
// decorate room name based on room state
|
|
85 |
if (role == Qt::DecorationRole)
|
|
86 |
{
|
|
87 |
if (column != 1)
|
|
88 |
return QVariant();
|
|
89 |
|
|
90 |
const QIcon roomBusyIcon(":/res/iconDamage.png");
|
|
91 |
const QIcon roomWaitingIcon(":/res/iconTime.png");
|
|
92 |
|
|
93 |
if (m_data.at(row).at(0).isEmpty())
|
|
94 |
return QVariant(roomWaitingIcon);
|
|
95 |
else
|
|
96 |
return QVariant(roomBusyIcon);
|
|
97 |
}
|
|
98 |
|
|
99 |
QString content = m_data.at(row).at(column);
|
|
100 |
|
|
101 |
if (column == 0)
|
|
102 |
return QVariant(!content.isEmpty());
|
|
103 |
|
|
104 |
return content;
|
6732
|
105 |
}
|
6733
|
106 |
|
6973
|
107 |
|
6733
|
108 |
void RoomsListModel::setRoomsList(const QStringList & rooms)
|
|
109 |
{
|
6973
|
110 |
beginResetModel();
|
|
111 |
|
|
112 |
m_data.clear();
|
6733
|
113 |
|
6973
|
114 |
int nRooms = rooms.size();
|
|
115 |
|
|
116 |
for (int i = 0; i < nRooms; i += c_nColumns)
|
6733
|
117 |
{
|
|
118 |
QStringList l;
|
6973
|
119 |
|
|
120 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
|
|
121 |
l.reserve(c_nColumns); // small optimisation not supported in old Qt
|
|
122 |
#endif
|
|
123 |
|
|
124 |
for (int t = 0; t < c_nColumns; t++)
|
|
125 |
{
|
6733
|
126 |
l.append(rooms[i + t]);
|
6973
|
127 |
}
|
6733
|
128 |
|
|
129 |
m_data.append(roomInfo2RoomRecord(l));
|
|
130 |
}
|
|
131 |
|
6973
|
132 |
endResetModel();
|
6733
|
133 |
}
|
|
134 |
|
6973
|
135 |
|
6733
|
136 |
void RoomsListModel::addRoom(const QStringList & info)
|
|
137 |
{
|
|
138 |
beginInsertRows(QModelIndex(), 0, 0);
|
|
139 |
|
|
140 |
m_data.prepend(roomInfo2RoomRecord(info));
|
|
141 |
|
|
142 |
endInsertRows();
|
|
143 |
}
|
|
144 |
|
6973
|
145 |
|
|
146 |
int RoomsListModel::rowOfRoom(const QString & name)
|
|
147 |
{
|
|
148 |
int size = m_data.size();
|
|
149 |
|
|
150 |
if (size < 1)
|
|
151 |
return -1;
|
|
152 |
|
|
153 |
int i = 0;
|
|
154 |
|
|
155 |
// search for record with matching room name
|
|
156 |
while(m_data[i].at(1) != name)
|
|
157 |
{
|
|
158 |
i++;
|
|
159 |
if(i >= size)
|
|
160 |
return -1;
|
|
161 |
}
|
|
162 |
|
|
163 |
return i;
|
|
164 |
}
|
|
165 |
|
|
166 |
|
6733
|
167 |
void RoomsListModel::removeRoom(const QString & name)
|
|
168 |
{
|
6973
|
169 |
int i = rowOfRoom(name);
|
|
170 |
|
|
171 |
if (i < 0)
|
6733
|
172 |
return;
|
|
173 |
|
|
174 |
beginRemoveRows(QModelIndex(), i, i);
|
|
175 |
|
|
176 |
m_data.removeAt(i);
|
|
177 |
|
|
178 |
endRemoveRows();
|
|
179 |
}
|
|
180 |
|
6973
|
181 |
|
6733
|
182 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
|
|
183 |
{
|
6973
|
184 |
int i = rowOfRoom(name);
|
|
185 |
|
|
186 |
if (i < 0)
|
6733
|
187 |
return;
|
|
188 |
|
|
189 |
m_data[i] = roomInfo2RoomRecord(info);
|
|
190 |
|
|
191 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
|
|
192 |
}
|
|
193 |
|
6973
|
194 |
|
6733
|
195 |
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
|
|
196 |
{
|
|
197 |
QStringList result;
|
|
198 |
|
|
199 |
result = info;
|
|
200 |
|
6973
|
201 |
// for matters of less memory usage and quicker access store
|
|
202 |
// the boolean string as either "t" or empty
|
|
203 |
if (info[0].toLower() == "true")
|
|
204 |
result[0] = "t";
|
|
205 |
else
|
|
206 |
result[0] = QString();
|
|
207 |
|
6733
|
208 |
return result;
|
|
209 |
}
|