]> sigrok.org Git - pulseview.git/blob - pv/widgets/sweeptimingwidget.cpp
9bb75c869a8f780c52e3e6590e2a54ebad94931e
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "sweeptimingwidget.h"
22
23 #include <vector>
24
25 #include <assert.h>
26
27 #include <extdef.h>
28
29 using std::vector;
30
31 namespace pv {
32 namespace widgets {
33
34 SweepTimingWidget::SweepTimingWidget(const char *suffix,
35         QWidget *parent) :
36         QWidget(parent),
37         _layout(this),
38         _value(this),
39         _list(this),
40         _value_type(None)
41 {
42         setContentsMargins(0, 0, 0, 0);
43
44         _value.setDecimals(0);
45         _value.setSuffix(QString::fromUtf8(suffix));
46
47         connect(&_list, SIGNAL(currentIndexChanged(int)),
48                 this, SIGNAL(value_changed()));
49         connect(&_value, SIGNAL(editingFinished()),
50                 this, SIGNAL(value_changed()));
51
52         setLayout(&_layout);
53         _layout.setMargin(0);
54         _layout.addWidget(&_list);
55         _layout.addWidget(&_value);
56
57         show_none();
58 }
59
60 void SweepTimingWidget::show_none()
61 {
62         _value_type = None;
63         _value.hide();
64         _list.hide();
65 }
66
67 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
68         uint64_t step)
69 {
70         assert(max > min);
71         assert(step > 0);
72
73         _value_type = MinMaxStep;
74
75         _value.setRange(min, max);
76         _value.setSingleStep(step);
77
78         _value.show();
79         _list.hide();
80 }
81
82 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
83 {
84         _value_type = List;
85
86         _list.clear();
87         for (size_t i = 0; i < count; i++)
88         {
89                 char *const s = sr_samplerate_string(vals[i]);
90                 _list.addItem(QString::fromUtf8(s),
91                         qVariantFromValue(vals[i]));
92                 g_free(s);
93         }
94
95         _value.hide();
96         _list.show();
97 }
98
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
139 uint64_t SweepTimingWidget::value() const
140 {
141         switch(_value_type)
142         {
143         case None:
144                 return 0;
145
146         case MinMaxStep:
147                 return (uint64_t)_value.value();
148
149         case List:
150         {
151                 const int index = _list.currentIndex();
152                 return (index >= 0) ? _list.itemData(
153                         index).value<uint64_t>() : 0;
154         }
155
156         default:
157                 // Unexpected value type
158                 assert(0);
159                 return 0;
160         }
161 }
162
163 void SweepTimingWidget::set_value(uint64_t value)
164 {
165         _value.setValue(value);
166
167         for (int i = 0; i < _list.count(); i++)
168                 if (value == _list.itemData(i).value<uint64_t>())
169                         _list.setCurrentIndex(i);
170 }
171
172 } // widgets
173 } // pv