From 47db70a12e71ba59aff3abcc153d5b9b8108100a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Wed, 23 Apr 2025 15:40:04 +0200 Subject: [PATCH] Preselect most likely matching traces in touchstone export dialog --- .../CustomWidgets/tracesetselector.h | 1 + .../Traces/tracetouchstoneexport.cpp | 44 ++++++++++++++++--- .../Traces/tracetouchstoneexport.h | 3 +- .../LibreVNA-GUI/VNA/tracewidgetvna.cpp | 10 ----- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Software/PC_Application/LibreVNA-GUI/CustomWidgets/tracesetselector.h b/Software/PC_Application/LibreVNA-GUI/CustomWidgets/tracesetselector.h index 33a4b3a..1382d1d 100644 --- a/Software/PC_Application/LibreVNA-GUI/CustomWidgets/tracesetselector.h +++ b/Software/PC_Application/LibreVNA-GUI/CustomWidgets/tracesetselector.h @@ -35,6 +35,7 @@ public: double getLowerFreq() const; double getUpperFreq() const; double getReferenceImpedance() const; + TraceModel* getModel() const {return model;} signals: void selectionChanged(); diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.cpp b/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.cpp index 3e604bb..f091c22 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.cpp @@ -17,7 +17,23 @@ TraceTouchstoneExport::TraceTouchstoneExport(TraceModel &model, QWidget *parent) ui->selector->setModel(&model); ui->selector->setPartialSelectionAllowed(true); connect(ui->selector, qOverload<>(&TraceSetSelector::selectionChanged), this, &TraceTouchstoneExport::selectionChanged); - on_sbPorts_valueChanged(ui->sbPorts->value()); + connect(ui->sbPorts, &QSpinBox::valueChanged, this, &TraceTouchstoneExport::setPortNum); + // figure out how many ports the user most likely needs + unsigned int p; + for(p=4;p>=1;p--) { + // do we have a trace name which could indicate such a number of ports? + for(unsigned int i=1;i<=p;i++) { + auto n1 = "S"+QString::number(p)+QString::number(i); + auto n2 = "S"+QString::number(i)+QString::number(p); + for(auto t : model.getTraces()) { + if(t->name().contains(n1) || t->name().contains(n2)) { + goto traceFound; + } + } + } + } +traceFound: + setPortNum(p); } TraceTouchstoneExport::~TraceTouchstoneExport() @@ -30,13 +46,32 @@ bool TraceTouchstoneExport::setTrace(int portFrom, int portTo, Trace *t) return ui->selector->setTrace(portTo, portFrom, t); } -bool TraceTouchstoneExport::setPortNum(int ports) +bool TraceTouchstoneExport::setPortNum(unsigned int ports) { if(ports < 1 || ports > 4) { return false; } + if((unsigned int) ui->sbPorts->value() == ports && ui->selector->getPorts() == ports) { + // already set correctly, nothing to do + return true; + } ui->sbPorts->setValue(ports); ui->selector->setPorts(ports); + // Attempt to set default traces (this will result in correctly populated + // 2 port export if the initial 4 traces have not been modified) + auto traces = ui->selector->getModel()->getTraces(); + for(unsigned int i=1;i<=ports;i++) { + for(unsigned int j=1;j<=ports;j++) { + auto name = "S"+QString::number(i)+QString::number(j); + for(auto t : traces) { + if(t->name().contains(name)) { + // this could be the correct trace + setTrace(j, i, t); + break; + } + } + } + } return true; } @@ -86,11 +121,6 @@ void TraceTouchstoneExport::on_buttonBox_accepted() } } -void TraceTouchstoneExport::on_sbPorts_valueChanged(int ports) -{ - ui->selector->setPorts(ports); -} - void TraceTouchstoneExport::selectionChanged() { auto valid = ui->selector->selectionValid(); diff --git a/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.h b/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.h index ae27d7f..c984a39 100644 --- a/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.h +++ b/Software/PC_Application/LibreVNA-GUI/Traces/tracetouchstoneexport.h @@ -19,11 +19,10 @@ public: explicit TraceTouchstoneExport(TraceModel &model, QWidget *parent = nullptr); ~TraceTouchstoneExport(); bool setTrace(int portFrom, int portTo, Trace *t); - bool setPortNum(int ports); + bool setPortNum(unsigned int ports); private slots: void on_buttonBox_accepted(); - void on_sbPorts_valueChanged(int ports); void selectionChanged(); private: diff --git a/Software/PC_Application/LibreVNA-GUI/VNA/tracewidgetvna.cpp b/Software/PC_Application/LibreVNA-GUI/VNA/tracewidgetvna.cpp index 6327e5c..cfdf8f8 100644 --- a/Software/PC_Application/LibreVNA-GUI/VNA/tracewidgetvna.cpp +++ b/Software/PC_Application/LibreVNA-GUI/VNA/tracewidgetvna.cpp @@ -35,16 +35,6 @@ void TraceWidgetVNA::exportCSV() void TraceWidgetVNA::exportTouchstone() { auto e = new TraceTouchstoneExport(model); - // Attempt to set default traces (this will result in correctly populated - // 2 port export if the initial 4 traces have not been modified) - e->setPortNum(2); - auto traces = model.getTraces(); - for(unsigned int i=0;i<4;i++) { - if(i >= traces.size()) { - break; - } - e->setTrace(i%2+1, i/2+1, traces[i]); - } if(AppWindow::showGUI()) { e->show(); }