on cd4bc68 help sign in
unity8/tests/mocks/Unity/InputInfo/mockqdeclarativeinputdeviceinfo.cpp
173 lines 5.7 kB history raw
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"
44QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
45 QAbstractListModel(parent),
46 deviceInfo(new QInputDeviceInfo)
48 connect(deviceInfo, &QInputDeviceInfo::deviceAdded,this,&QDeclarativeInputDeviceInfo::addedDevice);
49 connect(deviceInfo, &QInputDeviceInfo::deviceRemoved,this,&QDeclarativeInputDeviceInfo::removedDevice);
52QDeclarativeInputDeviceInfo::~QDeclarativeInputDeviceInfo()
54 delete deviceInfo;
57QVariant QDeclarativeInputDeviceInfo::data(const QModelIndex &index, int role) const
59 switch (role) {
60 case ServiceRole:
61 return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
62 }
64 return QVariant();
67int QDeclarativeInputDeviceInfo::rowCount(const QModelIndex &parent) const
69 Q_UNUSED(parent);
71 return inputDevices.count();
74int QDeclarativeInputDeviceInfo::indexOf(const QString &devicePath) const
76 int idx(-1);
77 Q_FOREACH (QInputDevice *device, inputDevices) {
78 idx++;
79 if (device->devicePath() == devicePath) return idx;
80 }
82 return -1;
85QInputDevice *QDeclarativeInputDeviceInfo::get(int index) const
87 if (index < 0 || index > inputDevices.count())
88 return 0;
89 return inputDevices.value(index);
92void QDeclarativeInputDeviceInfo::addMockMouse()
94 deviceInfo->addMockDevice(QInputDeviceInfo::Mouse);
97void QDeclarativeInputDeviceInfo::addMockKeyboard()
99 deviceInfo->addMockDevice(QInputDeviceInfo::Keyboard);
102void QDeclarativeInputDeviceInfo::removeMockMouse()
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 }
112void QDeclarativeInputDeviceInfo::removeMockKeyboard()
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 }
122void QDeclarativeInputDeviceInfo::updateDeviceList()
124 QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
126 int numNew = newDevices.count();
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 }
148 int numOld = inputDevices.count();
149 if (numOld > numNew) {
150 beginRemoveRows(QModelIndex(), numNew, numOld - 1);
151 inputDevices.remove(numNew, numOld - numNew);
152 endRemoveRows();
153 }
156void QDeclarativeInputDeviceInfo::addedDevice(const QString &devicePath)
158 updateDeviceList();
159 Q_EMIT newDevice(devicePath);
162void QDeclarativeInputDeviceInfo::removedDevice(const QString &devicePath)
164 updateDeviceList();
165 Q_EMIT deviceRemoved(devicePath);
168QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames() const
170 QHash<int, QByteArray> roles;
171 roles.insert(ServiceRole, "service");
172 return roles;