]>
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 | |
aca9aa83 | 22 | #include <cassert> |
ff30bdba | 23 | #include <cstdlib> |
67d16394 | 24 | #include <vector> |
1198b887 | 25 | |
67d16394 JH |
26 | #include <extdef.h> |
27 | ||
c8870d9e | 28 | using std::abs; |
67d16394 JH |
29 | using std::vector; |
30 | ||
1198b887 JH |
31 | namespace pv { |
32 | namespace widgets { | |
33 | ||
34 | SweepTimingWidget::SweepTimingWidget(const char *suffix, | |
35 | QWidget *parent) : | |
36 | QWidget(parent), | |
8dbbc7f0 JH |
37 | suffix_(suffix), |
38 | layout_(this), | |
39 | value_(this), | |
40 | list_(this), | |
41 | value_type_(None) | |
1198b887 JH |
42 | { |
43 | setContentsMargins(0, 0, 0, 0); | |
44 | ||
8dbbc7f0 JH |
45 | value_.setDecimals(0); |
46 | value_.setSuffix(QString::fromUtf8(suffix)); | |
1198b887 | 47 | |
8dbbc7f0 | 48 | connect(&list_, SIGNAL(currentIndexChanged(int)), |
1198b887 | 49 | this, SIGNAL(value_changed())); |
bc4b9ccf SA |
50 | connect(&list_, SIGNAL(editTextChanged(const QString&)), |
51 | this, SIGNAL(value_changed())); | |
52 | ||
8dbbc7f0 | 53 | connect(&value_, SIGNAL(editingFinished()), |
1198b887 JH |
54 | this, SIGNAL(value_changed())); |
55 | ||
8dbbc7f0 | 56 | setLayout(&layout_); |
1ed73ebd VPP |
57 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
58 | layout_.setContentsMargins(0, 0, 0, 0); | |
59 | #else | |
8dbbc7f0 | 60 | layout_.setMargin(0); |
1ed73ebd | 61 | #endif |
8dbbc7f0 JH |
62 | layout_.addWidget(&list_); |
63 | layout_.addWidget(&value_); | |
1198b887 JH |
64 | |
65 | show_none(); | |
66 | } | |
67 | ||
bc4b9ccf SA |
68 | void SweepTimingWidget::allow_user_entered_values(bool value) |
69 | { | |
70 | list_.setEditable(value); | |
71 | } | |
72 | ||
1198b887 JH |
73 | void SweepTimingWidget::show_none() |
74 | { | |
8dbbc7f0 JH |
75 | value_type_ = None; |
76 | value_.hide(); | |
77 | list_.hide(); | |
1198b887 JH |
78 | } |
79 | ||
80 | void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max, | |
81 | uint64_t step) | |
82 | { | |
67d16394 JH |
83 | assert(max > min); |
84 | assert(step > 0); | |
85 | ||
8dbbc7f0 | 86 | value_type_ = MinMaxStep; |
1198b887 | 87 | |
8dbbc7f0 JH |
88 | value_.setRange(min, max); |
89 | value_.setSingleStep(step); | |
1198b887 | 90 | |
8dbbc7f0 JH |
91 | value_.show(); |
92 | list_.hide(); | |
1198b887 JH |
93 | } |
94 | ||
95 | void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) | |
96 | { | |
8dbbc7f0 | 97 | value_type_ = List; |
1198b887 | 98 | |
8dbbc7f0 | 99 | list_.clear(); |
2ad82c2e | 100 | for (size_t i = 0; i < count; i++) { |
8dbbc7f0 | 101 | char *const s = sr_si_string_u64(vals[i], suffix_); |
009fc9ae | 102 | list_.addItem(QString::fromUtf8(s), QVariant::fromValue(vals[i])); |
1198b887 JH |
103 | g_free(s); |
104 | } | |
105 | ||
8dbbc7f0 JH |
106 | value_.hide(); |
107 | list_.show(); | |
1198b887 JH |
108 | } |
109 | ||
67d16394 JH |
110 | void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max) |
111 | { | |
112 | assert(max > min); | |
113 | ||
114 | // Create a 1-2-5-10 list of entries. | |
115 | const unsigned int FineScales[] = {1, 2, 5}; | |
116 | uint64_t value, decade; | |
117 | unsigned int fine; | |
118 | vector<uint64_t> values; | |
119 | ||
120 | // Compute the starting decade | |
121 | for (decade = 1; decade * 10 <= min; decade *= 10); | |
122 | ||
123 | // Compute the first entry | |
124 | for (fine = 0; fine < countof(FineScales); fine++) | |
125 | if (FineScales[fine] * decade >= min) | |
126 | break; | |
127 | ||
128 | assert(fine < countof(FineScales)); | |
129 | ||
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); | |
133 | ||
134 | while ((value = FineScales[fine] * decade) < max) { | |
135 | values.push_back(value); | |
136 | if (++fine >= countof(FineScales)) | |
137 | fine = 0, decade *= 10; | |
138 | } | |
139 | ||
140 | // Add the max value | |
141 | values.push_back(max); | |
142 | ||
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; | |
148 | } | |
149 | ||
1198b887 JH |
150 | uint64_t SweepTimingWidget::value() const |
151 | { | |
c063290a | 152 | switch (value_type_) { |
1198b887 | 153 | case None: |
1198b887 | 154 | return 0; |
1198b887 | 155 | case MinMaxStep: |
8dbbc7f0 | 156 | return (uint64_t)value_.value(); |
1198b887 JH |
157 | case List: |
158 | { | |
bc4b9ccf SA |
159 | if (list_.isEditable()) { |
160 | uint64_t value; | |
161 | sr_parse_sizestring(list_.currentText().toUtf8().data(), &value); | |
162 | return value; | |
163 | } | |
164 | ||
8dbbc7f0 | 165 | const int index = list_.currentIndex(); |
bc4b9ccf | 166 | return (index >= 0) ? list_.itemData(index).value<uint64_t>() : 0; |
1198b887 | 167 | } |
1198b887 JH |
168 | default: |
169 | // Unexpected value type | |
0402d7a3 | 170 | assert(false); |
1198b887 JH |
171 | return 0; |
172 | } | |
173 | } | |
174 | ||
175 | void SweepTimingWidget::set_value(uint64_t value) | |
176 | { | |
8dbbc7f0 | 177 | value_.setValue(value); |
1198b887 | 178 | |
bc4b9ccf SA |
179 | if (list_.isEditable()) { |
180 | char *const s = sr_si_string_u64(value, suffix_); | |
181 | list_.lineEdit()->setText(QString::fromUtf8(s)); | |
182 | g_free(s); | |
183 | } else { | |
184 | int best_match = list_.count() - 1; | |
185 | int64_t best_variance = INT64_MAX; | |
186 | ||
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; | |
192 | best_match = i; | |
193 | } | |
d01c9439 | 194 | } |
d01c9439 | 195 | |
bc4b9ccf SA |
196 | list_.setCurrentIndex(best_match); |
197 | } | |
1198b887 JH |
198 | } |
199 | ||
870ea3db UH |
200 | } // namespace widgets |
201 | } // namespace pv |