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