#include "qbluetoothleuartdevicemodel.h" QBluetoothLeUartDeviceModel::QBluetoothLeUartDeviceModel(QList availableDevices, QObject* parent) : QAbstractListModel(parent) { this->availableDevices = availableDevices; connect(this, &QBluetoothLeUartDeviceModel::rowsInserted, this, &QBluetoothLeUartDeviceModel::rowCountChanged); connect(this, &QBluetoothLeUartDeviceModel::rowsRemoved, this, &QBluetoothLeUartDeviceModel::rowCountChanged); } int QBluetoothLeUartDeviceModel::rowCount(const QModelIndex &) const { return this->availableDevices.length(); } QVariant QBluetoothLeUartDeviceModel::data(const QModelIndex &index, int role) const { if (index.row() < rowCount()) switch (role) { case NameRole: return this->availableDevices[index.row()]->getName(); case IdRole: return index.row(); case AddressRole: return this->availableDevices[index.row()]->getAddress(); case DeviceRole: return QVariant::fromValue(this->availableDevices[index.row()]); default: return QVariant(); } return QVariant(); } QHash QBluetoothLeUartDeviceModel::roleNames() const { static const QHash roles { { NameRole, "name" }, { IdRole, "id" }, { AddressRole, "address"}, { DeviceRole, "device" } }; return roles; } void QBluetoothLeUartDeviceModel::append(QBluetoothLeUartDevice* device) { foreach(QBluetoothLeUartDevice* existingDevice, this->availableDevices){ if(device == existingDevice) // dublicates aren't allowed return; } int row = this->availableDevices.length(); this->beginInsertRows(QModelIndex(), row, row); this->availableDevices.insert(row, device); this->endInsertRows(); } void QBluetoothLeUartDeviceModel::remove(int row) { if (row < 0 || row >= this->availableDevices.length()) return; beginRemoveRows(QModelIndex(), row, row); this->availableDevices.removeAt(row); endRemoveRows(); } void QBluetoothLeUartDeviceModel::clear() { this->beginResetModel(); this->resetInternalData(); this->availableDevices.clear(); this->endResetModel(); }