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 |
|
|
26 |
RoomsListModel::RoomsListModel(QObject *parent) :
|
|
27 |
QAbstractTableModel(parent)
|
|
28 |
{
|
|
29 |
m_headerData =
|
|
30 |
QStringList()
|
6736
|
31 |
<< QString()
|
6732
|
32 |
<< tr("Room Name")
|
|
33 |
<< tr("C")
|
|
34 |
<< tr("T")
|
|
35 |
<< tr("Owner")
|
|
36 |
<< tr("Map")
|
|
37 |
<< tr("Rules")
|
|
38 |
<< tr("Weapons");
|
|
39 |
}
|
|
40 |
|
|
41 |
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
42 |
{
|
|
43 |
if(orientation == Qt::Vertical || role != Qt::DisplayRole)
|
|
44 |
return QVariant();
|
|
45 |
else
|
|
46 |
return QVariant(m_headerData.at(section));
|
|
47 |
}
|
|
48 |
|
|
49 |
int RoomsListModel::rowCount(const QModelIndex & parent) const
|
|
50 |
{
|
|
51 |
if(parent.isValid())
|
|
52 |
return 0;
|
|
53 |
else
|
|
54 |
return m_data.size();
|
|
55 |
}
|
|
56 |
|
|
57 |
int RoomsListModel::columnCount(const QModelIndex & parent) const
|
|
58 |
{
|
|
59 |
if(parent.isValid())
|
|
60 |
return 0;
|
|
61 |
else
|
6736
|
62 |
return 8;
|
6732
|
63 |
}
|
|
64 |
|
|
65 |
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
|
|
66 |
{
|
|
67 |
if (!index.isValid() || index.row() < 0
|
|
68 |
|| index.row() >= m_data.size()
|
6736
|
69 |
|| index.column() >= 8
|
6732
|
70 |
|| (role != Qt::EditRole && role != Qt::DisplayRole)
|
|
71 |
)
|
|
72 |
return QVariant();
|
|
73 |
|
|
74 |
return m_data.at(index.row()).at(index.column());
|
|
75 |
}
|
6733
|
76 |
|
|
77 |
void RoomsListModel::setRoomsList(const QStringList & rooms)
|
|
78 |
{
|
|
79 |
if(m_data.size())
|
|
80 |
{
|
|
81 |
beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
|
|
82 |
m_data.clear();
|
|
83 |
endRemoveRows();
|
|
84 |
}
|
|
85 |
|
|
86 |
for(int i = 0; i < rooms.size(); i += 8)
|
|
87 |
{
|
|
88 |
QStringList l;
|
6757
|
89 |
//l.reserve(8); not really that useful an optimisation and causes problems w/ old Qt. Harmless to leave it out.
|
6733
|
90 |
for(int t = 0; t < 8; ++t)
|
|
91 |
l.append(rooms[i + t]);
|
|
92 |
|
|
93 |
m_data.append(roomInfo2RoomRecord(l));
|
|
94 |
}
|
|
95 |
|
|
96 |
beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
|
|
97 |
endInsertRows();
|
|
98 |
}
|
|
99 |
|
|
100 |
void RoomsListModel::addRoom(const QStringList & info)
|
|
101 |
{
|
|
102 |
beginInsertRows(QModelIndex(), 0, 0);
|
|
103 |
|
|
104 |
m_data.prepend(roomInfo2RoomRecord(info));
|
|
105 |
|
|
106 |
endInsertRows();
|
|
107 |
}
|
|
108 |
|
|
109 |
void RoomsListModel::removeRoom(const QString & name)
|
|
110 |
{
|
|
111 |
int i = 0;
|
|
112 |
while(i < m_data.size() && m_data[i].at(0) != name)
|
|
113 |
++i;
|
|
114 |
if(i >= m_data.size())
|
|
115 |
return;
|
|
116 |
|
|
117 |
beginRemoveRows(QModelIndex(), i, i);
|
|
118 |
|
|
119 |
m_data.removeAt(i);
|
|
120 |
|
|
121 |
endRemoveRows();
|
|
122 |
}
|
|
123 |
|
|
124 |
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
|
|
125 |
{
|
|
126 |
int i = 0;
|
|
127 |
while(i < m_data.size() && m_data[i].at(0) != name)
|
|
128 |
++i;
|
|
129 |
if(i >= m_data.size())
|
|
130 |
return;
|
|
131 |
|
|
132 |
|
|
133 |
m_data[i] = roomInfo2RoomRecord(info);
|
|
134 |
|
|
135 |
emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
|
|
136 |
}
|
|
137 |
|
|
138 |
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
|
|
139 |
{
|
|
140 |
QStringList result;
|
|
141 |
|
|
142 |
result = info;
|
|
143 |
|
|
144 |
return result;
|
|
145 |
}
|