2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 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
30 #include "samplingbar.h"
32 #include <pv/devicemanager.h>
33 #include <pv/popups/deviceoptions.h>
34 #include <pv/popups/channels.h>
37 #include <libsigrok/libsigrok.hpp>
43 using std::shared_ptr;
46 using sigrok::Capability;
47 using sigrok::ConfigKey;
54 const uint64_t SamplingBar::MinSampleCount = 100ULL;
55 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
56 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
58 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
59 QToolBar("Sampling Bar", parent),
61 _device_selector(this),
62 _updating_device_selector(false),
63 _configure_button(this),
64 _configure_button_action(NULL),
65 _channels_button(this),
66 _sample_count(" samples", this),
67 _sample_rate("Hz", this),
68 _updating_sample_rate(false),
69 _updating_sample_count(false),
70 _sample_count_supported(false),
71 _icon_red(":/icons/status-red.svg"),
72 _icon_green(":/icons/status-green.svg"),
73 _icon_grey(":/icons/status-grey.svg"),
74 _run_stop_button(this)
76 setObjectName(QString::fromUtf8("SamplingBar"));
78 connect(&_run_stop_button, SIGNAL(clicked()),
79 this, SLOT(on_run_stop()));
80 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
81 this, SLOT(on_device_selected()));
82 connect(&_sample_count, SIGNAL(value_changed()),
83 this, SLOT(on_sample_count_changed()));
84 connect(&_sample_rate, SIGNAL(value_changed()),
85 this, SLOT(on_sample_rate_changed()));
87 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
89 set_capture_state(pv::SigSession::Stopped);
91 _configure_button.setIcon(QIcon::fromTheme("configure",
92 QIcon(":/icons/configure.png")));
94 _channels_button.setIcon(QIcon::fromTheme("channels",
95 QIcon(":/icons/channels.svg")));
97 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
99 addWidget(&_device_selector);
100 _configure_button_action = addWidget(&_configure_button);
101 addWidget(&_channels_button);
102 addWidget(&_sample_count);
103 addWidget(&_sample_rate);
105 addWidget(&_run_stop_button);
107 _sample_count.installEventFilter(this);
108 _sample_rate.installEventFilter(this);
111 void SamplingBar::set_device_list(
112 const std::map< shared_ptr<Device>, string > &device_names,
113 shared_ptr<Device> selected)
115 int selected_index = -1;
119 _updating_device_selector = true;
121 _device_selector.clear();
123 for (auto entry : device_names) {
124 auto device = entry.first;
125 auto description = entry.second;
129 if (selected == device)
130 selected_index = _device_selector.count();
132 _device_selector.addItem(description.c_str(),
133 qVariantFromValue(device));
136 // The selected device should have been in the list
137 assert(selected_index != -1);
138 _device_selector.setCurrentIndex(selected_index);
140 update_device_config_widgets();
142 _updating_device_selector = false;
145 shared_ptr<Device> SamplingBar::get_selected_device() const
147 const int index = _device_selector.currentIndex();
149 return shared_ptr<Device>();
151 return _device_selector.itemData(index).value<shared_ptr<Device>>();
154 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
156 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
157 _run_stop_button.setIcon(*icons[state]);
158 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
159 tr("Run") : tr("Stop"));
162 void SamplingBar::update_sample_rate_selector()
164 Glib::VariantContainerBase gvar_dict;
166 const uint64_t *elements = NULL;
169 if (_updating_sample_rate)
172 const shared_ptr<Device> device = get_selected_device();
176 assert(!_updating_sample_rate);
177 _updating_sample_rate = true;
180 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
181 } catch (Error error) {
182 _sample_rate.show_none();
183 _updating_sample_rate = false;
187 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
188 "samplerate-steps", G_VARIANT_TYPE("at"))))
190 elements = (const uint64_t *)g_variant_get_fixed_array(
191 gvar_list, &num_elements, sizeof(uint64_t));
193 const uint64_t min = elements[0];
194 const uint64_t max = elements[1];
195 const uint64_t step = elements[2];
197 g_variant_unref(gvar_list);
205 _sample_rate.show_125_list(min, max);
208 // When the step is not 1, we cam't make a 1-2-5-10
209 // list of sample rates, because we may not be able to
210 // make round numbers. Therefore in this case, show a
212 _sample_rate.show_min_max_step(min, max, step);
215 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
216 "samplerates", G_VARIANT_TYPE("at"))))
218 elements = (const uint64_t *)g_variant_get_fixed_array(
219 gvar_list, &num_elements, sizeof(uint64_t));
220 _sample_rate.show_list(elements, num_elements);
221 g_variant_unref(gvar_list);
223 _updating_sample_rate = false;
225 update_sample_rate_selector_value();
228 void SamplingBar::update_sample_rate_selector_value()
230 if (_updating_sample_rate)
233 const shared_ptr<Device> device = get_selected_device();
238 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
239 uint64_t samplerate =
240 Glib::VariantBase::cast_dynamic<Glib::Variant<uint64_t>>(gvar).get();
241 assert(!_updating_sample_rate);
242 _updating_sample_rate = true;
243 _sample_rate.set_value(samplerate);
244 _updating_sample_rate = false;
245 } catch (Error error) {
246 qDebug() << "WARNING: Failed to get value of sample rate";
251 void SamplingBar::update_sample_count_selector()
253 if (_updating_sample_count)
256 const shared_ptr<Device> device = get_selected_device();
260 assert(!_updating_sample_count);
261 _updating_sample_count = true;
263 if (_sample_count_supported)
265 uint64_t sample_count = _sample_count.value();
266 uint64_t min_sample_count = 0;
267 uint64_t max_sample_count = MaxSampleCount;
269 if (sample_count == 0)
270 sample_count = DefaultSampleCount;
273 auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES);
274 g_variant_get(gvar.gobj(), "(tt)",
275 &min_sample_count, &max_sample_count);
276 } catch (Error error) {}
278 min_sample_count = min(max(min_sample_count, MinSampleCount),
281 _sample_count.show_125_list(
282 min_sample_count, max_sample_count);
285 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
286 sample_count = g_variant_get_uint64(gvar.gobj());
287 if (sample_count == 0)
288 sample_count = DefaultSampleCount;
289 sample_count = min(max(sample_count, MinSampleCount),
291 } catch (Error error) {}
293 _sample_count.set_value(sample_count);
296 _sample_count.show_none();
298 _updating_sample_count = false;
301 void SamplingBar::update_device_config_widgets()
303 using namespace pv::popups;
305 const shared_ptr<Device> device = get_selected_device();
309 // Update the configure popup
310 DeviceOptions *const opts = new DeviceOptions(device, this);
311 _configure_button_action->setVisible(
312 !opts->binding().properties().empty());
313 _configure_button.set_popup(opts);
315 // Update the channels popup
316 Channels *const channels = new Channels(_session, this);
317 _channels_button.set_popup(channels);
319 // Update supported options.
320 _sample_count_supported = false;
323 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
325 auto key = entry.first;
326 auto capabilities = entry.second;
328 case SR_CONF_LIMIT_SAMPLES:
329 if (capabilities.count(Capability::SET))
330 _sample_count_supported = true;
332 case SR_CONF_LIMIT_FRAMES:
333 if (capabilities.count(Capability::SET))
335 device->config_set(ConfigKey::LIMIT_FRAMES,
336 Glib::Variant<uint64_t>::create(1));
344 } catch (Error error) {}
346 // Add notification of reconfigure events
347 disconnect(this, SLOT(on_config_changed()));
348 connect(&opts->binding(), SIGNAL(config_changed()),
349 this, SLOT(on_config_changed()));
351 // Update sweep timing widgets.
352 update_sample_count_selector();
353 update_sample_rate_selector();
356 void SamplingBar::commit_sample_count()
358 uint64_t sample_count = 0;
360 if (_updating_sample_count)
363 const shared_ptr<Device> device = get_selected_device();
367 sample_count = _sample_count.value();
369 // Set the sample count
370 assert(!_updating_sample_count);
371 _updating_sample_count = true;
372 if (_sample_count_supported)
375 device->config_set(ConfigKey::LIMIT_SAMPLES,
376 Glib::Variant<uint64_t>::create(sample_count));
378 } catch (Error error) {
379 qDebug() << "Failed to configure sample count.";
383 _updating_sample_count = false;
386 void SamplingBar::commit_sample_rate()
388 uint64_t sample_rate = 0;
390 if (_updating_sample_rate)
393 const shared_ptr<Device> device = get_selected_device();
397 sample_rate = _sample_rate.value();
398 if (sample_rate == 0)
401 // Set the samplerate
402 assert(!_updating_sample_rate);
403 _updating_sample_rate = true;
405 device->config_set(ConfigKey::SAMPLERATE,
406 Glib::Variant<uint64_t>::create(sample_rate));
408 } catch (Error error) {
409 qDebug() << "Failed to configure samplerate.";
412 _updating_sample_rate = false;
415 void SamplingBar::on_device_selected()
417 if (_updating_device_selector)
420 shared_ptr<Device> device = get_selected_device();
424 _session.set_device(device);
426 update_device_config_widgets();
429 void SamplingBar::on_sample_count_changed()
431 commit_sample_count();
434 void SamplingBar::on_sample_rate_changed()
436 commit_sample_rate();
439 void SamplingBar::on_run_stop()
441 commit_sample_count();
442 commit_sample_rate();
446 void SamplingBar::on_config_changed()
448 commit_sample_count();
449 update_sample_count_selector();
450 commit_sample_rate();
451 update_sample_rate_selector();
454 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
456 if ((watched == &_sample_count || watched == &_sample_rate) &&
457 (event->type() == QEvent::ToolTip)) {
458 double sec = (double)_sample_count.value() / _sample_rate.value();
459 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
461 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
462 QToolTip::showText(help_event->globalPos(), str);
470 } // namespace toolbars