X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=pv%2Fwidgets%2Fsweeptimingwidget.cpp;h=00ebb7697e010fccdb6d256db53292e6a7a01447;hb=65a53225414471cb2701db2429aa3ab9e74e5b21;hp=d34d07e5f7c2a43cea1ed8b870253c358c2550ad;hpb=5cacd7d9fbc6ba6e9e67940d2497f04dd54e7f76;p=pulseview.git diff --git a/pv/widgets/sweeptimingwidget.cpp b/pv/widgets/sweeptimingwidget.cpp index d34d07e..00ebb76 100644 --- a/pv/widgets/sweeptimingwidget.cpp +++ b/pv/widgets/sweeptimingwidget.cpp @@ -20,14 +20,21 @@ #include "sweeptimingwidget.h" +#include + #include +#include + +using std::vector; + namespace pv { namespace widgets { SweepTimingWidget::SweepTimingWidget(const char *suffix, QWidget *parent) : QWidget(parent), + _suffix(suffix), _layout(this), _value(this), _list(this), @@ -61,6 +68,9 @@ void SweepTimingWidget::show_none() void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max, uint64_t step) { + assert(max > min); + assert(step > 0); + _value_type = MinMaxStep; _value.setRange(min, max); @@ -77,7 +87,7 @@ void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) _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); @@ -87,6 +97,46 @@ void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) _list.show(); } +void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max) +{ + assert(max > min); + + // Create a 1-2-5-10 list of entries. + const unsigned int FineScales[] = {1, 2, 5}; + uint64_t value, decade; + unsigned int fine; + vector values; + + // Compute the starting decade + for (decade = 1; decade * 10 <= min; decade *= 10); + + // Compute the first entry + for (fine = 0; fine < countof(FineScales); fine++) + if (FineScales[fine] * decade >= min) + break; + + assert(fine < countof(FineScales)); + + // Add the minimum entry if it's not on the 1-2-5 progression + if (min != FineScales[fine] * decade) + values.push_back(min); + + while ((value = FineScales[fine] * decade) < max) { + values.push_back(value); + if (++fine >= countof(FineScales)) + fine = 0, decade *= 10; + } + + // Add the max value + values.push_back(max); + + // Make a C array, and give it to the sweep timing widget + uint64_t *const values_array = new uint64_t[values.size()]; + copy(values.begin(), values.end(), values_array); + show_list(values_array, values.size()); + delete[] values_array; +} + uint64_t SweepTimingWidget::value() const { switch(_value_type) @@ -115,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