#include <extdef.h>
+using std::abs;
using std::vector;
namespace pv {
SweepTimingWidget::SweepTimingWidget(const char *suffix,
QWidget *parent) :
QWidget(parent),
+ _suffix(suffix),
_layout(this),
_value(this),
_list(this),
_list.clear();
for (size_t i = 0; i < count; i++)
{
- char *const s = sr_samplerate_string(vals[i]);
+ char *const s = sr_si_string_u64(vals[i], _suffix);
_list.addItem(QString::fromUtf8(s),
qVariantFromValue(vals[i]));
g_free(s);
{
_value.setValue(value);
- for (int i = 0; i < _list.count(); i++)
- if (value == _list.itemData(i).value<uint64_t>())
- _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<int64_t>());
+ if (this_variance < best_variance) {
+ best_variance = this_variance;
+ best_match = i;
+ }
+ }
+
+ _list.setCurrentIndex(best_match);
}
} // widgets