diff --git a/sdrbase/feature/featurewebapiutils.cpp b/sdrbase/feature/featurewebapiutils.cpp index 84303acda..fb284afb3 100644 --- a/sdrbase/feature/featurewebapiutils.cpp +++ b/sdrbase/feature/featurewebapiutils.cpp @@ -56,6 +56,34 @@ bool FeatureWebAPIUtils::run(int featureSetIndex, int featureIndex) } } +// Stop feature +bool FeatureWebAPIUtils::stop(int featureSetIndex, int featureIndex) +{ + Feature *feature = FeatureWebAPIUtils::getFeature(featureSetIndex, featureIndex, ""); + if (feature != nullptr) + { + SWGSDRangel::SWGDeviceState runResponse; + QString errorResponse; + + runResponse.setState(new QString()); + const int httpRC = feature->webapiRun(false, runResponse, errorResponse); + + if (httpRC/100 != 2) + { + qWarning("FeatureWebAPIUtils::stop: stop error %d: %s", + httpRC, qPrintable(errorResponse)); + return false; + } + + return true; + } + else + { + qWarning("FeatureWebAPIUtils::stop: no feature F%d:%d", featureSetIndex, featureIndex); + return false; + } +} + // Find the specified target on the map bool FeatureWebAPIUtils::mapFind(const QString& target, int featureSetIndex, int featureIndex) { diff --git a/sdrbase/feature/featurewebapiutils.h b/sdrbase/feature/featurewebapiutils.h index ddad1ae82..535a35e27 100644 --- a/sdrbase/feature/featurewebapiutils.h +++ b/sdrbase/feature/featurewebapiutils.h @@ -48,6 +48,7 @@ class SDRBASE_API FeatureWebAPIUtils { public: static bool run(int featureSetIndex, int featureIndex); + static bool stop(int featureSetIndex, int featureIndex); static bool mapFind(const QString& target, int featureSetIndex=-1, int featureIndex=-1); static bool mapSetDateTime(const QDateTime& dateTime, int featureSetIndex=-1, int featureIndex=-1); static bool skyMapFind(const QString& target, int featureSetIndex=-1, int featureIndex=-1); diff --git a/sdrgui/gui/workspace.cpp b/sdrgui/gui/workspace.cpp index 066cebd0e..7f771c316 100644 --- a/sdrgui/gui/workspace.cpp +++ b/sdrgui/gui/workspace.cpp @@ -30,12 +30,15 @@ #include #include #include +#include #include "gui/samplingdevicedialog.h" #include "gui/rollupcontents.h" #include "gui/buttonswitch.h" #include "gui/crightclickenabler.h" #include "channel/channelgui.h" +#include "feature/feature.h" +#include "feature/featureset.h" #include "feature/featuregui.h" #include "device/devicegui.h" #include "device/deviceset.h" @@ -47,6 +50,8 @@ Workspace::Workspace(int index, QWidget *parent, Qt::WindowFlags flags) : QDockWidget(parent, flags), m_index(index), m_menuButton(nullptr), + m_startStopMenu(nullptr), + m_startStopMode(StartStopCurrentWorkspaceDevices), m_featureAddDialog(this), m_stacking(false), m_autoStack(false), @@ -88,8 +93,28 @@ Workspace::Workspace(int index, QWidget *parent, Qt::WindowFlags flags) : m_startStopButton = new ButtonSwitch(); m_startStopButton->setCheckable(true); + m_startStopMenu = new QMenu(m_startStopButton); + QActionGroup *startStopActionGroup = new QActionGroup(m_startStopMenu); + startStopActionGroup->setExclusive(true); + const QStringList startStopModes { + tr("Start/stop devices in current workspace"), + tr("Start/stop devices and features in current workspace"), + tr("Start/stop devices in all workspaces"), + tr("Start/stop devices and features in all workspaces") + }; + for (int i = 0; i < startStopModes.size(); i++) + { + QAction *action = m_startStopMenu->addAction(startStopModes[i]); + action->setCheckable(true); + action->setData(i); + action->setActionGroup(startStopActionGroup); + action->setChecked(i == static_cast(m_startStopMode)); + } + connect(startStopActionGroup, &QActionGroup::triggered, this, &Workspace::startStopModeChanged); + m_startStopButton->setMenu(m_startStopMenu); + m_startStopButton->setPopupMode(QToolButton::MenuButtonPopup); updateStartStopButton(false); - m_startStopButton->setFixedSize(20, 20); + m_startStopButton->setFixedSize(34, 20); m_startStopButton->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); m_vline1 = new QFrame(); @@ -921,59 +946,99 @@ void Workspace::layoutSubWindows() } } -// Start/stop all devices in workspace +// Start/stop the devices and features selected in the button menu void Workspace::startStopClicked(bool checked) { - if (!checked) { - emit stopAllDevices(this); - } else { - emit startAllDevices(this); - } + const bool includeFeatures = (m_startStopMode == StartStopCurrentWorkspaceDevicesAndFeatures) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + const bool allWorkspaces = (m_startStopMode == StartStopAllWorkspacesDevices) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + emit startStopRequested(this, checked, includeFeatures, allWorkspaces); updateStartStopButton(checked); } +void Workspace::startStopModeChanged(QAction *action) +{ + m_startStopMode = static_cast(action->data().toInt()); + updateStartStopButtonState(); +} + void Workspace::updateStartStopButton(bool checked) { + const bool includeFeatures = (m_startStopMode == StartStopCurrentWorkspaceDevicesAndFeatures) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + const bool allWorkspaces = (m_startStopMode == StartStopAllWorkspacesDevices) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + const QString targets = includeFeatures ? tr("devices and features") : tr("devices"); + const QString scope = allWorkspaces ? tr("all workspaces") : tr("current workspace"); + if (!checked) { QIcon startIcon(":/play.png"); m_startStopButton->setIcon(startIcon); m_startStopButton->setStyleSheet("QToolButton { background-color : blue; }"); - m_startStopButton->setToolTip("Start all devices in workspace"); + m_startStopButton->setToolTip(tr("Start %1 in %2").arg(targets, scope)); } else { QIcon stopIcon(":/stop.png"); m_startStopButton->setIcon(stopIcon); m_startStopButton->setStyleSheet("QToolButton { background-color : green; }"); - m_startStopButton->setToolTip("Stop all devices in workspace"); + m_startStopButton->setToolTip(tr("Stop %1 in %2").arg(targets, scope)); } } -void Workspace::deviceStateChanged(int, DeviceAPI *deviceAPI) +void Workspace::updateStartStopButtonState() { - if (deviceAPI->getWorkspaceIndex() == m_index) + const bool includeFeatures = (m_startStopMode == StartStopCurrentWorkspaceDevicesAndFeatures) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + const bool allWorkspaces = (m_startStopMode == StartStopAllWorkspacesDevices) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + bool running = false; + bool error = false; + const std::vector& deviceSets = MainCore::instance()->getDeviceSets(); + + for (const auto *deviceSet : deviceSets) { - // Check state of all devices in workspace, to see if any are running or have errors - bool running = false; - bool error = false; - std::vector deviceSets = MainCore::instance()->getDeviceSets(); - for (auto deviceSet : deviceSets) + if (allWorkspaces || (deviceSet->m_deviceAPI->getWorkspaceIndex() == m_index)) { - DeviceAPI::EngineState state = deviceSet->m_deviceAPI->state(); - if (state == DeviceAPI::StRunning) { - running = true; - } else if (state == DeviceAPI::StError) { - error = true; - } + const DeviceAPI::EngineState state = deviceSet->m_deviceAPI->state(); + running |= state == DeviceAPI::StRunning; + error |= state == DeviceAPI::StError; } - // Update start/stop button to reflect current state of devices - updateStartStopButton(running); - m_startStopButton->setChecked(running); - if (error) { - m_startStopButton->setStyleSheet("QToolButton { background-color : red; }"); + } + + if (includeFeatures) + { + const std::vector& featureSets = MainCore::instance()->getFeatureeSets(); + for (const auto *featureSet : featureSets) + { + for (int i = 0; i < featureSet->getNumberOfFeatures(); i++) + { + const Feature *feature = featureSet->getFeatureAt(i); + if (allWorkspaces || (feature->getWorkspaceIndex() == m_index)) + { + running |= feature->getState() == Feature::StRunning; + error |= feature->getState() == Feature::StError; + } + } } } + + updateStartStopButton(running); + m_startStopButton->setChecked(running); + if (error) { + m_startStopButton->setStyleSheet("QToolButton { background-color : red; }"); + } +} + +void Workspace::deviceStateChanged(int, DeviceAPI *deviceAPI) +{ + const bool allWorkspaces = (m_startStopMode == StartStopAllWorkspacesDevices) + || (m_startStopMode == StartStopAllWorkspacesDevicesAndFeatures); + if (allWorkspaces || (deviceAPI->getWorkspaceIndex() == m_index)) { + updateStartStopButtonState(); + } } void Workspace::resizeEvent(QResizeEvent *event) @@ -1138,4 +1203,3 @@ void Workspace::adjustSubWindowsAfterRestore() } } } - diff --git a/sdrgui/gui/workspace.h b/sdrgui/gui/workspace.h index d01571832..f737b7e1b 100644 --- a/sdrgui/gui/workspace.h +++ b/sdrgui/gui/workspace.h @@ -33,6 +33,8 @@ class QHBoxLayout; class QLabel; class QToolButton; class QPushButton; +class QMenu; +class QAction; class QMdiArea; class QMdiSubWindow; class QFrame; @@ -71,10 +73,20 @@ public: QToolButton *getMenuButton() const { return m_menuButton; } private: + enum StartStopMode + { + StartStopCurrentWorkspaceDevices, + StartStopCurrentWorkspaceDevicesAndFeatures, + StartStopAllWorkspacesDevices, + StartStopAllWorkspacesDevicesAndFeatures + }; + int m_index; QToolButton *m_menuButton; QPushButton *m_configurationPresetsButton; + QMenu *m_startStopMenu; ButtonSwitch *m_startStopButton; + StartStopMode m_startStopMode; QFrame *m_vline1; QPushButton *m_addRxDeviceButton; QPushButton *m_addTxDeviceButton; @@ -101,6 +113,7 @@ private: int m_autoStackChannelMinWidth; // Width of channel requested by stackSubWindows() void unmaximizeSubWindows(); + void updateStartStopButtonState(); protected: void resizeEvent(QResizeEvent *event) override; @@ -120,6 +133,7 @@ private slots: void autoStackSubWindows(const QPoint&); void tabSubWindows(); void startStopClicked(bool checked = false); + void startStopModeChanged(QAction *action); void addFeatureEmitted(int featureIndex); void toggleFloating(); void deviceStateChanged(int, DeviceAPI *deviceAPI); @@ -134,8 +148,7 @@ signals: void addFeature(Workspace*, int); void featurePresetsDialogRequested(QPoint, Workspace*); void configurationPresetsDialogRequested(); - void startAllDevices(Workspace *inWorkspace); - void stopAllDevices(Workspace *inWorkspace); + void startStopRequested(Workspace *inWorkspace, bool start, bool includeFeatures, bool allWorkspaces); }; diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index 6b4c3c508..ba1acc3b2 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -2519,16 +2519,9 @@ void MainWindow::addWorkspace() QObject::connect( m_workspaces.back(), - &Workspace::startAllDevices, + &Workspace::startStopRequested, this, - &MainWindow::startAllDevices - ); - - QObject::connect( - m_workspaces.back(), - &Workspace::stopAllDevices, - this, - &MainWindow::stopAllDevices + &MainWindow::startStop ); if (m_workspaces.size() > 1) @@ -3461,6 +3454,45 @@ void MainWindow::stopAllDevices(const Workspace *workspace) const } } +void MainWindow::startStop(const Workspace *workspace, bool start, bool includeFeatures, bool allWorkspaces) const +{ + const int workspaceIndex = workspace->getIndex(); + + for (const auto *deviceUI : m_deviceUIs) + { + DeviceAPI *deviceAPI = deviceUI->m_deviceAPI; + if (allWorkspaces || (deviceAPI->getWorkspaceIndex() == workspaceIndex)) + { + const int deviceIndex = deviceAPI->getDeviceSetIndex(); + if (start) { + ChannelWebAPIUtils::run(deviceIndex); + } else { + ChannelWebAPIUtils::stop(deviceIndex); + } + } +} + + if (includeFeatures) + { + for (std::size_t featureSetIndex = 0; featureSetIndex < m_featureUIs.size(); featureSetIndex++) + { + FeatureUISet *featureUISet = m_featureUIs[featureSetIndex]; + for (int featureIndex = 0; featureIndex < featureUISet->getNumberOfFeatures(); featureIndex++) + { + Feature *feature = featureUISet->getFeatureAt(featureIndex); + if (allWorkspaces || (feature->getWorkspaceIndex() == workspaceIndex)) + { + if (start) { + FeatureWebAPIUtils::run(featureSetIndex, featureIndex); + } else { + FeatureWebAPIUtils::stop(featureSetIndex, featureIndex); + } + } + } + } + } +} + void MainWindow::deviceStateChanged(DeviceAPI *deviceAPI) { emit m_mainCore->deviceStateChanged(deviceAPI->getDeviceSetIndex(), deviceAPI); diff --git a/sdrgui/mainwindow.h b/sdrgui/mainwindow.h index d1599f398..863a5a83b 100644 --- a/sdrgui/mainwindow.h +++ b/sdrgui/mainwindow.h @@ -465,6 +465,7 @@ private slots: void startAll(); void startAllDevices(const Workspace *workspace) const; void stopAllDevices(const Workspace *workspace) const; + void startStop(const Workspace *workspace, bool start, bool includeFeatures, bool allWorkspaces) const; void deviceMove(DeviceGUI *gui, int wsIndexDestnation); void mainSpectrumMove(MainSpectrumGUI *gui, int wsIndexDestnation); void mainSpectrumShow(int deviceSetIndex);