| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 Canonical Limited and/or its subsidiary(-ies). |
| 4 | ** Copyright (C) 2018 Jolla. |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qdeclarativeinputdevicemodel_p.h" |
| 42 | #include "qinputinfo.h" |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | QDeclarativeInputDeviceModel::QDeclarativeInputDeviceModel(QObject *parent) : |
| 47 | QAbstractListModel(parent), |
| 48 | deviceInfoManager(new QInputInfoManager), |
| 49 | currentFilter(QInputDevice::UnknownType) |
| 50 | { |
| 51 | connect(deviceInfoManager,SIGNAL(ready()),this,SLOT(updateDeviceList())); |
| 52 | connect(deviceInfoManager,SIGNAL(filterChanged(QInputDevice::InputTypeFlags)), |
| 53 | this,SLOT(updateDeviceList())); |
| 54 | |
| 55 | connect(deviceInfoManager, &QInputInfoManager::deviceAdded, |
| 56 | this,&QDeclarativeInputDeviceModel::addedDevice); |
| 57 | connect(deviceInfoManager, &QInputInfoManager::deviceRemoved, |
| 58 | this,&QDeclarativeInputDeviceModel::removedDevice); |
| 59 | connect(deviceInfoManager, &QInputInfoManager::countChanged, |
| 60 | this,&QDeclarativeInputDeviceModel::countChanged); |
| 61 | } |
| 62 | |
| 63 | QDeclarativeInputDeviceModel::~QDeclarativeInputDeviceModel() |
| 64 | { |
| 65 | delete deviceInfoManager; |
| 66 | } |
| 67 | |
| 68 | QVariant QDeclarativeInputDeviceModel::data(const QModelIndex &index, int role) const |
| 69 | { |
| 70 | QInputDevice *device = inputDevices.value(index.row()); |
| 71 | if (!device) |
| 72 | return QVariant(); |
| 73 | |
| 74 | switch (role) { |
| 75 | case ServiceRole: |
| 76 | return QVariant::fromValue(static_cast<QObject *>(device)); |
| 77 | break; |
| 78 | case NameRole: |
| 79 | return QVariant::fromValue(static_cast<QString>(device->name())); |
| 80 | break; |
| 81 | case IdentifierRole: |
| 82 | return QVariant::fromValue(static_cast<QString>(device->identifier())); |
| 83 | break; |
| 84 | case ButtonsRole: |
| 85 | return QVariant::fromValue(static_cast<QList <int> >(device->buttons())); |
| 86 | break; |
| 87 | case SwitchesRole: |
| 88 | return QVariant::fromValue(static_cast<QList <int> >(device->switches())); |
| 89 | break; |
| 90 | case RelativeAxesRole: |
| 91 | return QVariant::fromValue(static_cast<QList <int> >(device->relativeAxes())); |
| 92 | break; |
| 93 | case AbsoluteAxesRole: |
| 94 | return QVariant::fromValue(static_cast<QList <int> >(device->absoluteAxes())); |
| 95 | break; |
| 96 | case TypesRole: |
| 97 | return QVariant::fromValue(static_cast<int>(device->types())); |
| 98 | break; |
| 99 | }; |
| 100 | |
| 101 | return QVariant(); |
| 102 | } |
| 103 | |
| 104 | int QDeclarativeInputDeviceModel::rowCount(const QModelIndex &parent) const |
| 105 | { |
| 106 | Q_UNUSED(parent); |
| 107 | |
| 108 | return inputDevices.count(); |
| 109 | } |
| 110 | |
| 111 | int QDeclarativeInputDeviceModel::indexOf(const QString &devicePath) const |
| 112 | { |
| 113 | int idx(-1); |
| 114 | Q_FOREACH (QInputDevice *device, inputDevices) { |
| 115 | idx++; |
| 116 | if (device->identifier() == devicePath) return idx; |
| 117 | } |
| 118 | |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | QInputDevice *QDeclarativeInputDeviceModel::get(int index) const |
| 123 | { |
| 124 | if (index < 0 || index > inputDevices.count()) |
| 125 | return 0; |
| 126 | return inputDevices.value(index); |
| 127 | } |
| 128 | |
| 129 | void QDeclarativeInputDeviceModel::updateDeviceList() |
| 130 | { |
| 131 | QList <QInputDevice *> newDevices = deviceInfoManager->deviceMap().values(); |
| 132 | int numNew = newDevices.count(); |
| 133 | |
| 134 | for (int i = 0; i < numNew; i++) { |
| 135 | int j = inputDevices.indexOf(newDevices.value(i)); |
| 136 | |
| 137 | if (j == -1) { |
| 138 | beginInsertRows(QModelIndex(), i, i); |
| 139 | inputDevices.insert(i, newDevices.value(i)); |
| 140 | endInsertRows(); |
| 141 | } else if (i != j) { |
| 142 | // changed its position -> move it |
| 143 | QInputDevice* device = inputDevices.value(j); |
| 144 | beginMoveRows(QModelIndex(), j, j, QModelIndex(), i); |
| 145 | inputDevices.remove(j); |
| 146 | if (i >= inputDevices.size()) |
| 147 | inputDevices.resize(i + 1); |
| 148 | inputDevices.insert(i, device); |
| 149 | endMoveRows(); |
| 150 | } //else { |
| 151 | QModelIndex changedIndex(this->index(j, 0, QModelIndex())); |
| 152 | Q_EMIT dataChanged(changedIndex, changedIndex); |
| 153 | } |
| 154 | |
| 155 | int numOld = inputDevices.count(); |
| 156 | if (numOld > numNew) { |
| 157 | beginRemoveRows(QModelIndex(), numNew, numOld - 1); |
| 158 | inputDevices.remove(numNew, numOld - numNew); |
| 159 | endRemoveRows(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void QDeclarativeInputDeviceModel::addedDevice(QInputDevice *device) |
| 164 | { |
| 165 | updateDeviceList(); |
| 166 | setFilter(currentFilter); |
| 167 | Q_EMIT added(device); |
| 168 | } |
| 169 | |
| 170 | void QDeclarativeInputDeviceModel::removedDevice(const QString &devicePath) |
| 171 | { |
| 172 | updateDeviceList(); |
| 173 | setFilter(currentFilter); |
| 174 | Q_EMIT removed(devicePath); |
| 175 | } |
| 176 | |
| 177 | QHash<int,QByteArray> QDeclarativeInputDeviceModel::roleNames() const |
| 178 | { |
| 179 | QHash<int, QByteArray> roles; |
| 180 | roles[NameRole] = "name"; |
| 181 | roles[IdentifierRole] = "identifier"; |
| 182 | roles[ButtonsRole] = "buttons"; |
| 183 | roles[SwitchesRole] = "switches"; |
| 184 | roles[RelativeAxesRole] = "rAxis"; |
| 185 | roles[AbsoluteAxesRole] = "aAxis"; |
| 186 | roles[TypesRole] = "types"; |
| 187 | return roles; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Returns the currently set device filter. |
| 192 | * */ |
| 193 | QInputDevice::InputTypeFlags QDeclarativeInputDeviceModel::filter() |
| 194 | { |
| 195 | return currentFilter; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Sets the current input device filter to filter. |
| 200 | * */ |
| 201 | void QDeclarativeInputDeviceModel::setFilter(QInputDevice::InputTypeFlags filter) |
| 202 | { |
| 203 | if (filter != currentFilter) { |
| 204 | deviceInfoManager->setFilter(filter); |
| 205 | currentFilter = filter; |
| 206 | Q_EMIT filterChanged(filter); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | QT_END_NAMESPACE |