Save/load trace and graph setup

This commit is contained in:
Jan Käberich 2020-12-04 23:49:52 +01:00
parent b91f431473
commit 9ad8def2ea
33 changed files with 605 additions and 28 deletions

View file

@ -55,6 +55,20 @@ QWidget *MedianFilter::createExplanationWidget()
return w;
}
nlohmann::json MedianFilter::toJSON()
{
nlohmann::json j;
j["kernel"] = kernelSize;
j["order"] = order;
return j;
}
void MedianFilter::fromJSON(nlohmann::json j)
{
kernelSize = j.value("kernel", 3);
order = j.value("order", Order::AbsoluteValue);
}
void MedianFilter::inputSamplesChanged(unsigned int begin, unsigned int end) {
if(data.size() != input->rData().size()) {
data.resize(input->rData().size());

View file

@ -17,6 +17,10 @@ public:
static QWidget *createExplanationWidget();
virtual nlohmann::json toJSON() override;
virtual void fromJSON(nlohmann::json j) override;
Type getType() override {return Type::MedianFilter;};
public slots:
// a single value of the input data has changed, index determines which sample has changed
virtual void inputSamplesChanged(unsigned int begin, unsigned int end) override;

View file

@ -111,6 +111,44 @@ QWidget *TDR::createExplanationWidget()
return new QLabel("Test");
}
nlohmann::json TDR::toJSON()
{
nlohmann::json j;
j["bandpass_mode"] = mode == Mode::Bandpass;
j["window"] = window.toJSON();
if(mode == Mode::Lowpass) {
j["step_response"] = stepResponse;
if(stepResponse) {
j["automatic_DC"] = automaticDC;
if(!automaticDC) {
j["manual_DC_real"] = manualDC.real();
j["manual_DC_imag"] = manualDC.imag();
}
}
}
return j;
}
void TDR::fromJSON(nlohmann::json j)
{
if(j.value("bandpass_mode", true)) {
mode = Mode::Bandpass;
} else {
mode = Mode::Lowpass;
if(j.value("step_response", true)) {
stepResponse = true;
if(j.value("automatic_DC", true)) {
automaticDC = true;
} else {
automaticDC = false;
manualDC = complex<double>(j.value("manual_DC_real", 1.0), j.value("manual_DC_imag", 0.0));
}
} else {
stepResponse = false;
}
}
}
void TDR::inputSamplesChanged(unsigned int begin, unsigned int end)
{
Q_UNUSED(begin);

View file

@ -17,6 +17,10 @@ public:
static QWidget* createExplanationWidget();
virtual nlohmann::json toJSON() override;
virtual void fromJSON(nlohmann::json j) override;
Type getType() override {return Type::TDR;};
public slots:
void inputSamplesChanged(unsigned int begin, unsigned int end) override;

View file

@ -4,6 +4,7 @@
#include <QObject>
#include <vector>
#include <complex>
#include "savable.h"
/*
* How to implement a new type of math operation:
@ -34,6 +35,8 @@
* Provide a hint by passing a short description string
* error(): something went wrong (called with wrong type of data, mathematical error, ...).
* Provide a hint by passing a short description string
* e. getType(): return the type of the operation
* f. toJSON() and fromJSON(). Save/load all internal parameters
* 3. Add a new type to the Type enum for your operation
* 4. Extend the createMath(Type type) factory function to create an instance of your operation
* 5. Add a static function "createExplanationWidget" which returns a QWidget explaining what your operation does.
@ -43,7 +46,7 @@
class Trace;
class TraceMath : public QObject {
class TraceMath : public QObject, public Savable {
Q_OBJECT
public:
TraceMath();
@ -98,6 +101,7 @@ public:
std::vector<Data>& rData() { return data;};
Status getStatus() const;
QString getStatusDescription() const;
virtual Type getType() = 0;
// returns the trace this math operation is attached to
Trace* root();

View file

@ -5,6 +5,7 @@
#include <QLabel>
#include <QFormLayout>
#include "CustomWidgets/siunitedit.h"
#include <QDebug>
QString WindowFunction::typeToName(WindowFunction::Type type)
{
@ -108,6 +109,45 @@ QString WindowFunction::getDescription()
return ret;
}
nlohmann::json WindowFunction::toJSON()
{
nlohmann::json j;
j["type"] = typeToName(type).toStdString();
// add additional parameter if type has one
switch(type) {
case Type::Gaussian:
j["sigma"] = gaussian_sigma;
break;
default:
break;
}
return j;
}
void WindowFunction::fromJSON(nlohmann::json j)
{
qDebug() << "Setting window function from json";
QString typeName = QString::fromStdString(j["type"]);
unsigned int i=0;
for(;i<(int) Type::Last;i++) {
if(typeToName((Type) i) == typeName) {
type = Type(i);
break;
}
}
if(i>=(int) Type::Last) {
qWarning() << "Invalid window type specified, defaulting to hamming";
type = Type::Hamming;
}
switch(type) {
case Type::Gaussian:
gaussian_sigma = j.value("sigma", 0.4);
break;
default:
break;
}
}
double WindowFunction::getFactor(unsigned int n, unsigned int N)
{
// all formulas from https://en.wikipedia.org/wiki/Window_function

View file

@ -4,8 +4,9 @@
#include <QWidget>
#include <complex>
#include <vector>
#include "savable.h"
class WindowFunction : public QObject
class WindowFunction : public QObject, public Savable
{
Q_OBJECT;
public:
@ -31,6 +32,9 @@ public:
Type getType() const;
QString getDescription();
virtual nlohmann::json toJSON() override;
virtual void fromJSON(nlohmann::json j) override;
signals:
void changed();