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());
148 menu_view->addSeparator();
149 menu_view->addAction(main_window.action_view_coloured_bg());
151 QMenu *const menu_help = new QMenu;
152 menu_help->setTitle(tr("&Help"));
153 menu_help->addAction(main_window.action_about());
155 menu->addAction(menu_view->menuAction());
156 menu->addSeparator();
157 menu->addAction(menu_help->menuAction());
158 menu->addSeparator();
159 menu->addAction(main_window.action_quit());
161 menu_button_.setMenu(menu);
162 menu_button_.setPopupMode(QToolButton::InstantPopup);
163 menu_button_.setIcon(QIcon::fromTheme("menu",
164 QIcon(":/icons/menu.svg")));
167 addWidget(open_button);
168 addWidget(save_button);
170 addAction(main_window.action_view_zoom_in());
171 addAction(main_window.action_view_zoom_out());
172 addAction(main_window.action_view_zoom_fit());
173 addAction(main_window.action_view_zoom_one_to_one());
175 addAction(main_window.action_view_show_cursors());
178 connect(&run_stop_button_, SIGNAL(clicked()),
179 this, SLOT(on_run_stop()));
180 connect(&sample_count_, SIGNAL(value_changed()),
181 this, SLOT(on_sample_count_changed()));
182 connect(&sample_rate_, SIGNAL(value_changed()),
183 this, SLOT(on_sample_rate_changed()));
185 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
187 set_capture_state(pv::Session::Stopped);
189 configure_button_.setIcon(QIcon::fromTheme("configure",
190 QIcon(":/icons/configure.png")));
192 channels_button_.setIcon(QIcon::fromTheme("channels",
193 QIcon(":/icons/channels.svg")));
195 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
197 addWidget(&device_selector_);
198 configure_button_action_ = addWidget(&configure_button_);
199 channels_button_action_ = addWidget(&channels_button_);
200 addWidget(&sample_count_);
201 addWidget(&sample_rate_);
202 run_stop_button_action_ = addWidget(&run_stop_button_);
205 addWidget(add_decoder_button);
208 QWidget *const spacer = new QWidget();
209 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
212 addWidget(&menu_button_);
214 sample_count_.installEventFilter(this);
215 sample_rate_.installEventFilter(this);
218 void MainBar::update_device_list()
220 DeviceManager &mgr = session_.device_manager();
221 shared_ptr<devices::Device> selected_device = session_.device();
222 list< shared_ptr<devices::Device> > devs;
224 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
226 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
227 devs.push_back(selected_device);
229 device_selector_.set_device_list(devs, selected_device);
230 update_device_config_widgets();
234 void MainBar::set_capture_state(pv::Session::capture_state state)
236 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
237 run_stop_button_.setIcon(*icons[state]);
238 run_stop_button_.setText((state == pv::Session::Stopped) ?
239 tr("Run") : tr("Stop"));
240 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
242 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
244 device_selector_.setEnabled(ui_enabled);
245 configure_button_.setEnabled(ui_enabled);
246 channels_button_.setEnabled(ui_enabled);
247 sample_count_.setEnabled(ui_enabled);
248 sample_rate_.setEnabled(ui_enabled);
251 void MainBar::reset_device_selector()
253 device_selector_.reset();
256 void MainBar::update_sample_rate_selector()
258 Glib::VariantContainerBase gvar_dict;
260 const uint64_t *elements = nullptr;
262 map< const ConfigKey*, std::set<Capability> > keys;
264 if (updating_sample_rate_) {
265 sample_rate_.show_none();
269 const shared_ptr<devices::Device> device =
270 device_selector_.selected_device();
274 assert(!updating_sample_rate_);
275 updating_sample_rate_ = true;
277 const shared_ptr<sigrok::Device> sr_dev = device->device();
279 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
280 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
282 sample_rate_.show_none();
283 updating_sample_rate_ = false;
287 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
288 "samplerate-steps", G_VARIANT_TYPE("at")))) {
289 elements = (const uint64_t *)g_variant_get_fixed_array(
290 gvar_list, &num_elements, sizeof(uint64_t));
292 const uint64_t min = elements[0];
293 const uint64_t max = elements[1];
294 const uint64_t step = elements[2];
296 g_variant_unref(gvar_list);
304 sample_rate_.show_125_list(min, max);
306 // When the step is not 1, we cam't make a 1-2-5-10
307 // list of sample rates, because we may not be able to
308 // make round numbers. Therefore in this case, show a
310 sample_rate_.show_min_max_step(min, max, step);
312 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
313 "samplerates", G_VARIANT_TYPE("at")))) {
314 elements = (const uint64_t *)g_variant_get_fixed_array(
315 gvar_list, &num_elements, sizeof(uint64_t));
316 sample_rate_.show_list(elements, num_elements);
317 g_variant_unref(gvar_list);
319 updating_sample_rate_ = false;
321 update_sample_rate_selector_value();
324 void MainBar::update_sample_rate_selector_value()
326 if (updating_sample_rate_)
329 const shared_ptr<devices::Device> device =
330 device_selector_.selected_device();
335 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
336 uint64_t samplerate =
337 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
338 assert(!updating_sample_rate_);
339 updating_sample_rate_ = true;
340 sample_rate_.set_value(samplerate);
341 updating_sample_rate_ = false;
342 } catch (Error error) {
343 qDebug() << "WARNING: Failed to get value of sample rate";
348 void MainBar::update_sample_count_selector()
350 if (updating_sample_count_)
353 const shared_ptr<devices::Device> device =
354 device_selector_.selected_device();
358 const shared_ptr<sigrok::Device> sr_dev = device->device();
360 assert(!updating_sample_count_);
361 updating_sample_count_ = true;
363 if (!sample_count_supported_) {
364 sample_count_.show_none();
365 updating_sample_count_ = false;
369 uint64_t sample_count = sample_count_.value();
370 uint64_t min_sample_count = 0;
371 uint64_t max_sample_count = MaxSampleCount;
373 if (sample_count == 0)
374 sample_count = DefaultSampleCount;
376 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
377 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
379 g_variant_get(gvar.gobj(), "(tt)",
380 &min_sample_count, &max_sample_count);
383 min_sample_count = min(max(min_sample_count, MinSampleCount),
386 sample_count_.show_125_list(
387 min_sample_count, max_sample_count);
389 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
390 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
391 sample_count = g_variant_get_uint64(gvar.gobj());
392 if (sample_count == 0)
393 sample_count = DefaultSampleCount;
394 sample_count = min(max(sample_count, MinSampleCount),
398 sample_count_.set_value(sample_count);
400 updating_sample_count_ = false;
403 void MainBar::update_device_config_widgets()
405 using namespace pv::popups;
407 const shared_ptr<devices::Device> device =
408 device_selector_.selected_device();
410 // Hide the widgets if no device is selected
411 channels_button_action_->setVisible(!!device);
412 run_stop_button_action_->setVisible(!!device);
414 configure_button_action_->setVisible(false);
415 sample_count_.show_none();
416 sample_rate_.show_none();
420 const shared_ptr<sigrok::Device> sr_dev = device->device();
424 // Update the configure popup
425 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
426 configure_button_action_->setVisible(
427 !opts->binding().properties().empty());
428 configure_button_.set_popup(opts);
430 // Update the channels popup
431 Channels *const channels = new Channels(session_, this);
432 channels_button_.set_popup(channels);
434 // Update supported options.
435 sample_count_supported_ = false;
437 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
438 sample_count_supported_ = true;
440 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
441 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
442 Glib::Variant<guint64>::create(1));
446 // Add notification of reconfigure events
447 disconnect(this, SLOT(on_config_changed()));
448 connect(&opts->binding(), SIGNAL(config_changed()),
449 this, SLOT(on_config_changed()));
451 // Update sweep timing widgets.
452 update_sample_count_selector();
453 update_sample_rate_selector();
456 void MainBar::commit_sample_count()
458 uint64_t sample_count = 0;
460 const shared_ptr<devices::Device> device =
461 device_selector_.selected_device();
465 const shared_ptr<sigrok::Device> sr_dev = device->device();
467 sample_count = sample_count_.value();
468 if (sample_count_supported_) {
470 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
471 Glib::Variant<guint64>::create(sample_count));
472 update_sample_count_selector();
473 } catch (Error error) {
474 qDebug() << "Failed to configure sample count.";
479 // Devices with built-in memory might impose limits on certain
480 // configurations, so let's check what sample rate the driver
482 update_sample_rate_selector();
485 void MainBar::commit_sample_rate()
487 uint64_t sample_rate = 0;
489 const shared_ptr<devices::Device> device =
490 device_selector_.selected_device();
494 const shared_ptr<sigrok::Device> sr_dev = device->device();
496 sample_rate = sample_rate_.value();
497 if (sample_rate == 0)
501 sr_dev->config_set(ConfigKey::SAMPLERATE,
502 Glib::Variant<guint64>::create(sample_rate));
503 update_sample_rate_selector();
504 } catch (Error error) {
505 qDebug() << "Failed to configure samplerate.";
509 // Devices with built-in memory might impose limits on certain
510 // configurations, so let's check what sample count the driver
512 update_sample_count_selector();
515 void MainBar::on_device_selected()
517 shared_ptr<devices::Device> device = device_selector_.selected_device();
521 main_window_.select_device(device);
523 update_device_config_widgets();
526 void MainBar::on_sample_count_changed()
528 if (!updating_sample_count_)
529 commit_sample_count();
532 void MainBar::on_sample_rate_changed()
534 if (!updating_sample_rate_)
535 commit_sample_rate();
538 void MainBar::on_run_stop()
540 commit_sample_count();
541 commit_sample_rate();
542 main_window_.run_stop();
545 void MainBar::on_config_changed()
547 commit_sample_count();
548 commit_sample_rate();
551 bool MainBar::eventFilter(QObject *watched, QEvent *event)
553 if (sample_count_supported_ && (watched == &sample_count_ ||
554 watched == &sample_rate_) &&
555 (event->type() == QEvent::ToolTip)) {
556 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
557 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
559 QString str = tr("Total sampling time: %1").arg(
560 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
561 QToolTip::showText(help_event->globalPos(), str);
569 } // namespace toolbars