Unity 8
qdeclarativeinputdeviceinfo.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 "qdeclarativeinputdeviceinfo_p.h"
42 
43 QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
44  QAbstractListModel(parent),
45  deviceInfo(new QInputDeviceInfo)
46 {
47  connect(deviceInfo, &QInputDeviceInfo::ready, this, &QDeclarativeInputDeviceInfo::updateDeviceList);
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::updateDeviceList()
93 {
94  QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
95 
96  int numNew = newDevices.count();
97 
98  for (int i = 0; i < numNew; i++) {
99  int j = inputDevices.indexOf(newDevices.value(i));
100  if (j == -1) {
101  // not found -> remove from list
102  beginInsertRows(QModelIndex(), i, i);
103  inputDevices.insert(i, newDevices.value(i));
104  endInsertRows();
105  } else if (i != j) {
106  // changed its position -> move it
107  QInputDevice* device = inputDevices.value(j);
108  beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
109  inputDevices.remove(j);
110  inputDevices.insert(i, device);
111  endMoveRows();
112  } else {
113  QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
114  Q_EMIT dataChanged(changedIndex, changedIndex);
115  }
116  }
117 
118  int numOld = inputDevices.count();
119  if (numOld > numNew) {
120  beginRemoveRows(QModelIndex(), numNew, numOld - 1);
121  inputDevices.remove(numNew, numOld - numNew);
122  endRemoveRows();
123  }
124 }
125 
126 void QDeclarativeInputDeviceInfo::addedDevice(const QString &devicePath)
127 {
128  updateDeviceList();
129  Q_EMIT newDevice(devicePath);
130 }
131 
132 void QDeclarativeInputDeviceInfo::removedDevice(const QString &devicePath)
133 {
134  updateDeviceList();
135  Q_EMIT deviceRemoved(devicePath);
136 }
137 
138 QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames() const
139 {
140  QHash<int, QByteArray> roles;
141  roles.insert(ServiceRole, "service");
142  return roles;
143 }