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 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
114 session.device_manager().context(),
115 main_window.action_save_as());
117 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
119 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
121 save_button->setMenu(export_menu);
122 save_button->setDefaultAction(main_window.action_save_as());
123 save_button->setPopupMode(QToolButton::MenuButtonPopup);
125 // Device selector menu
126 connect(&device_selector_, SIGNAL(device_selected()),
127 this, SLOT(on_device_selected()));
129 // Setup the decoder button
131 QToolButton *add_decoder_button = new QToolButton(this);
132 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
133 QIcon(":/icons/add-decoder.svg")));
134 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
135 add_decoder_button->setMenu(main_window_.menu_decoder_add());
139 QMenu *const menu = new QMenu(this);
141 QMenu *const menu_help = new QMenu;
142 menu_help->setTitle(tr("&Help"));
143 menu_help->addAction(main_window.action_about());
145 menu->addAction(menu_help->menuAction());
146 menu->addSeparator();
147 menu->addAction(main_window.action_quit());
149 menu_button_.setMenu(menu);
150 menu_button_.setPopupMode(QToolButton::InstantPopup);
151 menu_button_.setIcon(QIcon::fromTheme("menu",
152 QIcon(":/icons/menu.svg")));
155 addWidget(open_button);
156 addWidget(save_button);
158 addAction(main_window.action_view_zoom_in());
159 addAction(main_window.action_view_zoom_out());
160 addAction(main_window.action_view_zoom_fit());
161 addAction(main_window.action_view_zoom_one_to_one());
163 addAction(main_window.action_view_show_cursors());
166 connect(&run_stop_button_, SIGNAL(clicked()),
167 this, SLOT(on_run_stop()));
168 connect(&sample_count_, SIGNAL(value_changed()),
169 this, SLOT(on_sample_count_changed()));
170 connect(&sample_rate_, SIGNAL(value_changed()),
171 this, SLOT(on_sample_rate_changed()));
173 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
175 set_capture_state(pv::Session::Stopped);
177 configure_button_.setIcon(QIcon::fromTheme("configure",
178 QIcon(":/icons/configure.png")));
180 channels_button_.setIcon(QIcon::fromTheme("channels",
181 QIcon(":/icons/channels.svg")));
183 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
185 addWidget(&device_selector_);
186 configure_button_action_ = addWidget(&configure_button_);
187 channels_button_action_ = addWidget(&channels_button_);
188 addWidget(&sample_count_);
189 addWidget(&sample_rate_);
190 run_stop_button_action_ = addWidget(&run_stop_button_);
193 addWidget(add_decoder_button);
196 QWidget *const spacer = new QWidget();
197 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
200 addWidget(&menu_button_);
202 sample_count_.installEventFilter(this);
203 sample_rate_.installEventFilter(this);
206 void MainBar::update_device_list()
208 DeviceManager &mgr = session_.device_manager();
209 shared_ptr<devices::Device> selected_device = session_.device();
210 list< shared_ptr<devices::Device> > devs;
212 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
214 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
215 devs.push_back(selected_device);
217 device_selector_.set_device_list(devs, selected_device);
218 update_device_config_widgets();
222 void MainBar::set_capture_state(pv::Session::capture_state state)
224 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
225 run_stop_button_.setIcon(*icons[state]);
226 run_stop_button_.setText((state == pv::Session::Stopped) ?
227 tr("Run") : tr("Stop"));
228 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
231 void MainBar::update_sample_rate_selector()
233 Glib::VariantContainerBase gvar_dict;
235 const uint64_t *elements = nullptr;
237 map< const ConfigKey*, std::set<Capability> > keys;
239 if (updating_sample_rate_) {
240 sample_rate_.show_none();
244 const shared_ptr<devices::Device> device =
245 device_selector_.selected_device();
249 assert(!updating_sample_rate_);
250 updating_sample_rate_ = true;
252 const shared_ptr<sigrok::Device> sr_dev = device->device();
255 keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
258 const auto iter = keys.find(ConfigKey::SAMPLERATE);
259 if (iter != keys.end() &&
260 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
262 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
263 } catch(const sigrok::Error &e) {
264 // Failed to enunmerate samplerate
269 if (!gvar_dict.gobj()) {
270 sample_rate_.show_none();
271 updating_sample_rate_ = false;
275 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
276 "samplerate-steps", G_VARIANT_TYPE("at"))))
278 elements = (const uint64_t *)g_variant_get_fixed_array(
279 gvar_list, &num_elements, sizeof(uint64_t));
281 const uint64_t min = elements[0];
282 const uint64_t max = elements[1];
283 const uint64_t step = elements[2];
285 g_variant_unref(gvar_list);
293 sample_rate_.show_125_list(min, max);
296 // When the step is not 1, we cam't make a 1-2-5-10
297 // list of sample rates, because we may not be able to
298 // make round numbers. Therefore in this case, show a
300 sample_rate_.show_min_max_step(min, max, step);
303 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
304 "samplerates", G_VARIANT_TYPE("at"))))
306 elements = (const uint64_t *)g_variant_get_fixed_array(
307 gvar_list, &num_elements, sizeof(uint64_t));
308 sample_rate_.show_list(elements, num_elements);
309 g_variant_unref(gvar_list);
311 updating_sample_rate_ = false;
313 update_sample_rate_selector_value();
316 void MainBar::update_sample_rate_selector_value()
318 if (updating_sample_rate_)
321 const shared_ptr<devices::Device> device =
322 device_selector_.selected_device();
327 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
328 uint64_t samplerate =
329 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
330 assert(!updating_sample_rate_);
331 updating_sample_rate_ = true;
332 sample_rate_.set_value(samplerate);
333 updating_sample_rate_ = false;
334 } catch (Error error) {
335 qDebug() << "WARNING: Failed to get value of sample rate";
340 void MainBar::update_sample_count_selector()
342 if (updating_sample_count_)
345 const shared_ptr<devices::Device> device =
346 device_selector_.selected_device();
350 const shared_ptr<sigrok::Device> sr_dev = device->device();
352 assert(!updating_sample_count_);
353 updating_sample_count_ = true;
355 if (!sample_count_supported_)
357 sample_count_.show_none();
358 updating_sample_count_ = false;
362 uint64_t sample_count = sample_count_.value();
363 uint64_t min_sample_count = 0;
364 uint64_t max_sample_count = MaxSampleCount;
366 if (sample_count == 0)
367 sample_count = DefaultSampleCount;
369 const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
370 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
371 if (iter != keys.end() &&
372 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
375 sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
377 g_variant_get(gvar.gobj(), "(tt)",
378 &min_sample_count, &max_sample_count);
379 } catch(const sigrok::Error &e) {
380 // Failed to query sample limit
385 min_sample_count = min(max(min_sample_count, MinSampleCount),
388 sample_count_.show_125_list(
389 min_sample_count, max_sample_count);
392 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
393 sample_count = g_variant_get_uint64(gvar.gobj());
394 if (sample_count == 0)
395 sample_count = DefaultSampleCount;
396 sample_count = min(max(sample_count, MinSampleCount),
398 } catch (Error error) {}
400 sample_count_.set_value(sample_count);
402 updating_sample_count_ = false;
405 void MainBar::update_device_config_widgets()
407 using namespace pv::popups;
409 const shared_ptr<devices::Device> device =
410 device_selector_.selected_device();
412 // Hide the widgets if no device is selected
413 channels_button_action_->setVisible(!!device);
414 run_stop_button_action_->setVisible(!!device);
416 configure_button_action_->setVisible(false);
417 sample_count_.show_none();
418 sample_rate_.show_none();
422 const shared_ptr<sigrok::Device> sr_dev = device->device();
426 // Update the configure popup
427 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
428 configure_button_action_->setVisible(
429 !opts->binding().properties().empty());
430 configure_button_.set_popup(opts);
432 // Update the channels popup
433 Channels *const channels = new Channels(session_, this);
434 channels_button_.set_popup(channels);
436 // Update supported options.
437 sample_count_supported_ = false;
440 for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS))
442 auto key = entry.first;
443 auto capabilities = entry.second;
445 case SR_CONF_LIMIT_SAMPLES:
446 if (capabilities.count(Capability::SET))
447 sample_count_supported_ = true;
449 case SR_CONF_LIMIT_FRAMES:
450 if (capabilities.count(Capability::SET))
452 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
453 Glib::Variant<guint64>::create(1));
461 } catch (Error error) {}
463 // Add notification of reconfigure events
464 disconnect(this, SLOT(on_config_changed()));
465 connect(&opts->binding(), SIGNAL(config_changed()),
466 this, SLOT(on_config_changed()));
468 // Update sweep timing widgets.
469 update_sample_count_selector();
470 update_sample_rate_selector();
473 void MainBar::commit_sample_count()
475 uint64_t sample_count = 0;
477 const shared_ptr<devices::Device> device =
478 device_selector_.selected_device();
482 const shared_ptr<sigrok::Device> sr_dev = device->device();
484 sample_count = sample_count_.value();
485 if (sample_count_supported_)
488 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
489 Glib::Variant<guint64>::create(sample_count));
490 update_sample_count_selector();
491 } catch (Error error) {
492 qDebug() << "Failed to configure sample count.";
497 // Devices with built-in memory might impose limits on certain
498 // configurations, so let's check what sample rate the driver
500 update_sample_rate_selector();
503 void MainBar::commit_sample_rate()
505 uint64_t sample_rate = 0;
507 const shared_ptr<devices::Device> device =
508 device_selector_.selected_device();
512 const shared_ptr<sigrok::Device> sr_dev = device->device();
514 sample_rate = sample_rate_.value();
515 if (sample_rate == 0)
519 sr_dev->config_set(ConfigKey::SAMPLERATE,
520 Glib::Variant<guint64>::create(sample_rate));
521 update_sample_rate_selector();
522 } catch (Error error) {
523 qDebug() << "Failed to configure samplerate.";
527 // Devices with built-in memory might impose limits on certain
528 // configurations, so let's check what sample count the driver
530 update_sample_count_selector();
533 void MainBar::on_device_selected()
535 shared_ptr<devices::Device> device = device_selector_.selected_device();
539 main_window_.select_device(device);
541 update_device_config_widgets();
544 void MainBar::on_sample_count_changed()
546 if (!updating_sample_count_)
547 commit_sample_count();
550 void MainBar::on_sample_rate_changed()
552 if (!updating_sample_rate_)
553 commit_sample_rate();
556 void MainBar::on_run_stop()
558 commit_sample_count();
559 commit_sample_rate();
560 main_window_.run_stop();
563 void MainBar::on_config_changed()
565 commit_sample_count();
566 commit_sample_rate();
569 bool MainBar::eventFilter(QObject *watched, QEvent *event)
571 if ((watched == &sample_count_ || watched == &sample_rate_) &&
572 (event->type() == QEvent::ToolTip)) {
573 double sec = (double)sample_count_.value() / sample_rate_.value();
574 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
576 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
577 QToolTip::showText(help_event->globalPos(), str);
585 } // namespace toolbars