]>
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> |
67d16394 | 23 | #include <vector> |
eb8269e3 | 24 | #include <cassert> |
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())); |
8dbbc7f0 | 50 | connect(&value_, SIGNAL(editingFinished()), |
1198b887 JH |
51 | this, SIGNAL(value_changed())); |
52 | ||
8dbbc7f0 JH |
53 | setLayout(&layout_); |
54 | layout_.setMargin(0); | |
55 | layout_.addWidget(&list_); | |
56 | layout_.addWidget(&value_); | |
1198b887 JH |
57 | |
58 | show_none(); | |
59 | } | |
60 | ||
61 | void SweepTimingWidget::show_none() | |
62 | { | |
8dbbc7f0 JH |
63 | value_type_ = None; |
64 | value_.hide(); | |
65 | list_.hide(); | |
1198b887 JH |
66 | } |
67 | ||
68 | void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max, | |
69 | uint64_t step) | |
70 | { | |
67d16394 JH |
71 | assert(max > min); |
72 | assert(step > 0); | |
73 | ||
8dbbc7f0 | 74 | value_type_ = MinMaxStep; |
1198b887 | 75 | |
8dbbc7f0 JH |
76 | value_.setRange(min, max); |
77 | value_.setSingleStep(step); | |
1198b887 | 78 | |
8dbbc7f0 JH |
79 | value_.show(); |
80 | list_.hide(); | |
1198b887 JH |
81 | } |
82 | ||
83 | void SweepTimingWidget::show_list(const uint64_t *vals, size_t count) | |
84 | { | |
8dbbc7f0 | 85 | value_type_ = List; |
1198b887 | 86 | |
8dbbc7f0 | 87 | list_.clear(); |
2ad82c2e | 88 | for (size_t i = 0; i < count; i++) { |
8dbbc7f0 JH |
89 | char *const s = sr_si_string_u64(vals[i], suffix_); |
90 | list_.addItem(QString::fromUtf8(s), | |
1198b887 JH |
91 | qVariantFromValue(vals[i])); |
92 | g_free(s); | |
93 | } | |
94 | ||
8dbbc7f0 JH |
95 | value_.hide(); |
96 | list_.show(); | |
1198b887 JH |
97 | } |
98 | ||
67d16394 JH |
99 | void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max) |
100 | { | |
101 | assert(max > min); | |
102 | ||
103 | // Create a 1-2-5-10 list of entries. | |
104 | const unsigned int FineScales[] = {1, 2, 5}; | |
105 | uint64_t value, decade; | |
106 | unsigned int fine; | |
107 | vector<uint64_t> values; | |
108 | ||
109 | // Compute the starting decade | |
110 | for (decade = 1; decade * 10 <= min; decade *= 10); | |
111 | ||
112 | // Compute the first entry | |
113 | for (fine = 0; fine < countof(FineScales); fine++) | |
114 | if (FineScales[fine] * decade >= min) | |
115 | break; | |
116 | ||
117 | assert(fine < countof(FineScales)); | |
118 | ||
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); | |
122 | ||
123 | while ((value = FineScales[fine] * decade) < max) { | |
124 | values.push_back(value); | |
125 | if (++fine >= countof(FineScales)) | |
126 | fine = 0, decade *= 10; | |
127 | } | |
128 | ||
129 | // Add the max value | |
130 | values.push_back(max); | |
131 | ||
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; | |
137 | } | |
138 | ||
1198b887 JH |
139 | uint64_t SweepTimingWidget::value() const |
140 | { | |
2ad82c2e | 141 | switch(value_type_) { |
1198b887 | 142 | case None: |
1198b887 JH |
143 | return 0; |
144 | ||
145 | case MinMaxStep: | |
8dbbc7f0 | 146 | return (uint64_t)value_.value(); |
1198b887 JH |
147 | |
148 | case List: | |
149 | { | |
8dbbc7f0 JH |
150 | const int index = list_.currentIndex(); |
151 | return (index >= 0) ? list_.itemData( | |
1198b887 JH |
152 | index).value<uint64_t>() : 0; |
153 | } | |
154 | ||
155 | default: | |
156 | // Unexpected value type | |
157 | assert(0); | |
158 | return 0; | |
159 | } | |
160 | } | |
161 | ||
162 | void SweepTimingWidget::set_value(uint64_t value) | |
163 | { | |
8dbbc7f0 | 164 | value_.setValue(value); |
1198b887 | 165 | |
8dbbc7f0 | 166 | int best_match = list_.count() - 1; |
d01c9439 JH |
167 | int64_t best_variance = INT64_MAX; |
168 | ||
8dbbc7f0 | 169 | for (int i = 0; i < list_.count(); i++) { |
d01c9439 | 170 | const int64_t this_variance = abs( |
8dbbc7f0 | 171 | (int64_t)value - list_.itemData(i).value<int64_t>()); |
d01c9439 JH |
172 | if (this_variance < best_variance) { |
173 | best_variance = this_variance; | |
174 | best_match = i; | |
175 | } | |
176 | } | |
177 | ||
8dbbc7f0 | 178 | list_.setCurrentIndex(best_match); |
1198b887 JH |
179 | } |
180 | ||
181 | } // widgets | |
182 | } // pv |