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(&value_, SIGNAL(editingFinished()),
51 this, SIGNAL(value_changed()));
55 layout_.addWidget(&list_);
56 layout_.addWidget(&value_);
61 void SweepTimingWidget::show_none()
68 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
74 value_type_ = MinMaxStep;
76 value_.setRange(min, max);
77 value_.setSingleStep(step);
83 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
88 for (size_t i = 0; i < count; i++) {
89 char *const s = sr_si_string_u64(vals[i], suffix_);
90 list_.addItem(QString::fromUtf8(s),
91 qVariantFromValue(vals[i]));
99 void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
103 // Create a 1-2-5-10 list of entries.
104 const unsigned int FineScales[] = {1, 2, 5};
105 uint64_t value, decade;
107 vector<uint64_t> values;
109 // Compute the starting decade
110 for (decade = 1; decade * 10 <= min; decade *= 10);
112 // Compute the first entry
113 for (fine = 0; fine < countof(FineScales); fine++)
114 if (FineScales[fine] * decade >= min)
117 assert(fine < countof(FineScales));
119 // Add the minimum entry if it's not on the 1-2-5 progression
120 if (min != FineScales[fine] * decade)
121 values.push_back(min);
123 while ((value = FineScales[fine] * decade) < max) {
124 values.push_back(value);
125 if (++fine >= countof(FineScales))
126 fine = 0, decade *= 10;
130 values.push_back(max);
132 // Make a C array, and give it to the sweep timing widget
133 uint64_t *const values_array = new uint64_t[values.size()];
134 copy(values.begin(), values.end(), values_array);
135 show_list(values_array, values.size());
136 delete[] values_array;
139 uint64_t SweepTimingWidget::value() const
141 switch(value_type_) {
146 return (uint64_t)value_.value();
150 const int index = list_.currentIndex();
151 return (index >= 0) ? list_.itemData(
152 index).value<uint64_t>() : 0;
156 // Unexpected value type
162 void SweepTimingWidget::set_value(uint64_t value)
164 value_.setValue(value);
166 int best_match = list_.count() - 1;
167 int64_t best_variance = INT64_MAX;
169 for (int i = 0; i < list_.count(); i++) {
170 const int64_t this_variance = abs(
171 (int64_t)value - list_.itemData(i).value<int64_t>());
172 if (this_variance < best_variance) {
173 best_variance = this_variance;
178 list_.setCurrentIndex(best_match);