]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
MainBar: Added add-decoder button
[pulseview.git] / pv / toolbars / mainbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
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
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
40065ab6 27#include <QHelpEvent>
87f0df9b 28#include <QMenu>
40065ab6 29#include <QToolTip>
dde1a563 30
7c657094 31#include "mainbar.hpp"
d4984fe7 32
2acdb232 33#include <pv/devicemanager.hpp>
4e7f5ba8 34#include <pv/mainwindow.hpp>
2acdb232
JH
35#include <pv/popups/deviceoptions.hpp>
36#include <pv/popups/channels.hpp>
37#include <pv/util.hpp>
cdb50f67 38
e8d00928
ML
39#include <libsigrok/libsigrok.hpp>
40
19adbc2c 41using std::map;
e8d00928 42using std::vector;
b50ef520
JH
43using std::max;
44using std::min;
f9abf97e 45using std::shared_ptr;
819f4c25 46using std::string;
dd63af74 47
e8d00928
ML
48using sigrok::Capability;
49using sigrok::ConfigKey;
50using sigrok::Device;
51using sigrok::Error;
52
51e77110 53namespace pv {
f4c92e1c 54namespace toolbars {
51e77110 55
7c657094
JH
56const uint64_t MainBar::MinSampleCount = 100ULL;
57const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
58const uint64_t MainBar::DefaultSampleCount = 1000000;
09f5d123 59
7c657094 60MainBar::MainBar(Session &session, MainWindow &main_window) :
4e7f5ba8 61 QToolBar("Sampling Bar", &main_window),
8dbbc7f0 62 session_(session),
4e7f5ba8 63 main_window_(main_window),
8dbbc7f0
JH
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"),
87f0df9b
JH
77 run_stop_button_(this),
78 menu_button_(this)
6fb67b27 79{
7c657094 80 setObjectName(QString::fromUtf8("MainBar"));
0c7cdea2 81
f2dbf150
JH
82 setMovable(false);
83 setFloatable(false);
84
168888e2
JH
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
87f0df9b
JH
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();
bdf57963
JH
119 addAction(main_window.action_view_show_cursors());
120 addSeparator();
87f0df9b 121
8dbbc7f0 122 connect(&run_stop_button_, SIGNAL(clicked()),
9f3d12f3 123 this, SLOT(on_run_stop()));
8dbbc7f0 124 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
dde1a563 125 this, SLOT(on_device_selected()));
8dbbc7f0 126 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 127 this, SLOT(on_sample_count_changed()));
8dbbc7f0 128 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 129 this, SLOT(on_sample_rate_changed()));
dde1a563 130
8dbbc7f0 131 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 132
2b81ae46 133 set_capture_state(pv::Session::Stopped);
274d4f13 134
8dbbc7f0 135 configure_button_.setIcon(QIcon::fromTheme("configure",
688ef645 136 QIcon(":/icons/configure.png")));
b7b659aa 137
8dbbc7f0 138 channels_button_.setIcon(QIcon::fromTheme("channels",
6ac6242b 139 QIcon(":/icons/channels.svg")));
cdb50f67 140
8dbbc7f0 141 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
f5798068 142
8dbbc7f0
JH
143 addWidget(&device_selector_);
144 configure_button_action_ = addWidget(&configure_button_);
145 addWidget(&channels_button_);
146 addWidget(&sample_count_);
147 addWidget(&sample_rate_);
8dbbc7f0 148 addWidget(&run_stop_button_);
168888e2
JH
149#ifdef ENABLE_DECODE
150 addSeparator();
151 addWidget(add_decoder_button);
152#endif
40065ab6 153
87f0df9b
JH
154 QWidget *const spacer = new QWidget();
155 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
156 addWidget(spacer);
157
158 addWidget(&menu_button_);
159
8dbbc7f0
JH
160 sample_count_.installEventFilter(this);
161 sample_rate_.installEventFilter(this);
6fb67b27
JH
162}
163
7c657094 164void MainBar::set_device_list(
53a7cce4 165 const std::list< std::shared_ptr<sigrok::Device> > &devices,
e8d00928 166 shared_ptr<Device> selected)
18203d86 167{
e95e8563
JH
168 int selected_index = -1;
169
170 assert(selected);
171
8dbbc7f0 172 updating_device_selector_ = true;
95237c18 173
8dbbc7f0 174 device_selector_.clear();
18203d86 175
53a7cce4 176 for (auto device : devices) {
e8d00928
ML
177 assert(device);
178
53a7cce4
SA
179 string display_name =
180 session_.device_manager().get_display_name(device);
181
e8d00928 182 if (selected == device)
8dbbc7f0 183 selected_index = device_selector_.count();
e95e8563 184
8dbbc7f0 185 device_selector_.addItem(display_name.c_str(),
e8d00928 186 qVariantFromValue(device));
18203d86
JH
187 }
188
e95e8563
JH
189 // The selected device should have been in the list
190 assert(selected_index != -1);
8dbbc7f0 191 device_selector_.setCurrentIndex(selected_index);
e95e8563
JH
192
193 update_device_config_widgets();
194
8dbbc7f0 195 updating_device_selector_ = false;
18203d86
JH
196}
197
7c657094 198shared_ptr<Device> MainBar::get_selected_device() const
d4984fe7 199{
8dbbc7f0 200 const int index = device_selector_.currentIndex();
333d5bbc 201 if (index < 0)
e8d00928 202 return shared_ptr<Device>();
6fb67b27 203
8dbbc7f0 204 return device_selector_.itemData(index).value<shared_ptr<Device>>();
6fb67b27
JH
205}
206
7c657094 207void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 208{
8dbbc7f0
JH
209 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
210 run_stop_button_.setIcon(*icons[state]);
2b81ae46 211 run_stop_button_.setText((state == pv::Session::Stopped) ?
2b49eeb0 212 tr("Run") : tr("Stop"));
8dbbc7f0 213 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
6ac96c2e
JH
214}
215
7c657094 216void MainBar::update_sample_rate_selector()
dde1a563 217{
e8d00928
ML
218 Glib::VariantContainerBase gvar_dict;
219 GVariant *gvar_list;
488f5d3f
BV
220 const uint64_t *elements = NULL;
221 gsize num_elements;
dde1a563 222
8dbbc7f0 223 if (updating_sample_rate_)
3d4f16af
JH
224 return;
225
e8d00928
ML
226 const shared_ptr<Device> device = get_selected_device();
227 if (!device)
ef4d0201
JH
228 return;
229
8dbbc7f0
JH
230 assert(!updating_sample_rate_);
231 updating_sample_rate_ = true;
1198b887 232
ed1d9d81
JH
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()) {
c2db8ec3
JH
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) {
8dbbc7f0
JH
248 sample_rate_.show_none();
249 updating_sample_rate_ = false;
dde1a563 250 return;
1198b887 251 }
dde1a563 252
e8d00928 253 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
1198b887
JH
254 "samplerate-steps", G_VARIANT_TYPE("at"))))
255 {
488f5d3f
BV
256 elements = (const uint64_t *)g_variant_get_fixed_array(
257 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
258
259 const uint64_t min = elements[0];
260 const uint64_t max = elements[1];
261 const uint64_t step = elements[2];
262
488f5d3f 263 g_variant_unref(gvar_list);
aafe53af
JH
264
265 assert(min > 0);
266 assert(max > 0);
267 assert(max > min);
268 assert(step > 0);
269
270 if (step == 1)
8dbbc7f0 271 sample_rate_.show_125_list(min, max);
aafe53af
JH
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.
8dbbc7f0 278 sample_rate_.show_min_max_step(min, max, step);
aafe53af 279 }
dde1a563 280 }
e8d00928 281 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
488f5d3f 282 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 283 {
488f5d3f
BV
284 elements = (const uint64_t *)g_variant_get_fixed_array(
285 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 286 sample_rate_.show_list(elements, num_elements);
488f5d3f 287 g_variant_unref(gvar_list);
dde1a563 288 }
8dbbc7f0 289 updating_sample_rate_ = false;
48888313
JH
290
291 update_sample_rate_selector_value();
292}
293
7c657094 294void MainBar::update_sample_rate_selector_value()
48888313 295{
8dbbc7f0 296 if (updating_sample_rate_)
3d4f16af
JH
297 return;
298
e8d00928
ML
299 const shared_ptr<Device> device = get_selected_device();
300 if (!device)
19adbc2c
JH
301 return;
302
e8d00928
ML
303 try {
304 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
305 uint64_t samplerate =
15289d5c 306 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
307 assert(!updating_sample_rate_);
308 updating_sample_rate_ = true;
309 sample_rate_.set_value(samplerate);
310 updating_sample_rate_ = false;
e8d00928 311 } catch (Error error) {
8dd44190 312 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
313 return;
314 }
48888313
JH
315}
316
7c657094 317void MainBar::update_sample_count_selector()
85756012 318{
8dbbc7f0 319 if (updating_sample_count_)
3d4f16af
JH
320 return;
321
e8d00928
ML
322 const shared_ptr<Device> device = get_selected_device();
323 if (!device)
19adbc2c
JH
324 return;
325
8dbbc7f0
JH
326 assert(!updating_sample_count_);
327 updating_sample_count_ = true;
85756012 328
3e7636f9 329 if (!sample_count_supported_)
df3c1aa3 330 {
3e7636f9
JH
331 sample_count_.show_none();
332 updating_sample_count_ = false;
333 return;
334 }
b50ef520 335
3e7636f9
JH
336 uint64_t sample_count = sample_count_.value();
337 uint64_t min_sample_count = 0;
338 uint64_t max_sample_count = MaxSampleCount;
3cbffdcf 339
3e7636f9
JH
340 if (sample_count == 0)
341 sample_count = DefaultSampleCount;
b50ef520 342
ed1d9d81
JH
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()) {
6454b1e9
JH
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 }
ed1d9d81 357 }
1d04852f 358
3e7636f9
JH
359 min_sample_count = min(max(min_sample_count, MinSampleCount),
360 max_sample_count);
b50ef520 361
3e7636f9
JH
362 sample_count_.show_125_list(
363 min_sample_count, max_sample_count);
b50ef520 364
3e7636f9
JH
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);
df3c1aa3 375
8dbbc7f0 376 updating_sample_count_ = false;
85756012
ML
377}
378
7c657094 379void MainBar::update_device_config_widgets()
e95e8563 380{
e95e8563
JH
381 using namespace pv::popups;
382
e8d00928
ML
383 const shared_ptr<Device> device = get_selected_device();
384 if (!device)
e95e8563
JH
385 return;
386
387 // Update the configure popup
e8d00928 388 DeviceOptions *const opts = new DeviceOptions(device, this);
8dbbc7f0 389 configure_button_action_->setVisible(
e95e8563 390 !opts->binding().properties().empty());
8dbbc7f0 391 configure_button_.set_popup(opts);
e95e8563 392
6ac6242b 393 // Update the channels popup
8dbbc7f0
JH
394 Channels *const channels = new Channels(session_, this);
395 channels_button_.set_popup(channels);
e95e8563
JH
396
397 // Update supported options.
8dbbc7f0 398 sample_count_supported_ = false;
e95e8563 399
e8d00928
ML
400 try {
401 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
e95e8563 402 {
e8d00928
ML
403 auto key = entry.first;
404 auto capabilities = entry.second;
405 switch (key->id()) {
e95e8563 406 case SR_CONF_LIMIT_SAMPLES:
e8d00928 407 if (capabilities.count(Capability::SET))
8dbbc7f0 408 sample_count_supported_ = true;
e95e8563
JH
409 break;
410 case SR_CONF_LIMIT_FRAMES:
e8d00928
ML
411 if (capabilities.count(Capability::SET))
412 {
413 device->config_set(ConfigKey::LIMIT_FRAMES,
15289d5c 414 Glib::Variant<guint64>::create(1));
e8d00928
ML
415 on_config_changed();
416 }
417 break;
418 default:
e95e8563
JH
419 break;
420 }
421 }
e8d00928 422 } catch (Error error) {}
e95e8563
JH
423
424 // Add notification of reconfigure events
425 disconnect(this, SLOT(on_config_changed()));
e8d00928 426 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
427 this, SLOT(on_config_changed()));
428
429 // Update sweep timing widgets.
430 update_sample_count_selector();
431 update_sample_rate_selector();
432}
433
7c657094 434void MainBar::commit_sample_count()
124d97de
ML
435{
436 uint64_t sample_count = 0;
437
8dbbc7f0 438 if (updating_sample_count_)
3d4f16af
JH
439 return;
440
e8d00928
ML
441 const shared_ptr<Device> device = get_selected_device();
442 if (!device)
19adbc2c
JH
443 return;
444
8dbbc7f0 445 sample_count = sample_count_.value();
124d97de
ML
446
447 // Set the sample count
8dbbc7f0
JH
448 assert(!updating_sample_count_);
449 updating_sample_count_ = true;
450 if (sample_count_supported_)
e8d00928
ML
451 {
452 try {
453 device->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 454 Glib::Variant<guint64>::create(sample_count));
e8d00928
ML
455 on_config_changed();
456 } catch (Error error) {
457 qDebug() << "Failed to configure sample count.";
458 return;
459 }
124d97de 460 }
8dbbc7f0 461 updating_sample_count_ = false;
124d97de
ML
462}
463
7c657094 464void MainBar::commit_sample_rate()
48888313
JH
465{
466 uint64_t sample_rate = 0;
467
8dbbc7f0 468 if (updating_sample_rate_)
3d4f16af
JH
469 return;
470
e8d00928
ML
471 const shared_ptr<Device> device = get_selected_device();
472 if (!device)
19adbc2c
JH
473 return;
474
8dbbc7f0 475 sample_rate = sample_rate_.value();
f9541bde
JH
476 if (sample_rate == 0)
477 return;
478
48888313 479 // Set the samplerate
8dbbc7f0
JH
480 assert(!updating_sample_rate_);
481 updating_sample_rate_ = true;
e8d00928
ML
482 try {
483 device->config_set(ConfigKey::SAMPLERATE,
15289d5c 484 Glib::Variant<guint64>::create(sample_rate));
e8d00928
ML
485 on_config_changed();
486 } catch (Error error) {
48888313
JH
487 qDebug() << "Failed to configure samplerate.";
488 return;
489 }
8dbbc7f0 490 updating_sample_rate_ = false;
dde1a563
JH
491}
492
7c657094 493void MainBar::on_device_selected()
dde1a563 494{
8dbbc7f0 495 if (updating_device_selector_)
95237c18
JH
496 return;
497
e8d00928
ML
498 shared_ptr<Device> device = get_selected_device();
499 if (!device)
19adbc2c
JH
500 return;
501
51cf49fe 502 main_window_.select_device(device);
51d4a9ab 503
e95e8563 504 update_device_config_widgets();
dde1a563 505}
51e77110 506
7c657094 507void MainBar::on_sample_count_changed()
124d97de 508{
3d4f16af 509 commit_sample_count();
124d97de
ML
510}
511
7c657094 512void MainBar::on_sample_rate_changed()
48888313 513{
3d4f16af 514 commit_sample_rate();
48888313
JH
515}
516
7c657094 517void MainBar::on_run_stop()
9f3d12f3 518{
b50ef520 519 commit_sample_count();
9f3d12f3 520 commit_sample_rate();
4e7f5ba8 521 main_window_.run_stop();
9f3d12f3
JH
522}
523
7c657094 524void MainBar::on_config_changed()
82afd5e3
JH
525{
526 commit_sample_count();
527 update_sample_count_selector();
528 commit_sample_rate();
529 update_sample_rate_selector();
530}
531
7c657094 532bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 533{
8dbbc7f0 534 if ((watched == &sample_count_ || watched == &sample_rate_) &&
40065ab6 535 (event->type() == QEvent::ToolTip)) {
8dbbc7f0 536 double sec = (double)sample_count_.value() / sample_rate_.value();
40065ab6 537 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45
JS
538
539 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
40065ab6
JS
540 QToolTip::showText(help_event->globalPos(), str);
541
542 return true;
543 }
544
545 return false;
546}
547
f4c92e1c 548} // namespace toolbars
51e77110 549} // namespace pv