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"
34 SweepTimingWidget::SweepTimingWidget(const char *suffix,
43 setContentsMargins(0, 0, 0, 0);
45 value_.setDecimals(0);
46 value_.setSuffix(QString::fromUtf8(suffix));
48 connect(&list_, SIGNAL(currentIndexChanged(int)),
49 this, SIGNAL(value_changed()));
50 connect(&list_, SIGNAL(editTextChanged(const QString&)),
51 this, SIGNAL(value_changed()));
53 connect(&value_, SIGNAL(editingFinished()),
54 this, SIGNAL(value_changed()));
57 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
58 layout_.setContentsMargins(0, 0, 0, 0);
62 layout_.addWidget(&list_);
63 layout_.addWidget(&value_);
68 void SweepTimingWidget::allow_user_entered_values(bool value)
70 list_.setEditable(value);
73 void SweepTimingWidget::show_none()
80 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
86 value_type_ = MinMaxStep;
88 value_.setRange(min, max);
89 value_.setSingleStep(step);
95 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
100 for (size_t i = 0; i < count; i++) {
101 char *const s = sr_si_string_u64(vals[i], suffix_);
102 list_.addItem(QString::fromUtf8(s), QVariant::fromValue(vals[i]));
110 void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
114 // Create a 1-2-5-10 list of entries.
115 const unsigned int FineScales[] = {1, 2, 5};
116 uint64_t value, decade;
118 vector<uint64_t> values;
120 // Compute the starting decade
121 for (decade = 1; decade * 10 <= min; decade *= 10);
123 // Compute the first entry
124 for (fine = 0; fine < countof(FineScales); fine++)
125 if (FineScales[fine] * decade >= min)
128 assert(fine < countof(FineScales));
130 // Add the minimum entry if it's not on the 1-2-5 progression
131 if (min != FineScales[fine] * decade)
132 values.push_back(min);
134 while ((value = FineScales[fine] * decade) < max) {
135 values.push_back(value);
136 if (++fine >= countof(FineScales))
137 fine = 0, decade *= 10;
141 values.push_back(max);
143 // Make a C array, and give it to the sweep timing widget
144 uint64_t *const values_array = new uint64_t[values.size()];
145 copy(values.begin(), values.end(), values_array);
146 show_list(values_array, values.size());
147 delete[] values_array;
150 uint64_t SweepTimingWidget::value() const
152 switch (value_type_) {
156 return (uint64_t)value_.value();
159 if (list_.isEditable()) {
161 sr_parse_sizestring(list_.currentText().toUtf8().data(), &value);
165 const int index = list_.currentIndex();
166 return (index >= 0) ? list_.itemData(index).value<uint64_t>() : 0;
169 // Unexpected value type
175 void SweepTimingWidget::set_value(uint64_t value)
177 value_.setValue(value);
179 if (list_.isEditable()) {
180 char *const s = sr_si_string_u64(value, suffix_);
181 list_.lineEdit()->setText(QString::fromUtf8(s));
184 int best_match = list_.count() - 1;
185 int64_t best_variance = INT64_MAX;
187 for (int i = 0; i < list_.count(); i++) {
188 const int64_t this_variance = abs(
189 (int64_t)value - list_.itemData(i).value<int64_t>());
190 if (this_variance < best_variance) {
191 best_variance = this_variance;
196 list_.setCurrentIndex(best_match);
200 } // namespace widgets