]> sigrok.org Git - pulseview.git/blame - pv/widgets/sweeptimingwidget.cpp
InputFile: Don't try to create device twice
[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
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
2acdb232 21#include "sweeptimingwidget.hpp"
1198b887 22
ff30bdba 23#include <cstdlib>
61d64fe9 24
67d16394
JH
25#include <vector>
26
1198b887
JH
27#include <assert.h>
28
67d16394
JH
29#include <extdef.h>
30
c8870d9e 31using std::abs;
67d16394
JH
32using std::vector;
33
1198b887
JH
34namespace pv {
35namespace widgets {
36
37SweepTimingWidget::SweepTimingWidget(const char *suffix,
38 QWidget *parent) :
39 QWidget(parent),
8dbbc7f0
JH
40 suffix_(suffix),
41 layout_(this),
42 value_(this),
43 list_(this),
44 value_type_(None)
1198b887
JH
45{
46 setContentsMargins(0, 0, 0, 0);
47
8dbbc7f0
JH
48 value_.setDecimals(0);
49 value_.setSuffix(QString::fromUtf8(suffix));
1198b887 50
8dbbc7f0 51 connect(&list_, SIGNAL(currentIndexChanged(int)),
1198b887 52 this, SIGNAL(value_changed()));
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
64void SweepTimingWidget::show_none()
65{
8dbbc7f0
JH
66 value_type_ = None;
67 value_.hide();
68 list_.hide();
1198b887
JH
69}
70
71void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
72 uint64_t step)
73{
67d16394
JH
74 assert(max > min);
75 assert(step > 0);
76
8dbbc7f0 77 value_type_ = MinMaxStep;
1198b887 78
8dbbc7f0
JH
79 value_.setRange(min, max);
80 value_.setSingleStep(step);
1198b887 81
8dbbc7f0
JH
82 value_.show();
83 list_.hide();
1198b887
JH
84}
85
86void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
87{
8dbbc7f0 88 value_type_ = List;
1198b887 89
8dbbc7f0 90 list_.clear();
2ad82c2e 91 for (size_t i = 0; i < count; i++) {
8dbbc7f0
JH
92 char *const s = sr_si_string_u64(vals[i], suffix_);
93 list_.addItem(QString::fromUtf8(s),
1198b887
JH
94 qVariantFromValue(vals[i]));
95 g_free(s);
96 }
97
8dbbc7f0
JH
98 value_.hide();
99 list_.show();
1198b887
JH
100}
101
67d16394
JH
102void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
103{
104 assert(max > min);
105
106 // Create a 1-2-5-10 list of entries.
107 const unsigned int FineScales[] = {1, 2, 5};
108 uint64_t value, decade;
109 unsigned int fine;
110 vector<uint64_t> values;
111
112 // Compute the starting decade
113 for (decade = 1; decade * 10 <= min; decade *= 10);
114
115 // Compute the first entry
116 for (fine = 0; fine < countof(FineScales); fine++)
117 if (FineScales[fine] * decade >= min)
118 break;
119
120 assert(fine < countof(FineScales));
121
122 // Add the minimum entry if it's not on the 1-2-5 progression
123 if (min != FineScales[fine] * decade)
124 values.push_back(min);
125
126 while ((value = FineScales[fine] * decade) < max) {
127 values.push_back(value);
128 if (++fine >= countof(FineScales))
129 fine = 0, decade *= 10;
130 }
131
132 // Add the max value
133 values.push_back(max);
134
135 // Make a C array, and give it to the sweep timing widget
136 uint64_t *const values_array = new uint64_t[values.size()];
137 copy(values.begin(), values.end(), values_array);
138 show_list(values_array, values.size());
139 delete[] values_array;
140}
141
1198b887
JH
142uint64_t SweepTimingWidget::value() const
143{
2ad82c2e 144 switch(value_type_) {
1198b887 145 case None:
1198b887
JH
146 return 0;
147
148 case MinMaxStep:
8dbbc7f0 149 return (uint64_t)value_.value();
1198b887
JH
150
151 case List:
152 {
8dbbc7f0
JH
153 const int index = list_.currentIndex();
154 return (index >= 0) ? list_.itemData(
1198b887
JH
155 index).value<uint64_t>() : 0;
156 }
157
158 default:
159 // Unexpected value type
160 assert(0);
161 return 0;
162 }
163}
164
165void SweepTimingWidget::set_value(uint64_t value)
166{
8dbbc7f0 167 value_.setValue(value);
1198b887 168
8dbbc7f0 169 int best_match = list_.count() - 1;
d01c9439
JH
170 int64_t best_variance = INT64_MAX;
171
8dbbc7f0 172 for (int i = 0; i < list_.count(); i++) {
d01c9439 173 const int64_t this_variance = abs(
8dbbc7f0 174 (int64_t)value - list_.itemData(i).value<int64_t>());
d01c9439
JH
175 if (this_variance < best_variance) {
176 best_variance = this_variance;
177 best_match = i;
178 }
179 }
180
8dbbc7f0 181 list_.setCurrentIndex(best_match);
1198b887
JH
182}
183
184} // widgets
185} // pv