File: sweeptimingwidget.cpp

package info (click to toggle)
pulseview 0.4.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,092 kB
  • sloc: cpp: 25,958; xml: 215; java: 42; makefile: 2
file content (197 lines) | stat: -rw-r--r-- 4,557 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * This file is part of the PulseView project.
 *
 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "sweeptimingwidget.hpp"

#include <cassert>
#include <cstdlib>
#include <vector>

#include <extdef.h>

using std::abs;
using std::vector;

namespace pv {
namespace widgets {

SweepTimingWidget::SweepTimingWidget(const char *suffix,
	QWidget *parent) :
	QWidget(parent),
	suffix_(suffix),
	layout_(this),
	value_(this),
	list_(this),
	value_type_(None)
{
	setContentsMargins(0, 0, 0, 0);

	value_.setDecimals(0);
	value_.setSuffix(QString::fromUtf8(suffix));

	connect(&list_, SIGNAL(currentIndexChanged(int)),
		this, SIGNAL(value_changed()));
	connect(&list_, SIGNAL(editTextChanged(const QString&)),
		this, SIGNAL(value_changed()));

	connect(&value_, SIGNAL(editingFinished()),
		this, SIGNAL(value_changed()));

	setLayout(&layout_);
	layout_.setMargin(0);
	layout_.addWidget(&list_);
	layout_.addWidget(&value_);

	show_none();
}

void SweepTimingWidget::allow_user_entered_values(bool value)
{
	list_.setEditable(value);
}

void SweepTimingWidget::show_none()
{
	value_type_ = None;
	value_.hide();
	list_.hide();
}

void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
	uint64_t step)
{
	assert(max > min);
	assert(step > 0);

	value_type_ = MinMaxStep;

	value_.setRange(min, max);
	value_.setSingleStep(step);

	value_.show();
	list_.hide();
}

void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
{
	value_type_ = List;

	list_.clear();
	for (size_t i = 0; i < count; i++) {
		char *const s = sr_si_string_u64(vals[i], suffix_);
		list_.addItem(QString::fromUtf8(s), qVariantFromValue(vals[i]));
		g_free(s);
	}

	value_.hide();
	list_.show();
}

void SweepTimingWidget::show_125_list(uint64_t min, uint64_t max)
{
	assert(max > min);

	// Create a 1-2-5-10 list of entries.
	const unsigned int FineScales[] = {1, 2, 5};
	uint64_t value, decade;
	unsigned int fine;
	vector<uint64_t> values;

	// Compute the starting decade
	for (decade = 1; decade * 10 <= min; decade *= 10);

	// Compute the first entry
	for (fine = 0; fine < countof(FineScales); fine++)
		if (FineScales[fine] * decade >= min)
			break;

	assert(fine < countof(FineScales));

	// Add the minimum entry if it's not on the 1-2-5 progression
	if (min != FineScales[fine] * decade)
		values.push_back(min);

	while ((value = FineScales[fine] * decade) < max) {
		values.push_back(value);
		if (++fine >= countof(FineScales))
			fine = 0, decade *= 10;
	}

	// Add the max value
	values.push_back(max);

	// Make a C array, and give it to the sweep timing widget
	uint64_t *const values_array = new uint64_t[values.size()];
	copy(values.begin(), values.end(), values_array);
	show_list(values_array, values.size());
	delete[] values_array;
}

uint64_t SweepTimingWidget::value() const
{
	switch (value_type_) {
	case None:
		return 0;
	case MinMaxStep:
		return (uint64_t)value_.value();
	case List:
	{
		if (list_.isEditable()) {
			uint64_t value;
			sr_parse_sizestring(list_.currentText().toUtf8().data(), &value);
			return value;
		}

		const int index = list_.currentIndex();
		return (index >= 0) ? list_.itemData(index).value<uint64_t>() : 0;
	}
	default:
		// Unexpected value type
		assert(false);
		return 0;
	}
}

void SweepTimingWidget::set_value(uint64_t value)
{
	value_.setValue(value);

	if (list_.isEditable()) {
		char *const s = sr_si_string_u64(value, suffix_);
		list_.lineEdit()->setText(QString::fromUtf8(s));
		g_free(s);
	} else {
		int best_match = list_.count() - 1;
		int64_t best_variance = INT64_MAX;

		for (int i = 0; i < list_.count(); i++) {
			const int64_t this_variance = abs(
				(int64_t)value - list_.itemData(i).value<int64_t>());
			if (this_variance < best_variance) {
				best_variance = this_variance;
				best_match = i;
			}
		}

		list_.setCurrentIndex(best_match);
	}
}

}  // namespace widgets
}  // namespace pv