Added devices::File as a common base of file devices
[pulseview.git] / pv / widgets / sweeptimingwidget.cpp
blobb2c0e6370d44587ce88a2063704bc14bb3a7b54d
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include "sweeptimingwidget.hpp"
23 #include <cstdlib>
25 #include <vector>
27 #include <assert.h>
29 #include <extdef.h>
31 using std::abs;
32 using std::vector;
34 namespace pv {
35 namespace widgets {
37 SweepTimingWidget::SweepTimingWidget(const char *suffix,
38 QWidget *parent) :
39 QWidget(parent),
40 suffix_(suffix),
41 layout_(this),
42 value_(this),
43 list_(this),
44 value_type_(None)
46 setContentsMargins(0, 0, 0, 0);
48 value_.setDecimals(0);
49 value_.setSuffix(QString::fromUtf8(suffix));
51 connect(&list_, SIGNAL(currentIndexChanged(int)),
52 this, SIGNAL(value_changed()));
53 connect(&value_, SIGNAL(editingFinished()),
54 this, SIGNAL(value_changed()));
56 setLayout(&layout_);
57 layout_.setMargin(0);
58 layout_.addWidget(&list_);
59 layout_.addWidget(&value_);
61 show_none();
64 void SweepTimingWidget::show_none()
66 value_type_ = None;
67 value_.hide();
68 list_.hide();
71 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
72 uint64_t step)
74 assert(max > min);
75 assert(step > 0);
77 value_type_ = MinMaxStep;
79 value_.setRange(min, max);
80 value_.setSingleStep(step);
82 value_.show();
83 list_.hide();
86 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
88 value_type_ = List;
90 list_.clear();
91 for (size_t i = 0; i < count; i++)
93 char *const s = sr_si_string_u64(vals[i], suffix_);
94 list_.addItem(QString::fromUtf8(s),
95 qVariantFromValue(vals[i]));
96 g_free(s);
99 value_.hide();
100 list_.show();
103 void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
105 assert(max > min);
107 // Create a 1-2-5-10 list of entries.
108 const unsigned int FineScales[] = {1, 2, 5};
109 uint64_t value, decade;
110 unsigned int fine;
111 vector<uint64_t> values;
113 // Compute the starting decade
114 for (decade = 1; decade * 10 <= min; decade *= 10);
116 // Compute the first entry
117 for (fine = 0; fine < countof(FineScales); fine++)
118 if (FineScales[fine] * decade >= min)
119 break;
121 assert(fine < countof(FineScales));
123 // Add the minimum entry if it's not on the 1-2-5 progression
124 if (min != FineScales[fine] * decade)
125 values.push_back(min);
127 while ((value = FineScales[fine] * decade) < max) {
128 values.push_back(value);
129 if (++fine >= countof(FineScales))
130 fine = 0, decade *= 10;
133 // Add the max value
134 values.push_back(max);
136 // Make a C array, and give it to the sweep timing widget
137 uint64_t *const values_array = new uint64_t[values.size()];
138 copy(values.begin(), values.end(), values_array);
139 show_list(values_array, values.size());
140 delete[] values_array;
143 uint64_t SweepTimingWidget::value() const
145 switch(value_type_)
147 case None:
148 return 0;
150 case MinMaxStep:
151 return (uint64_t)value_.value();
153 case List:
155 const int index = list_.currentIndex();
156 return (index >= 0) ? list_.itemData(
157 index).value<uint64_t>() : 0;
160 default:
161 // Unexpected value type
162 assert(0);
163 return 0;
167 void SweepTimingWidget::set_value(uint64_t value)
169 value_.setValue(value);
171 int best_match = list_.count() - 1;
172 int64_t best_variance = INT64_MAX;
174 for (int i = 0; i < list_.count(); i++) {
175 const int64_t this_variance = abs(
176 (int64_t)value - list_.itemData(i).value<int64_t>());
177 if (this_variance < best_variance) {
178 best_variance = this_variance;
179 best_match = i;
183 list_.setCurrentIndex(best_match);
186 } // widgets
187 } // pv