]> sigrok.org Git - pulseview.git/blame - pv/widgets/sweeptimingwidget.cpp
Use <cstdlib> for std::abs() on integer datatypes.
[pulseview.git] / pv / widgets / sweeptimingwidget.cpp
CommitLineData
1198b887
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "sweeptimingwidget.h"
22
ff30bdba 23#include <cstdlib>
61d64fe9 24
67d16394
JH
25#include <vector>
26
1198b887
JH
27#include <assert.h>
28
67d16394
JH
29#include <extdef.h>
30
c8870d9e 31using std::abs;
67d16394
JH
32using std::vector;
33
1198b887
JH
34namespace pv {
35namespace widgets {
36
37SweepTimingWidget::SweepTimingWidget(const char *suffix,
38 QWidget *parent) :
39 QWidget(parent),
b0ddc6bc 40 _suffix(suffix),
1198b887 41 _layout(this),
1198b887
JH
42 _value(this),
43 _list(this),
44 _value_type(None)
45{
46 setContentsMargins(0, 0, 0, 0);
47
48 _value.setDecimals(0);
49 _value.setSuffix(QString::fromUtf8(suffix));
50
51 connect(&_list, SIGNAL(currentIndexChanged(int)),
52 this, SIGNAL(value_changed()));
53 connect(&_value, SIGNAL(editingFinished()),
54 this, SIGNAL(value_changed()));
55
56 setLayout(&_layout);
57 _layout.setMargin(0);
1198b887
JH
58 _layout.addWidget(&_list);
59 _layout.addWidget(&_value);
60
61 show_none();
62}
63
64void SweepTimingWidget::show_none()
65{
66 _value_type = None;
1198b887
JH
67 _value.hide();
68 _list.hide();
69}
70
71void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
72 uint64_t step)
73{
67d16394
JH
74 assert(max > min);
75 assert(step > 0);
76
1198b887
JH
77 _value_type = MinMaxStep;
78
79 _value.setRange(min, max);
80 _value.setSingleStep(step);
81
1198b887
JH
82 _value.show();
83 _list.hide();
84}
85
86void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
87{
88 _value_type = List;
89
90 _list.clear();
91 for (size_t i = 0; i < count; i++)
92 {
b0ddc6bc 93 char *const s = sr_si_string_u64(vals[i], _suffix);
1198b887
JH
94 _list.addItem(QString::fromUtf8(s),
95 qVariantFromValue(vals[i]));
96 g_free(s);
97 }
98
1198b887
JH
99 _value.hide();
100 _list.show();
101}
102
67d16394
JH
103void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
104{
105 assert(max > min);
106
107 // Create a 1-2-5-10 list of entries.
108 const unsigned int FineScales[] = {1, 2, 5};
109 uint64_t value, decade;
110 unsigned int fine;
111 vector<uint64_t> values;
112
113 // Compute the starting decade
114 for (decade = 1; decade * 10 <= min; decade *= 10);
115
116 // Compute the first entry
117 for (fine = 0; fine < countof(FineScales); fine++)
118 if (FineScales[fine] * decade >= min)
119 break;
120
121 assert(fine < countof(FineScales));
122
123 // Add the minimum entry if it's not on the 1-2-5 progression
124 if (min != FineScales[fine] * decade)
125 values.push_back(min);
126
127 while ((value = FineScales[fine] * decade) < max) {
128 values.push_back(value);
129 if (++fine >= countof(FineScales))
130 fine = 0, decade *= 10;
131 }
132
133 // Add the max value
134 values.push_back(max);
135
136 // Make a C array, and give it to the sweep timing widget
137 uint64_t *const values_array = new uint64_t[values.size()];
138 copy(values.begin(), values.end(), values_array);
139 show_list(values_array, values.size());
140 delete[] values_array;
141}
142
1198b887
JH
143uint64_t SweepTimingWidget::value() const
144{
145 switch(_value_type)
146 {
147 case None:
1198b887
JH
148 return 0;
149
150 case MinMaxStep:
151 return (uint64_t)_value.value();
152
153 case List:
154 {
155 const int index = _list.currentIndex();
156 return (index >= 0) ? _list.itemData(
157 index).value<uint64_t>() : 0;
158 }
159
160 default:
161 // Unexpected value type
162 assert(0);
163 return 0;
164 }
165}
166
167void SweepTimingWidget::set_value(uint64_t value)
168{
1198b887
JH
169 _value.setValue(value);
170
d01c9439
JH
171 int best_match = _list.count() - 1;
172 int64_t best_variance = INT64_MAX;
173
174 for (int i = 0; i < _list.count(); i++) {
175 const int64_t this_variance = abs(
176 (int64_t)value - _list.itemData(i).value<int64_t>());
177 if (this_variance < best_variance) {
178 best_variance = this_variance;
179 best_match = i;
180 }
181 }
182
183 _list.setCurrentIndex(best_match);
1198b887
JH
184}
185
186} // widgets
187} // pv