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());
138 // Setup the burger menu
139 QMenu *const menu = new QMenu(this);
141 QMenu *const menu_view = new QMenu;
142 menu_view->setTitle(tr("&View"));
143 menu_view->addAction(main_window.action_view_sticky_scrolling());
145 QMenu *const menu_help = new QMenu;
146 menu_help->setTitle(tr("&Help"));
147 menu_help->addAction(main_window.action_about());
149 menu->addAction(menu_view->menuAction());
150 menu->addSeparator();
151 menu->addAction(menu_help->menuAction());
152 menu->addSeparator();
153 menu->addAction(main_window.action_quit());
155 menu_button_.setMenu(menu);
156 menu_button_.setPopupMode(QToolButton::InstantPopup);
157 menu_button_.setIcon(QIcon::fromTheme("menu",
158 QIcon(":/icons/menu.svg")));
161 addWidget(open_button);
162 addWidget(save_button);
164 addAction(main_window.action_view_zoom_in());
165 addAction(main_window.action_view_zoom_out());
166 addAction(main_window.action_view_zoom_fit());
167 addAction(main_window.action_view_zoom_one_to_one());
169 addAction(main_window.action_view_show_cursors());
172 connect(&run_stop_button_, SIGNAL(clicked()),
173 this, SLOT(on_run_stop()));
174 connect(&sample_count_, SIGNAL(value_changed()),
175 this, SLOT(on_sample_count_changed()));
176 connect(&sample_rate_, SIGNAL(value_changed()),
177 this, SLOT(on_sample_rate_changed()));
179 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
181 set_capture_state(pv::Session::Stopped);
183 configure_button_.setIcon(QIcon::fromTheme("configure",
184 QIcon(":/icons/configure.png")));
186 channels_button_.setIcon(QIcon::fromTheme("channels",
187 QIcon(":/icons/channels.svg")));
189 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
191 addWidget(&device_selector_);
192 configure_button_action_ = addWidget(&configure_button_);
193 channels_button_action_ = addWidget(&channels_button_);
194 addWidget(&sample_count_);
195 addWidget(&sample_rate_);
196 run_stop_button_action_ = addWidget(&run_stop_button_);
199 addWidget(add_decoder_button);
202 QWidget *const spacer = new QWidget();
203 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
206 addWidget(&menu_button_);
208 sample_count_.installEventFilter(this);
209 sample_rate_.installEventFilter(this);
212 void MainBar::update_device_list()
214 DeviceManager &mgr = session_.device_manager();
215 shared_ptr<devices::Device> selected_device = session_.device();
216 list< shared_ptr<devices::Device> > devs;
218 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
220 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
221 devs.push_back(selected_device);
223 device_selector_.set_device_list(devs, selected_device);
224 update_device_config_widgets();
228 void MainBar::set_capture_state(pv::Session::capture_state state)
230 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
231 run_stop_button_.setIcon(*icons[state]);
232 run_stop_button_.setText((state == pv::Session::Stopped) ?
233 tr("Run") : tr("Stop"));
234 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
236 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
238 device_selector_.setEnabled(ui_enabled);
239 configure_button_.setEnabled(ui_enabled);
240 channels_button_.setEnabled(ui_enabled);
241 sample_count_.setEnabled(ui_enabled);
242 sample_rate_.setEnabled(ui_enabled);
245 void MainBar::update_sample_rate_selector()
247 Glib::VariantContainerBase gvar_dict;
249 const uint64_t *elements = nullptr;
251 map< const ConfigKey*, std::set<Capability> > keys;
253 if (updating_sample_rate_) {
254 sample_rate_.show_none();
258 const shared_ptr<devices::Device> device =
259 device_selector_.selected_device();
263 assert(!updating_sample_rate_);
264 updating_sample_rate_ = true;
266 const shared_ptr<sigrok::Device> sr_dev = device->device();
269 keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
272 const auto iter = keys.find(ConfigKey::SAMPLERATE);
273 if (iter != keys.end() &&
274 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
276 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
277 } catch(const sigrok::Error &e) {
278 // Failed to enunmerate samplerate
283 if (!gvar_dict.gobj()) {
284 sample_rate_.show_none();
285 updating_sample_rate_ = false;
289 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
290 "samplerate-steps", G_VARIANT_TYPE("at"))))
292 elements = (const uint64_t *)g_variant_get_fixed_array(
293 gvar_list, &num_elements, sizeof(uint64_t));
295 const uint64_t min = elements[0];
296 const uint64_t max = elements[1];
297 const uint64_t step = elements[2];
299 g_variant_unref(gvar_list);
307 sample_rate_.show_125_list(min, max);
310 // When the step is not 1, we cam't make a 1-2-5-10
311 // list of sample rates, because we may not be able to
312 // make round numbers. Therefore in this case, show a
314 sample_rate_.show_min_max_step(min, max, step);
317 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
318 "samplerates", G_VARIANT_TYPE("at"))))
320 elements = (const uint64_t *)g_variant_get_fixed_array(
321 gvar_list, &num_elements, sizeof(uint64_t));
322 sample_rate_.show_list(elements, num_elements);
323 g_variant_unref(gvar_list);
325 updating_sample_rate_ = false;
327 update_sample_rate_selector_value();
330 void MainBar::update_sample_rate_selector_value()
332 if (updating_sample_rate_)
335 const shared_ptr<devices::Device> device =
336 device_selector_.selected_device();
341 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
342 uint64_t samplerate =
343 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
344 assert(!updating_sample_rate_);
345 updating_sample_rate_ = true;
346 sample_rate_.set_value(samplerate);
347 updating_sample_rate_ = false;
348 } catch (Error error) {
349 qDebug() << "WARNING: Failed to get value of sample rate";
354 void MainBar::update_sample_count_selector()
356 if (updating_sample_count_)
359 const shared_ptr<devices::Device> device =
360 device_selector_.selected_device();
364 const shared_ptr<sigrok::Device> sr_dev = device->device();
366 assert(!updating_sample_count_);
367 updating_sample_count_ = true;
369 if (!sample_count_supported_)
371 sample_count_.show_none();
372 updating_sample_count_ = false;
376 uint64_t sample_count = sample_count_.value();
377 uint64_t min_sample_count = 0;
378 uint64_t max_sample_count = MaxSampleCount;
380 if (sample_count == 0)
381 sample_count = DefaultSampleCount;
383 const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
384 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
385 if (iter != keys.end() &&
386 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
389 sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
391 g_variant_get(gvar.gobj(), "(tt)",
392 &min_sample_count, &max_sample_count);
393 } catch(const sigrok::Error &e) {
394 // Failed to query sample limit
399 min_sample_count = min(max(min_sample_count, MinSampleCount),
402 sample_count_.show_125_list(
403 min_sample_count, max_sample_count);
406 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
407 sample_count = g_variant_get_uint64(gvar.gobj());
408 if (sample_count == 0)
409 sample_count = DefaultSampleCount;
410 sample_count = min(max(sample_count, MinSampleCount),
412 } catch (Error error) {}
414 sample_count_.set_value(sample_count);
416 updating_sample_count_ = false;
419 void MainBar::update_device_config_widgets()
421 using namespace pv::popups;
423 const shared_ptr<devices::Device> device =
424 device_selector_.selected_device();
426 // Hide the widgets if no device is selected
427 channels_button_action_->setVisible(!!device);
428 run_stop_button_action_->setVisible(!!device);
430 configure_button_action_->setVisible(false);
431 sample_count_.show_none();
432 sample_rate_.show_none();
436 const shared_ptr<sigrok::Device> sr_dev = device->device();
440 // Update the configure popup
441 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
442 configure_button_action_->setVisible(
443 !opts->binding().properties().empty());
444 configure_button_.set_popup(opts);
446 // Update the channels popup
447 Channels *const channels = new Channels(session_, this);
448 channels_button_.set_popup(channels);
450 // Update supported options.
451 sample_count_supported_ = false;
454 for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS))
456 auto key = entry.first;
457 auto capabilities = entry.second;
459 case SR_CONF_LIMIT_SAMPLES:
460 if (capabilities.count(Capability::SET))
461 sample_count_supported_ = true;
463 case SR_CONF_LIMIT_FRAMES:
464 if (capabilities.count(Capability::SET))
466 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
467 Glib::Variant<guint64>::create(1));
475 } catch (Error error) {}
477 // Add notification of reconfigure events
478 disconnect(this, SLOT(on_config_changed()));
479 connect(&opts->binding(), SIGNAL(config_changed()),
480 this, SLOT(on_config_changed()));
482 // Update sweep timing widgets.
483 update_sample_count_selector();
484 update_sample_rate_selector();
487 void MainBar::commit_sample_count()
489 uint64_t sample_count = 0;
491 const shared_ptr<devices::Device> device =
492 device_selector_.selected_device();
496 const shared_ptr<sigrok::Device> sr_dev = device->device();
498 sample_count = sample_count_.value();
499 if (sample_count_supported_)
502 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
503 Glib::Variant<guint64>::create(sample_count));
504 update_sample_count_selector();
505 } catch (Error error) {
506 qDebug() << "Failed to configure sample count.";
511 // Devices with built-in memory might impose limits on certain
512 // configurations, so let's check what sample rate the driver
514 update_sample_rate_selector();
517 void MainBar::commit_sample_rate()
519 uint64_t sample_rate = 0;
521 const shared_ptr<devices::Device> device =
522 device_selector_.selected_device();
526 const shared_ptr<sigrok::Device> sr_dev = device->device();
528 sample_rate = sample_rate_.value();
529 if (sample_rate == 0)
533 sr_dev->config_set(ConfigKey::SAMPLERATE,
534 Glib::Variant<guint64>::create(sample_rate));
535 update_sample_rate_selector();
536 } catch (Error error) {
537 qDebug() << "Failed to configure samplerate.";
541 // Devices with built-in memory might impose limits on certain
542 // configurations, so let's check what sample count the driver
544 update_sample_count_selector();
547 void MainBar::on_device_selected()
549 shared_ptr<devices::Device> device = device_selector_.selected_device();
553 main_window_.select_device(device);
555 update_device_config_widgets();
558 void MainBar::on_sample_count_changed()
560 if (!updating_sample_count_)
561 commit_sample_count();
564 void MainBar::on_sample_rate_changed()
566 if (!updating_sample_rate_)
567 commit_sample_rate();
570 void MainBar::on_run_stop()
572 commit_sample_count();
573 commit_sample_rate();
574 main_window_.run_stop();
577 void MainBar::on_config_changed()
579 commit_sample_count();
580 commit_sample_rate();
583 bool MainBar::eventFilter(QObject *watched, QEvent *event)
585 if ((watched == &sample_count_ || watched == &sample_rate_) &&
586 (event->type() == QEvent::ToolTip)) {
587 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
588 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
590 QString str = tr("Total sampling time: %1").arg(
591 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
592 QToolTip::showText(help_event->globalPos(), str);
600 } // namespace toolbars