]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
icons: Added menu icon
[pulseview.git] / pv / toolbars / samplingbar.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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 <extdef.h>
22
23#include <assert.h>
24
25#include <QAction>
26#include <QDebug>
27#include <QHelpEvent>
28#include <QToolTip>
29
30#include "samplingbar.hpp"
31
32#include <pv/devicemanager.hpp>
33#include <pv/mainwindow.hpp>
34#include <pv/popups/deviceoptions.hpp>
35#include <pv/popups/channels.hpp>
36#include <pv/util.hpp>
37
38#include <libsigrok/libsigrok.hpp>
39
40using std::map;
41using std::vector;
42using std::max;
43using std::min;
44using std::shared_ptr;
45using std::string;
46
47using sigrok::Capability;
48using sigrok::ConfigKey;
49using sigrok::Device;
50using sigrok::Error;
51
52namespace pv {
53namespace toolbars {
54
55const uint64_t SamplingBar::MinSampleCount = 100ULL;
56const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
57const uint64_t SamplingBar::DefaultSampleCount = 1000000;
58
59SamplingBar::SamplingBar(Session &session, MainWindow &main_window) :
60 QToolBar("Sampling Bar", &main_window),
61 session_(session),
62 main_window_(main_window),
63 device_selector_(this),
64 updating_device_selector_(false),
65 configure_button_(this),
66 configure_button_action_(NULL),
67 channels_button_(this),
68 sample_count_(" samples", this),
69 sample_rate_("Hz", this),
70 updating_sample_rate_(false),
71 updating_sample_count_(false),
72 sample_count_supported_(false),
73 icon_red_(":/icons/status-red.svg"),
74 icon_green_(":/icons/status-green.svg"),
75 icon_grey_(":/icons/status-grey.svg"),
76 run_stop_button_(this)
77{
78 setObjectName(QString::fromUtf8("SamplingBar"));
79
80 connect(&run_stop_button_, SIGNAL(clicked()),
81 this, SLOT(on_run_stop()));
82 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
83 this, SLOT(on_device_selected()));
84 connect(&sample_count_, SIGNAL(value_changed()),
85 this, SLOT(on_sample_count_changed()));
86 connect(&sample_rate_, SIGNAL(value_changed()),
87 this, SLOT(on_sample_rate_changed()));
88
89 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
90
91 set_capture_state(pv::Session::Stopped);
92
93 configure_button_.setIcon(QIcon::fromTheme("configure",
94 QIcon(":/icons/configure.png")));
95
96 channels_button_.setIcon(QIcon::fromTheme("channels",
97 QIcon(":/icons/channels.svg")));
98
99 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
100
101 addWidget(&device_selector_);
102 configure_button_action_ = addWidget(&configure_button_);
103 addWidget(&channels_button_);
104 addWidget(&sample_count_);
105 addWidget(&sample_rate_);
106
107 addWidget(&run_stop_button_);
108
109 sample_count_.installEventFilter(this);
110 sample_rate_.installEventFilter(this);
111}
112
113void SamplingBar::set_device_list(
114 const std::list< std::shared_ptr<sigrok::Device> > &devices,
115 shared_ptr<Device> selected)
116{
117 int selected_index = -1;
118
119 assert(selected);
120
121 updating_device_selector_ = true;
122
123 device_selector_.clear();
124
125 for (auto device : devices) {
126 assert(device);
127
128 string display_name =
129 session_.device_manager().get_display_name(device);
130
131 if (selected == device)
132 selected_index = device_selector_.count();
133
134 device_selector_.addItem(display_name.c_str(),
135 qVariantFromValue(device));
136 }
137
138 // The selected device should have been in the list
139 assert(selected_index != -1);
140 device_selector_.setCurrentIndex(selected_index);
141
142 update_device_config_widgets();
143
144 updating_device_selector_ = false;
145}
146
147shared_ptr<Device> SamplingBar::get_selected_device() const
148{
149 const int index = device_selector_.currentIndex();
150 if (index < 0)
151 return shared_ptr<Device>();
152
153 return device_selector_.itemData(index).value<shared_ptr<Device>>();
154}
155
156void SamplingBar::set_capture_state(pv::Session::capture_state state)
157{
158 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
159 run_stop_button_.setIcon(*icons[state]);
160 run_stop_button_.setText((state == pv::Session::Stopped) ?
161 tr("Run") : tr("Stop"));
162 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
163}
164
165void SamplingBar::update_sample_rate_selector()
166{
167 Glib::VariantContainerBase gvar_dict;
168 GVariant *gvar_list;
169 const uint64_t *elements = NULL;
170 gsize num_elements;
171
172 if (updating_sample_rate_)
173 return;
174
175 const shared_ptr<Device> device = get_selected_device();
176 if (!device)
177 return;
178
179 assert(!updating_sample_rate_);
180 updating_sample_rate_ = true;
181
182 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
183 const auto iter = keys.find(ConfigKey::SAMPLERATE);
184 if (iter != keys.end() &&
185 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
186 const auto keys = device->config_keys(
187 ConfigKey::DEVICE_OPTIONS);
188 try {
189 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
190 } catch(const sigrok::Error &e) {
191 // Failed to enunmerate samplerate
192 (void)e;
193 }
194 }
195
196 if (!gvar_dict) {
197 sample_rate_.show_none();
198 updating_sample_rate_ = false;
199 return;
200 }
201
202 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
203 "samplerate-steps", G_VARIANT_TYPE("at"))))
204 {
205 elements = (const uint64_t *)g_variant_get_fixed_array(
206 gvar_list, &num_elements, sizeof(uint64_t));
207
208 const uint64_t min = elements[0];
209 const uint64_t max = elements[1];
210 const uint64_t step = elements[2];
211
212 g_variant_unref(gvar_list);
213
214 assert(min > 0);
215 assert(max > 0);
216 assert(max > min);
217 assert(step > 0);
218
219 if (step == 1)
220 sample_rate_.show_125_list(min, max);
221 else
222 {
223 // When the step is not 1, we cam't make a 1-2-5-10
224 // list of sample rates, because we may not be able to
225 // make round numbers. Therefore in this case, show a
226 // spin box.
227 sample_rate_.show_min_max_step(min, max, step);
228 }
229 }
230 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
231 "samplerates", G_VARIANT_TYPE("at"))))
232 {
233 elements = (const uint64_t *)g_variant_get_fixed_array(
234 gvar_list, &num_elements, sizeof(uint64_t));
235 sample_rate_.show_list(elements, num_elements);
236 g_variant_unref(gvar_list);
237 }
238 updating_sample_rate_ = false;
239
240 update_sample_rate_selector_value();
241}
242
243void SamplingBar::update_sample_rate_selector_value()
244{
245 if (updating_sample_rate_)
246 return;
247
248 const shared_ptr<Device> device = get_selected_device();
249 if (!device)
250 return;
251
252 try {
253 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
254 uint64_t samplerate =
255 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
256 assert(!updating_sample_rate_);
257 updating_sample_rate_ = true;
258 sample_rate_.set_value(samplerate);
259 updating_sample_rate_ = false;
260 } catch (Error error) {
261 qDebug() << "WARNING: Failed to get value of sample rate";
262 return;
263 }
264}
265
266void SamplingBar::update_sample_count_selector()
267{
268 if (updating_sample_count_)
269 return;
270
271 const shared_ptr<Device> device = get_selected_device();
272 if (!device)
273 return;
274
275 assert(!updating_sample_count_);
276 updating_sample_count_ = true;
277
278 if (!sample_count_supported_)
279 {
280 sample_count_.show_none();
281 updating_sample_count_ = false;
282 return;
283 }
284
285 uint64_t sample_count = sample_count_.value();
286 uint64_t min_sample_count = 0;
287 uint64_t max_sample_count = MaxSampleCount;
288
289 if (sample_count == 0)
290 sample_count = DefaultSampleCount;
291
292 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
293 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
294 if (iter != keys.end() &&
295 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
296 try {
297 auto gvar =
298 device->config_list(ConfigKey::LIMIT_SAMPLES);
299 if (gvar)
300 g_variant_get(gvar.gobj(), "(tt)",
301 &min_sample_count, &max_sample_count);
302 } catch(const sigrok::Error &e) {
303 // Failed to query sample limit
304 (void)e;
305 }
306 }
307
308 min_sample_count = min(max(min_sample_count, MinSampleCount),
309 max_sample_count);
310
311 sample_count_.show_125_list(
312 min_sample_count, max_sample_count);
313
314 try {
315 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
316 sample_count = g_variant_get_uint64(gvar.gobj());
317 if (sample_count == 0)
318 sample_count = DefaultSampleCount;
319 sample_count = min(max(sample_count, MinSampleCount),
320 max_sample_count);
321 } catch (Error error) {}
322
323 sample_count_.set_value(sample_count);
324
325 updating_sample_count_ = false;
326}
327
328void SamplingBar::update_device_config_widgets()
329{
330 using namespace pv::popups;
331
332 const shared_ptr<Device> device = get_selected_device();
333 if (!device)
334 return;
335
336 // Update the configure popup
337 DeviceOptions *const opts = new DeviceOptions(device, this);
338 configure_button_action_->setVisible(
339 !opts->binding().properties().empty());
340 configure_button_.set_popup(opts);
341
342 // Update the channels popup
343 Channels *const channels = new Channels(session_, this);
344 channels_button_.set_popup(channels);
345
346 // Update supported options.
347 sample_count_supported_ = false;
348
349 try {
350 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
351 {
352 auto key = entry.first;
353 auto capabilities = entry.second;
354 switch (key->id()) {
355 case SR_CONF_LIMIT_SAMPLES:
356 if (capabilities.count(Capability::SET))
357 sample_count_supported_ = true;
358 break;
359 case SR_CONF_LIMIT_FRAMES:
360 if (capabilities.count(Capability::SET))
361 {
362 device->config_set(ConfigKey::LIMIT_FRAMES,
363 Glib::Variant<guint64>::create(1));
364 on_config_changed();
365 }
366 break;
367 default:
368 break;
369 }
370 }
371 } catch (Error error) {}
372
373 // Add notification of reconfigure events
374 disconnect(this, SLOT(on_config_changed()));
375 connect(&opts->binding(), SIGNAL(config_changed()),
376 this, SLOT(on_config_changed()));
377
378 // Update sweep timing widgets.
379 update_sample_count_selector();
380 update_sample_rate_selector();
381}
382
383void SamplingBar::commit_sample_count()
384{
385 uint64_t sample_count = 0;
386
387 if (updating_sample_count_)
388 return;
389
390 const shared_ptr<Device> device = get_selected_device();
391 if (!device)
392 return;
393
394 sample_count = sample_count_.value();
395
396 // Set the sample count
397 assert(!updating_sample_count_);
398 updating_sample_count_ = true;
399 if (sample_count_supported_)
400 {
401 try {
402 device->config_set(ConfigKey::LIMIT_SAMPLES,
403 Glib::Variant<guint64>::create(sample_count));
404 on_config_changed();
405 } catch (Error error) {
406 qDebug() << "Failed to configure sample count.";
407 return;
408 }
409 }
410 updating_sample_count_ = false;
411}
412
413void SamplingBar::commit_sample_rate()
414{
415 uint64_t sample_rate = 0;
416
417 if (updating_sample_rate_)
418 return;
419
420 const shared_ptr<Device> device = get_selected_device();
421 if (!device)
422 return;
423
424 sample_rate = sample_rate_.value();
425 if (sample_rate == 0)
426 return;
427
428 // Set the samplerate
429 assert(!updating_sample_rate_);
430 updating_sample_rate_ = true;
431 try {
432 device->config_set(ConfigKey::SAMPLERATE,
433 Glib::Variant<guint64>::create(sample_rate));
434 on_config_changed();
435 } catch (Error error) {
436 qDebug() << "Failed to configure samplerate.";
437 return;
438 }
439 updating_sample_rate_ = false;
440}
441
442void SamplingBar::on_device_selected()
443{
444 if (updating_device_selector_)
445 return;
446
447 shared_ptr<Device> device = get_selected_device();
448 if (!device)
449 return;
450
451 main_window_.select_device(device);
452
453 update_device_config_widgets();
454}
455
456void SamplingBar::on_sample_count_changed()
457{
458 commit_sample_count();
459}
460
461void SamplingBar::on_sample_rate_changed()
462{
463 commit_sample_rate();
464}
465
466void SamplingBar::on_run_stop()
467{
468 commit_sample_count();
469 commit_sample_rate();
470 main_window_.run_stop();
471}
472
473void SamplingBar::on_config_changed()
474{
475 commit_sample_count();
476 update_sample_count_selector();
477 commit_sample_rate();
478 update_sample_rate_selector();
479}
480
481bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
482{
483 if ((watched == &sample_count_ || watched == &sample_rate_) &&
484 (event->type() == QEvent::ToolTip)) {
485 double sec = (double)sample_count_.value() / sample_rate_.value();
486 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
487
488 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
489 QToolTip::showText(help_event->globalPos(), str);
490
491 return true;
492 }
493
494 return false;
495}
496
497} // namespace toolbars
498} // namespace pv