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

RemoteLinux: The device manager should not be itself a model.



The ListModel functionality is only needed in very few contexts,
so we move it into a dedicated class. This cleans up the actual device
manager.

Change-Id: I325bf43a9260ab9a8e484f8972e2d962ea2f1b3e
Reviewed-by: default avatarKai Koehne <kai.koehne@nokia.com>
parent 4e83fb79
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "devicemanagermodel.h"
#include "linuxdeviceconfigurations.h"
#include "remotelinuxutils.h"
#include <QString>
namespace RemoteLinux {
namespace Internal {
class DeviceManagerModelPrivate
{
public:
const LinuxDeviceConfigurations *deviceManager;
QList<LinuxDeviceConfiguration::ConstPtr> devices;
};
} // namespace Internal
DeviceManagerModel::DeviceManagerModel(const LinuxDeviceConfigurations *deviceManager,
QObject *parent) :
QAbstractListModel(parent), d(new Internal::DeviceManagerModelPrivate)
{
d->deviceManager = deviceManager;
handleDeviceListChanged();
connect(deviceManager, SIGNAL(deviceAdded(QSharedPointer<const LinuxDeviceConfiguration>)),
SLOT(handleDeviceAdded(QSharedPointer<const LinuxDeviceConfiguration>)));
connect(deviceManager, SIGNAL(deviceRemoved(int)), SLOT(handleDeviceRemoved(int)));
connect(deviceManager, SIGNAL(displayNameChanged(int)), SLOT(handleDataChanged(int)));
connect(deviceManager, SIGNAL(deviceListChanged()), SLOT(handleDeviceListChanged()));
}
DeviceManagerModel::~DeviceManagerModel()
{
delete d;
}
void DeviceManagerModel::handleDeviceAdded(const LinuxDeviceConfiguration::ConstPtr &device)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
d->devices << device;
endInsertRows();
}
void DeviceManagerModel::handleDeviceRemoved(int idx)
{
beginRemoveRows(QModelIndex(), idx, idx);
d->devices.removeAt(idx);
endRemoveRows();
}
void DeviceManagerModel::handleDataChanged(int idx)
{
const QModelIndex changedIndex = index(idx, 0);
emit dataChanged(changedIndex, changedIndex);
}
void DeviceManagerModel::handleDeviceListChanged()
{
beginResetModel();
d->devices.clear();
for (int i = 0; i < d->deviceManager->deviceCount(); ++i)
d->devices << d->deviceManager->deviceAt(i);
endResetModel();
}
int DeviceManagerModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return d->devices.count();
}
QVariant DeviceManagerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= rowCount() || role != Qt::DisplayRole)
return QVariant();
const LinuxDeviceConfiguration::ConstPtr devConf = d->devices.at(index.row());
QString name = devConf->displayName();
if (d->deviceManager->defaultDeviceConfig(devConf->osType()) == devConf) {
name += QLatin1Char(' ') + tr("(default for %1)")
.arg(RemoteLinuxUtils::osTypeToString(devConf->osType()));
}
return name;
}
} // namespace RemoteLinux
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef DEVICEMANAGERMODEL_H
#define DEVICEMANAGERMODEL_H
#include "remotelinux_export.h"
#include <QAbstractListModel>
#include <QSharedPointer>
namespace RemoteLinux {
namespace Internal { class DeviceManagerModelPrivate; }
class LinuxDeviceConfiguration;
class LinuxDeviceConfigurations;
class REMOTELINUX_EXPORT DeviceManagerModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit DeviceManagerModel(const LinuxDeviceConfigurations *deviceManager,
QObject *parent = 0);
~DeviceManagerModel();
private slots:
void handleDeviceAdded(const QSharedPointer<const LinuxDeviceConfiguration> &device);
void handleDeviceRemoved(int idx);
void handleDataChanged(int idx);
void handleDeviceListChanged();
private:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
Internal::DeviceManagerModelPrivate * const d;
};
} // namespace RemoteLinux
#endif // DEVICEMANAGERMODEL_H
......@@ -95,15 +95,19 @@ LinuxDeviceConfigurations *LinuxDeviceConfigurations::instance(QObject *parent)
return LinuxDeviceConfigurationsPrivate::instance;
}
int LinuxDeviceConfigurations::deviceCount() const
{
return d->devConfigs.count();
}
void LinuxDeviceConfigurations::replaceInstance()
{
Q_ASSERT(LinuxDeviceConfigurationsPrivate::instance);
QTC_ASSERT(LinuxDeviceConfigurationsPrivate::instance, return);
LinuxDeviceConfigurationsPrivate::instance->beginResetModel();
copy(LinuxDeviceConfigurationsPrivate::clonedInstance,
LinuxDeviceConfigurationsPrivate::instance, false);
LinuxDeviceConfigurationsPrivate::instance->save();
LinuxDeviceConfigurationsPrivate::instance->endResetModel();
emit LinuxDeviceConfigurationsPrivate::instance->deviceListChanged();
emit LinuxDeviceConfigurationsPrivate::instance->updated();
}
......@@ -181,40 +185,38 @@ void LinuxDeviceConfigurations::addConfiguration(const LinuxDeviceConfiguration:
}
devConfig->setDisplayName(name);
if (this == LinuxDeviceConfigurations::instance())
devConfig->setInternalId(d->nextId++);
devConfig->setInternalId(d->nextId++);
beginInsertRows(QModelIndex(), rowCount(), rowCount());
if (!defaultDeviceConfig(devConfig->osType()))
d->defaultConfigs.insert(devConfig->osType(), devConfig->internalId());
d->devConfigs << devConfig;
endInsertRows();
if (this == d->instance && d->clonedInstance) {
d->clonedInstance->addConfiguration(
LinuxDeviceConfiguration::Ptr(new LinuxDeviceConfiguration(devConfig)));
}
emit deviceAdded(devConfig);
emit updated();
}
void LinuxDeviceConfigurations::removeConfiguration(int idx)
{
QTC_ASSERT(idx >= 0 && idx < rowCount(), return);
QTC_ASSERT(idx >= 0 && idx < deviceCount(), return);
const LinuxDeviceConfiguration::ConstPtr deviceConfig = deviceAt(idx);
QTC_ASSERT(this != LinuxDeviceConfigurationsPrivate::instance
|| deviceConfig->isAutoDetected(), return);
beginRemoveRows(QModelIndex(), idx, idx);
const bool wasDefault
= d->defaultConfigs.value(deviceConfig->osType()) == deviceConfig->internalId();
const QString osType = deviceConfig->osType();
d->devConfigs.removeAt(idx);
endRemoveRows();
emit deviceRemoved(idx);
if (wasDefault) {
for (int i = 0; i < d->devConfigs.count(); ++i) {
if (deviceAt(i)->osType() == osType) {
d->defaultConfigs.insert(deviceAt(i)->osType(), deviceAt(i)->internalId());
const QModelIndex changedIndex = index(i, 0);
emit dataChanged(changedIndex, changedIndex);
emit defaultStatusChanged(i);
break;
}
}
......@@ -223,6 +225,7 @@ void LinuxDeviceConfigurations::removeConfiguration(int idx)
d->clonedInstance->removeConfiguration(d->clonedInstance->
indexForInternalId(deviceConfig->internalId()));
}
emit updated();
}
......@@ -241,46 +244,42 @@ QString LinuxDeviceConfigurations::defaultSshKeyFilePath() const
void LinuxDeviceConfigurations::setConfigurationName(int i, const QString &name)
{
QTC_ASSERT(this != LinuxDeviceConfigurationsPrivate::instance, return);
Q_ASSERT(i >= 0 && i < rowCount());
QTC_ASSERT(i >= 0 && i < deviceCount(), return);
d->devConfigs.at(i)->setDisplayName(name);
const QModelIndex changedIndex = index(i, 0);
emit dataChanged(changedIndex, changedIndex);
emit displayNameChanged(i);
}
void LinuxDeviceConfigurations::setDefaultDevice(int idx)
{
QTC_ASSERT(this != LinuxDeviceConfigurationsPrivate::instance, return);
Q_ASSERT(idx >= 0 && idx < rowCount());
QTC_ASSERT(idx >= 0 && idx < deviceCount(), return);
const LinuxDeviceConfiguration::ConstPtr &devConf = d->devConfigs.at(idx);
const LinuxDeviceConfiguration::ConstPtr &oldDefaultDevConf
= defaultDeviceConfig(devConf->osType());
if (defaultDeviceConfig(devConf->osType()) == devConf)
return;
QModelIndex oldDefaultIndex;
d->defaultConfigs.insert(devConf->osType(), devConf->internalId());
emit defaultStatusChanged(idx);
for (int i = 0; i < d->devConfigs.count(); ++i) {
if (d->devConfigs.at(i) == oldDefaultDevConf) {
oldDefaultIndex = index(i, 0);
emit defaultStatusChanged(i);
break;
}
}
QTC_CHECK(oldDefaultIndex.isValid());
d->defaultConfigs.insert(devConf->osType(), devConf->internalId());
emit dataChanged(oldDefaultIndex, oldDefaultIndex);
const QModelIndex newDefaultIndex = index(idx, 0);
emit dataChanged(newDefaultIndex, newDefaultIndex);
emit updated();
}
LinuxDeviceConfigurations::LinuxDeviceConfigurations(QObject *parent)
: QAbstractListModel(parent), d(new LinuxDeviceConfigurationsPrivate)
: QObject(parent), d(new LinuxDeviceConfigurationsPrivate)
{
}
LinuxDeviceConfiguration::Ptr LinuxDeviceConfigurations::mutableDeviceAt(int idx) const
{
Q_ASSERT(idx >= 0 && idx < rowCount());
QTC_ASSERT(idx >= 0 && idx < deviceCount(), return LinuxDeviceConfiguration::Ptr());
return d->devConfigs.at(idx);
}
......@@ -315,7 +314,7 @@ void LinuxDeviceConfigurations::load()
LinuxDeviceConfiguration::ConstPtr LinuxDeviceConfigurations::deviceAt(int idx) const
{
Q_ASSERT(idx >= 0 && idx < rowCount());
QTC_ASSERT(idx >= 0 && idx < deviceCount(), return LinuxDeviceConfiguration::ConstPtr());
return d->devConfigs.at(idx);
}
......@@ -364,23 +363,4 @@ void LinuxDeviceConfigurations::ensureOneDefaultConfigurationPerOsType()
}
}
int LinuxDeviceConfigurations::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return d->devConfigs.count();
}
QVariant LinuxDeviceConfigurations::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= rowCount() || role != Qt::DisplayRole)
return QVariant();
const LinuxDeviceConfiguration::ConstPtr devConf = deviceAt(index.row());
QString name = devConf->displayName();
if (defaultDeviceConfig(devConf->osType()) == devConf) {
name += QLatin1Char(' ') + tr("(default for %1)")
.arg(RemoteLinuxUtils::osTypeToString(devConf->osType()));
}
return name;
}
} // namespace RemoteLinux
......@@ -35,7 +35,7 @@
#include "linuxdeviceconfiguration.h"
#include "remotelinux_export.h"
#include <QAbstractListModel>
#include <QObject>
QT_FORWARD_DECLARE_CLASS(QString)
......@@ -45,7 +45,7 @@ class LinuxDeviceConfigurationsPrivate;
class LinuxDeviceConfigurationsSettingsWidget;
} // namespace Internal
class REMOTELINUX_EXPORT LinuxDeviceConfigurations : public QAbstractListModel
class REMOTELINUX_EXPORT LinuxDeviceConfigurations : public QObject
{
Q_OBJECT
friend class Internal::LinuxDeviceConfigurationsSettingsWidget;
......@@ -54,6 +54,7 @@ public:
static LinuxDeviceConfigurations *instance(QObject *parent = 0);
int deviceCount() const;
LinuxDeviceConfiguration::ConstPtr deviceAt(int index) const;
LinuxDeviceConfiguration::ConstPtr find(LinuxDeviceConfiguration::Id id) const;
LinuxDeviceConfiguration::ConstPtr defaultDeviceConfig(const QString &osType) const;
......@@ -68,15 +69,16 @@ public:
void setConfigurationName(int i, const QString &name);
void setDefaultDevice(int index);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const;
public slots:
void setDefaultSshKeyFilePath(const QString &path);
signals:
void updated();
void deviceAdded(const QSharedPointer<const LinuxDeviceConfiguration> &device);
void deviceRemoved(int index);
void displayNameChanged(int index);
void defaultStatusChanged(int index);
void deviceListChanged();
void updated(); // Emitted for all of the above.
private:
LinuxDeviceConfigurations(QObject *parent);
......
......@@ -32,6 +32,7 @@
#include "ui_linuxdeviceconfigurationssettingswidget.h"
#include "devicemanagermodel.h"
#include "linuxdeviceconfigurations.h"
#include "linuxdevicefactoryselectiondialog.h"
#include "remotelinuxutils.h"
......@@ -137,7 +138,8 @@ QString LinuxDeviceConfigurationsSettingsWidget::searchKeywords() const
void LinuxDeviceConfigurationsSettingsWidget::initGui()
{
m_ui->setupUi(this);
m_ui->configurationComboBox->setModel(m_devConfigs);
DeviceManagerModel * const model = new DeviceManagerModel(m_devConfigs, this);
m_ui->configurationComboBox->setModel(model);
m_ui->nameLineEdit->setValidator(m_nameValidator);
int lastIndex = Core::ICore::settings()
......@@ -177,7 +179,7 @@ void LinuxDeviceConfigurationsSettingsWidget::addConfig()
void LinuxDeviceConfigurationsSettingsWidget::deleteConfig()
{
m_devConfigs->removeConfiguration(currentIndex());
if (m_devConfigs->rowCount() == 0)
if (m_devConfigs->deviceCount() == 0)
currentConfigChanged(-1);
}
......
......@@ -62,7 +62,8 @@ HEADERS += \
remotelinuxcustomcommanddeployservice.h \
remotelinuxcustomcommanddeploymentstep.h \
abstractembeddedlinuxtarget.h \
genericlinuxdeviceconfigurationwidget.h
genericlinuxdeviceconfigurationwidget.h \
devicemanagermodel.h
SOURCES += \
genericembeddedlinuxtarget.cpp \
......@@ -118,7 +119,8 @@ SOURCES += \
remotelinuxcustomcommanddeployservice.cpp \
remotelinuxcustomcommanddeploymentstep.cpp \
abstractembeddedlinuxtarget.cpp \
genericlinuxdeviceconfigurationwidget.cpp
genericlinuxdeviceconfigurationwidget.cpp \
devicemanagermodel.cpp
FORMS += \
linuxdevicefactoryselectiondialog.ui \
......
......@@ -141,6 +141,8 @@ QtcPlugin {
"remotelinuxruncontrolfactory.h",
"tarpackagecreationstep.cpp",
"uploadandinstalltarpackagestep.cpp",
"devicemanagermodel.h",
"devicemanagermodel.cpp",
"images/embeddedtarget.png"
]
......
......@@ -32,6 +32,7 @@
#include "startgdbserverdialog.h"
#include "devicemanagermodel.h"
#include "remotelinuxprocesslist.h"
#include "linuxdeviceconfiguration.h"
#include "linuxdeviceconfigurations.h"
......@@ -174,14 +175,15 @@ StartGdbServerDialog::StartGdbServerDialog(QWidget *parent) :
setWindowTitle(tr("List of Remote Processes"));
LinuxDeviceConfigurations *devices = LinuxDeviceConfigurations::instance();
DeviceManagerModel * const model = new DeviceManagerModel(devices, this);
QObject::connect(d->closeButton, SIGNAL(clicked()), this, SLOT(reject()));
d->deviceComboBox->setModel(devices);
d->deviceComboBox->setModel(model);
d->deviceComboBox->setCurrentIndex(d->settings->value(LastDevice).toInt());
connect(&d->gatherer, SIGNAL(error(QString)), SLOT(portGathererError(QString)));
connect(&d->gatherer, SIGNAL(portListReady()), SLOT(portListReady()));
if (devices->rowCount() == 0) {
if (devices->deviceCount() == 0) {
d->tableView->setEnabled(false);
} else {
d->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
......
......@@ -42,7 +42,6 @@ TypeSpecificDeviceConfigurationListModel::TypeSpecificDeviceConfigurationListMod
{
const LinuxDeviceConfigurations * const devConfs
= LinuxDeviceConfigurations::instance();
connect(devConfs, SIGNAL(modelReset()), this, SIGNAL(modelReset()));
connect(devConfs, SIGNAL(updated()), this, SIGNAL(updated()));
}
......@@ -57,7 +56,7 @@ int TypeSpecificDeviceConfigurationListModel::rowCount(const QModelIndex &parent
int count = 0;
const LinuxDeviceConfigurations * const devConfs
= LinuxDeviceConfigurations::instance();
const int devConfsCount = devConfs->rowCount();
const int devConfsCount = devConfs->deviceCount();
if (m_targetOsType == QLatin1String(Constants::GenericLinuxOsType))
return devConfsCount;
for (int i = 0; i < devConfsCount; ++i) {
......@@ -89,7 +88,7 @@ LinuxDeviceConfiguration::ConstPtr TypeSpecificDeviceConfigurationListModel::dev
= LinuxDeviceConfigurations::instance();
if (m_targetOsType == QLatin1String(Constants::GenericLinuxOsType))
return devConfs->deviceAt(idx);
const int devConfsCount = devConfs->rowCount();
const int devConfsCount = devConfs->deviceCount();
for (int i = 0; i < devConfsCount; ++i) {
if (devConfs->deviceAt(i)->osType() == m_targetOsType) {
if (++currentRow == idx)
......
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