Commit f4514a2f authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Device support: Make "replaced" semantics more sensible.



When adding a device whose id is already in the list, we interpret this
as an update. So far, so good. However, the current code implements this
rather simplistically by just doing remove+add, which potentially puts
the device into a different position in the list and also emits "added"
and "removed" signals for the respective id, which is conceptually
wrong. The new implementation distinguishes the two cases more
carefully.

Change-Id: Id2480947141e4515606adfd3e0d84f65fe7b9cc5
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@nokia.com>
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent af3d9f39
......@@ -222,12 +222,14 @@ void DeviceManager::addDevice(const IDevice::Ptr &device)
{
QTC_ASSERT(this != instance() || (device->isAutoDetected()), return);
QString name = device->displayName();
const int pos = indexForId(device->id());
if (pos >= 0)
removeDevice(device->id());
if (pos >= 0) {
device->setDisplayName(QString()); // For name uniquification to work.
d->devices[pos] = device;
}
// Ensure uniqueness of name.
QString name = device->displayName();
if (hasDevice(name)) {
const QString nameTemplate = name + QLatin1String(" (%1)");
int suffix = 2;
......@@ -236,26 +238,29 @@ void DeviceManager::addDevice(const IDevice::Ptr &device)
while (hasDevice(name));
}
device->setDisplayName(name);
if (!defaultDevice(device->type()))
d->defaultDevices.insert(device->type(), device->id());
d->devices << device;
if (this == instance() && d->clonedInstance)
d->clonedInstance->addDevice(device->clone());
if (this == instance()) {
QList<IDevice::Ptr>::Iterator it = d->inactiveAutoDetectedDevices.begin();
while (it != d->inactiveAutoDetectedDevices.end()) {
if (it->data()->id() == device->id()) {
d->inactiveAutoDetectedDevices.erase(it);
break;
if (pos >= 0) {
emit deviceUpdated(device->id());
} else {
d->devices << device;
if (this == instance()) {
QList<IDevice::Ptr>::Iterator it = d->inactiveAutoDetectedDevices.begin();
while (it != d->inactiveAutoDetectedDevices.end()) {
if (it->data()->id() == device->id()) {
d->inactiveAutoDetectedDevices.erase(it);
break;
}
++it;
}
++it;
}
emit deviceAdded(device->id());
}
emit deviceAdded(device->id());
if (pos >= 0)
emit deviceUpdated(device->id());
emit updated();
}
......
......@@ -67,6 +67,7 @@ public:
bool hasDevice(const QString &name) const;
Core::Id deviceId(const IDevice::ConstPtr &device) const;
int indexForId(Core::Id id) const;
int indexOf(const IDevice::ConstPtr &device) const;
void addDevice(const IDevice::Ptr &device);
......@@ -89,7 +90,6 @@ private:
void fromMap(const QVariantMap &map);
QVariantMap toMap() const;
void ensureOneDefaultDevicePerType();
int indexForId(Core::Id id) const;
// For SettingsWidget.
IDevice::Ptr mutableDeviceAt(int index) const;
......
......@@ -83,7 +83,9 @@ void DeviceManagerModel::handleDeviceRemoved(Core::Id id)
void DeviceManagerModel::handleDeviceUpdated(Core::Id id)
{
const QModelIndex changedIndex = index(indexForId(id), 0);
const int idx = indexForId(id);
d->devices[idx] = d->deviceManager->deviceAt(idx);
const QModelIndex changedIndex = index(idx, 0);
emit dataChanged(changedIndex, changedIndex);
}
......
......@@ -107,6 +107,7 @@ DeviceSettingsWidget::DeviceSettingsWidget(QWidget *parent)
initGui();
connect(m_additionalActionsMapper, SIGNAL(mapped(QString)),
SLOT(handleAdditionalActionRequest(QString)));
connect(m_deviceManager, SIGNAL(deviceUpdated(Core::Id)), SLOT(handleDeviceUpdated(Core::Id)));
}
DeviceSettingsWidget::~DeviceSettingsWidget()
......@@ -236,6 +237,13 @@ void DeviceSettingsWidget::setDefaultDevice()
m_ui->defaultDeviceButton->setEnabled(false);
}
void DeviceSettingsWidget::handleDeviceUpdated(Id id)
{
const int index = m_deviceManager->indexForId(id);
if (index == currentIndex())
currentDeviceChanged(index);
}
void DeviceSettingsWidget::currentDeviceChanged(int index)
{
qDeleteAll(m_additionalActionButtons);
......
......@@ -32,6 +32,8 @@
#ifndef DEVICESETTINGSWIDGET_H
#define DEVICESETTINGSWIDGET_H
#include <coreplugin/id.h>
#include <QList>
#include <QString>
#include <QPushButton>
......@@ -62,6 +64,7 @@ public:
QString searchKeywords() const;
private slots:
void handleDeviceUpdated(Core::Id id);
void currentDeviceChanged(int index);
void addDevice();
void removeDevice();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment