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