@@ -0,0 +1,173 @@
1+/****************************************************************************
2+**
3+** Copyright (C) 2015 Jolla.
4+** Copyright (C) 2015 Canonical Ltd.
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 Digia. For licensing terms and
15+** conditions see http://qt.digia.com/licensing. For further information
16+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
21+** Foundation and appearing in the file LICENSE.LGPL included in the
22+** packaging of this file. Please review the following information to
23+** ensure the GNU Lesser General Public License version 2.1 requirements
24+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25+**
26+** In addition, as a special exception, Digia gives you certain additional
27+** rights. These rights are described in the Digia Qt LGPL Exception
28+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29+**
30+** GNU General Public License Usage
31+** Alternatively, this file may be used under the terms of the GNU
32+** General Public License version 3.0 as published by the Free Software
33+** Foundation and appearing in the file LICENSE.GPL included in the
34+** packaging of this file. Please review the following information to
35+** ensure the GNU General Public License version 3.0 requirements will be
36+** met: http://www.gnu.org/copyleft/gpl.html.
37+**
38+**
39+** $QT_END_LICENSE$
40+**
41+****************************************************************************/
42+#include "mockqdeclarativeinputdeviceinfo_p.h"
43+
44+QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
45+ QAbstractListModel(parent),
46+ deviceInfo(new QInputDeviceInfo)
47+{
48+ connect(deviceInfo, &QInputDeviceInfo::deviceAdded,this,&QDeclarativeInputDeviceInfo::addedDevice);
49+ connect(deviceInfo, &QInputDeviceInfo::deviceRemoved,this,&QDeclarativeInputDeviceInfo::removedDevice);
50+}
51+
52+QDeclarativeInputDeviceInfo::~QDeclarativeInputDeviceInfo()
53+{
54+ delete deviceInfo;
55+}
56+
57+QVariant QDeclarativeInputDeviceInfo::data(const QModelIndex &index, int role) const
58+{
59+ switch (role) {
60+ case ServiceRole:
61+ return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
62+ }
63+
64+ return QVariant();
65+}
66+
67+int QDeclarativeInputDeviceInfo::rowCount(const QModelIndex &parent) const
68+{
69+ Q_UNUSED(parent);
70+
71+ return inputDevices.count();
72+}
73+
74+int QDeclarativeInputDeviceInfo::indexOf(const QString &devicePath) const
75+{
76+ int idx(-1);
77+ Q_FOREACH (QInputDevice *device, inputDevices) {
78+ idx++;
79+ if (device->devicePath() == devicePath) return idx;
80+ }
81+
82+ return -1;
83+}
84+
85+QInputDevice *QDeclarativeInputDeviceInfo::get(int index) const
86+{
87+ if (index < 0 || index > inputDevices.count())
88+ return 0;
89+ return inputDevices.value(index);
90+}
91+
92+void QDeclarativeInputDeviceInfo::addMockMouse()
93+{
94+ deviceInfo->addMockDevice(QInputDeviceInfo::Mouse);
95+}
96+
97+void QDeclarativeInputDeviceInfo::addMockKeyboard()
98+{
99+ deviceInfo->addMockDevice(QInputDeviceInfo::Keyboard);
100+}
101+
102+void QDeclarativeInputDeviceInfo::removeMockMouse()
103+{
104+ for (int i = 0; i < inputDevices.count(); i++) {
105+ if (inputDevices.at(i)->types().testFlag(QInputDeviceInfo::Mouse)) {
106+ deviceInfo->removeMockDevice(i);
107+ return;
108+ }
109+ }
110+}
111+
112+void QDeclarativeInputDeviceInfo::removeMockKeyboard()
113+{
114+ for (int i = 0; i < inputDevices.count(); i++) {
115+ if (inputDevices.at(i)->types().testFlag(QInputDeviceInfo::Keyboard)) {
116+ deviceInfo->removeMockDevice(i);
117+ return;
118+ }
119+ }
120+}
121+
122+void QDeclarativeInputDeviceInfo::updateDeviceList()
123+{
124+ QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
125+
126+ int numNew = newDevices.count();
127+
128+ for (int i = 0; i < numNew; i++) {
129+ int j = inputDevices.indexOf(newDevices.value(i));
130+ if (j == -1) {
131+ // not found -> remove from list
132+ beginInsertRows(QModelIndex(), i, i);
133+ inputDevices.insert(i, newDevices.value(i));
134+ endInsertRows();
135+ } else if (i != j) {
136+ // changed its position -> move it
137+ QInputDevice* device = inputDevices.value(j);
138+ beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
139+ inputDevices.remove(j);
140+ inputDevices.insert(i, device);
141+ endMoveRows();
142+ } else {
143+ QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
144+ Q_EMIT dataChanged(changedIndex, changedIndex);
145+ }
146+ }
147+
148+ int numOld = inputDevices.count();
149+ if (numOld > numNew) {
150+ beginRemoveRows(QModelIndex(), numNew, numOld - 1);
151+ inputDevices.remove(numNew, numOld - numNew);
152+ endRemoveRows();
153+ }
154+}
155+
156+void QDeclarativeInputDeviceInfo::addedDevice(const QString &devicePath)
157+{
158+ updateDeviceList();
159+ Q_EMIT newDevice(devicePath);
160+}
161+
162+void QDeclarativeInputDeviceInfo::removedDevice(const QString &devicePath)
163+{
164+ updateDeviceList();
165+ Q_EMIT deviceRemoved(devicePath);
166+}
167+
168+QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames() const
169+{
170+ QHash<int, QByteArray> roles;
171+ roles.insert(ServiceRole, "service");
172+ return roles;
173+}