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>
42 #include <libsigrokcxx/libsigrokcxx.hpp>
44 using std::back_inserter;
50 using std::shared_ptr;
54 using sigrok::Capability;
55 using sigrok::ConfigKey;
61 const uint64_t MainBar::MinSampleCount = 100ULL;
62 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
63 const uint64_t MainBar::DefaultSampleCount = 1000000;
65 MainBar::MainBar(Session &session, MainWindow &main_window) :
66 QToolBar("Sampling Bar", &main_window),
68 main_window_(main_window),
69 device_selector_(this, session.device_manager(),
70 main_window.action_connect()),
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"),
82 run_stop_button_(this),
85 setObjectName(QString::fromUtf8("MainBar"));
89 setContextMenuPolicy(Qt::PreventContextMenu);
92 QToolButton *const save_button = new QToolButton(this);
94 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
95 session.device_manager().context(),
96 main_window.action_save_as());
98 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
100 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
102 save_button->setMenu(export_menu);
103 save_button->setDefaultAction(main_window.action_save_as());
104 save_button->setPopupMode(QToolButton::MenuButtonPopup);
106 // Device selector menu
107 connect(&device_selector_, SIGNAL(device_selected()),
108 this, SLOT(on_device_selected()));
110 // Setup the decoder button
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());
120 QMenu *const menu = new QMenu(this);
122 QMenu *const menu_help = new QMenu;
123 menu_help->setTitle(tr("&Help"));
124 menu_help->addAction(main_window.action_about());
126 menu->addAction(menu_help->menuAction());
127 menu->addSeparator();
128 menu->addAction(main_window.action_quit());
130 menu_button_.setMenu(menu);
131 menu_button_.setPopupMode(QToolButton::InstantPopup);
132 menu_button_.setIcon(QIcon::fromTheme("menu",
133 QIcon(":/icons/menu.svg")));
136 addAction(main_window.action_open());
137 addWidget(save_button);
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());
144 addAction(main_window.action_view_show_cursors());
147 connect(&run_stop_button_, SIGNAL(clicked()),
148 this, SLOT(on_run_stop()));
149 connect(&sample_count_, SIGNAL(value_changed()),
150 this, SLOT(on_sample_count_changed()));
151 connect(&sample_rate_, SIGNAL(value_changed()),
152 this, SLOT(on_sample_rate_changed()));
154 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
156 set_capture_state(pv::Session::Stopped);
158 configure_button_.setIcon(QIcon::fromTheme("configure",
159 QIcon(":/icons/configure.png")));
161 channels_button_.setIcon(QIcon::fromTheme("channels",
162 QIcon(":/icons/channels.svg")));
164 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
166 addWidget(&device_selector_);
167 configure_button_action_ = addWidget(&configure_button_);
168 addWidget(&channels_button_);
169 addWidget(&sample_count_);
170 addWidget(&sample_rate_);
171 addWidget(&run_stop_button_);
174 addWidget(add_decoder_button);
177 QWidget *const spacer = new QWidget();
178 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
181 addWidget(&menu_button_);
183 sample_count_.installEventFilter(this);
184 sample_rate_.installEventFilter(this);
187 void MainBar::update_device_list()
189 DeviceManager &mgr = session_.device_manager();
190 shared_ptr<devices::Device> selected_device = session_.device();
191 list< shared_ptr<devices::Device> > devs;
193 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
195 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
196 devs.push_back(selected_device);
197 assert(selected_device);
199 device_selector_.set_device_list(devs, selected_device);
200 update_device_config_widgets();
204 void MainBar::set_capture_state(pv::Session::capture_state state)
206 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
207 run_stop_button_.setIcon(*icons[state]);
208 run_stop_button_.setText((state == pv::Session::Stopped) ?
209 tr("Run") : tr("Stop"));
210 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
213 void MainBar::update_sample_rate_selector()
215 Glib::VariantContainerBase gvar_dict;
217 const uint64_t *elements = NULL;
219 map< const ConfigKey*, std::set<Capability> > keys;
221 if (updating_sample_rate_)
224 const shared_ptr<devices::Device> device =
225 device_selector_.selected_device();
229 assert(!updating_sample_rate_);
230 updating_sample_rate_ = true;
232 const shared_ptr<sigrok::Device> sr_dev = device->device();
235 keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
238 const auto iter = keys.find(ConfigKey::SAMPLERATE);
239 if (iter != keys.end() &&
240 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
242 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
243 } catch(const sigrok::Error &e) {
244 // Failed to enunmerate samplerate
249 if (!gvar_dict.gobj()) {
250 sample_rate_.show_none();
251 updating_sample_rate_ = false;
255 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
256 "samplerate-steps", G_VARIANT_TYPE("at"))))
258 elements = (const uint64_t *)g_variant_get_fixed_array(
259 gvar_list, &num_elements, sizeof(uint64_t));
261 const uint64_t min = elements[0];
262 const uint64_t max = elements[1];
263 const uint64_t step = elements[2];
265 g_variant_unref(gvar_list);
273 sample_rate_.show_125_list(min, max);
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
280 sample_rate_.show_min_max_step(min, max, step);
283 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
284 "samplerates", G_VARIANT_TYPE("at"))))
286 elements = (const uint64_t *)g_variant_get_fixed_array(
287 gvar_list, &num_elements, sizeof(uint64_t));
288 sample_rate_.show_list(elements, num_elements);
289 g_variant_unref(gvar_list);
291 updating_sample_rate_ = false;
293 update_sample_rate_selector_value();
296 void MainBar::update_sample_rate_selector_value()
298 if (updating_sample_rate_)
301 const shared_ptr<devices::Device> device =
302 device_selector_.selected_device();
307 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
308 uint64_t samplerate =
309 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
310 assert(!updating_sample_rate_);
311 updating_sample_rate_ = true;
312 sample_rate_.set_value(samplerate);
313 updating_sample_rate_ = false;
314 } catch (Error error) {
315 qDebug() << "WARNING: Failed to get value of sample rate";
320 void MainBar::update_sample_count_selector()
322 if (updating_sample_count_)
325 const shared_ptr<devices::Device> device =
326 device_selector_.selected_device();
330 const shared_ptr<sigrok::Device> sr_dev = device->device();
332 assert(!updating_sample_count_);
333 updating_sample_count_ = true;
335 if (!sample_count_supported_)
337 sample_count_.show_none();
338 updating_sample_count_ = false;
342 uint64_t sample_count = sample_count_.value();
343 uint64_t min_sample_count = 0;
344 uint64_t max_sample_count = MaxSampleCount;
346 if (sample_count == 0)
347 sample_count = DefaultSampleCount;
349 const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS);
350 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
351 if (iter != keys.end() &&
352 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
355 sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
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
365 min_sample_count = min(max(min_sample_count, MinSampleCount),
368 sample_count_.show_125_list(
369 min_sample_count, max_sample_count);
372 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
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),
378 } catch (Error error) {}
380 sample_count_.set_value(sample_count);
382 updating_sample_count_ = false;
385 void MainBar::update_device_config_widgets()
387 using namespace pv::popups;
389 const shared_ptr<devices::Device> device =
390 device_selector_.selected_device();
394 const shared_ptr<sigrok::Device> sr_dev = device->device();
398 // Update the configure popup
399 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
400 configure_button_action_->setVisible(
401 !opts->binding().properties().empty());
402 configure_button_.set_popup(opts);
404 // Update the channels popup
405 Channels *const channels = new Channels(session_, this);
406 channels_button_.set_popup(channels);
408 // Update supported options.
409 sample_count_supported_ = false;
412 for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS))
414 auto key = entry.first;
415 auto capabilities = entry.second;
417 case SR_CONF_LIMIT_SAMPLES:
418 if (capabilities.count(Capability::SET))
419 sample_count_supported_ = true;
421 case SR_CONF_LIMIT_FRAMES:
422 if (capabilities.count(Capability::SET))
424 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
425 Glib::Variant<guint64>::create(1));
433 } catch (Error error) {}
435 // Add notification of reconfigure events
436 disconnect(this, SLOT(on_config_changed()));
437 connect(&opts->binding(), SIGNAL(config_changed()),
438 this, SLOT(on_config_changed()));
440 // Update sweep timing widgets.
441 update_sample_count_selector();
442 update_sample_rate_selector();
445 void MainBar::commit_sample_count()
447 uint64_t sample_count = 0;
449 if (updating_sample_count_)
452 const shared_ptr<devices::Device> device =
453 device_selector_.selected_device();
457 const shared_ptr<sigrok::Device> sr_dev = device->device();
459 sample_count = sample_count_.value();
461 // Set the sample count
462 assert(!updating_sample_count_);
463 updating_sample_count_ = true;
464 if (sample_count_supported_)
467 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
468 Glib::Variant<guint64>::create(sample_count));
470 } catch (Error error) {
471 qDebug() << "Failed to configure sample count.";
475 updating_sample_count_ = false;
478 void MainBar::commit_sample_rate()
480 uint64_t sample_rate = 0;
482 if (updating_sample_rate_)
485 const shared_ptr<devices::Device> device =
486 device_selector_.selected_device();
490 const shared_ptr<sigrok::Device> sr_dev = device->device();
492 sample_rate = sample_rate_.value();
493 if (sample_rate == 0)
496 // Set the samplerate
497 assert(!updating_sample_rate_);
498 updating_sample_rate_ = true;
500 sr_dev->config_set(ConfigKey::SAMPLERATE,
501 Glib::Variant<guint64>::create(sample_rate));
503 } catch (Error error) {
504 qDebug() << "Failed to configure samplerate.";
507 updating_sample_rate_ = false;
510 void MainBar::on_device_selected()
512 shared_ptr<devices::Device> device = device_selector_.selected_device();
516 main_window_.select_device(device);
518 update_device_config_widgets();
521 void MainBar::on_sample_count_changed()
523 commit_sample_count();
526 void MainBar::on_sample_rate_changed()
528 commit_sample_rate();
531 void MainBar::on_run_stop()
533 commit_sample_count();
534 commit_sample_rate();
535 main_window_.run_stop();
538 void MainBar::on_config_changed()
540 commit_sample_count();
541 update_sample_count_selector();
542 commit_sample_rate();
543 update_sample_rate_selector();
546 bool MainBar::eventFilter(QObject *watched, QEvent *event)
548 if ((watched == &sample_count_ || watched == &sample_rate_) &&
549 (event->type() == QEvent::ToolTip)) {
550 double sec = (double)sample_count_.value() / sample_rate_.value();
551 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
553 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
554 QToolTip::showText(help_event->globalPos(), str);
562 } // namespace toolbars