File: enum.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 (294 lines) | stat: -rw-r--r-- 7,410 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * This file is part of the PulseView project.
 *
 * Copyright (C) 2012 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 <cassert>
#include <cfloat>
#include <cmath>
#include <limits>
#include <vector>

#include <QComboBox>
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>

#include <libsigrokcxx/libsigrokcxx.hpp>

#include "enum.hpp"

using std::abs;
// Note that "using std::isnan;" is _not_ put here since that would break
// compilation on some platforms. Use "std::isnan()" instead in checks below.
using std::numeric_limits;
using std::pair;
using std::vector;

namespace pv {
namespace prop {

Enum::Enum(QString name, QString desc,
	vector<pair<Glib::VariantBase, QString> > values,
	Getter getter, Setter setter) :
	Property(name, desc, getter, setter),
	values_(values),
	is_range_(false),
	selector_(nullptr),
	slider_layout_widget_(nullptr),
	slider_(nullptr),
	slider_label_(nullptr)
{
	// Try to determine whether the values make up a range, created by e.g.
	// std_gvar_min_max_step_thresholds()

	vector<double> deltas;
	double prev_value = 0;

	for (const pair<Glib::VariantBase, QString> &v : values_) {
		gdouble value;
		if (v.first.is_of_type(Glib::VariantType("d"))) {
			g_variant_get((GVariant*)(v.first.gobj()), "d", &value);
		} else if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
			gdouble dummy;
			g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &value, &dummy);
		} else
			break; // Type not d or (dd), so not a range that we can handle
		deltas.push_back(value - prev_value);
		prev_value = value;
	}

	if (deltas.size() > 0) {
		bool constant_delta = true;
		double prev_delta = numeric_limits<double>::quiet_NaN();

		bool skip_first = true;
		for (double delta : deltas) {
			// First value is incorrect, it's the delta to 0 since no
			// previous value existed yet
			if (skip_first) {
				skip_first = false;
				continue;
			}
			if (std::isnan(prev_delta))
				prev_delta = delta;

			// 2*DBL_EPSILON doesn't work here, so use a workaround
			if (abs(delta - prev_delta) > (delta/10))
				constant_delta = false;

			prev_delta = delta;
		}

		if (constant_delta)
			is_range_ = true;
	}
}

QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
{
	if (!getter_)
		return nullptr;

	Glib::VariantBase variant;

	try {
		 variant = getter_();
	} catch (const sigrok::Error &e) {
		qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what());
		return nullptr;
	}

	if (!variant.gobj())
		return nullptr;

	if (is_range_) {
		// Use slider
		if (slider_layout_widget_)
			return slider_layout_widget_;

		slider_ = new QSlider();
		// Sliders can't handle float values, so we just use it to specify
		// the number of steps that we're away from the range's beginning
		slider_->setOrientation(Qt::Horizontal);
		slider_->setMinimum(0);
		slider_->setMaximum(values_.size() - 1);
		slider_->setSingleStep(1);

		slider_label_ = new QLabel();

		slider_layout_widget_ = new QWidget(parent);
		QHBoxLayout *layout = new QHBoxLayout(slider_layout_widget_);
		layout->addWidget(slider_);
		layout->addWidget(slider_label_);

		update_widget();

		if (auto_commit)
			connect(slider_, SIGNAL(valueChanged(int)),
				this, SLOT(on_value_changed(int)));

		return slider_layout_widget_;

	} else {
		// Use combo box
		if (selector_)
			return selector_;

		selector_ = new QComboBox(parent);
		for (unsigned int i = 0; i < values_.size(); i++) {
			const pair<Glib::VariantBase, QString> &v = values_[i];
			selector_->addItem(v.second, qVariantFromValue(v.first));
		}

		update_widget();

		if (auto_commit)
			connect(selector_, SIGNAL(currentIndexChanged(int)),
				this, SLOT(on_current_index_changed(int)));

		return selector_;
	}
}

void Enum::update_widget()
{
	Glib::VariantBase variant;

	try {
		variant = getter_();
	} catch (const sigrok::Error &e) {
		qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what());
		return;
	}

	assert(variant.gobj());

	if (is_range_) {

		// Use slider
		if (!slider_layout_widget_)
			return;

		for (unsigned int i = 0; i < values_.size(); i++) {
			const pair<Glib::VariantBase, QString> &v = values_[i];

			// g_variant_equal() doesn't handle floating point properly
			if (v.first.is_of_type(Glib::VariantType("d"))) {
				gdouble a, b;
				g_variant_get(variant.gobj(), "d", &a);
				g_variant_get((GVariant*)(v.first.gobj()), "d", &b);

				if (abs(a - b) <= 2 * DBL_EPSILON) {
					slider_->setValue(i);
					slider_label_->setText(v.second);
				}
			} else {
				// Check for "(dd)" type and handle it if it's found
				if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
					gdouble a1, a2, b1, b2;
					g_variant_get(variant.gobj(), "(dd)", &a1, &a2);
					g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &b1, &b2);

					if ((abs(a1 - b1) <= 2 * DBL_EPSILON) && \
						(abs(a2 - b2) <= 2 * DBL_EPSILON)) {
						slider_->setValue(i);
						slider_label_->setText(v.second);
					}

				} else {
					qWarning() << "Enum property" << name() << "encountered unsupported type";
					return;
				}
			}
		}

	} else {
		// Use combo box
		if (!selector_)
			return;

		for (unsigned int i = 0; i < values_.size(); i++) {
			const pair<Glib::VariantBase, QString> &v = values_[i];

			// g_variant_equal() doesn't handle floating point properly
			if (v.first.is_of_type(Glib::VariantType("d"))) {
				gdouble a, b;
				g_variant_get(variant.gobj(), "d", &a);
				g_variant_get((GVariant*)(v.first.gobj()), "d", &b);
				if (abs(a - b) <= 2 * DBL_EPSILON)
					selector_->setCurrentIndex(i);
			} else {
				// Check for "(dd)" type and handle it if it's found
				if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
					gdouble a1, a2, b1, b2;
					g_variant_get(variant.gobj(), "(dd)", &a1, &a2);
					g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &b1, &b2);
					if ((abs(a1 - b1) <= 2 * DBL_EPSILON) && \
						(abs(a2 - b2) <= 2 * DBL_EPSILON))
						selector_->setCurrentIndex(i);

				} else
					// Handle all other types
					if (v.first.equal(variant))
						selector_->setCurrentIndex(i);
			}
		}
	}
}

void Enum::commit()
{
	assert(setter_);

	if (is_range_) {
		// Use slider
		if (!slider_layout_widget_)
			return;

		setter_(values_.at(slider_->value()).first);

		update_widget();
	} else {
		// Use combo box
		if (!selector_)
			return;

		const int index = selector_->currentIndex();
		if (index < 0)
			return;

		setter_(selector_->itemData(index).value<Glib::VariantBase>());

		// The combo box needs no update, it already shows the current value
		// by definition: the user picked it
	}
}

void Enum::on_current_index_changed(int)
{
	commit();
}

void Enum::on_value_changed(int)
{
	commit();
}

}  // namespace prop
}  // namespace pv