summaryrefslogtreecommitdiffstats
path: root/bluetooth/bluetoothmodel.h
blob: 0fc07aaca24ae92f6b5b2b9f0e49b70cc8ff25ab (plain)
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
#ifndef BLUETOOTH_MODEL_H
#define BLUETOOTH_MODEL_H

#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include <QStringList>
#include <QtQml/QQmlContext>
#include <QJsonObject>

class BluetoothDevice
{
    public:
        BluetoothDevice(const QString &id,
                        const QString &address,
                        const QString &name,
                        const bool paired,
                        const bool connected);
        QString id() const;
        QString address() const;
        QString name() const;
        bool paired() const;
        bool connected() const;
        void setId(const QString id);
        void setAddress(const QString address);
        void setName(const QString name);
        void setPaired(const bool paired);
        void setConnected(const bool connected);

    private:
        QString m_id;
        QString m_address;
        QString m_name;
        bool m_paired;
        bool m_connected;
};

class BluetoothModel : public QAbstractListModel
{
    Q_OBJECT

    public:
        enum BluetoothRoles {
            IdRole = Qt::UserRole + 1,
            AddressRole,
            NameRole,
            PairedRole,
            ConnectedRole
        };

        BluetoothModel(QObject *parent = Q_NULLPTR);

        void addDevice(BluetoothDevice *device);
        void removeDevice(BluetoothDevice *device);
        void removeAllDevices();
        BluetoothDevice *getDevice(QString address);
        BluetoothDevice *updateDeviceProperties(BluetoothDevice *device, QJsonObject data);
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    signals:
        void propertiesChanged(int connected);

    protected:
        QHash<int, QByteArray> roleNames() const;

    private:
        QList<BluetoothDevice *> m_devices;
        QModelIndex indexOf(BluetoothDevice *device);
};

class BluetoothModelFilter : public QSortFilterProxyModel
{
    Q_OBJECT

    public:
        BluetoothModelFilter(QObject *parent = nullptr);

    protected:
        bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};
#endif // BLUETOOTH_MODEL_H