-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth_device_list_model.cpp
More file actions
84 lines (71 loc) · 2.05 KB
/
Copy pathbluetooth_device_list_model.cpp
File metadata and controls
84 lines (71 loc) · 2.05 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
#include "bluetooth_device_list_model.h"
#include <QBluetoothLocalDevice>
BluetoothDeviceListModel::BluetoothDeviceListModel(QObject *parent)
: QAbstractListModel(parent)
{
update();
}
QVariant BluetoothDeviceListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
Q_UNUSED(orientation);
Q_UNUSED(role);
if (role == Qt::DisplayRole) {
return QVariant(tr("Device Address"));
} else {
return QVariant();
}
}
int BluetoothDeviceListModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
return devices_.size();
}
QVariant BluetoothDeviceListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
return QVariant(devices_[index.row()]);
} else {
return QVariant();
}
}
QModelIndex BluetoothDeviceListModel::indexOf(const QString &adapter) const
{
int pos = devices_.indexOf(adapter);
if (pos == -1) {
return QModelIndex();
}
return index(pos);
}
void BluetoothDeviceListModel::update()
{
QStringList all;
foreach (auto adapter, QBluetoothLocalDevice::allDevices()) {
all << adapter.address().toString();
}
QStringList remove_list;
foreach (auto addr, devices_) {
if (!all.contains(addr)) {
remove_list << addr;
}
}
foreach (auto addr, remove_list) {
int pos = devices_.indexOf(addr);
beginRemoveRows(QModelIndex(), pos, pos);
devices_.removeAt(pos);
endRemoveRows();
}
foreach(auto addr, all) {
if (!devices_.contains(addr)) {
int pos = devices_.size();
beginInsertRows(QModelIndex(), pos, pos);
devices_.append(addr);
endInsertRows();
}
}
}