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