-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbssmodel.cpp
More file actions
121 lines (101 loc) · 3.11 KB
/
bssmodel.cpp
File metadata and controls
121 lines (101 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "bssmodel.h"
BssModel::BssModel(QObject *parent)
: QAbstractTableModel(parent), mMeasurements(nullptr) {}
QHash<int, QByteArray> BssModel::roleNames() const {
return {{Qt::DisplayRole, "display"}, {Qt::CheckStateRole, "checkstate"}};
}
void BssModel::measurementsChanged(Measurements *measurements) {
beginResetModel();
if (measurements)
measurements->disconnect(this);
mMeasurements = measurements;
selectedBss = {};
if (mMeasurements) {
connect(this, &BssModel::selectedBssChanged, measurements,
&Measurements::selectedBssChanged);
connect(mMeasurements, &Measurements::preBssAppended, this,
[=](int index, int count) {
beginInsertRows(QModelIndex(), index, index + count - 1);
});
connect(mMeasurements, &Measurements::postBssAppended, this,
[=]() { endInsertRows(); });
connect(mMeasurements, &Measurements::preBssRemoved, this,
[=](int index, int count) {
beginRemoveRows(QModelIndex(), index, index + count - 1);
});
connect(mMeasurements, &Measurements::postBssRemoved, this,
[=]() { endRemoveRows(); });
}
endResetModel();
}
int BssModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid() || !mMeasurements)
return 0;
return mMeasurements->bsss().size();
}
QVariant BssModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || !mMeasurements)
return {};
auto bss = mMeasurements->bsss().at(index.row());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0:
return bss.ssid;
case 1:
return bss.bssid;
case 2:
return bss.freq;
case 3:
return bss.channel;
default:
break;
}
}
if (role == Qt::CheckStateRole && index.column() == 4) {
auto iter = std::find(selectedBss.begin(), selectedBss.end(), bss);
if (iter != selectedBss.end()) {
return Qt::Checked;
} else {
return Qt::Unchecked;
}
}
return {};
}
QVariant BssModel::headerData(int section, Qt::Orientation orientation,
int role) const {
switch (section) {
case 0:
return "ssid";
case 1:
return "bss";
case 2:
return "freq";
case 3:
return "ch";
default:
return "";
}
}
int BssModel::columnCount(const QModelIndex &parent) const { return 5; }
bool BssModel::setData(const QModelIndex &index, const QVariant &value,
int role) {
if (!index.isValid() || !mMeasurements || !value.isValid())
return false;
if (role == Qt::CheckStateRole && index.column() == 4) {
auto checkedNew = value.toBool();
auto bss = mMeasurements->bsss().at(index.row());
auto iter = std::find(selectedBss.begin(), selectedBss.end(), bss);
auto checkedOld = iter != selectedBss.end();
if (checkedOld && !checkedNew) {
selectedBss.erase(iter);
} else if (!checkedOld && checkedNew) {
selectedBss.push_back(bss);
} else {
return false;
}
emit selectedBssChanged(selectedBss);
emit dataChanged(index, index, {role});
return true;
}
return false;
}