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
This commit is contained in:
Martin Gräßlin 2016-05-06 14:48:25 +02:00
parent 7dbad7bade
commit a3ca25ecd4
2 changed files with 58 additions and 6 deletions

View file

@ -1127,14 +1127,31 @@ QVariant SurfaceTreeModel::data(const QModelIndex &index, int role) const
#if HAVE_INPUT #if HAVE_INPUT
InputDeviceModel::InputDeviceModel(QObject *parent) InputDeviceModel::InputDeviceModel(QObject *parent)
: QAbstractItemModel(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 c = LibInput::Connection::self();
auto resetModel = [this] { connect(c, &LibInput::Connection::deviceAdded, this,
beginResetModel(); [this] (LibInput::Device *d) {
endResetModel(); beginInsertRows(QModelIndex(), m_devices.count(), m_devices.count());
}; m_devices << d;
connect(c, &LibInput::Connection::deviceAdded, this, resetModel); setupDeviceConnections(d);
connect(c, &LibInput::Connection::deviceRemoved, this, resetModel); 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; InputDeviceModel::~InputDeviceModel() = default;
@ -1215,6 +1232,31 @@ QModelIndex InputDeviceModel::parent(const QModelIndex &child) const
return QModelIndex(); 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<int>{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<int>{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<int>{Qt::DisplayRole});
}
);
}
#endif #endif
} }

View file

@ -138,6 +138,12 @@ private:
}; };
#if HAVE_INPUT #if HAVE_INPUT
namespace LibInput
{
class Device;
}
class InputDeviceModel : public QAbstractItemModel class InputDeviceModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
@ -150,6 +156,10 @@ public:
QModelIndex index(int row, int column, const QModelIndex & parent) const override; QModelIndex index(int row, int column, const QModelIndex & parent) const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &child) const override; QModelIndex parent(const QModelIndex &child) const override;
private:
void setupDeviceConnections(LibInput::Device *device);
QVector<LibInput::Device*> m_devices;
}; };
#endif #endif