X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fwidgets%2Fsweeptimingwidget.cpp;h=7a734a134ede774702094792341dcbbf7e9df462;hp=ee37d8d98d55d2b2cbe879c0399a864f5dec130f;hb=c8870d9e7af0af746776680a266d0da3ec3280ec;hpb=b0ddc6bcc68bf8414021838515acd0decd3f0a77 diff --git a/pv/widgets/sweeptimingwidget.cpp b/pv/widgets/sweeptimingwidget.cpp index ee37d8d9..7a734a13 100644 --- a/pv/widgets/sweeptimingwidget.cpp +++ b/pv/widgets/sweeptimingwidget.cpp @@ -26,6 +26,7 @@ #include +using std::abs; using std::vector; namespace pv { @@ -165,9 +166,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