From a3ca25ecd49572d36220431e47d7bacc5bbfccaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Fri, 6 May 2016 14:48:25 +0200 Subject: [PATCH] Don't reset InputDevicesModel when devices change Summary: Instead of reset a proper beginInsertRows, beginRemoveRows is used. In addition changes in the device are also listened on and emit the proper dateChanged signal. Test Plan: Opened debug console, selected the touchpad device, toggled shortcut and verified that this updated the data column. Reviewers: #plasma Subscribers: plasma-devel Projects: #plasma Differential Revision: https://phabricator.kde.org/D1547 --- debug_console.cpp | 54 +++++++++++++++++++++++++++++++++++++++++------ debug_console.h | 10 +++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/debug_console.cpp b/debug_console.cpp index 7151784c29..726cd459d3 100644 --- a/debug_console.cpp +++ b/debug_console.cpp @@ -1127,14 +1127,31 @@ QVariant SurfaceTreeModel::data(const QModelIndex &index, int role) const #if HAVE_INPUT InputDeviceModel::InputDeviceModel(QObject *parent) : QAbstractItemModel(parent) + , m_devices(LibInput::Connection::self()->devices()) { + for (auto it = m_devices.constBegin(); it != m_devices.constEnd(); ++it) { + setupDeviceConnections(*it); + } auto c = LibInput::Connection::self(); - auto resetModel = [this] { - beginResetModel(); - endResetModel(); - }; - connect(c, &LibInput::Connection::deviceAdded, this, resetModel); - connect(c, &LibInput::Connection::deviceRemoved, this, resetModel); + connect(c, &LibInput::Connection::deviceAdded, this, + [this] (LibInput::Device *d) { + beginInsertRows(QModelIndex(), m_devices.count(), m_devices.count()); + m_devices << d; + setupDeviceConnections(d); + endInsertRows(); + } + ); + connect(c, &LibInput::Connection::deviceRemoved, this, + [this] (LibInput::Device *d) { + const int index = m_devices.indexOf(d); + if (index == -1) { + return; + } + beginRemoveRows(QModelIndex(), index, index); + m_devices.removeAt(index); + endRemoveRows(); + } + ); } InputDeviceModel::~InputDeviceModel() = default; @@ -1215,6 +1232,31 @@ QModelIndex InputDeviceModel::parent(const QModelIndex &child) const return QModelIndex(); } +void InputDeviceModel::setupDeviceConnections(LibInput::Device *device) +{ + connect(device, &LibInput::Device::enabledChanged, this, + [this, device] { + const QModelIndex parent = index(m_devices.indexOf(device), 0, QModelIndex()); + const QModelIndex child = index(device->metaObject()->indexOfProperty("enabled"), 1, parent); + emit dataChanged(child, child, QVector{Qt::DisplayRole}); + } + ); + connect(device, &LibInput::Device::leftHandedChanged, this, + [this, device] { + const QModelIndex parent = index(m_devices.indexOf(device), 0, QModelIndex()); + const QModelIndex child = index(device->metaObject()->indexOfProperty("leftHanded"), 1, parent); + emit dataChanged(child, child, QVector{Qt::DisplayRole}); + } + ); + connect(device, &LibInput::Device::pointerAccelerationChanged, this, + [this, device] { + const QModelIndex parent = index(m_devices.indexOf(device), 0, QModelIndex()); + const QModelIndex child = index(device->metaObject()->indexOfProperty("pointerAcceleration"), 1, parent); + emit dataChanged(child, child, QVector{Qt::DisplayRole}); + } + ); +} + #endif } diff --git a/debug_console.h b/debug_console.h index 8309a463ff..f76699a215 100644 --- a/debug_console.h +++ b/debug_console.h @@ -138,6 +138,12 @@ private: }; #if HAVE_INPUT + +namespace LibInput +{ +class Device; +} + class InputDeviceModel : public QAbstractItemModel { Q_OBJECT @@ -150,6 +156,10 @@ public: QModelIndex index(int row, int column, const QModelIndex & parent) const override; int rowCount(const QModelIndex &parent) const override; QModelIndex parent(const QModelIndex &child) const override; + +private: + void setupDeviceConnections(LibInput::Device *device); + QVector m_devices; }; #endif