]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
1198b887 JH |
18 | */ |
19 | ||
2acdb232 | 20 | #include "sweeptimingwidget.hpp" |
1198b887 | 21 | |
ff30bdba | 22 | #include <cstdlib> |
61d64fe9 | 23 | |
67d16394 JH |
24 | #include <vector> |
25 | ||
1198b887 JH |
26 | #include <assert.h> |
27 | ||
67d16394 JH |
28 | #include <extdef.h> |
29 | ||
c8870d9e | 30 | using std::abs; |
67d16394 JH |
31 | using std::vector; |
32 | ||
1198b887 JH |
33 | namespace pv { |
34 | namespace widgets { | |
35 | ||
36 | SweepTimingWidget::SweepTimingWidget(const char *suffix, | |
37 | QWidget *parent) : | |
38 | QWidget(parent), | |
8dbbc7f0 JH |
39 | suffix_(suffix), |
40 | layout_(this), | |
41 | value_(this), | |
42 | list_(this), | |
43 | value_type_(None) | |
1198b887 JH |
44 | { |
45 | setContentsMargins(0, 0, 0, 0); | |
46 | ||
8dbbc7f0 JH |
47 | value_.setDecimals(0); |
48 | value_.setSuffix(QString::fromUtf8(suffix)); | |
1198b887 | 49 | |
8dbbc7f0 | 50 | connect(&list_, SIGNAL(currentIndexChanged(int)), |
1198b887 | 51 | this, SIGNAL(value_changed())); |
8dbbc7f0 | 52 | connect(&value_, SIGNAL(editingFinished()), |
1198b887 JH |
53 | this, SIGNAL(value_changed())); |
54 | ||
8dbbc7f0 JH |
55 | setLayout(&layout_); |
56 | layout_.setMargin(0); | |
57 | layout_.addWidget(&list_); | |
58 | layout_.addWidget(&value_); | |
1198b887 JH |
59 | |
60 | show_none(); | |
61 | } | |
62 | ||
63 | void SweepTimingWidget::show_none() | |
64 | { | |
8dbbc7f0 JH |
65 | value_type_ = None; |
66 | value_.hide(); | |
67 | list_.hide(); | |
1198b887 JH |
68 | } |
69 | ||
70 | void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max, | |
71 | uint64_t step) | |
72 | { | |
67d16394 JH |
73 | assert(max > min); |
74 | assert(step > 0); | |
75 | ||
8dbbc7f0 | 76 | value_type_ = MinMaxStep; |
1198b887 | 77 | |
8dbbc7f0 JH |
78 | value_.setRange(min, max); |
79 | value_.setSingleStep(step); | |
1198b887 | 80 | |
8dbbc7f0 JH |
81 | value_.show(); |
82 | list_.hide(); | |
1198b887 JH |
83 | } |
84 | ||
85 | void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) | |
86 | { | |
8dbbc7f0 | 87 | value_type_ = List; |
1198b887 | 88 | |
8dbbc7f0 | 89 | list_.clear(); |
2ad82c2e | 90 | for (size_t i = 0; i < count; i++) { |
8dbbc7f0 JH |
91 | char *const s = sr_si_string_u64(vals[i], suffix_); |
92 | list_.addItem(QString::fromUtf8(s), | |
1198b887 JH |
93 | qVariantFromValue(vals[i])); |
94 | g_free(s); | |
95 | } | |
96 | ||
8dbbc7f0 JH |
97 | value_.hide(); |
98 | list_.show(); | |
1198b887 JH |
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 | { | |
2ad82c2e | 143 | switch(value_type_) { |
1198b887 | 144 | case None: |
1198b887 JH |
145 | return 0; |
146 | ||
147 | case MinMaxStep: | |
8dbbc7f0 | 148 | return (uint64_t)value_.value(); |
1198b887 JH |
149 | |
150 | case List: | |
151 | { | |
8dbbc7f0 JH |
152 | const int index = list_.currentIndex(); |
153 | return (index >= 0) ? list_.itemData( | |
1198b887 JH |
154 | index).value<uint64_t>() : 0; |
155 | } | |
156 | ||
157 | default: | |
158 | // Unexpected value type | |
159 | assert(0); | |
160 | return 0; | |
161 | } | |
162 | } | |
163 | ||
164 | void SweepTimingWidget::set_value(uint64_t value) | |
165 | { | |
8dbbc7f0 | 166 | value_.setValue(value); |
1198b887 | 167 | |
8dbbc7f0 | 168 | int best_match = list_.count() - 1; |
d01c9439 JH |
169 | int64_t best_variance = INT64_MAX; |
170 | ||
8dbbc7f0 | 171 | for (int i = 0; i < list_.count(); i++) { |
d01c9439 | 172 | const int64_t this_variance = abs( |
8dbbc7f0 | 173 | (int64_t)value - list_.itemData(i).value<int64_t>()); |
d01c9439 JH |
174 | if (this_variance < best_variance) { |
175 | best_variance = this_variance; | |
176 | best_match = i; | |
177 | } | |
178 | } | |
179 | ||
8dbbc7f0 | 180 | list_.setCurrentIndex(best_match); |
1198b887 JH |
181 | } |
182 | ||
183 | } // widgets | |
184 | } // pv |