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