From c21845aa016786fb92a08c5d01029218ad1e10fb Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 16 Jul 2017 21:45:21 -0700 Subject: [PATCH] Add ConnectionListModel We'll use that to do a better job of showing the connection used when talking to a dive computer. Signed-off-by: Dirk Hohndel --- core/btdiscovery.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ core/btdiscovery.h | 17 +++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/core/btdiscovery.cpp b/core/btdiscovery.cpp index c25e6cfda..a9f8c861a 100644 --- a/core/btdiscovery.cpp +++ b/core/btdiscovery.cpp @@ -9,6 +9,47 @@ extern QMap descriptorLookup; BTDiscovery *BTDiscovery::m_instance = NULL; +ConnectionListModel::ConnectionListModel(QObject *parent) : + QAbstractListModel(parent) +{ +} + +QHash ConnectionListModel::roleNames() const +{ + QHash roles; + roles[AddressRole] = "address"; + return roles; +} + +QVariant ConnectionListModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < 0 || index.row() >= m_addresses.count()) + return QVariant(); + if (role != AddressRole) + return QVariant(); + return m_addresses[index.row()]; +} + +QString ConnectionListModel::address(int idx) const +{ + if (idx < 0 || idx >> m_addresses.count()) + return QString(); + return m_addresses[idx]; +} + +int ConnectionListModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_addresses.count(); +} + +void ConnectionListModel::addAddress(const QString address) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + m_addresses.append(address); + endInsertRows(); +} + static dc_descriptor_t *getDeviceType(QString btName) // central function to convert a BT name to a Subsurface known vendor/model pair { diff --git a/core/btdiscovery.h b/core/btdiscovery.h index 4b4e4b802..71df24f24 100644 --- a/core/btdiscovery.h +++ b/core/btdiscovery.h @@ -5,6 +5,7 @@ #include #include #include +#include #if defined(BT_SUPPORT) #include #include @@ -17,6 +18,22 @@ #include #endif +class ConnectionListModel : public QAbstractListModel { + Q_OBJECT +public: + enum CLMRole { + AddressRole = Qt::UserRole + 1 + }; + ConnectionListModel(QObject *parent = 0); + QHash roleNames() const; + QVariant data(const QModelIndex &index, int role = AddressRole) const; + QString address(int idx) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + void addAddress(const QString address); +private: + QStringList m_addresses; +}; + class BTDiscovery : public QObject { Q_OBJECT