]> sigrok.org Git - pulseview.git/commitdiff
SweepTimingWidget: When the exact value isn't available try to set a close match
authorJoel Holdsworth <redacted>
Tue, 11 Feb 2014 21:39:07 +0000 (21:39 +0000)
committerJoel Holdsworth <redacted>
Tue, 11 Feb 2014 21:45:57 +0000 (21:45 +0000)
pv/widgets/sweeptimingwidget.cpp

index ee37d8d98d55d2b2cbe879c0399a864f5dec130f..00ebb7697e010fccdb6d256db53292e6a7a01447 100644 (file)
@@ -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<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