41 #include "qdeclarativeinputdeviceinfo_p.h"
43 QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
44 QAbstractListModel(parent),
45 deviceInfo(new QInputDeviceInfo)
47 connect(deviceInfo, &QInputDeviceInfo::ready,
this, &QDeclarativeInputDeviceInfo::updateDeviceList);
48 connect(deviceInfo, &QInputDeviceInfo::deviceAdded,
this,&QDeclarativeInputDeviceInfo::addedDevice);
49 connect(deviceInfo, &QInputDeviceInfo::deviceRemoved,
this,&QDeclarativeInputDeviceInfo::removedDevice);
52 QDeclarativeInputDeviceInfo::~QDeclarativeInputDeviceInfo()
57 QVariant QDeclarativeInputDeviceInfo::data(
const QModelIndex &index,
int role)
const
61 return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
67 int QDeclarativeInputDeviceInfo::rowCount(
const QModelIndex &parent)
const
71 return inputDevices.count();
74 int QDeclarativeInputDeviceInfo::indexOf(
const QString &devicePath)
const
77 Q_FOREACH (QInputDevice *device, inputDevices) {
79 if (device->devicePath() == devicePath)
return idx;
85 QInputDevice *QDeclarativeInputDeviceInfo::get(
int index)
const
87 if (index < 0 || index > inputDevices.count())
89 return inputDevices.value(index);
92 void QDeclarativeInputDeviceInfo::updateDeviceList()
94 QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
96 int numNew = newDevices.count();
98 for (
int i = 0; i < numNew; i++) {
99 int j = inputDevices.indexOf(newDevices.value(i));
102 beginInsertRows(QModelIndex(), i, i);
103 inputDevices.insert(i, newDevices.value(i));
107 QInputDevice* device = inputDevices.value(j);
108 beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
109 inputDevices.remove(j);
110 inputDevices.insert(i, device);
113 QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
114 Q_EMIT dataChanged(changedIndex, changedIndex);
118 int numOld = inputDevices.count();
119 if (numOld > numNew) {
120 beginRemoveRows(QModelIndex(), numNew, numOld - 1);
121 inputDevices.remove(numNew, numOld - numNew);
126 void QDeclarativeInputDeviceInfo::addedDevice(
const QString &devicePath)
129 Q_EMIT newDevice(devicePath);
132 void QDeclarativeInputDeviceInfo::removedDevice(
const QString &devicePath)
135 Q_EMIT deviceRemoved(devicePath);
138 QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames()
const
140 QHash<int, QByteArray> roles;
141 roles.insert(ServiceRole,
"service");