diff --git a/src/plugins/projectexplorer/devicesupport/devicemanagermodel.cpp b/src/plugins/projectexplorer/devicesupport/devicemanagermodel.cpp index f3c006ef6e5..5d59864394e 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanagermodel.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicemanagermodel.cpp @@ -33,6 +33,8 @@ #include "devicemanager.h" +#include + #include namespace ProjectExplorer { @@ -66,6 +68,31 @@ void DeviceManagerModel::updateDevice(Core::Id id) handleDeviceUpdated(id); } +IDevice::ConstPtr DeviceManagerModel::device(int pos) const +{ + if (pos < 0 || pos >= d->devices.count()) + return IDevice::ConstPtr(); + return d->devices.at(pos); +} + +Core::Id DeviceManagerModel::deviceId(int pos) const +{ + IDevice::ConstPtr dev = device(pos); + if (dev.isNull()) + return IDevice::invalidId(); + return dev->id(); +} + +int DeviceManagerModel::indexOf(IDevice::ConstPtr dev) const +{ + for (int i = 0; i < d->devices.count(); ++i) { + IDevice::ConstPtr current = d->devices.at(i); + if (current->id() == dev->id()) + return i; + } + return -1; +} + void DeviceManagerModel::handleDeviceAdded(Core::Id id) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); @@ -93,6 +120,7 @@ void DeviceManagerModel::handleDeviceListChanged() { beginResetModel(); d->devices.clear(); + for (int i = 0; i < d->deviceManager->deviceCount(); ++i) d->devices << d->deviceManager->deviceAt(i); endResetModel(); @@ -106,12 +134,18 @@ int DeviceManagerModel::rowCount(const QModelIndex &parent) const QVariant DeviceManagerModel::data(const QModelIndex &index, int role) const { - if (!index.isValid() || index.row() >= rowCount() || role != Qt::DisplayRole) + if (!index.isValid() || index.row() >= rowCount()) return QVariant(); - const IDevice::ConstPtr device = d->devices.at(index.row()); - QString name = device->displayName(); - if (d->deviceManager->defaultDevice(device->type()) == device) - name = tr("%1 (default for %2)").arg(name, device->displayType()); + if (role != Qt::DisplayRole && role != Qt::UserRole) + return QVariant(); + const IDevice::ConstPtr dev = device(index.row()); + if (role == Qt::UserRole) + return dev->id().uniqueIdentifier(); + QString name; + if (d->deviceManager->defaultDevice(dev->type()) == dev) + name = tr("%1 (default for %2)").arg(dev->displayName(), dev->displayType()); + else + name = dev->displayName(); return name; } diff --git a/src/plugins/projectexplorer/devicesupport/devicemanagermodel.h b/src/plugins/projectexplorer/devicesupport/devicemanagermodel.h index f83b10a7fb1..53c1a6a8f8a 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanagermodel.h +++ b/src/plugins/projectexplorer/devicesupport/devicemanagermodel.h @@ -32,8 +32,10 @@ #ifndef DEVICEMANAGERMODEL_H #define DEVICEMANAGERMODEL_H +#include "../projectexplorer_export.h" +#include "idevice.h" + #include -#include #include @@ -51,6 +53,10 @@ public: void updateDevice(Core::Id id); + IDevice::ConstPtr device(int pos) const; + Core::Id deviceId(int pos) const; + int indexOf(IDevice::ConstPtr dev) const; + private slots: void handleDeviceAdded(Core::Id id); void handleDeviceRemoved(Core::Id id); diff --git a/src/plugins/projectexplorer/devicesupport/devicesettingswidget.cpp b/src/plugins/projectexplorer/devicesupport/devicesettingswidget.cpp index cc11299b09f..ecf0e04c7e6 100644 --- a/src/plugins/projectexplorer/devicesupport/devicesettingswidget.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicesettingswidget.cpp @@ -174,7 +174,7 @@ void DeviceSettingsWidget::addDevice() m_deviceManager->addDevice(device); m_ui->removeConfigButton->setEnabled(true); - m_ui->configurationComboBox->setCurrentIndex(m_ui->configurationComboBox->count()-1); + m_ui->configurationComboBox->setCurrentIndex(m_deviceManagerModel->indexOf(device)); } void DeviceSettingsWidget::removeDevice() @@ -217,7 +217,7 @@ int DeviceSettingsWidget::currentIndex() const QSharedPointer DeviceSettingsWidget::currentDevice() const { Q_ASSERT(currentIndex() != -1); - return m_deviceManager->deviceAt(currentIndex()); + return m_deviceManagerModel->device(currentIndex()); } void DeviceSettingsWidget::deviceNameEditingFinished() @@ -250,14 +250,13 @@ void DeviceSettingsWidget::currentDeviceChanged(int index) delete m_configWidget; m_configWidget = 0; m_additionalActionButtons.clear(); - QTC_ASSERT(index >= -1 && index < m_deviceManager->deviceCount(), return); - if (index == -1) { + const IDevice::ConstPtr device = m_deviceManagerModel->device(index); + if (device.isNull()) { m_ui->removeConfigButton->setEnabled(false); clearDetails(); m_ui->defaultDeviceButton->setEnabled(false); } else { m_ui->removeConfigButton->setEnabled(true); - const IDevice::ConstPtr device = m_deviceManager->deviceAt(index); foreach (const Core::Id actionId, device->actionIds()) { QPushButton * const button = new QPushButton(device->displayNameForActionId(actionId)); m_additionalActionButtons << button; @@ -267,7 +266,8 @@ void DeviceSettingsWidget::currentDeviceChanged(int index) } if (!m_ui->osSpecificGroupBox->layout()) new QVBoxLayout(m_ui->osSpecificGroupBox); - m_configWidget = m_deviceManager->mutableDeviceAt(index)->createWidget(); + int managerIndex = m_deviceManager->indexOf(device); + m_configWidget = m_deviceManager->mutableDeviceAt(managerIndex)->createWidget(); if (m_configWidget) m_ui->osSpecificGroupBox->layout()->addWidget(m_configWidget); displayCurrent();