2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "sweeptimingwidget.hpp"
36 SweepTimingWidget::SweepTimingWidget(const char *suffix,
45 setContentsMargins(0, 0, 0, 0);
47 value_.setDecimals(0);
48 value_.setSuffix(QString::fromUtf8(suffix));
50 connect(&list_, SIGNAL(currentIndexChanged(int)),
51 this, SIGNAL(value_changed()));
52 connect(&value_, SIGNAL(editingFinished()),
53 this, SIGNAL(value_changed()));
57 layout_.addWidget(&list_);
58 layout_.addWidget(&value_);
63 void SweepTimingWidget::show_none()
70 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
76 value_type_ = MinMaxStep;
78 value_.setRange(min, max);
79 value_.setSingleStep(step);
85 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
90 for (size_t i = 0; i < count; i++) {
91 char *const s = sr_si_string_u64(vals[i], suffix_);
92 list_.addItem(QString::fromUtf8(s),
93 qVariantFromValue(vals[i]));
101 void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
105 // Create a 1-2-5-10 list of entries.
106 const unsigned int FineScales[] = {1, 2, 5};
107 uint64_t value, decade;
109 vector<uint64_t> values;
111 // Compute the starting decade
112 for (decade = 1; decade * 10 <= min; decade *= 10);
114 // Compute the first entry
115 for (fine = 0; fine < countof(FineScales); fine++)
116 if (FineScales[fine] * decade >= min)
119 assert(fine < countof(FineScales));
121 // Add the minimum entry if it's not on the 1-2-5 progression
122 if (min != FineScales[fine] * decade)
123 values.push_back(min);
125 while ((value = FineScales[fine] * decade) < max) {
126 values.push_back(value);
127 if (++fine >= countof(FineScales))
128 fine = 0, decade *= 10;
132 values.push_back(max);
134 // Make a C array, and give it to the sweep timing widget
135 uint64_t *const values_array = new uint64_t[values.size()];
136 copy(values.begin(), values.end(), values_array);
137 show_list(values_array, values.size());
138 delete[] values_array;
141 uint64_t SweepTimingWidget::value() const
143 switch(value_type_) {
148 return (uint64_t)value_.value();
152 const int index = list_.currentIndex();
153 return (index >= 0) ? list_.itemData(
154 index).value<uint64_t>() : 0;
158 // Unexpected value type
164 void SweepTimingWidget::set_value(uint64_t value)
166 value_.setValue(value);
168 int best_match = list_.count() - 1;
169 int64_t best_variance = INT64_MAX;
171 for (int i = 0; i < list_.count(); i++) {
172 const int64_t this_variance = abs(
173 (int64_t)value - list_.itemData(i).value<int64_t>());
174 if (this_variance < best_variance) {
175 best_variance = this_variance;
180 list_.setCurrentIndex(best_match);