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