From c54d2682808cda547f841cc9d3ccd18f03908cc6 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Wed, 4 Jan 2023 15:22:41 +0000 Subject: [PATCH] Add progress dialog for device enumeration, as some drivers are slow. Don't automatically enumerate. --- sdrbase/device/deviceenumerator.cpp | 1 + sdrbase/device/deviceenumerator.h | 7 +++- sdrgui/gui/samplingdevicedialog.cpp | 52 ++++++++++++++++++++++------- sdrgui/gui/samplingdevicedialog.h | 25 ++++++++++++++ 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/sdrbase/device/deviceenumerator.cpp b/sdrbase/device/deviceenumerator.cpp index 8ca2e8038..981d69fdd 100644 --- a/sdrbase/device/deviceenumerator.cpp +++ b/sdrbase/device/deviceenumerator.cpp @@ -253,6 +253,7 @@ void DeviceEnumerator::enumerateDevices(PluginAPI::SamplingDeviceRegistrations& for (int i = 0; i < deviceRegistrations.count(); i++) { qDebug("DeviceEnumerator::enumerateDevices: %s", qPrintable(deviceRegistrations[i].m_deviceId)); + emit enumeratingDevices(deviceRegistrations[i].m_deviceId); deviceRegistrations[i].m_plugin->enumOriginDevices(originDevicesHwIds, originDevices); PluginInterface::SamplingDevices samplingDevices; if (type == PluginInterface::SamplingDevice::StreamSingleRx) { diff --git a/sdrbase/device/deviceenumerator.h b/sdrbase/device/deviceenumerator.h index 0c35f3f43..8ea27b40b 100644 --- a/sdrbase/device/deviceenumerator.h +++ b/sdrbase/device/deviceenumerator.h @@ -27,8 +27,10 @@ class PluginManager; -class SDRBASE_API DeviceEnumerator +class SDRBASE_API DeviceEnumerator : public QObject { + Q_OBJECT + public: DeviceEnumerator(); ~DeviceEnumerator(); @@ -67,6 +69,9 @@ public: int getBestTxSamplingDeviceIndex(const QString& deviceId, const QString& serial, int sequence, int deviceItemIndex); int getBestMIMOSamplingDeviceIndex(const QString& deviceId, const QString& serial, int sequence); +signals: + void enumeratingDevices(const QString &deviceId); + private: struct DeviceEnumeration { diff --git a/sdrgui/gui/samplingdevicedialog.cpp b/sdrgui/gui/samplingdevicedialog.cpp index f9251b951..5b2aeb692 100644 --- a/sdrgui/gui/samplingdevicedialog.cpp +++ b/sdrgui/gui/samplingdevicedialog.cpp @@ -19,6 +19,8 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// +#include + #include "samplingdevicedialog.h" #include "ui_samplingdevicedialog.h" #include "device/deviceenumerator.h" @@ -32,7 +34,8 @@ SamplingDeviceDialog::SamplingDeviceDialog(int deviceType, QWidget* parent) : m_hasChanged(false) { ui->setupUi(this); - on_refreshDevices_clicked(); + // Don't automatically call on_refreshDevices_clicked(), some drivers can be slow to enumerate + displayDevices(); } SamplingDeviceDialog::~SamplingDeviceDialog() @@ -84,17 +87,22 @@ void SamplingDeviceDialog::on_deviceSelect_currentIndexChanged(int index) void SamplingDeviceDialog::on_refreshDevices_clicked() { - PluginManager *pluginManager = MainCore::instance()->getPluginManager(); - - if (m_deviceType == 0) { - DeviceEnumerator::instance()->enumerateRxDevices(pluginManager); - } else if (m_deviceType == 1) { - DeviceEnumerator::instance()->enumerateTxDevices(pluginManager); - } else if (m_deviceType == 2) { - DeviceEnumerator::instance()->enumerateMIMODevices(pluginManager); - } - - displayDevices(); + QProgressDialog *progressDialog = new QProgressDialog("Enumerating devices", "", 0, 0, this); + progressDialog->setWindowModality(Qt::WindowModal); + progressDialog->setCancelButton(nullptr); + progressDialog->setWindowFlag(Qt::WindowCloseButtonHint, false); + progressDialog->show(); + SamplingDeviceDialogWorker *worker = new SamplingDeviceDialogWorker(m_deviceType, progressDialog); + QThread *thread = new QThread(); + worker->moveToThread(thread); + connect(thread, &QThread::started, worker, &SamplingDeviceDialogWorker::enumerateDevices); + connect(worker, &SamplingDeviceDialogWorker::finishedWork, thread, &QThread::quit); + connect(worker, &SamplingDeviceDialogWorker::finishedWork, progressDialog, &QProgressDialog::close); + connect(worker, &SamplingDeviceDialogWorker::finishedWork, progressDialog, &QProgressDialog::deleteLater); + connect(worker, &SamplingDeviceDialogWorker::finishedWork, this, &SamplingDeviceDialog::displayDevices); + connect(worker, &SamplingDeviceDialogWorker::finishedWork, worker, &SamplingDeviceDialog::deleteLater); + connect(thread, &QThread::finished, thread, &QThread::deleteLater); + thread->start(); } void SamplingDeviceDialog::accept() @@ -108,3 +116,23 @@ void SamplingDeviceDialog::reject() m_hasChanged = false; QDialog::reject(); } + +void SamplingDeviceDialogWorker::enumerateDevices() +{ + PluginManager *pluginManager = MainCore::instance()->getPluginManager(); + connect(DeviceEnumerator::instance(), &DeviceEnumerator::enumeratingDevices, this, &SamplingDeviceDialogWorker::enumeratingDevices); + if (m_deviceType == 0) { + DeviceEnumerator::instance()->enumerateRxDevices(pluginManager); + } else if (m_deviceType == 1) { + DeviceEnumerator::instance()->enumerateTxDevices(pluginManager); + } else if (m_deviceType == 2) { + DeviceEnumerator::instance()->enumerateMIMODevices(pluginManager); + } + emit finishedWork(); +} + +void SamplingDeviceDialogWorker::enumeratingDevices(const QString &deviceId) +{ + m_progressDialog->setLabelText("Enumerating " + deviceId); +} + diff --git a/sdrgui/gui/samplingdevicedialog.h b/sdrgui/gui/samplingdevicedialog.h index 573212eb6..522b2e31b 100644 --- a/sdrgui/gui/samplingdevicedialog.h +++ b/sdrgui/gui/samplingdevicedialog.h @@ -23,6 +23,8 @@ #define SDRGUI_GUI_SAMPLINGDEVICEDIALOG_H_ #include +#include + #include #include "export.h" @@ -31,6 +33,28 @@ namespace Ui { class SamplingDeviceDialog; } +class SDRGUI_API SamplingDeviceDialogWorker : public QObject { + Q_OBJECT + +public: + SamplingDeviceDialogWorker(int deviceType, QProgressDialog *progressDialog) : + m_deviceType(deviceType), + m_progressDialog(progressDialog) + { + } + void enumerateDevices(); + +signals: + void finishedWork(); + +private slots: + void enumeratingDevices(const QString &deviceId); + +private: + int m_deviceType; + QProgressDialog *m_progressDialog; +}; + class SDRGUI_API SamplingDeviceDialog : public QDialog { Q_OBJECT @@ -60,3 +84,4 @@ private slots: }; #endif /* SDRGUI_GUI_SAMPLINGDEVICEDIALOG_H_ */ +