From 3a88e10875f9ee0730d61477ae98ae81c94994c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Wed, 25 Nov 2020 21:43:42 +0100 Subject: [PATCH 1/3] SIUnitEdit changeable with scroll wheel --- .../CustomWidgets/siunitedit.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Software/PC_Application/CustomWidgets/siunitedit.cpp b/Software/PC_Application/CustomWidgets/siunitedit.cpp index c20a9ef..4705934 100644 --- a/Software/PC_Application/CustomWidgets/siunitedit.cpp +++ b/Software/PC_Application/CustomWidgets/siunitedit.cpp @@ -6,6 +6,7 @@ #include #include #include +#include SIUnitEdit::SIUnitEdit(QString unit, QString prefixes, int precision, QWidget *parent) : QLineEdit(parent) @@ -88,6 +89,30 @@ bool SIUnitEdit::eventFilter(QObject *, QEvent *event) // online found clumsy way to select all text when clicked!?! // just selectAll() alone does _not_ work! QTimer::singleShot(0, this, &SIUnitEdit::continueEditing); + } else if(event->type() == QEvent::Wheel) { + if(_value == 0.0) { + // can't figure out step size with zero value + return false; + } + auto wheel = static_cast(event); + // most mousewheel have 15 degree increments, the reported delta is in 1/8th degree -> 120 + auto increment = wheel->angleDelta().y() / 120.0; + // round toward bigger step in case of special higher resolution mousewheel + unsigned int steps = abs(increment > 0 ? ceil(increment) : floor(increment)); + int sign = increment > 0 ? 1 : -1; + // figure out step increment + auto newVal = _value; + while(steps > 0) { + // do update in multiple steps because the step size could change inbetween + constexpr int nthDigit = 3; + auto step_size = pow(10, floor(log10(abs(newVal))) - nthDigit + 1); + newVal += step_size * sign; + steps--; + } + setValue(newVal); + continueEditing(); + setFocus(); + return true; } return false; } From 1a7212191a4ce8198d51c7570ab641b03a9ffc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Thu, 26 Nov 2020 17:45:55 +0100 Subject: [PATCH 2/3] Graph display bugfixes --- Software/PC_Application/Traces/trace.cpp | 7 ++- Software/PC_Application/Traces/traceplot.cpp | 2 +- .../PC_Application/Traces/tracexyplot.cpp | 48 ++++++++++++------- Software/PC_Application/Traces/tracexyplot.h | 4 +- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Software/PC_Application/Traces/trace.cpp b/Software/PC_Application/Traces/trace.cpp index 5f089c9..5c9702a 100644 --- a/Software/PC_Application/Traces/trace.cpp +++ b/Software/PC_Application/Traces/trace.cpp @@ -166,6 +166,11 @@ void Trace::removeMarker(TraceMarker *m) void Trace::updateTimeDomainData() { + if(_data.size() < 2) { + // can't compute anything + timeDomain.clear(); + return; + } // using namespace std::chrono; // auto starttime = duration_cast< milliseconds >( // system_clock::now().time_since_epoch() @@ -221,7 +226,7 @@ void Trace::updateTimeDomainData() t.impedance = numeric_limits::quiet_NaN(); } last_step += t.impulseResponse; - timeDomain.push_back(t); + timeDomain[i] = t; } // auto duration = duration_cast< milliseconds >( // system_clock::now().time_since_epoch() diff --git a/Software/PC_Application/Traces/traceplot.cpp b/Software/PC_Application/Traces/traceplot.cpp index 933dad5..0c953a6 100644 --- a/Software/PC_Application/Traces/traceplot.cpp +++ b/Software/PC_Application/Traces/traceplot.cpp @@ -234,7 +234,7 @@ void TracePlot::dragLeaveEvent(QDragLeaveEvent *event) Q_UNUSED(event) dropPending = false; dropTrace = nullptr; - triggerReplot(); + replot(); } std::set TracePlot::getPlots() diff --git a/Software/PC_Application/Traces/tracexyplot.cpp b/Software/PC_Application/Traces/tracexyplot.cpp index 319e4c3..bb53d33 100644 --- a/Software/PC_Application/Traces/tracexyplot.cpp +++ b/Software/PC_Application/Traces/tracexyplot.cpp @@ -355,8 +355,8 @@ void TraceXYPlot::draw(QPainter &p) p.setPen(pen); auto nPoints = numTraceSamples(t); for(unsigned int j=1;j XAxis.rangeMax) { continue; } - QPointF markerPoint = QPointF(xPosition, transformY(m->getData(), YAxis[i].type)); + QPointF markerPoint = QPointF(xPosition, traceToCoordinate(m->getData(), YAxis[i].type)); auto point = plotValueToPixel(markerPoint, i); if(!plotRect.contains(point)) { // out of screen @@ -451,7 +451,12 @@ void TraceXYPlot::updateAxisTicks() { auto createEvenlySpacedTicks = [](vector& ticks, double start, double stop, double step) { ticks.clear(); - for(double tick = start; tick - stop < numeric_limits::epsilon() ;tick+= step) { + if(start > stop) { + swap(start, stop); + } + step = abs(step); + constexpr unsigned int maxTicks = 100; + for(double tick = start; tick - stop < numeric_limits::epsilon() && ticks.size() <= maxTicks;tick+= step) { ticks.push_back(tick); } }; @@ -494,6 +499,10 @@ void TraceXYPlot::updateAxisTicks() || tracesAxis[1].find(t.first) != tracesAxis[1].end()); auto trace = t.first; if(enabled && trace->isVisible()) { + if(!numTraceSamples(trace)) { + // empty trace, do not use for automatic axis calculation + continue; + } // this trace is currently displayed double trace_min = std::numeric_limits::max(); double trace_max = std::numeric_limits::lowest(); @@ -520,13 +529,12 @@ void TraceXYPlot::updateAxisTicks() } } } - if(min >= max) { - // still at initial values, no traces are active, leave axis unchanged - return; + if(min < max) { + // found min/max values + XAxis.rangeMin = min; + XAxis.rangeMax = max; + XAxis.rangeDiv = createAutomaticTicks(XAxis.ticks, min, max, 8); } - XAxis.rangeMin = min; - XAxis.rangeMax = max; - XAxis.rangeDiv = createAutomaticTicks(XAxis.ticks, min, max, 8); } for(int i=0;i<2;i++) { @@ -539,7 +547,12 @@ void TraceXYPlot::updateAxisTicks() for(auto t : tracesAxis[i]) { unsigned int samples = numTraceSamples(t); for(unsigned int j=0;j XAxis.rangeMax) { + // this point is not in the displayed X range, skip for auto Y range calculation + continue; + } if(point.y() > max) { max = point.y(); @@ -557,7 +570,6 @@ void TraceXYPlot::updateAxisTicks() YAxis[i].rangeDiv = createAutomaticTicks(YAxis[i].ticks, min, max, 8); } } - triggerReplot(); } QString TraceXYPlot::AxisTypeToName(TraceXYPlot::YAxisType type) @@ -620,7 +632,7 @@ bool TraceXYPlot::supported(Trace *t, TraceXYPlot::YAxisType type) return true; } -double TraceXYPlot::transformY(std::complex data, TraceXYPlot::YAxisType type) +double TraceXYPlot::traceToCoordinate(std::complex data, TraceXYPlot::YAxisType type) { switch(type) { case YAxisType::Magnitude: @@ -646,7 +658,7 @@ double TraceXYPlot::transformY(std::complex data, TraceXYPlot::YAxisType return numeric_limits::quiet_NaN(); } -QPointF TraceXYPlot::transformY(Trace *t, unsigned int sample, TraceXYPlot::YAxisType type) +QPointF TraceXYPlot::traceToCoordinate(Trace *t, unsigned int sample, TraceXYPlot::YAxisType type) { QPointF ret = QPointF(numeric_limits::quiet_NaN(), numeric_limits::quiet_NaN()); switch(type) { @@ -654,7 +666,7 @@ QPointF TraceXYPlot::transformY(Trace *t, unsigned int sample, TraceXYPlot::YAxi case YAxisType::Phase: case YAxisType::VSWR: { auto d = t->sample(sample); - ret.setY(transformY(d.S, type)); + ret.setY(traceToCoordinate(d.S, type)); ret.setX(d.frequency); } break; @@ -705,7 +717,7 @@ QPoint TraceXYPlot::dataToPixel(Trace::Data d) if(d.frequency < XAxis.rangeMin || d.frequency > XAxis.rangeMax) { return QPoint(); } - auto y = transformY(d.S, YAxis[0].type); + auto y = traceToCoordinate(d.S, YAxis[0].type); QPoint p; p.setX(Util::Scale(d.frequency, XAxis.rangeMin, XAxis.rangeMax, plotAreaLeft, plotAreaLeft + plotAreaWidth)); p.setY(Util::Scale(y, YAxis[0].rangeMin, YAxis[0].rangeMax, plotAreaBottom, 0)); @@ -736,7 +748,7 @@ QPoint TraceXYPlot::markerToPixel(TraceMarker *m) return ret; } QPointF plotPoint; - plotPoint.setY(transformY(m->getData(), YAxis[0].type)); + plotPoint.setY(traceToCoordinate(m->getData(), YAxis[0].type)); if(m->isTimeDomain()) { auto timedata = m->getTimeData(); if(XAxis.type == XAxisType::Distance) { @@ -760,7 +772,7 @@ double TraceXYPlot::nearestTracePoint(Trace *t, QPoint pixel) double closestXpos = 0; auto samples = numTraceSamples(t); for(unsigned int i=0;i data, YAxisType type); - QPointF transformY(Trace *t, unsigned int sample, YAxisType type); + double traceToCoordinate(std::complex data, YAxisType type); + QPointF traceToCoordinate(Trace *t, unsigned int sample, YAxisType type); unsigned int numTraceSamples(Trace *t); QPoint dataToPixel(Trace::Data d); QPoint plotValueToPixel(QPointF plotValue, int Yaxis); From 19bc31b7cc34ba6e28a45f2d46ad174de72ad396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Thu, 26 Nov 2020 21:00:16 +0100 Subject: [PATCH 3/3] Overflow/replot bugfix when moving markers --- Software/PC_Application/Traces/traceplot.cpp | 1 + Software/PC_Application/Traces/tracexyplot.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Software/PC_Application/Traces/traceplot.cpp b/Software/PC_Application/Traces/traceplot.cpp index 0c953a6..083cb18 100644 --- a/Software/PC_Application/Traces/traceplot.cpp +++ b/Software/PC_Application/Traces/traceplot.cpp @@ -227,6 +227,7 @@ void TracePlot::dropEvent(QDropEvent *event) } dropPending = false; dropTrace = nullptr; + replot(); } void TracePlot::dragLeaveEvent(QDragLeaveEvent *event) diff --git a/Software/PC_Application/Traces/tracexyplot.cpp b/Software/PC_Application/Traces/tracexyplot.cpp index bb53d33..fd9eb65 100644 --- a/Software/PC_Application/Traces/tracexyplot.cpp +++ b/Software/PC_Application/Traces/tracexyplot.cpp @@ -777,8 +777,8 @@ double TraceXYPlot::nearestTracePoint(Trace *t, QPoint pixel) continue; } auto plotPoint = plotValueToPixel(point, 0); - auto diff = plotPoint - pixel; - unsigned int distance = diff.x() * diff.x() + diff.y() * diff.y(); + QPointF diff = plotPoint - pixel; + auto distance = diff.x() * diff.x() + diff.y() * diff.y(); if(distance < closestDistance) { closestDistance = distance; closestXpos = point.x();