]> sigrok.org Git - pulseview.git/blame - pv/widgets/sweeptimingwidget.cpp
Accept user-entered sample rates when external clock is enabled
[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
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 28using std::abs;
67d16394
JH
29using std::vector;
30
1198b887
JH
31namespace pv {
32namespace widgets {
33
34SweepTimingWidget::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
JH
56 setLayout(&layout_);
57 layout_.setMargin(0);
58 layout_.addWidget(&list_);
59 layout_.addWidget(&value_);
1198b887
JH
60
61 show_none();
62}
63
bc4b9ccf
SA
64void SweepTimingWidget::allow_user_entered_values(bool value)
65{
66 list_.setEditable(value);
67}
68
1198b887
JH
69void SweepTimingWidget::show_none()
70{
8dbbc7f0
JH
71 value_type_ = None;
72 value_.hide();
73 list_.hide();
1198b887
JH
74}
75
76void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
77 uint64_t step)
78{
67d16394
JH
79 assert(max > min);
80 assert(step > 0);
81
8dbbc7f0 82 value_type_ = MinMaxStep;
1198b887 83
8dbbc7f0
JH
84 value_.setRange(min, max);
85 value_.setSingleStep(step);
1198b887 86
8dbbc7f0
JH
87 value_.show();
88 list_.hide();
1198b887
JH
89}
90
91void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
92{
8dbbc7f0 93 value_type_ = List;
1198b887 94
8dbbc7f0 95 list_.clear();
2ad82c2e 96 for (size_t i = 0; i < count; i++) {
8dbbc7f0 97 char *const s = sr_si_string_u64(vals[i], suffix_);
c063290a 98 list_.addItem(QString::fromUtf8(s), qVariantFromValue(vals[i]));
1198b887
JH
99 g_free(s);
100 }
101
8dbbc7f0
JH
102 value_.hide();
103 list_.show();
1198b887
JH
104}
105
67d16394
JH
106void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
107{
108 assert(max > min);
109
110 // Create a 1-2-5-10 list of entries.
111 const unsigned int FineScales[] = {1, 2, 5};
112 uint64_t value, decade;
113 unsigned int fine;
114 vector<uint64_t> values;
115
116 // Compute the starting decade
117 for (decade = 1; decade * 10 <= min; decade *= 10);
118
119 // Compute the first entry
120 for (fine = 0; fine < countof(FineScales); fine++)
121 if (FineScales[fine] * decade >= min)
122 break;
123
124 assert(fine < countof(FineScales));
125
126 // Add the minimum entry if it's not on the 1-2-5 progression
127 if (min != FineScales[fine] * decade)
128 values.push_back(min);
129
130 while ((value = FineScales[fine] * decade) < max) {
131 values.push_back(value);
132 if (++fine >= countof(FineScales))
133 fine = 0, decade *= 10;
134 }
135
136 // Add the max value
137 values.push_back(max);
138
139 // Make a C array, and give it to the sweep timing widget
140 uint64_t *const values_array = new uint64_t[values.size()];
141 copy(values.begin(), values.end(), values_array);
142 show_list(values_array, values.size());
143 delete[] values_array;
144}
145
1198b887
JH
146uint64_t SweepTimingWidget::value() const
147{
c063290a 148 switch (value_type_) {
1198b887 149 case None:
1198b887 150 return 0;
1198b887 151 case MinMaxStep:
8dbbc7f0 152 return (uint64_t)value_.value();
1198b887
JH
153 case List:
154 {
bc4b9ccf
SA
155 if (list_.isEditable()) {
156 uint64_t value;
157 sr_parse_sizestring(list_.currentText().toUtf8().data(), &value);
158 return value;
159 }
160
8dbbc7f0 161 const int index = list_.currentIndex();
bc4b9ccf 162 return (index >= 0) ? list_.itemData(index).value<uint64_t>() : 0;
1198b887 163 }
1198b887
JH
164 default:
165 // Unexpected value type
0402d7a3 166 assert(false);
1198b887
JH
167 return 0;
168 }
169}
170
171void SweepTimingWidget::set_value(uint64_t value)
172{
8dbbc7f0 173 value_.setValue(value);
1198b887 174
bc4b9ccf
SA
175 if (list_.isEditable()) {
176 char *const s = sr_si_string_u64(value, suffix_);
177 list_.lineEdit()->setText(QString::fromUtf8(s));
178 g_free(s);
179 } else {
180 int best_match = list_.count() - 1;
181 int64_t best_variance = INT64_MAX;
182
183 for (int i = 0; i < list_.count(); i++) {
184 const int64_t this_variance = abs(
185 (int64_t)value - list_.itemData(i).value<int64_t>());
186 if (this_variance < best_variance) {
187 best_variance = this_variance;
188 best_match = i;
189 }
d01c9439 190 }
d01c9439 191
bc4b9ccf
SA
192 list_.setCurrentIndex(best_match);
193 }
1198b887
JH
194}
195
870ea3db
UH
196} // namespace widgets
197} // namespace pv