ProjectExplorer: Use a lambda for DeviceFactory::create()

Somewhat slimmer interface on the user code side and follows
existing practice.

Change-Id: I20ed8f5a00591265d32ea9ce93e1f1bbc76d2437
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2022-01-26 09:05:35 +01:00
parent f6cb638345
commit 2b6f26dee2
20 changed files with 96 additions and 125 deletions

View File

@@ -663,34 +663,33 @@ AndroidDeviceManager::AndroidDeviceManager(QObject *parent)
} }
// Factory // Factory
AndroidDeviceFactory::AndroidDeviceFactory() AndroidDeviceFactory::AndroidDeviceFactory()
: ProjectExplorer::IDeviceFactory(Constants::ANDROID_DEVICE_TYPE), : IDeviceFactory(Constants::ANDROID_DEVICE_TYPE),
m_androidConfig(AndroidConfigurations::currentConfig()) m_androidConfig(AndroidConfigurations::currentConfig())
{ {
setDisplayName(AndroidDevice::tr("Android Device")); setDisplayName(AndroidDevice::tr("Android Device"));
setCombinedIcon(":/android/images/androiddevicesmall.png", setCombinedIcon(":/android/images/androiddevicesmall.png",
":/android/images/androiddevice.png"); ":/android/images/androiddevice.png");
setConstructionFunction(&AndroidDevice::create); setConstructionFunction(&AndroidDevice::create);
setCanCreate(m_androidConfig.sdkToolsOk()); setCanCreate(m_androidConfig.sdkToolsOk());
} setCreator([this] {
IDevice::Ptr AndroidDeviceFactory::create() const
{
AvdDialog dialog = AvdDialog(m_androidConfig, Core::ICore::dialogParent()); AvdDialog dialog = AvdDialog(m_androidConfig, Core::ICore::dialogParent());
if (dialog.exec() != QDialog::Accepted) if (dialog.exec() != QDialog::Accepted)
return ProjectExplorer::IDevice::Ptr(); return IDevice::Ptr();
const ProjectExplorer::IDevice::Ptr dev = dialog.device(); const IDevice::Ptr dev = dialog.device();
const AndroidDevice *androidDev = static_cast<AndroidDevice*>(dev.data()); if (const auto androidDev = static_cast<AndroidDevice *>(dev.data())) {
if (androidDev) {
qCDebug(androidDeviceLog, "Created new Android AVD id \"%s\".", qCDebug(androidDeviceLog, "Created new Android AVD id \"%s\".",
qPrintable(androidDev->avdName())); qPrintable(androidDev->avdName()));
} else { } else {
AndroidDeviceWidget::criticalDialog( AndroidDeviceWidget::criticalDialog(
QObject::tr("The device info returned from AvdDialog is invalid.")); AndroidDevice::tr("The device info returned from AvdDialog is invalid."));
} }
return IDevice::Ptr(dev); return IDevice::Ptr(dev);
});
} }
} // namespace Internal } // namespace Internal

View File

@@ -88,7 +88,6 @@ class AndroidDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
AndroidDeviceFactory(); AndroidDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
private: private:
const AndroidConfig &m_androidConfig; const AndroidConfig &m_androidConfig;

View File

@@ -125,16 +125,14 @@ BareMetalDeviceFactory::BareMetalDeviceFactory()
setDisplayName(BareMetalDevice::tr("Bare Metal Device")); setDisplayName(BareMetalDevice::tr("Bare Metal Device"));
setCombinedIcon(":/baremetal/images/baremetaldevicesmall.png", setCombinedIcon(":/baremetal/images/baremetaldevicesmall.png",
":/baremetal/images/baremetaldevice.png"); ":/baremetal/images/baremetaldevice.png");
setCanCreate(true);
setConstructionFunction(&BareMetalDevice::create); setConstructionFunction(&BareMetalDevice::create);
} setCanCreate(true);
setCreator([] {
IDevice::Ptr BareMetalDeviceFactory::create() const
{
BareMetalDeviceConfigurationWizard wizard; BareMetalDeviceConfigurationWizard wizard;
if (wizard.exec() != QDialog::Accepted) if (wizard.exec() != QDialog::Accepted)
return {}; return IDevice::Ptr();
return wizard.device(); return wizard.device();
});
} }
} //namespace Internal } //namespace Internal

View File

@@ -71,9 +71,7 @@ private:
class BareMetalDeviceFactory final : public ProjectExplorer::IDeviceFactory class BareMetalDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
explicit BareMetalDeviceFactory(); BareMetalDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const final;
}; };
} //namespace Internal } //namespace Internal

View File

@@ -286,17 +286,14 @@ QdbLinuxDeviceFactory::QdbLinuxDeviceFactory()
{ {
setDisplayName(QdbDevice::tr("Boot2Qt Device")); setDisplayName(QdbDevice::tr("Boot2Qt Device"));
setCombinedIcon(":/qdb/images/qdbdevicesmall.png", ":/qdb/images/qdbdevice.png"); setCombinedIcon(":/qdb/images/qdbdevicesmall.png", ":/qdb/images/qdbdevice.png");
setCanCreate(true);
setConstructionFunction(&QdbDevice::create); setConstructionFunction(&QdbDevice::create);
} setCanCreate(true);
setCreator([] {
IDevice::Ptr QdbLinuxDeviceFactory::create() const
{
QdbDeviceWizard wizard(Core::ICore::dialogParent()); QdbDeviceWizard wizard(Core::ICore::dialogParent());
if (wizard.exec() != QDialog::Accepted) if (wizard.exec() != QDialog::Accepted)
return IDevice::Ptr(); return IDevice::Ptr();
return wizard.device(); return wizard.device();
});
} }
} // namespace Internal } // namespace Internal

View File

@@ -65,9 +65,6 @@ class QdbLinuxDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
QdbLinuxDeviceFactory(); QdbLinuxDeviceFactory();
private:
ProjectExplorer::IDevice::Ptr create() const final;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -1803,15 +1803,6 @@ QString DockerDevicePrivate::outputForRunInShell(const CommandLine &cmd) const
// Factory // Factory
DockerDeviceFactory::DockerDeviceFactory()
: IDeviceFactory(Constants::DOCKER_DEVICE_TYPE)
{
setDisplayName(DockerDevice::tr("Docker Device"));
setIcon(QIcon());
setCanCreate(true);
setConstructionFunction([] { return DockerDevice::create({}); });
}
class DockerImageItem final : public TreeItem, public DockerDeviceData class DockerImageItem final : public TreeItem, public DockerDeviceData
{ {
public: public:
@@ -1915,7 +1906,7 @@ public:
m_process->start(); m_process->start();
} }
DockerDevice::Ptr device() const IDevice::Ptr device() const
{ {
const QModelIndexList selectedRows = m_view->selectionModel()->selectedRows(); const QModelIndexList selectedRows = m_view->selectionModel()->selectedRows();
QTC_ASSERT(selectedRows.size() == 1, return {}); QTC_ASSERT(selectedRows.size() == 1, return {});
@@ -1940,14 +1931,6 @@ public:
QString m_selectedId; QString m_selectedId;
}; };
IDevice::Ptr DockerDeviceFactory::create() const
{
DockerDeviceSetupWizard wizard;
if (wizard.exec() != QDialog::Accepted)
return IDevice::Ptr();
return wizard.device();
}
void DockerDeviceWidget::updateDaemonStateTexts() void DockerDeviceWidget::updateDaemonStateTexts()
{ {
Utils::optional<bool> daemonState = DockerPlugin::isDaemonRunning(); Utils::optional<bool> daemonState = DockerPlugin::isDaemonRunning();
@@ -1963,5 +1946,22 @@ void DockerDeviceWidget::updateDaemonStateTexts()
} }
} }
// Factory
DockerDeviceFactory::DockerDeviceFactory()
: IDeviceFactory(Constants::DOCKER_DEVICE_TYPE)
{
setDisplayName(DockerDevice::tr("Docker Device"));
setIcon(QIcon());
setCanCreate(true);
setCreator([] {
DockerDeviceSetupWizard wizard;
if (wizard.exec() != QDialog::Accepted)
return IDevice::Ptr();
return wizard.device();
});
setConstructionFunction([] { return DockerDevice::create({}); });
}
} // Internal } // Internal
} // Docker } // Docker

View File

@@ -157,8 +157,6 @@ class DockerDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
DockerDeviceFactory(); DockerDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
} // Internal } // Internal

View File

@@ -59,13 +59,9 @@ McuSupportDeviceFactory::McuSupportDeviceFactory()
setDisplayName(McuSupportDevice::tr("MCU Device")); setDisplayName(McuSupportDevice::tr("MCU Device"));
setCombinedIcon(":/mcusupport/images/mcusupportdevicesmall.png", setCombinedIcon(":/mcusupport/images/mcusupportdevicesmall.png",
":/mcusupport/images/mcusupportdevice.png"); ":/mcusupport/images/mcusupportdevice.png");
setCanCreate(true);
setConstructionFunction(&McuSupportDevice::create); setConstructionFunction(&McuSupportDevice::create);
} setCanCreate(true);
setCreator(&McuSupportDevice::create);
ProjectExplorer::IDevice::Ptr McuSupportDeviceFactory::create() const
{
return McuSupportDevice::create();
} }
} // namespace Internal } // namespace Internal

View File

@@ -45,8 +45,6 @@ class McuSupportDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
McuSupportDeviceFactory(); McuSupportDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -81,6 +81,11 @@ bool IDeviceFactory::canCreate() const
return m_canCreate; return m_canCreate;
} }
IDevice::Ptr IDeviceFactory::create() const
{
return m_creator ? m_creator() : IDevice::Ptr();
}
IDevice::Ptr IDeviceFactory::construct() const IDevice::Ptr IDeviceFactory::construct() const
{ {
return m_constructor ? m_constructor() : IDevice::Ptr(); return m_constructor ? m_constructor() : IDevice::Ptr();
@@ -119,6 +124,11 @@ void IDeviceFactory::setCanCreate(bool canCreate)
m_canCreate = canCreate; m_canCreate = canCreate;
} }
void IDeviceFactory::setCreator(const std::function<IDevice::Ptr ()> &creator)
{
m_creator = creator;
}
void IDeviceFactory::setConstructionFunction(const std::function<IDevice::Ptr ()> &constructor) void IDeviceFactory::setConstructionFunction(const std::function<IDevice::Ptr ()> &constructor)
{ {
m_constructor = constructor; m_constructor = constructor;

View File

@@ -44,8 +44,7 @@ public:
QIcon icon() const { return m_icon; } QIcon icon() const { return m_icon; }
bool canCreate() const; bool canCreate() const;
IDevice::Ptr construct() const; IDevice::Ptr construct() const;
IDevice::Ptr create() const;
virtual IDevice::Ptr create() const { return IDevice::Ptr(); }
virtual bool canRestore(const QVariantMap &) const { return true; } virtual bool canRestore(const QVariantMap &) const { return true; }
@@ -61,8 +60,10 @@ protected:
void setCombinedIcon(const Utils::FilePath &small, const Utils::FilePath &large); void setCombinedIcon(const Utils::FilePath &small, const Utils::FilePath &large);
void setCanCreate(bool canCreate); void setCanCreate(bool canCreate);
void setConstructionFunction(const std::function<IDevice::Ptr ()> &constructor); void setConstructionFunction(const std::function<IDevice::Ptr ()> &constructor);
void setCreator(const std::function<IDevice::Ptr()> &creator);
private: private:
std::function<IDevice::Ptr()> m_creator;
const Utils::Id m_deviceType; const Utils::Id m_deviceType;
QString m_displayName; QString m_displayName;
QIcon m_icon; QIcon m_icon;

View File

@@ -172,22 +172,19 @@ DeviceProcessSignalOperation::Ptr QnxDevice::signalOperation() const
// Factory // Factory
QnxDeviceFactory::QnxDeviceFactory() QnxDeviceFactory::QnxDeviceFactory() : IDeviceFactory(Constants::QNX_QNX_OS_TYPE)
: ProjectExplorer::IDeviceFactory(Constants::QNX_QNX_OS_TYPE)
{ {
setDisplayName(QnxDevice::tr("QNX Device")); setDisplayName(QnxDevice::tr("QNX Device"));
setCombinedIcon(":/qnx/images/qnxdevicesmall.png", setCombinedIcon(":/qnx/images/qnxdevicesmall.png",
":/qnx/images/qnxdevice.png"); ":/qnx/images/qnxdevice.png");
setCanCreate(true);
setConstructionFunction(&QnxDevice::create); setConstructionFunction(&QnxDevice::create);
} setCanCreate(true);
setCreator([] {
ProjectExplorer::IDevice::Ptr QnxDeviceFactory::create() const
{
QnxDeviceWizard wizard; QnxDeviceWizard wizard;
if (wizard.exec() != QDialog::Accepted) if (wizard.exec() != QDialog::Accepted)
return ProjectExplorer::IDevice::Ptr(); return IDevice::Ptr();
return wizard.device(); return wizard.device();
});
} }
} // namespace Internal } // namespace Internal

View File

@@ -70,8 +70,6 @@ class QnxDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
QnxDeviceFactory(); QnxDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -51,12 +51,9 @@ TestLinuxDeviceFactory::TestLinuxDeviceFactory()
{ {
setDisplayName("Generic Linux Device"); setDisplayName("Generic Linux Device");
setIcon(QIcon()); setIcon(QIcon());
setCanCreate(true);
setConstructionFunction(&LinuxDevice::create); setConstructionFunction(&LinuxDevice::create);
} setCanCreate(true);
setCreator([] {
IDevice::Ptr TestLinuxDeviceFactory::create() const
{
LinuxDevice::Ptr newDev = LinuxDevice::create(); LinuxDevice::Ptr newDev = LinuxDevice::create();
qDebug() << "device : " << newDev->type(); qDebug() << "device : " << newDev->type();
newDev->setType("test"); newDev->setType("test");
@@ -65,6 +62,7 @@ IDevice::Ptr TestLinuxDeviceFactory::create() const
sshParams.setPort(22); sshParams.setPort(22);
newDev->setSshParameters(sshParams); newDev->setSshParameters(sshParams);
return newDev; return newDev;
});
} }
FilePath createFile(const QString &name) FilePath createFile(const QString &name)

View File

@@ -37,8 +37,6 @@ class TestLinuxDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
TestLinuxDeviceFactory(); TestLinuxDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
class FileSystemAccessTest : public QObject class FileSystemAccessTest : public QObject

View File

@@ -754,17 +754,15 @@ LinuxDeviceFactory::LinuxDeviceFactory()
{ {
setDisplayName(LinuxDevice::tr("Generic Linux Device")); setDisplayName(LinuxDevice::tr("Generic Linux Device"));
setIcon(QIcon()); setIcon(QIcon());
setCanCreate(true);
setConstructionFunction(&LinuxDevice::create); setConstructionFunction(&LinuxDevice::create);
} setCanCreate(true);
setCreator([] {
IDevice::Ptr LinuxDeviceFactory::create() const
{
GenericLinuxDeviceConfigurationWizard wizard(Core::ICore::dialogParent()); GenericLinuxDeviceConfigurationWizard wizard(Core::ICore::dialogParent());
if (wizard.exec() != QDialog::Accepted) if (wizard.exec() != QDialog::Accepted)
return IDevice::Ptr(); return IDevice::Ptr();
return wizard.device(); return wizard.device();
});
} }
} } // namespace Internal
} // namespace RemoteLinux } // namespace RemoteLinux

View File

@@ -100,8 +100,6 @@ class LinuxDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
LinuxDeviceFactory(); LinuxDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -47,10 +47,9 @@ WebAssemblyDevice::WebAssemblyDevice()
setOsType(OsTypeOther); setOsType(OsTypeOther);
} }
ProjectExplorer::IDevice::Ptr WebAssemblyDevice::create() IDevice::Ptr WebAssemblyDevice::create()
{ {
auto device = new WebAssemblyDevice; return IDevice::Ptr(new WebAssemblyDevice);
return ProjectExplorer::IDevice::Ptr(device);
} }
WebAssemblyDeviceFactory::WebAssemblyDeviceFactory() WebAssemblyDeviceFactory::WebAssemblyDeviceFactory()
@@ -59,13 +58,9 @@ WebAssemblyDeviceFactory::WebAssemblyDeviceFactory()
setDisplayName(WebAssemblyDevice::tr("WebAssembly Runtime")); setDisplayName(WebAssemblyDevice::tr("WebAssembly Runtime"));
setCombinedIcon(":/webassembly/images/webassemblydevicesmall.png", setCombinedIcon(":/webassembly/images/webassemblydevicesmall.png",
":/webassembly/images/webassemblydevice.png"); ":/webassembly/images/webassemblydevice.png");
setCanCreate(true);
setConstructionFunction(&WebAssemblyDevice::create); setConstructionFunction(&WebAssemblyDevice::create);
} setCanCreate(true);
setCreator(&WebAssemblyDevice::create);
ProjectExplorer::IDevice::Ptr WebAssemblyDeviceFactory::create() const
{
return WebAssemblyDevice::create();
} }
} // namespace Internal } // namespace Internal

View File

@@ -45,8 +45,6 @@ class WebAssemblyDeviceFactory final : public ProjectExplorer::IDeviceFactory
{ {
public: public:
WebAssemblyDeviceFactory(); WebAssemblyDeviceFactory();
ProjectExplorer::IDevice::Ptr create() const override;
}; };
} // namespace Internal } // namespace Internal