2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-2015 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
32 #include "mainbar.hpp"
34 #include <pv/devicemanager.hpp>
35 #include <pv/devices/hardwaredevice.hpp>
36 #include <pv/mainwindow.hpp>
37 #include <pv/popups/deviceoptions.hpp>
38 #include <pv/popups/channels.hpp>
39 #include <pv/util.hpp>
40 #include <pv/widgets/exportmenu.hpp>
41 #include <pv/widgets/importmenu.hpp>
43 #include <libsigrokcxx/libsigrokcxx.hpp>
45 using std::back_inserter;
51 using std::shared_ptr;
55 using sigrok::Capability;
56 using sigrok::ConfigKey;
58 using sigrok::InputFormat;
63 const uint64_t MainBar::MinSampleCount = 100ULL;
64 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
65 const uint64_t MainBar::DefaultSampleCount = 1000000;
67 MainBar::MainBar(Session &session, MainWindow &main_window) :
68 QToolBar("Sampling Bar", &main_window),
70 main_window_(main_window),
71 device_selector_(this, session.device_manager(),
72 main_window.action_connect()),
73 configure_button_(this),
74 configure_button_action_(nullptr),
75 channels_button_(this),
76 channels_button_action_(nullptr),
77 sample_count_(" samples", this),
78 sample_rate_("Hz", this),
79 updating_sample_rate_(false),
80 updating_sample_count_(false),
81 sample_count_supported_(false),
82 icon_red_(":/icons/status-red.svg"),
83 icon_green_(":/icons/status-green.svg"),
84 icon_grey_(":/icons/status-grey.svg"),
85 run_stop_button_(this),
86 run_stop_button_action_(nullptr),
89 setObjectName(QString::fromUtf8("MainBar"));
93 setContextMenuPolicy(Qt::PreventContextMenu);
96 QToolButton *const open_button = new QToolButton(this);
98 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
99 session.device_manager().context(),
100 main_window.action_open());
102 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
104 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
106 open_button->setMenu(import_menu);
107 open_button->setDefaultAction(main_window.action_open());
108 open_button->setPopupMode(QToolButton::MenuButtonPopup);
111 QToolButton *const save_button = new QToolButton(this);
113 vector<QAction *> open_actions;
114 open_actions.push_back(main_window.action_save_as());
115 open_actions.push_back(main_window.action_save_selection_as());
117 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
118 session.device_manager().context(),
121 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
123 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
125 save_button->setMenu(export_menu);
126 save_button->setDefaultAction(main_window.action_save_as());
127 save_button->setPopupMode(QToolButton::MenuButtonPopup);
129 // Device selector menu
130 connect(&device_selector_, SIGNAL(device_selected()),
131 this, SLOT(on_device_selected()));
133 // Setup the decoder button
135 QToolButton *add_decoder_button = new QToolButton(this);
136 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
137 QIcon(":/icons/add-decoder.svg")));
138 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
139 add_decoder_button->setMenu(main_window_.menu_decoder_add());
142 // Setup the burger menu
143 QMenu *const menu = new QMenu(this);
145 QMenu *const menu_view = new QMenu;
146 menu_view->setTitle(tr("&View"));
147 menu_view->addAction(main_window.action_view_sticky_scrolling());
149 QMenu *const menu_help = new QMenu;
150 menu_help->setTitle(tr("&Help"));
151 menu_help->addAction(main_window.action_about());
153 menu->addAction(menu_view->menuAction());
154 menu->addSeparator();
155 menu->addAction(menu_help->menuAction());
156 menu->addSeparator();
157 menu->addAction(main_window.action_quit());
159 menu_button_.setMenu(menu);
160 menu_button_.setPopupMode(QToolButton::InstantPopup);
161 menu_button_.setIcon(QIcon::fromTheme("menu",
162 QIcon(":/icons/menu.svg")));
165 addWidget(open_button);
166 addWidget(save_button);
168 addAction(main_window.action_view_zoom_in());
169 addAction(main_window.action_view_zoom_out());
170 addAction(main_window.action_view_zoom_fit());
171 addAction(main_window.action_view_zoom_one_to_one());
173 addAction(main_window.action_view_show_cursors());
176 connect(&run_stop_button_, SIGNAL(clicked()),
177 this, SLOT(on_run_stop()));
178 connect(&sample_count_, SIGNAL(value_changed()),
179 this, SLOT(on_sample_count_changed()));
180 connect(&sample_rate_, SIGNAL(value_changed()),
181 this, SLOT(on_sample_rate_changed()));
183 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
185 set_capture_state(pv::Session::Stopped);
187 configure_button_.setIcon(QIcon::fromTheme("configure",
188 QIcon(":/icons/configure.png")));
190 channels_button_.setIcon(QIcon::fromTheme("channels",
191 QIcon(":/icons/channels.svg")));
193 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
195 addWidget(&device_selector_);
196 configure_button_action_ = addWidget(&configure_button_);
197 channels_button_action_ = addWidget(&channels_button_);
198 addWidget(&sample_count_);
199 addWidget(&sample_rate_);
200 run_stop_button_action_ = addWidget(&run_stop_button_);
203 addWidget(add_decoder_button);
206 QWidget *const spacer = new QWidget();
207 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
210 addWidget(&menu_button_);
212 sample_count_.installEventFilter(this);
213 sample_rate_.installEventFilter(this);
216 void MainBar::update_device_list()
218 DeviceManager &mgr = session_.device_manager();
219 shared_ptr<devices::Device> selected_device = session_.device();
220 list< shared_ptr<devices::Device> > devs;
222 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
224 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
225 devs.push_back(selected_device);
227 device_selector_.set_device_list(devs, selected_device);
228 update_device_config_widgets();
232 void MainBar::set_capture_state(pv::Session::capture_state state)
234 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
235 run_stop_button_.setIcon(*icons[state]);
236 run_stop_button_.setText((state == pv::Session::Stopped) ?
237 tr("Run") : tr("Stop"));
238 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
240 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
242 device_selector_.setEnabled(ui_enabled);
243 configure_button_.setEnabled(ui_enabled);
244 channels_button_.setEnabled(ui_enabled);
245 sample_count_.setEnabled(ui_enabled);
246 sample_rate_.setEnabled(ui_enabled);
249 void MainBar::update_sample_rate_selector()
251 Glib::VariantContainerBase gvar_dict;
253 const uint64_t *elements = nullptr;
255 map< const ConfigKey*, std::set<Capability> > keys;
257 if (updating_sample_rate_) {
258 sample_rate_.show_none();
262 const shared_ptr<devices::Device> device =
263 device_selector_.selected_device();
267 assert(!updating_sample_rate_);
268 updating_sample_rate_ = true;
270 const shared_ptr<sigrok::Device> sr_dev = device->device();
273 keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
276 const auto iter = keys.find(ConfigKey::SAMPLERATE);
277 if (iter != keys.end() &&
278 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
280 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
281 } catch(const sigrok::Error &e) {
282 // Failed to enunmerate samplerate
287 if (!gvar_dict.gobj()) {
288 sample_rate_.show_none();
289 updating_sample_rate_ = false;
293 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
294 "samplerate-steps", G_VARIANT_TYPE("at"))))
296 elements = (const uint64_t *)g_variant_get_fixed_array(
297 gvar_list, &num_elements, sizeof(uint64_t));
299 const uint64_t min = elements[0];
300 const uint64_t max = elements[1];
301 const uint64_t step = elements[2];
303 g_variant_unref(gvar_list);
311 sample_rate_.show_125_list(min, max);
314 // When the step is not 1, we cam't make a 1-2-5-10
315 // list of sample rates, because we may not be able to
316 // make round numbers. Therefore in this case, show a
318 sample_rate_.show_min_max_step(min, max, step);
321 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
322 "samplerates", G_VARIANT_TYPE("at"))))
324 elements = (const uint64_t *)g_variant_get_fixed_array(
325 gvar_list, &num_elements, sizeof(uint64_t));
326 sample_rate_.show_list(elements, num_elements);
327 g_variant_unref(gvar_list);
329 updating_sample_rate_ = false;
331 update_sample_rate_selector_value();
334 void MainBar::update_sample_rate_selector_value()
336 if (updating_sample_rate_)
339 const shared_ptr<devices::Device> device =
340 device_selector_.selected_device();
345 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
346 uint64_t samplerate =
347 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
348 assert(!updating_sample_rate_);
349 updating_sample_rate_ = true;
350 sample_rate_.set_value(samplerate);
351 updating_sample_rate_ = false;
352 } catch (Error error) {
353 qDebug() << "WARNING: Failed to get value of sample rate";
358 void MainBar::update_sample_count_selector()
360 if (updating_sample_count_)
363 const shared_ptr<devices::Device> device =
364 device_selector_.selected_device();
368 const shared_ptr<sigrok::Device> sr_dev = device->device();
370 assert(!updating_sample_count_);
371 updating_sample_count_ = true;
373 if (!sample_count_supported_)
375 sample_count_.show_none();
376 updating_sample_count_ = false;
380 uint64_t sample_count = sample_count_.value();
381 uint64_t min_sample_count = 0;
382 uint64_t max_sample_count = MaxSampleCount;
384 if (sample_count == 0)
385 sample_count = DefaultSampleCount;
387 const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
388 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
389 if (iter != keys.end() &&
390 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
393 sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
395 g_variant_get(gvar.gobj(), "(tt)",
396 &min_sample_count, &max_sample_count);
397 } catch(const sigrok::Error &e) {
398 // Failed to query sample limit
403 min_sample_count = min(max(min_sample_count, MinSampleCount),
406 sample_count_.show_125_list(
407 min_sample_count, max_sample_count);
410 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
411 sample_count = g_variant_get_uint64(gvar.gobj());
412 if (sample_count == 0)
413 sample_count = DefaultSampleCount;
414 sample_count = min(max(sample_count, MinSampleCount),
416 } catch (Error error) {}
418 sample_count_.set_value(sample_count);
420 updating_sample_count_ = false;
423 void MainBar::update_device_config_widgets()
425 using namespace pv::popups;
427 const shared_ptr<devices::Device> device =
428 device_selector_.selected_device();
430 // Hide the widgets if no device is selected
431 channels_button_action_->setVisible(!!device);
432 run_stop_button_action_->setVisible(!!device);
434 configure_button_action_->setVisible(false);
435 sample_count_.show_none();
436 sample_rate_.show_none();
440 const shared_ptr<sigrok::Device> sr_dev = device->device();
444 // Update the configure popup
445 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
446 configure_button_action_->setVisible(
447 !opts->binding().properties().empty());
448 configure_button_.set_popup(opts);
450 // Update the channels popup
451 Channels *const channels = new Channels(session_, this);
452 channels_button_.set_popup(channels);
454 // Update supported options.
455 sample_count_supported_ = false;
458 for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS))
460 auto key = entry.first;
461 auto capabilities = entry.second;
463 case SR_CONF_LIMIT_SAMPLES:
464 if (capabilities.count(Capability::SET))
465 sample_count_supported_ = true;
467 case SR_CONF_LIMIT_FRAMES:
468 if (capabilities.count(Capability::SET))
470 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
471 Glib::Variant<guint64>::create(1));
479 } catch (Error error) {}
481 // Add notification of reconfigure events
482 disconnect(this, SLOT(on_config_changed()));
483 connect(&opts->binding(), SIGNAL(config_changed()),
484 this, SLOT(on_config_changed()));
486 // Update sweep timing widgets.
487 update_sample_count_selector();
488 update_sample_rate_selector();
491 void MainBar::commit_sample_count()
493 uint64_t sample_count = 0;
495 const shared_ptr<devices::Device> device =
496 device_selector_.selected_device();
500 const shared_ptr<sigrok::Device> sr_dev = device->device();
502 sample_count = sample_count_.value();
503 if (sample_count_supported_)
506 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
507 Glib::Variant<guint64>::create(sample_count));
508 update_sample_count_selector();
509 } catch (Error error) {
510 qDebug() << "Failed to configure sample count.";
515 // Devices with built-in memory might impose limits on certain
516 // configurations, so let's check what sample rate the driver
518 update_sample_rate_selector();
521 void MainBar::commit_sample_rate()
523 uint64_t sample_rate = 0;
525 const shared_ptr<devices::Device> device =
526 device_selector_.selected_device();
530 const shared_ptr<sigrok::Device> sr_dev = device->device();
532 sample_rate = sample_rate_.value();
533 if (sample_rate == 0)
537 sr_dev->config_set(ConfigKey::SAMPLERATE,
538 Glib::Variant<guint64>::create(sample_rate));
539 update_sample_rate_selector();
540 } catch (Error error) {
541 qDebug() << "Failed to configure samplerate.";
545 // Devices with built-in memory might impose limits on certain
546 // configurations, so let's check what sample count the driver
548 update_sample_count_selector();
551 void MainBar::on_device_selected()
553 shared_ptr<devices::Device> device = device_selector_.selected_device();
557 main_window_.select_device(device);
559 update_device_config_widgets();
562 void MainBar::on_sample_count_changed()
564 if (!updating_sample_count_)
565 commit_sample_count();
568 void MainBar::on_sample_rate_changed()
570 if (!updating_sample_rate_)
571 commit_sample_rate();
574 void MainBar::on_run_stop()
576 commit_sample_count();
577 commit_sample_rate();
578 main_window_.run_stop();
581 void MainBar::on_config_changed()
583 commit_sample_count();
584 commit_sample_rate();
587 bool MainBar::eventFilter(QObject *watched, QEvent *event)
589 if ((watched == &sample_count_ || watched == &sample_rate_) &&
590 (event->type() == QEvent::ToolTip)) {
591 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
592 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
594 QString str = tr("Total sampling time: %1").arg(
595 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
596 QToolTip::showText(help_event->globalPos(), str);
604 } // namespace toolbars