41#include "qdeclarativeinputdevicemodel_p.h"
42#include "qinputinfo.h"
44QDeclarativeInputDeviceModel::QDeclarativeInputDeviceModel(QObject *parent) :
45 QAbstractListModel(parent),
46 deviceInfo(new QInputDeviceManager),
47 currentFilter(QInputDevice::Unknown)
49 connect(deviceInfo,SIGNAL(ready()),
this,SLOT(updateDeviceList()));
50 connect(deviceInfo, &QInputDeviceManager::deviceAdded,
this,&QDeclarativeInputDeviceModel::addedDevice);
51 connect(deviceInfo, &QInputDeviceManager::deviceRemoved,
this,&QDeclarativeInputDeviceModel::removedDevice);
54QDeclarativeInputDeviceModel::~QDeclarativeInputDeviceModel()
59QVariant QDeclarativeInputDeviceModel::data(
const QModelIndex &index,
int role)
const
63 return QVariant::fromValue(
static_cast<QObject *
>(inputDevices.value(index.row())));
66 return QVariant::fromValue(
static_cast<QString
>(inputDevices.value(index.row())->name()));
69 return QVariant::fromValue(
static_cast<QString
>(inputDevices.value(index.row())->devicePath()));
72 return QVariant::fromValue(
static_cast<QList <int>
>(inputDevices.value(index.row())->buttons()));
75 return QVariant::fromValue(
static_cast<QList <int>
>(inputDevices.value(index.row())->switches()));
77 case RelativeAxisRole:
78 return QVariant::fromValue(
static_cast<QList <int>
>(inputDevices.value(index.row())->relativeAxis()));
80 case AbsoluteAxisRole:
81 return QVariant::fromValue(
static_cast<QList <int>
>(inputDevices.value(index.row())->absoluteAxis()));
84 return QVariant::fromValue(
static_cast<int>(inputDevices.value(index.row())->type()));
91int QDeclarativeInputDeviceModel::rowCount(
const QModelIndex &parent)
const
95 return inputDevices.count();
98int QDeclarativeInputDeviceModel::indexOf(
const QString &devicePath)
const
101 Q_FOREACH (QInputDevice *device, inputDevices) {
103 if (device->devicePath() == devicePath)
return idx;
109QInputDevice *QDeclarativeInputDeviceModel::get(
int index)
const
111 if (index < 0 || index > inputDevices.count())
113 return inputDevices.value(index);
116void QDeclarativeInputDeviceModel::updateDeviceList()
118 QVector <QInputDevice *> newDevices = deviceInfo->deviceListOfType(currentFilter);
120 int numNew = newDevices.count();
122 for (
int i = 0; i < numNew; i++) {
123 int j = inputDevices.indexOf(newDevices.value(i));
126 beginInsertRows(QModelIndex(), i, i);
127 inputDevices.insert(i, newDevices.value(i));
129 Q_EMIT countChanged();
132 QInputDevice* device = inputDevices.value(j);
133 beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
134 inputDevices.remove(j);
135 inputDevices.insert(i, device);
137 Q_EMIT countChanged();
139 QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
140 Q_EMIT dataChanged(changedIndex, changedIndex);
143 int numOld = inputDevices.count();
144 if (numOld > numNew) {
145 beginRemoveRows(QModelIndex(), numNew, numOld - 1);
146 inputDevices.remove(numNew, numOld - numNew);
148 Q_EMIT countChanged();
152void QDeclarativeInputDeviceModel::addedDevice(
const QString &devicePath)
155 Q_EMIT deviceAdded(devicePath);
158void QDeclarativeInputDeviceModel::removedDevice(
const QString &devicePath)
161 Q_EMIT deviceRemoved(devicePath);
164QHash<int,QByteArray> QDeclarativeInputDeviceModel::roleNames()
const
166 QHash<int, QByteArray> roles;
167 roles[NameRole] =
"name";
168 roles[DevicePathRole] =
"devicePath";
169 roles[ButtonsRole] =
"buttons";
170 roles[SwitchesRole] =
"switches";
171 roles[RelativeAxisRole] =
"rAxis";
172 roles[AbsoluteAxisRole] =
"aAxis";
173 roles[TypesRole] =
"types";
180QInputDevice::InputType QDeclarativeInputDeviceModel::deviceFilter()
182 return currentFilter;
188void QDeclarativeInputDeviceModel::setDeviceFilter(QInputDevice::InputType filter)
190 if (filter != currentFilter) {
191 deviceInfo->setDeviceFilter(filter);
192 currentFilter = filter;
194 Q_EMIT deviceFilterChanged(filter);