]>
Commit | Line | Data |
---|---|---|
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 | ||
67d16394 JH |
23 | #include <vector> |
24 | ||
1198b887 JH |
25 | #include <assert.h> |
26 | ||
67d16394 JH |
27 | #include <extdef.h> |
28 | ||
c8870d9e | 29 | using std::abs; |
67d16394 JH |
30 | using std::vector; |
31 | ||
1198b887 JH |
32 | namespace pv { |
33 | namespace widgets { | |
34 | ||
35 | SweepTimingWidget::SweepTimingWidget(const char *suffix, | |
36 | QWidget *parent) : | |
37 | QWidget(parent), | |
b0ddc6bc | 38 | _suffix(suffix), |
1198b887 | 39 | _layout(this), |
1198b887 JH |
40 | _value(this), |
41 | _list(this), | |
42 | _value_type(None) | |
43 | { | |
44 | setContentsMargins(0, 0, 0, 0); | |
45 | ||
46 | _value.setDecimals(0); | |
47 | _value.setSuffix(QString::fromUtf8(suffix)); | |
48 | ||
49 | connect(&_list, SIGNAL(currentIndexChanged(int)), | |
50 | this, SIGNAL(value_changed())); | |
51 | connect(&_value, SIGNAL(editingFinished()), | |
52 | this, SIGNAL(value_changed())); | |
53 | ||
54 | setLayout(&_layout); | |
55 | _layout.setMargin(0); | |
1198b887 JH |
56 | _layout.addWidget(&_list); |
57 | _layout.addWidget(&_value); | |
58 | ||
59 | show_none(); | |
60 | } | |
61 | ||
62 | void SweepTimingWidget::show_none() | |
63 | { | |
64 | _value_type = None; | |
1198b887 JH |
65 | _value.hide(); |
66 | _list.hide(); | |
67 | } | |
68 | ||
69 | void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max, | |
70 | uint64_t step) | |
71 | { | |
67d16394 JH |
72 | assert(max > min); |
73 | assert(step > 0); | |
74 | ||
1198b887 JH |
75 | _value_type = MinMaxStep; |
76 | ||
77 | _value.setRange(min, max); | |
78 | _value.setSingleStep(step); | |
79 | ||
1198b887 JH |
80 | _value.show(); |
81 | _list.hide(); | |
82 | } | |
83 | ||
84 | void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) | |
85 | { | |
86 | _value_type = List; | |
87 | ||
88 | _list.clear(); | |
89 | for (size_t i = 0; i < count; i++) | |
90 | { | |
b0ddc6bc | 91 | char *const s = sr_si_string_u64(vals[i], _suffix); |
1198b887 JH |
92 | _list.addItem(QString::fromUtf8(s), |
93 | qVariantFromValue(vals[i])); | |
94 | g_free(s); | |
95 | } | |
96 | ||
1198b887 JH |
97 | _value.hide(); |
98 | _list.show(); | |
99 | } | |
100 | ||
67d16394 JH |
101 | void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max) |
102 | { | |
103 | assert(max > min); | |
104 | ||
105 | // Create a 1-2-5-10 list of entries. | |
106 | const unsigned int FineScales[] = {1, 2, 5}; | |
107 | uint64_t value, decade; | |
108 | unsigned int fine; | |
109 | vector<uint64_t> values; | |
110 | ||
111 | // Compute the starting decade | |
112 | for (decade = 1; decade * 10 <= min; decade *= 10); | |
113 | ||
114 | // Compute the first entry | |
115 | for (fine = 0; fine < countof(FineScales); fine++) | |
116 | if (FineScales[fine] * decade >= min) | |
117 | break; | |
118 | ||
119 | assert(fine < countof(FineScales)); | |
120 | ||
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); | |
124 | ||
125 | while ((value = FineScales[fine] * decade) < max) { | |
126 | values.push_back(value); | |
127 | if (++fine >= countof(FineScales)) | |
128 | fine = 0, decade *= 10; | |
129 | } | |
130 | ||
131 | // Add the max value | |
132 | values.push_back(max); | |
133 | ||
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; | |
139 | } | |
140 | ||
1198b887 JH |
141 | uint64_t SweepTimingWidget::value() const |
142 | { | |
143 | switch(_value_type) | |
144 | { | |
145 | case None: | |
1198b887 JH |
146 | return 0; |
147 | ||
148 | case MinMaxStep: | |
149 | return (uint64_t)_value.value(); | |
150 | ||
151 | case List: | |
152 | { | |
153 | const int index = _list.currentIndex(); | |
154 | return (index >= 0) ? _list.itemData( | |
155 | index).value<uint64_t>() : 0; | |
156 | } | |
157 | ||
158 | default: | |
159 | // Unexpected value type | |
160 | assert(0); | |
161 | return 0; | |
162 | } | |
163 | } | |
164 | ||
165 | void SweepTimingWidget::set_value(uint64_t value) | |
166 | { | |
1198b887 JH |
167 | _value.setValue(value); |
168 | ||
d01c9439 JH |
169 | int best_match = _list.count() - 1; |
170 | int64_t best_variance = INT64_MAX; | |
171 | ||
172 | for (int i = 0; i < _list.count(); i++) { | |
173 | const int64_t this_variance = abs( | |
174 | (int64_t)value - _list.itemData(i).value<int64_t>()); | |
175 | if (this_variance < best_variance) { | |
176 | best_variance = this_variance; | |
177 | best_match = i; | |
178 | } | |
179 | } | |
180 | ||
181 | _list.setCurrentIndex(best_match); | |
1198b887 JH |
182 | } |
183 | ||
184 | } // widgets | |
185 | } // pv |