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