From: Joel Holdsworth Date: Tue, 11 Feb 2014 21:39:07 +0000 (+0000) Subject: SweepTimingWidget: When the exact value isn't available try to set a close match X-Git-Tag: pulseview-0.2.0~72 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d01c9439ccded6e751562996b80600a8c7397693;p=pulseview.git SweepTimingWidget: When the exact value isn't available try to set a close match --- diff --git a/pv/widgets/sweeptimingwidget.cpp b/pv/widgets/sweeptimingwidget.cpp index ee37d8d9..00ebb769 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