mode: move responsabilities from mode to modehandler

- Pass the following responsabilities from mode to mode handler:
   - active / deactive target mode
   - get active mode
   - create new modes

- Move back setting averaging mode when settings are
updated using getModes() method instead of handling
logic in mode handler.
This commit is contained in:
Kiara Navarro 2022-07-10 19:55:10 -05:00
parent f56e32488e
commit 2fbe6e84be
No known key found for this signature in database
GPG Key ID: CBA9F2172CE33FBA
17 changed files with 142 additions and 126 deletions

View File

@ -17,15 +17,16 @@
using namespace std; using namespace std;
using namespace nlohmann; using namespace nlohmann;
AmplitudeCalDialog::AmplitudeCalDialog(Device *dev, QWidget *parent) : AmplitudeCalDialog::AmplitudeCalDialog(Device *dev, ModeHandler *handler, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::AmplitudeCalDialog), ui(new Ui::AmplitudeCalDialog),
dev(dev), dev(dev),
modeHandler(handler),
model(this), model(this),
mode(CalibrationMode::BothPorts) mode(CalibrationMode::BothPorts)
{ {
activeMode = Mode::getActiveMode(); auto activeMode = modeHandler->getActiveMode();
activeMode->deactivate(); modeHandler->deactivate(activeMode);
dev->SetIdle(); dev->SetIdle();
ui->setupUi(this); ui->setupUi(this);
ui->view->setModel(&model); ui->view->setModel(&model);
@ -137,7 +138,8 @@ AmplitudeCalDialog::AmplitudeCalDialog(Device *dev, QWidget *parent) :
AmplitudeCalDialog::~AmplitudeCalDialog() AmplitudeCalDialog::~AmplitudeCalDialog()
{ {
delete ui; delete ui;
activeMode->activate(); auto activeMode = modeHandler->getActiveMode();
modeHandler->activate(activeMode);
} }
void AmplitudeCalDialog::reject() void AmplitudeCalDialog::reject()

View File

@ -2,6 +2,7 @@
#define AMPLITUDECALDIALOG_H #define AMPLITUDECALDIALOG_H
#include "mode.h" #include "mode.h"
#include "modehandler.h"
#include "Device/device.h" #include "Device/device.h"
#include <QDialog> #include <QDialog>
@ -42,7 +43,7 @@ class AmplitudeCalDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit AmplitudeCalDialog(Device *dev, QWidget *parent = nullptr); explicit AmplitudeCalDialog(Device *dev, ModeHandler *handler, QWidget *parent = nullptr);
~AmplitudeCalDialog(); ~AmplitudeCalDialog();
void reject() override; void reject() override;
@ -100,7 +101,7 @@ protected:
std::vector<CorrectionPoint> points; std::vector<CorrectionPoint> points;
Ui::AmplitudeCalDialog *ui; Ui::AmplitudeCalDialog *ui;
Device *dev; Device *dev;
Mode *activeMode; ModeHandler *modeHandler;
AmplitudeModel model; AmplitudeModel model;
bool edited; bool edited;
CalibrationMode mode; CalibrationMode mode;

View File

@ -2,7 +2,7 @@
#include "ui_frequencycaldialog.h" #include "ui_frequencycaldialog.h"
FrequencyCalDialog::FrequencyCalDialog(Device *dev, QWidget *parent) : FrequencyCalDialog::FrequencyCalDialog(Device *dev, ModeHandler *handler, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::FrequencyCalDialog), ui(new Ui::FrequencyCalDialog),
dev(dev) dev(dev)
@ -22,9 +22,9 @@ FrequencyCalDialog::FrequencyCalDialog(Device *dev, QWidget *parent) :
p.frequencyCorrection.ppm = ui->ppm->value(); p.frequencyCorrection.ppm = ui->ppm->value();
dev->SendPacket(p); dev->SendPacket(p);
// force restart of current mode for setting to take effect // force restart of current mode for setting to take effect
auto activeMode = Mode::getActiveMode(); auto activeMode = handler->getActiveMode();
activeMode->deactivate(); handler->deactivate(activeMode);
activeMode->activate(); handler->activate(activeMode);
accept(); accept();
delete this; delete this;
}); });

View File

@ -2,6 +2,7 @@
#define FREQUENCYCALDIALOG_H #define FREQUENCYCALDIALOG_H
#include "Device/device.h" #include "Device/device.h"
#include "modehandler.h"
#include "mode.h" #include "mode.h"
#include <QDialog> #include <QDialog>
@ -15,7 +16,7 @@ class FrequencyCalDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit FrequencyCalDialog(Device *dev, QWidget *parent = nullptr); explicit FrequencyCalDialog(Device *dev, ModeHandler *handler, QWidget *parent = nullptr);
~FrequencyCalDialog(); ~FrequencyCalDialog();
private: private:

View File

@ -1,7 +1,7 @@
#include "receivercaldialog.h" #include "receivercaldialog.h"
ReceiverCalDialog::ReceiverCalDialog(Device *dev) ReceiverCalDialog::ReceiverCalDialog(Device *dev, ModeHandler *handler)
: AmplitudeCalDialog(dev) : AmplitudeCalDialog(dev, handler)
{ {
setWindowTitle("Receiver Calibration Dialog"); setWindowTitle("Receiver Calibration Dialog");
LoadFromDevice(); LoadFromDevice();

View File

@ -2,12 +2,13 @@
#define RECEIVERCALDIALOG_H #define RECEIVERCALDIALOG_H
#include "amplitudecaldialog.h" #include "amplitudecaldialog.h"
#include "modehandler.h"
class ReceiverCalDialog : public AmplitudeCalDialog class ReceiverCalDialog : public AmplitudeCalDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
ReceiverCalDialog(Device *dev); ReceiverCalDialog(Device *dev, ModeHandler *handler);
protected: protected:
Protocol::PacketType requestCommand() override { return Protocol::PacketType::RequestReceiverCal; } Protocol::PacketType requestCommand() override { return Protocol::PacketType::RequestReceiverCal; }
Protocol::PacketType pointType() override { return Protocol::PacketType::ReceiverCalPoint; } Protocol::PacketType pointType() override { return Protocol::PacketType::ReceiverCalPoint; }

View File

@ -2,8 +2,8 @@
#include <QDebug> #include <QDebug>
SourceCalDialog::SourceCalDialog(Device *dev) SourceCalDialog::SourceCalDialog(Device *dev, ModeHandler *handler)
: AmplitudeCalDialog(dev) : AmplitudeCalDialog(dev, handler)
{ {
setWindowTitle("Source Calibration Dialog"); setWindowTitle("Source Calibration Dialog");
LoadFromDevice(); LoadFromDevice();

View File

@ -2,6 +2,7 @@
#define SOURCECALDIALOG_H #define SOURCECALDIALOG_H
#include "amplitudecaldialog.h" #include "amplitudecaldialog.h"
#include "modehandler.h"
#include <QObject> #include <QObject>
@ -9,7 +10,7 @@ class SourceCalDialog : public AmplitudeCalDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
SourceCalDialog(Device *dev); SourceCalDialog(Device *dev, ModeHandler *handler);
protected: protected:
Protocol::PacketType requestCommand() override { return Protocol::PacketType::RequestSourceCal; } Protocol::PacketType requestCommand() override { return Protocol::PacketType::RequestSourceCal; }
Protocol::PacketType pointType() override { return Protocol::PacketType::SourceCalPoint; } Protocol::PacketType pointType() override { return Protocol::PacketType::SourceCalPoint; }

View File

@ -1,4 +1,5 @@
#include "generator.h" #include "generator.h"
#include "modehandler.h"
#include <QSettings> #include <QSettings>
@ -55,7 +56,7 @@ void Generator::fromJSON(nlohmann::json j)
void Generator::updateDevice() void Generator::updateDevice()
{ {
if(!window->getDevice() || Mode::getActiveMode() != this) { if(!window->getDevice() || window->getModeHandler()->getActiveMode() != this) {
// can't update if not connected // can't update if not connected
return; return;
} }

View File

@ -17,6 +17,7 @@
#include "Device/firmwareupdatedialog.h" #include "Device/firmwareupdatedialog.h"
#include "preferences.h" #include "preferences.h"
#include "Generator/signalgenwidget.h" #include "Generator/signalgenwidget.h"
#include "modehandler.h"
#include <QDockWidget> #include <QDockWidget>
#include <QDesktopWidget> #include <QDesktopWidget>
@ -433,7 +434,7 @@ using namespace std;
void SpectrumAnalyzer::NewDatapoint(Protocol::SpectrumAnalyzerResult d) void SpectrumAnalyzer::NewDatapoint(Protocol::SpectrumAnalyzerResult d)
{ {
if(Mode::getActiveMode() != this) { if(window->getModeHandler()->getActiveMode() != this) {
return; return;
} }
@ -570,7 +571,7 @@ void SpectrumAnalyzer::SettingsChanged()
} }
} }
if(window->getDevice() && Mode::getActiveMode() == this) { if(window->getDevice() && window->getModeHandler()->getActiveMode() == this) {
window->getDevice()->Configure(settings, [=](Device::TransmissionResult res){ window->getDevice()->Configure(settings, [=](Device::TransmissionResult res){
// device received command // device received command
changingSettings = false; changingSettings = false;

View File

@ -22,6 +22,7 @@
#include "Calibration/manualcalibrationdialog.h" #include "Calibration/manualcalibrationdialog.h"
#include "Util/util.h" #include "Util/util.h"
#include "Tools/parameters.h" #include "Tools/parameters.h"
#include "modehandler.h"
#include <QGridLayout> #include <QGridLayout>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -801,7 +802,7 @@ using namespace std;
void VNA::NewDatapoint(Protocol::Datapoint d) void VNA::NewDatapoint(Protocol::Datapoint d)
{ {
if(Mode::getActiveMode() != this) { if(window->getModeHandler()->getActiveMode() != this) {
// ignore // ignore
return; return;
} }
@ -972,7 +973,7 @@ void VNA::SettingsChanged(bool resetTraces, std::function<void (Device::Transmis
s.cdbm_excitation_stop = stop * 100; s.cdbm_excitation_stop = stop * 100;
s.logSweep = false; s.logSweep = false;
} }
if(window->getDevice() && Mode::getActiveMode() == this) { if(window->getDevice() && window->getModeHandler()->getActiveMode() == this) {
if(s.excitePort1 == 0 && s.excitePort2 == 0) { if(s.excitePort1 == 0 && s.excitePort2 == 0) {
// no signal at either port, just set the device to idle // no signal at either port, just set the device to idle
window->getDevice()->SetIdle(); window->getDevice()->SetIdle();
@ -1486,7 +1487,7 @@ void VNA::SetupSCPI()
return ret; return ret;
})); }));
scpi_cal->add(new SCPICommand("MEASure", [=](QStringList params) -> QString { scpi_cal->add(new SCPICommand("MEASure", [=](QStringList params) -> QString {
if(params.size() != 1 || CalibrationMeasurementActive() || !window->getDevice() || Mode::getActiveMode() != this) { if(params.size() != 1 || CalibrationMeasurementActive() || !window->getDevice() || window->getModeHandler()->getActiveMode() != this) {
// no measurement specified, still busy or invalid mode // no measurement specified, still busy or invalid mode
return SCPI::getResultName(SCPI::Result::Error); return SCPI::getResultName(SCPI::Result::Error);
} else { } else {

View File

@ -264,7 +264,7 @@ AppWindow::AppWindow(QWidget *parent)
LoadSetup(filename); LoadSetup(filename);
}); });
connect(ui->actionSave_image, &QAction::triggered, [=](){ connect(ui->actionSave_image, &QAction::triggered, [=](){
Mode::getActiveMode()->saveSreenshot(); modeHandler->getActiveMode()->saveSreenshot();
}); });
connect(ui->actionManual_Control, &QAction::triggered, this, &AppWindow::StartManualControl); connect(ui->actionManual_Control, &QAction::triggered, this, &AppWindow::StartManualControl);
@ -284,17 +284,38 @@ AppWindow::AppWindow(QWidget *parent)
StartTCPServer(p.SCPIServer.port); StartTCPServer(p.SCPIServer.port);
} }
} }
// averaging mode may have changed, update for all relevant modes
if(p.Acquisition.useMedianAveraging) { for (auto m : modeHandler->getModes())
modeHandler->setAveragingMode(Averaging::Mode::Median); {
} else { switch (m->getType())
modeHandler->setAveragingMode(Averaging::Mode::Mean); {
case Mode::Type::VNA:
if(p.Acquisition.useMedianAveraging) {
static_cast<VNA*>(m)->setAveragingMode(Averaging::Mode::Median);
}
else {
static_cast<VNA*>(m)->setAveragingMode(Averaging::Mode::Mean);
}
break;
case Mode::Type::SA:
if(p.Acquisition.useMedianAveraging) {
static_cast<SpectrumAnalyzer*>(m)->setAveragingMode(Averaging::Mode::Median);
}
else {
static_cast<SpectrumAnalyzer*>(m)->setAveragingMode(Averaging::Mode::Mean);
}
break;
case Mode::Type::SG:
case Mode::Type::Last:
default:
break;
}
} }
// acquisition frequencies may have changed, update // acquisition frequencies may have changed, update
UpdateAcquisitionFrequencies(); UpdateAcquisitionFrequencies();
auto active = Mode::getActiveMode(); auto active = modeHandler->getActiveMode();
if (active) if (active)
{ {
active->updateGraphColors(); active->updateGraphColors();
@ -369,8 +390,8 @@ void AppWindow::closeEvent(QCloseEvent *event)
QSettings settings; QSettings settings;
settings.setValue("geometry", saveGeometry()); settings.setValue("geometry", saveGeometry());
// deactivate currently used mode (stores mode state in settings) // deactivate currently used mode (stores mode state in settings)
if(Mode::getActiveMode()) { if(modeHandler->getActiveMode()) {
Mode::getActiveMode()->deactivate(); modeHandler->deactivate(modeHandler->getActiveMode());
} }
delete device; delete device;
delete modeHandler; delete modeHandler;
@ -406,8 +427,8 @@ bool AppWindow::ConnectToDevice(QString serial)
ui->actionFrequency_Calibration->setEnabled(true); ui->actionFrequency_Calibration->setEnabled(true);
UpdateAcquisitionFrequencies(); UpdateAcquisitionFrequencies();
if (Mode::getActiveMode()) { if (modeHandler->getActiveMode()) {
Mode::getActiveMode()->initializeDevice(); modeHandler->getActiveMode()->initializeDevice();
} }
UpdateReference(); UpdateReference();
@ -445,8 +466,8 @@ void AppWindow::DisconnectDevice()
deviceActionGroup->checkedAction()->setChecked(false); deviceActionGroup->checkedAction()->setChecked(false);
} }
UpdateStatusBar(DeviceStatusBar::Disconnected); UpdateStatusBar(DeviceStatusBar::Disconnected);
if(Mode::getActiveMode()) { if(modeHandler->getActiveMode()) {
Mode::getActiveMode()->deviceDisconnected(); modeHandler->getActiveMode()->deviceDisconnected();
} }
qDebug() << "Disconnected device"; qDebug() << "Disconnected device";
} }
@ -585,7 +606,6 @@ void AppWindow::SetupSCPI()
return "INVALID MDOE"; return "INVALID MDOE";
} }
if(mode) { if(mode) {
mode->activate();
int index = modeHandler->findIndex(mode); int index = modeHandler->findIndex(mode);
modeHandler->setCurrentIndex(index); modeHandler->setCurrentIndex(index);
return SCPI::getResultName(SCPI::Result::Empty); return SCPI::getResultName(SCPI::Result::Empty);
@ -593,7 +613,7 @@ void AppWindow::SetupSCPI()
return SCPI::getResultName(SCPI::Result::Error); return SCPI::getResultName(SCPI::Result::Error);
} }
}, [=](QStringList) -> QString { }, [=](QStringList) -> QString {
auto active = Mode::getActiveMode(); auto active = modeHandler->getActiveMode();
if(active) { if(active) {
switch(active->getType()) { switch(active->getType()) {
case Mode::Type::VNA: return "VNA"; case Mode::Type::VNA: return "VNA";
@ -955,7 +975,7 @@ void AppWindow::StartManualControl()
connect(manual, &QDialog::finished, [=](){ connect(manual, &QDialog::finished, [=](){
manual = nullptr; manual = nullptr;
if(device) { if(device) {
Mode::getActiveMode()->initializeDevice(); modeHandler->getActiveMode()->initializeDevice();
} }
}); });
if(AppWindow::showGUI()) { if(AppWindow::showGUI()) {
@ -1035,7 +1055,7 @@ void AppWindow::DeviceStatusUpdated()
void AppWindow::SourceCalibrationDialog() void AppWindow::SourceCalibrationDialog()
{ {
auto d = new SourceCalDialog(device); auto d = new SourceCalDialog(device, modeHandler);
if(AppWindow::showGUI()) { if(AppWindow::showGUI()) {
d->exec(); d->exec();
} }
@ -1043,7 +1063,7 @@ void AppWindow::SourceCalibrationDialog()
void AppWindow::ReceiverCalibrationDialog() void AppWindow::ReceiverCalibrationDialog()
{ {
auto d = new ReceiverCalDialog(device); auto d = new ReceiverCalDialog(device, modeHandler);
if(AppWindow::showGUI()) { if(AppWindow::showGUI()) {
d->exec(); d->exec();
} }
@ -1051,7 +1071,7 @@ void AppWindow::ReceiverCalibrationDialog()
void AppWindow::FrequencyCalibrationDialog() void AppWindow::FrequencyCalibrationDialog()
{ {
auto d = new FrequencyCalDialog(device); auto d = new FrequencyCalDialog(device, modeHandler);
if(AppWindow::showGUI()) { if(AppWindow::showGUI()) {
d->exec(); d->exec();
} }
@ -1082,8 +1102,8 @@ nlohmann::json AppWindow::SaveSetup()
jm.push_back(jmode); jm.push_back(jmode);
} }
j["Modes"] = jm; j["Modes"] = jm;
if(Mode::getActiveMode()) { if(modeHandler->getActiveMode()) {
j["activeMode"] = Mode::getActiveMode()->getName().toStdString(); j["activeMode"] = modeHandler->getActiveMode()->getName().toStdString();
} }
nlohmann::json ref; nlohmann::json ref;
@ -1171,8 +1191,8 @@ void AppWindow::LoadSetup(nlohmann::json j)
} }
} }
// if no mode is activated, there might have been a problem with the setup file. Activate the first mode anyway, to prevent invalid GUI state // if no mode is activated, there might have been a problem with the setup file. Activate the first mode anyway, to prevent invalid GUI state
if(!Mode::getActiveMode() && modeHandler->getModes().size() > 0) { if(!modeHandler->getActiveMode() && modeHandler->getModes().size() > 0) {
modeHandler->getModes()[0]->activate(); modeHandler->activate(modeHandler->getModes()[0]);
} }
} }
@ -1186,6 +1206,12 @@ QStackedWidget *AppWindow::getCentral() const
return central; return central;
} }
ModeHandler* AppWindow::getModeHandler() const
{
return modeHandler;
}
Ui::MainWindow *AppWindow::getUi() const Ui::MainWindow *AppWindow::getUi() const
{ {
return ui; return ui;

View File

@ -42,6 +42,7 @@ public:
Ui::MainWindow *getUi() const; Ui::MainWindow *getUi() const;
QStackedWidget *getCentral() const; QStackedWidget *getCentral() const;
ModeHandler* getModeHandler() const;
Device*&getDevice(); Device*&getDevice();
const QString& getAppVersion() const; const QString& getAppVersion() const;

View File

@ -13,7 +13,6 @@
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>
Mode* Mode::activeMode = nullptr;
//QButtonGroup* Mode::modeButtonGroup = nullptr; //QButtonGroup* Mode::modeButtonGroup = nullptr;
Mode::Mode(AppWindow *window, QString name, QString SCPIname) Mode::Mode(AppWindow *window, QString name, QString SCPIname)
@ -29,9 +28,6 @@ Mode::Mode(AppWindow *window, QString name, QString SCPIname)
Mode::~Mode() Mode::~Mode()
{ {
window->getSCPI()->remove(this); window->getSCPI()->remove(this);
if(activeMode == this) {
deactivate();
}
window->getCentral()->removeWidget(central); window->getCentral()->removeWidget(central);
delete central; delete central;
for(auto d : docks) { for(auto d : docks) {
@ -44,13 +40,6 @@ Mode::~Mode()
void Mode::activate() void Mode::activate()
{ {
if(activeMode == this) {
// already active;
return;
} else if(activeMode) {
activeMode->deactivate();
}
qDebug() << "Activating mode" << name; qDebug() << "Activating mode" << name;
// show all mode specific GUI elements // show all mode specific GUI elements
for(auto t : toolbars) { for(auto t : toolbars) {
@ -88,8 +77,6 @@ void Mode::activate()
} }
} }
activeMode = this;
if(window->getDevice()) { if(window->getDevice()) {
initializeDevice(); initializeDevice();
} }
@ -126,12 +113,6 @@ void Mode::deactivate()
if(window->getDevice()) { if(window->getDevice()) {
window->getDevice()->SetIdle(); window->getDevice()->SetIdle();
} }
activeMode = nullptr;
}
Mode *Mode::getActiveMode()
{
return activeMode;
} }
QString Mode::TypeToName(Mode::Type t) QString Mode::TypeToName(Mode::Type t)
@ -168,16 +149,6 @@ void Mode::saveSreenshot()
central->grab().save(filename); central->grab().save(filename);
} }
Mode *Mode::createNew(AppWindow *window, QString name, Mode::Type t)
{
switch(t) {
case Type::VNA: return new VNA(window, name);
case Type::SG: return new Generator(window, name);
case Type::SA: return new SpectrumAnalyzer(window, name);
default: return nullptr;
}
}
void Mode::finalize(QWidget *centralWidget) void Mode::finalize(QWidget *centralWidget)
{ {
central = centralWidget; central = centralWidget;
@ -204,9 +175,7 @@ void Mode::finalize(QWidget *centralWidget)
void Mode::setStatusbarMessage(QString msg) void Mode::setStatusbarMessage(QString msg)
{ {
statusbarMsg = msg; statusbarMsg = msg;
if(this == activeMode) { emit statusbarMessage(msg);
emit statusbarMessage(msg);
}
} }
QString Mode::getName() const QString Mode::getName() const

View File

@ -16,6 +16,7 @@
class Mode : public QObject, public Savable, public SCPINode class Mode : public QObject, public Savable, public SCPINode
{ {
Q_OBJECT Q_OBJECT
friend class ModeHandler;
public: public:
enum class Type { enum class Type {
VNA, VNA,
@ -27,13 +28,10 @@ public:
Mode(AppWindow *window, QString name, QString SCPIname); Mode(AppWindow *window, QString name, QString SCPIname);
~Mode(); ~Mode();
virtual void activate(); // derived classes must call Mode::activate before doing anything
virtual void deactivate(); // derived classes must call Mode::deactivate before returning
virtual void shutdown(){}; // called when the application is about to exit virtual void shutdown(){}; // called when the application is about to exit
QString getName() const; QString getName() const;
void setName(const QString &value); void setName(const QString &value);
void updateGraphColors(); void updateGraphColors();
static Mode *getActiveMode();
static QString TypeToName(Type t); static QString TypeToName(Type t);
static Type TypeFromName(QString s); static Type TypeFromName(QString s);
virtual Type getType() = 0; virtual Type getType() = 0;
@ -43,11 +41,13 @@ public:
virtual void saveSreenshot(); virtual void saveSreenshot();
static Mode *createNew(AppWindow *window, QString name, Type t);
signals: signals:
void statusbarMessage(QString msg); void statusbarMessage(QString msg);
protected: protected:
virtual void activate(); // derived classes must call Mode::activate before doing anything
virtual void deactivate(); // derived classes must call Mode::deactivate before returning
void setStatusbarMessage(QString msg); void setStatusbarMessage(QString msg);
// call once the derived class is fully initialized // call once the derived class is fully initialized
void finalize(QWidget *centralWidget); void finalize(QWidget *centralWidget);
@ -57,8 +57,6 @@ protected:
std::set<QDockWidget*> docks; std::set<QDockWidget*> docks;
private: private:
static std::vector<Mode*> modes;
static Mode *activeMode;
// static QButtonGroup *modeButtonGroup; // static QButtonGroup *modeButtonGroup;
QString name; QString name;
QString statusbarMsg; QString statusbarMsg;

View File

@ -20,7 +20,7 @@ void ModeHandler::shutdown()
int ModeHandler::createMode(QString name, Mode::Type t) int ModeHandler::createMode(QString name, Mode::Type t)
{ {
auto mode = Mode::createNew(aw, name, t); auto mode = createNew(aw, name, t);
return createMode(mode); return createMode(mode);
} }
@ -30,18 +30,52 @@ int ModeHandler::createMode(Mode *mode)
currentModeIndex = int(modes.size()) - 1; currentModeIndex = int(modes.size()) - 1;
connect(mode, &Mode::statusbarMessage, this, &ModeHandler::setStatusBarMessageChanged); connect(mode, &Mode::statusbarMessage, this, &ModeHandler::setStatusBarMessageChanged);
auto * m = getMode(currentModeIndex); auto m = getMode(currentModeIndex);
m->activate(); activate(m);
emit ModeCreated(currentModeIndex); emit ModeCreated(currentModeIndex);
return (currentModeIndex); return (currentModeIndex);
} }
Mode *ModeHandler::createNew(AppWindow *aw, QString name, Mode::Type t)
{
switch(t) {
case Mode::Type::VNA: return new VNA(aw, name);
case Mode::Type::SG: return new Generator(aw, name);
case Mode::Type::SA: return new SpectrumAnalyzer(aw, name);
default: return nullptr;
}
}
Mode* ModeHandler::getActiveMode()
{
return activeMode;
}
Mode* ModeHandler::getMode(int index) Mode* ModeHandler::getMode(int index)
{ {
return modes.at(index); return modes.at(index);
} }
void ModeHandler::activate(Mode * mode)
{
if (getActiveMode() == mode) {
// Already active
return;
}
else if (getActiveMode()) {
deactivate(getActiveMode());
}
activeMode = mode;
mode->activate();
}
void ModeHandler::deactivate(Mode* mode)
{
mode->deactivate();
activeMode = nullptr;
}
std::vector<Mode*> ModeHandler::getModes() std::vector<Mode*> ModeHandler::getModes()
{ {
return modes; return modes;
@ -51,8 +85,8 @@ void ModeHandler::setCurrentIndex(int index)
{ {
if (index >= 0) { if (index >= 0) {
currentModeIndex = index; currentModeIndex = index;
auto * m = getMode(getCurrentIndex()); auto m = getMode(getCurrentIndex());
m->activate(); activate(m);
emit CurrentModeChanged(getCurrentIndex()); emit CurrentModeChanged(getCurrentIndex());
} }
} }
@ -111,6 +145,10 @@ void ModeHandler::closeMode(int index)
} }
} }
if (getActiveMode() == modes.at(index)) {
deactivate(getActiveMode());
}
delete modes.at(index); delete modes.at(index);
modes.erase(modes.begin() + index); modes.erase(modes.begin() + index);
@ -130,7 +168,10 @@ void ModeHandler::closeModes()
void ModeHandler::setStatusBarMessageChanged(const QString &msg) void ModeHandler::setStatusBarMessageChanged(const QString &msg)
{ {
emit StatusBarMessageChanged(msg); QObject* mode = sender();
if ( getActiveMode() == mode) {
emit StatusBarMessageChanged(msg);
}
} }
bool ModeHandler::nameAllowed(const QString &name) bool ModeHandler::nameAllowed(const QString &name)
@ -161,36 +202,3 @@ Mode* ModeHandler::findFirstOfType(Mode::Type t)
} }
return nullptr; return nullptr;
} }
void ModeHandler::setAveragingMode(Averaging::Mode value)
{
// averaging mode may have changed, update for all relevant modes
for (auto m : getModes())
{
switch (m->getType())
{
case Mode::Type::VNA:
static_cast<VNA*>(m)->setAveragingMode(value);
break;
case Mode::Type::SA:
static_cast<SpectrumAnalyzer*>(m)->setAveragingMode(value);
break;
case Mode::Type::SG:
case Mode::Type::Last:
default:
break;
}
}
for(auto m : modes) {
if (m->getType() == Mode::Type::SA) {
static_cast<SpectrumAnalyzer*>(m)->setAveragingMode(value);
}
else if (m->getType() == Mode::Type::VNA) {
static_cast<VNA*>(m)->setAveragingMode(value);
}
}
}

View File

@ -21,6 +21,11 @@ public:
void closeModes(); void closeModes();
int getCurrentIndex(); int getCurrentIndex();
Mode* getActiveMode();
void activate(Mode * mode);
void deactivate(Mode* mode);
Mode* getMode(int index); Mode* getMode(int index);
std::vector<Mode*> getModes(); std::vector<Mode*> getModes();
@ -28,8 +33,6 @@ public:
int findIndex(Mode *targetMode); int findIndex(Mode *targetMode);
Mode* findFirstOfType(Mode::Type t); Mode* findFirstOfType(Mode::Type t);
void setAveragingMode(Averaging::Mode m);
signals: signals:
void StatusBarMessageChanged(const QString &msg); void StatusBarMessageChanged(const QString &msg);
@ -44,7 +47,9 @@ private:
std::vector<Mode*> modes; std::vector<Mode*> modes;
int currentModeIndex; int currentModeIndex;
int createMode(Mode *mode); int createMode(Mode *mode);
Mode *createNew(AppWindow *window, QString name, Mode::Type t);
AppWindow *aw; AppWindow *aw;
Mode *activeMode = nullptr;
private slots: private slots:
void setStatusBarMessageChanged(const QString &msg); void setStatusBarMessageChanged(const QString &msg);