QBluetoothLeUart/qbluetoothleuartdevicemodel...

65 lines
1.9 KiB
C++

#include "qbluetoothleuartdevicemodel.h"
QBluetoothLeUartDeviceModel::QBluetoothLeUartDeviceModel(QList<QBluetoothLeUartDevice*> availableDevices, QObject* parent) : QAbstractListModel(parent)
{
this->availableDevices = availableDevices;
}
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<int, QByteArray> QBluetoothLeUartDeviceModel::roleNames() const
{
static const QHash<int, QByteArray> 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() {
for(int i = 0; i < this->availableDevices.length(); i++)
this->remove(i);
}