Device: Decouple DeviceManager and DeviceSettingsWidget

Decouple DeviceSettingsWidget from the DeviceManager by
extending the DeviceModel with some methods to query information
from there.

This allows to add things like filtering to the model.

Change-Id: I8ac45d3ae254c31e2fe36556978bd6af5adccd32
Reviewed-by: Christian Kandeler <christian.kandeler@nokia.com>
This commit is contained in:
Tobias Hunger
2012-05-02 15:55:34 +02:00
parent 1cc3a5ecd9
commit 566642731f
3 changed files with 52 additions and 12 deletions

View File

@@ -33,6 +33,8 @@
#include "devicemanager.h"
#include <coreplugin/id.h>
#include <QString>
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;
}

View File

@@ -32,8 +32,10 @@
#ifndef DEVICEMANAGERMODEL_H
#define DEVICEMANAGERMODEL_H
#include "../projectexplorer_export.h"
#include "idevice.h"
#include <coreplugin/id.h>
#include <projectexplorer/projectexplorer_export.h>
#include <QAbstractListModel>
@@ -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);

View File

@@ -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<const IDevice> 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();