From d01c9439ccded6e751562996b80600a8c7397693 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Tue, 11 Feb 2014 21:39:07 +0000 Subject: [PATCH] SweepTimingWidget: When the exact value isn't available try to set a close match --- pv/widgets/sweeptimingwidget.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pv/widgets/sweeptimingwidget.cpp b/pv/widgets/sweeptimingwidget.cpp index ee37d8d..00ebb76 100644 --- a/pv/widgets/sweeptimingwidget.cpp +++ b/pv/widgets/sweeptimingwidget.cpp @@ -165,9 +165,19 @@ void SweepTimingWidget::set_value(uint64_t value) { _value.setValue(value); - for (int i = 0; i < _list.count(); i++) - if (value == _list.itemData(i).value()) - _list.setCurrentIndex(i); + int best_match = _list.count() - 1; + int64_t best_variance = INT64_MAX; + + for (int i = 0; i < _list.count(); i++) { + const int64_t this_variance = abs( + (int64_t)value - _list.itemData(i).value()); + if (this_variance < best_variance) { + best_variance = this_variance; + best_match = i; + } + } + + _list.setCurrentIndex(best_match); } } // widgets -- 2.30.2