2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 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
31 #include "mainbar.hpp"
33 #include <pv/devicemanager.hpp>
34 #include <pv/mainwindow.hpp>
35 #include <pv/popups/deviceoptions.hpp>
36 #include <pv/popups/channels.hpp>
37 #include <pv/util.hpp>
39 #include <libsigrok/libsigrok.hpp>
45 using std::shared_ptr;
48 using sigrok::Capability;
49 using sigrok::ConfigKey;
56 const uint64_t MainBar::MinSampleCount = 100ULL;
57 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
58 const uint64_t MainBar::DefaultSampleCount = 1000000;
60 MainBar::MainBar(Session &session, MainWindow &main_window) :
61 QToolBar("Sampling Bar", &main_window),
63 main_window_(main_window),
64 device_selector_(this),
65 updating_device_selector_(false),
66 configure_button_(this),
67 configure_button_action_(NULL),
68 channels_button_(this),
69 sample_count_(" samples", this),
70 sample_rate_("Hz", this),
71 updating_sample_rate_(false),
72 updating_sample_count_(false),
73 sample_count_supported_(false),
74 icon_red_(":/icons/status-red.svg"),
75 icon_green_(":/icons/status-green.svg"),
76 icon_grey_(":/icons/status-grey.svg"),
77 run_stop_button_(this),
80 setObjectName(QString::fromUtf8("MainBar"));
85 // Setup the decoder button
87 QToolButton *add_decoder_button = new QToolButton(this);
88 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
89 QIcon(":/icons/add-decoder.svg")));
90 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
91 add_decoder_button->setMenu(main_window_.menu_decoder_add());
95 QMenu *const menu = new QMenu(this);
97 QMenu *const menu_help = new QMenu;
98 menu_help->setTitle(tr("&Help"));
99 menu_help->addAction(main_window.action_about());
101 menu->addAction(menu_help->menuAction());
102 menu->addSeparator();
103 menu->addAction(main_window.action_quit());
105 menu_button_.setMenu(menu);
106 menu_button_.setPopupMode(QToolButton::InstantPopup);
107 menu_button_.setIcon(QIcon::fromTheme("menu",
108 QIcon(":/icons/menu.svg")));
111 addAction(main_window.action_open());
112 addAction(main_window.action_save_as());
114 addAction(main_window.action_view_zoom_in());
115 addAction(main_window.action_view_zoom_out());
116 addAction(main_window.action_view_zoom_fit());
117 addAction(main_window.action_view_zoom_one_to_one());
119 addAction(main_window.action_view_show_cursors());
122 connect(&run_stop_button_, SIGNAL(clicked()),
123 this, SLOT(on_run_stop()));
124 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
125 this, SLOT(on_device_selected()));
126 connect(&sample_count_, SIGNAL(value_changed()),
127 this, SLOT(on_sample_count_changed()));
128 connect(&sample_rate_, SIGNAL(value_changed()),
129 this, SLOT(on_sample_rate_changed()));
131 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
133 set_capture_state(pv::Session::Stopped);
135 configure_button_.setIcon(QIcon::fromTheme("configure",
136 QIcon(":/icons/configure.png")));
138 channels_button_.setIcon(QIcon::fromTheme("channels",
139 QIcon(":/icons/channels.svg")));
141 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
143 addWidget(&device_selector_);
144 configure_button_action_ = addWidget(&configure_button_);
145 addWidget(&channels_button_);
146 addWidget(&sample_count_);
147 addWidget(&sample_rate_);
148 addWidget(&run_stop_button_);
151 addWidget(add_decoder_button);
154 QWidget *const spacer = new QWidget();
155 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
158 addWidget(&menu_button_);
160 sample_count_.installEventFilter(this);
161 sample_rate_.installEventFilter(this);
164 void MainBar::set_device_list(
165 const std::list< std::shared_ptr<sigrok::Device> > &devices,
166 shared_ptr<Device> selected)
168 int selected_index = -1;
172 updating_device_selector_ = true;
174 device_selector_.clear();
176 for (auto device : devices) {
179 string display_name =
180 session_.device_manager().get_display_name(device);
182 if (selected == device)
183 selected_index = device_selector_.count();
185 device_selector_.addItem(display_name.c_str(),
186 qVariantFromValue(device));
189 // The selected device should have been in the list
190 assert(selected_index != -1);
191 device_selector_.setCurrentIndex(selected_index);
193 update_device_config_widgets();
195 updating_device_selector_ = false;
198 shared_ptr<Device> MainBar::get_selected_device() const
200 const int index = device_selector_.currentIndex();
202 return shared_ptr<Device>();
204 return device_selector_.itemData(index).value<shared_ptr<Device>>();
207 void MainBar::set_capture_state(pv::Session::capture_state state)
209 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
210 run_stop_button_.setIcon(*icons[state]);
211 run_stop_button_.setText((state == pv::Session::Stopped) ?
212 tr("Run") : tr("Stop"));
213 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
216 void MainBar::update_sample_rate_selector()
218 Glib::VariantContainerBase gvar_dict;
220 const uint64_t *elements = NULL;
223 if (updating_sample_rate_)
226 const shared_ptr<Device> device = get_selected_device();
230 assert(!updating_sample_rate_);
231 updating_sample_rate_ = true;
233 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
234 const auto iter = keys.find(ConfigKey::SAMPLERATE);
235 if (iter != keys.end() &&
236 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
237 const auto keys = device->config_keys(
238 ConfigKey::DEVICE_OPTIONS);
240 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
241 } catch(const sigrok::Error &e) {
242 // Failed to enunmerate samplerate
248 sample_rate_.show_none();
249 updating_sample_rate_ = false;
253 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
254 "samplerate-steps", G_VARIANT_TYPE("at"))))
256 elements = (const uint64_t *)g_variant_get_fixed_array(
257 gvar_list, &num_elements, sizeof(uint64_t));
259 const uint64_t min = elements[0];
260 const uint64_t max = elements[1];
261 const uint64_t step = elements[2];
263 g_variant_unref(gvar_list);
271 sample_rate_.show_125_list(min, max);
274 // When the step is not 1, we cam't make a 1-2-5-10
275 // list of sample rates, because we may not be able to
276 // make round numbers. Therefore in this case, show a
278 sample_rate_.show_min_max_step(min, max, step);
281 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
282 "samplerates", G_VARIANT_TYPE("at"))))
284 elements = (const uint64_t *)g_variant_get_fixed_array(
285 gvar_list, &num_elements, sizeof(uint64_t));
286 sample_rate_.show_list(elements, num_elements);
287 g_variant_unref(gvar_list);
289 updating_sample_rate_ = false;
291 update_sample_rate_selector_value();
294 void MainBar::update_sample_rate_selector_value()
296 if (updating_sample_rate_)
299 const shared_ptr<Device> device = get_selected_device();
304 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
305 uint64_t samplerate =
306 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
307 assert(!updating_sample_rate_);
308 updating_sample_rate_ = true;
309 sample_rate_.set_value(samplerate);
310 updating_sample_rate_ = false;
311 } catch (Error error) {
312 qDebug() << "WARNING: Failed to get value of sample rate";
317 void MainBar::update_sample_count_selector()
319 if (updating_sample_count_)
322 const shared_ptr<Device> device = get_selected_device();
326 assert(!updating_sample_count_);
327 updating_sample_count_ = true;
329 if (!sample_count_supported_)
331 sample_count_.show_none();
332 updating_sample_count_ = false;
336 uint64_t sample_count = sample_count_.value();
337 uint64_t min_sample_count = 0;
338 uint64_t max_sample_count = MaxSampleCount;
340 if (sample_count == 0)
341 sample_count = DefaultSampleCount;
343 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
344 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
345 if (iter != keys.end() &&
346 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
349 device->config_list(ConfigKey::LIMIT_SAMPLES);
351 g_variant_get(gvar.gobj(), "(tt)",
352 &min_sample_count, &max_sample_count);
353 } catch(const sigrok::Error &e) {
354 // Failed to query sample limit
359 min_sample_count = min(max(min_sample_count, MinSampleCount),
362 sample_count_.show_125_list(
363 min_sample_count, max_sample_count);
366 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
367 sample_count = g_variant_get_uint64(gvar.gobj());
368 if (sample_count == 0)
369 sample_count = DefaultSampleCount;
370 sample_count = min(max(sample_count, MinSampleCount),
372 } catch (Error error) {}
374 sample_count_.set_value(sample_count);
376 updating_sample_count_ = false;
379 void MainBar::update_device_config_widgets()
381 using namespace pv::popups;
383 const shared_ptr<Device> device = get_selected_device();
387 // Update the configure popup
388 DeviceOptions *const opts = new DeviceOptions(device, this);
389 configure_button_action_->setVisible(
390 !opts->binding().properties().empty());
391 configure_button_.set_popup(opts);
393 // Update the channels popup
394 Channels *const channels = new Channels(session_, this);
395 channels_button_.set_popup(channels);
397 // Update supported options.
398 sample_count_supported_ = false;
401 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
403 auto key = entry.first;
404 auto capabilities = entry.second;
406 case SR_CONF_LIMIT_SAMPLES:
407 if (capabilities.count(Capability::SET))
408 sample_count_supported_ = true;
410 case SR_CONF_LIMIT_FRAMES:
411 if (capabilities.count(Capability::SET))
413 device->config_set(ConfigKey::LIMIT_FRAMES,
414 Glib::Variant<guint64>::create(1));
422 } catch (Error error) {}
424 // Add notification of reconfigure events
425 disconnect(this, SLOT(on_config_changed()));
426 connect(&opts->binding(), SIGNAL(config_changed()),
427 this, SLOT(on_config_changed()));
429 // Update sweep timing widgets.
430 update_sample_count_selector();
431 update_sample_rate_selector();
434 void MainBar::commit_sample_count()
436 uint64_t sample_count = 0;
438 if (updating_sample_count_)
441 const shared_ptr<Device> device = get_selected_device();
445 sample_count = sample_count_.value();
447 // Set the sample count
448 assert(!updating_sample_count_);
449 updating_sample_count_ = true;
450 if (sample_count_supported_)
453 device->config_set(ConfigKey::LIMIT_SAMPLES,
454 Glib::Variant<guint64>::create(sample_count));
456 } catch (Error error) {
457 qDebug() << "Failed to configure sample count.";
461 updating_sample_count_ = false;
464 void MainBar::commit_sample_rate()
466 uint64_t sample_rate = 0;
468 if (updating_sample_rate_)
471 const shared_ptr<Device> device = get_selected_device();
475 sample_rate = sample_rate_.value();
476 if (sample_rate == 0)
479 // Set the samplerate
480 assert(!updating_sample_rate_);
481 updating_sample_rate_ = true;
483 device->config_set(ConfigKey::SAMPLERATE,
484 Glib::Variant<guint64>::create(sample_rate));
486 } catch (Error error) {
487 qDebug() << "Failed to configure samplerate.";
490 updating_sample_rate_ = false;
493 void MainBar::on_device_selected()
495 if (updating_device_selector_)
498 shared_ptr<Device> device = get_selected_device();
502 main_window_.select_device(device);
504 update_device_config_widgets();
507 void MainBar::on_sample_count_changed()
509 commit_sample_count();
512 void MainBar::on_sample_rate_changed()
514 commit_sample_rate();
517 void MainBar::on_run_stop()
519 commit_sample_count();
520 commit_sample_rate();
521 main_window_.run_stop();
524 void MainBar::on_config_changed()
526 commit_sample_count();
527 update_sample_count_selector();
528 commit_sample_rate();
529 update_sample_rate_selector();
532 bool MainBar::eventFilter(QObject *watched, QEvent *event)
534 if ((watched == &sample_count_ || watched == &sample_rate_) &&
535 (event->type() == QEvent::ToolTip)) {
536 double sec = (double)sample_count_.value() / sample_rate_.value();
537 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
539 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
540 QToolTip::showText(help_event->globalPos(), str);
548 } // namespace toolbars