Lomiri
Loading...
Searching...
No Matches
qdeclarativeinputdevicemodel.cpp
1/****************************************************************************
2**
3** Copyright (C) 2015 Jolla.
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtSystems module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41#include "qdeclarativeinputdevicemodel_p.h"
42#include "qinputinfo.h"
43
44QDeclarativeInputDeviceModel::QDeclarativeInputDeviceModel(QObject *parent) :
45 QAbstractListModel(parent),
46 deviceInfo(new QInputDeviceManager),
47 currentFilter(QInputDevice::Unknown)
48{
49 connect(deviceInfo,SIGNAL(ready()),this,SLOT(updateDeviceList()));
50 connect(deviceInfo, &QInputDeviceManager::deviceAdded,this,&QDeclarativeInputDeviceModel::addedDevice);
51 connect(deviceInfo, &QInputDeviceManager::deviceRemoved,this,&QDeclarativeInputDeviceModel::removedDevice);
52}
53
54QDeclarativeInputDeviceModel::~QDeclarativeInputDeviceModel()
55{
56 delete deviceInfo;
57}
58
59QVariant QDeclarativeInputDeviceModel::data(const QModelIndex &index, int role) const
60{
61 switch (role) {
62 case ServiceRole:
63 return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
64 break;
65 case NameRole:
66 return QVariant::fromValue(static_cast<QString>(inputDevices.value(index.row())->name()));
67 break;
68 case DevicePathRole:
69 return QVariant::fromValue(static_cast<QString>(inputDevices.value(index.row())->devicePath()));
70 break;
71 case ButtonsRole:
72 return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->buttons()));
73 break;
74 case SwitchesRole:
75 return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->switches()));
76 break;
77 case RelativeAxisRole:
78 return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->relativeAxis()));
79 break;
80 case AbsoluteAxisRole:
81 return QVariant::fromValue(static_cast<QList <int> >(inputDevices.value(index.row())->absoluteAxis()));
82 break;
83 case TypesRole:
84 return QVariant::fromValue(static_cast<int>(inputDevices.value(index.row())->type()));
85 break;
86 };
87
88 return QVariant();
89}
90
91int QDeclarativeInputDeviceModel::rowCount(const QModelIndex &parent) const
92{
93 Q_UNUSED(parent);
94
95 return inputDevices.count();
96}
97
98int QDeclarativeInputDeviceModel::indexOf(const QString &devicePath) const
99{
100 int idx(-1);
101 Q_FOREACH (QInputDevice *device, inputDevices) {
102 idx++;
103 if (device->devicePath() == devicePath) return idx;
104 }
105
106 return -1;
107}
108
109QInputDevice *QDeclarativeInputDeviceModel::get(int index) const
110{
111 if (index < 0 || index > inputDevices.count())
112 return 0;
113 return inputDevices.value(index);
114}
115
116void QDeclarativeInputDeviceModel::updateDeviceList()
117{
118 QVector <QInputDevice *> newDevices = deviceInfo->deviceListOfType(currentFilter);
119
120 int numNew = newDevices.count();
121
122 for (int i = 0; i < numNew; i++) {
123 int j = inputDevices.indexOf(newDevices.value(i));
124
125 if (j == -1) {
126 beginInsertRows(QModelIndex(), i, i);
127 inputDevices.insert(i, newDevices.value(i));
128 endInsertRows();
129 Q_EMIT countChanged();
130 } else if (i != j) {
131 // changed its position -> move it
132 QInputDevice* device = inputDevices.value(j);
133 beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
134 inputDevices.remove(j);
135 inputDevices.insert(i, device);
136 endMoveRows();
137 Q_EMIT countChanged();
138 } //else {
139 QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
140 Q_EMIT dataChanged(changedIndex, changedIndex);
141 }
142
143 int numOld = inputDevices.count();
144 if (numOld > numNew) {
145 beginRemoveRows(QModelIndex(), numNew, numOld - 1);
146 inputDevices.remove(numNew, numOld - numNew);
147 endRemoveRows();
148 Q_EMIT countChanged();
149 }
150}
151
152void QDeclarativeInputDeviceModel::addedDevice(const QString &devicePath)
153{
154 updateDeviceList();
155 Q_EMIT deviceAdded(devicePath);
156}
157
158void QDeclarativeInputDeviceModel::removedDevice(const QString &devicePath)
159{
160 updateDeviceList();
161 Q_EMIT deviceRemoved(devicePath);
162}
163
164QHash<int,QByteArray> QDeclarativeInputDeviceModel::roleNames() const
165{
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";
174 return roles;
175}
176
177/*
178 * Returns the currently set device filter.
179 * */
180QInputDevice::InputType QDeclarativeInputDeviceModel::deviceFilter()
181{
182 return currentFilter;
183}
184
185/*
186 * Sets the current input device filter to filter.
187 * */
188void QDeclarativeInputDeviceModel::setDeviceFilter(QInputDevice::InputType filter)
189{
190 if (filter != currentFilter) {
191 deviceInfo->setDeviceFilter(filter);
192 currentFilter = filter;
193 updateDeviceList();
194 Q_EMIT deviceFilterChanged(filter);
195 }
196}