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"));
86 QMenu *const menu = new QMenu(this);
88 QMenu *const menu_help = new QMenu;
89 menu_help->setTitle(tr("&Help"));
90 menu_help->addAction(main_window.action_about());
92 menu->addAction(menu_help->menuAction());
94 menu->addAction(main_window.action_quit());
96 menu_button_.setMenu(menu);
97 menu_button_.setPopupMode(QToolButton::InstantPopup);
98 menu_button_.setIcon(QIcon::fromTheme("menu",
99 QIcon(":/icons/menu.svg")));
102 addAction(main_window.action_open());
103 addAction(main_window.action_save_as());
105 addAction(main_window.action_view_zoom_in());
106 addAction(main_window.action_view_zoom_out());
107 addAction(main_window.action_view_zoom_fit());
108 addAction(main_window.action_view_zoom_one_to_one());
111 connect(&run_stop_button_, SIGNAL(clicked()),
112 this, SLOT(on_run_stop()));
113 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
114 this, SLOT(on_device_selected()));
115 connect(&sample_count_, SIGNAL(value_changed()),
116 this, SLOT(on_sample_count_changed()));
117 connect(&sample_rate_, SIGNAL(value_changed()),
118 this, SLOT(on_sample_rate_changed()));
120 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
122 set_capture_state(pv::Session::Stopped);
124 configure_button_.setIcon(QIcon::fromTheme("configure",
125 QIcon(":/icons/configure.png")));
127 channels_button_.setIcon(QIcon::fromTheme("channels",
128 QIcon(":/icons/channels.svg")));
130 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
132 addWidget(&device_selector_);
133 configure_button_action_ = addWidget(&configure_button_);
134 addWidget(&channels_button_);
135 addWidget(&sample_count_);
136 addWidget(&sample_rate_);
137 addWidget(&run_stop_button_);
139 QWidget *const spacer = new QWidget();
140 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
143 addWidget(&menu_button_);
145 sample_count_.installEventFilter(this);
146 sample_rate_.installEventFilter(this);
149 void MainBar::set_device_list(
150 const std::list< std::shared_ptr<sigrok::Device> > &devices,
151 shared_ptr<Device> selected)
153 int selected_index = -1;
157 updating_device_selector_ = true;
159 device_selector_.clear();
161 for (auto device : devices) {
164 string display_name =
165 session_.device_manager().get_display_name(device);
167 if (selected == device)
168 selected_index = device_selector_.count();
170 device_selector_.addItem(display_name.c_str(),
171 qVariantFromValue(device));
174 // The selected device should have been in the list
175 assert(selected_index != -1);
176 device_selector_.setCurrentIndex(selected_index);
178 update_device_config_widgets();
180 updating_device_selector_ = false;
183 shared_ptr<Device> MainBar::get_selected_device() const
185 const int index = device_selector_.currentIndex();
187 return shared_ptr<Device>();
189 return device_selector_.itemData(index).value<shared_ptr<Device>>();
192 void MainBar::set_capture_state(pv::Session::capture_state state)
194 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
195 run_stop_button_.setIcon(*icons[state]);
196 run_stop_button_.setText((state == pv::Session::Stopped) ?
197 tr("Run") : tr("Stop"));
198 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
201 void MainBar::update_sample_rate_selector()
203 Glib::VariantContainerBase gvar_dict;
205 const uint64_t *elements = NULL;
208 if (updating_sample_rate_)
211 const shared_ptr<Device> device = get_selected_device();
215 assert(!updating_sample_rate_);
216 updating_sample_rate_ = true;
218 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
219 const auto iter = keys.find(ConfigKey::SAMPLERATE);
220 if (iter != keys.end() &&
221 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
222 const auto keys = device->config_keys(
223 ConfigKey::DEVICE_OPTIONS);
225 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
226 } catch(const sigrok::Error &e) {
227 // Failed to enunmerate samplerate
233 sample_rate_.show_none();
234 updating_sample_rate_ = false;
238 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
239 "samplerate-steps", G_VARIANT_TYPE("at"))))
241 elements = (const uint64_t *)g_variant_get_fixed_array(
242 gvar_list, &num_elements, sizeof(uint64_t));
244 const uint64_t min = elements[0];
245 const uint64_t max = elements[1];
246 const uint64_t step = elements[2];
248 g_variant_unref(gvar_list);
256 sample_rate_.show_125_list(min, max);
259 // When the step is not 1, we cam't make a 1-2-5-10
260 // list of sample rates, because we may not be able to
261 // make round numbers. Therefore in this case, show a
263 sample_rate_.show_min_max_step(min, max, step);
266 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
267 "samplerates", G_VARIANT_TYPE("at"))))
269 elements = (const uint64_t *)g_variant_get_fixed_array(
270 gvar_list, &num_elements, sizeof(uint64_t));
271 sample_rate_.show_list(elements, num_elements);
272 g_variant_unref(gvar_list);
274 updating_sample_rate_ = false;
276 update_sample_rate_selector_value();
279 void MainBar::update_sample_rate_selector_value()
281 if (updating_sample_rate_)
284 const shared_ptr<Device> device = get_selected_device();
289 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
290 uint64_t samplerate =
291 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
292 assert(!updating_sample_rate_);
293 updating_sample_rate_ = true;
294 sample_rate_.set_value(samplerate);
295 updating_sample_rate_ = false;
296 } catch (Error error) {
297 qDebug() << "WARNING: Failed to get value of sample rate";
302 void MainBar::update_sample_count_selector()
304 if (updating_sample_count_)
307 const shared_ptr<Device> device = get_selected_device();
311 assert(!updating_sample_count_);
312 updating_sample_count_ = true;
314 if (!sample_count_supported_)
316 sample_count_.show_none();
317 updating_sample_count_ = false;
321 uint64_t sample_count = sample_count_.value();
322 uint64_t min_sample_count = 0;
323 uint64_t max_sample_count = MaxSampleCount;
325 if (sample_count == 0)
326 sample_count = DefaultSampleCount;
328 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
329 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
330 if (iter != keys.end() &&
331 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
334 device->config_list(ConfigKey::LIMIT_SAMPLES);
336 g_variant_get(gvar.gobj(), "(tt)",
337 &min_sample_count, &max_sample_count);
338 } catch(const sigrok::Error &e) {
339 // Failed to query sample limit
344 min_sample_count = min(max(min_sample_count, MinSampleCount),
347 sample_count_.show_125_list(
348 min_sample_count, max_sample_count);
351 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
352 sample_count = g_variant_get_uint64(gvar.gobj());
353 if (sample_count == 0)
354 sample_count = DefaultSampleCount;
355 sample_count = min(max(sample_count, MinSampleCount),
357 } catch (Error error) {}
359 sample_count_.set_value(sample_count);
361 updating_sample_count_ = false;
364 void MainBar::update_device_config_widgets()
366 using namespace pv::popups;
368 const shared_ptr<Device> device = get_selected_device();
372 // Update the configure popup
373 DeviceOptions *const opts = new DeviceOptions(device, this);
374 configure_button_action_->setVisible(
375 !opts->binding().properties().empty());
376 configure_button_.set_popup(opts);
378 // Update the channels popup
379 Channels *const channels = new Channels(session_, this);
380 channels_button_.set_popup(channels);
382 // Update supported options.
383 sample_count_supported_ = false;
386 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
388 auto key = entry.first;
389 auto capabilities = entry.second;
391 case SR_CONF_LIMIT_SAMPLES:
392 if (capabilities.count(Capability::SET))
393 sample_count_supported_ = true;
395 case SR_CONF_LIMIT_FRAMES:
396 if (capabilities.count(Capability::SET))
398 device->config_set(ConfigKey::LIMIT_FRAMES,
399 Glib::Variant<guint64>::create(1));
407 } catch (Error error) {}
409 // Add notification of reconfigure events
410 disconnect(this, SLOT(on_config_changed()));
411 connect(&opts->binding(), SIGNAL(config_changed()),
412 this, SLOT(on_config_changed()));
414 // Update sweep timing widgets.
415 update_sample_count_selector();
416 update_sample_rate_selector();
419 void MainBar::commit_sample_count()
421 uint64_t sample_count = 0;
423 if (updating_sample_count_)
426 const shared_ptr<Device> device = get_selected_device();
430 sample_count = sample_count_.value();
432 // Set the sample count
433 assert(!updating_sample_count_);
434 updating_sample_count_ = true;
435 if (sample_count_supported_)
438 device->config_set(ConfigKey::LIMIT_SAMPLES,
439 Glib::Variant<guint64>::create(sample_count));
441 } catch (Error error) {
442 qDebug() << "Failed to configure sample count.";
446 updating_sample_count_ = false;
449 void MainBar::commit_sample_rate()
451 uint64_t sample_rate = 0;
453 if (updating_sample_rate_)
456 const shared_ptr<Device> device = get_selected_device();
460 sample_rate = sample_rate_.value();
461 if (sample_rate == 0)
464 // Set the samplerate
465 assert(!updating_sample_rate_);
466 updating_sample_rate_ = true;
468 device->config_set(ConfigKey::SAMPLERATE,
469 Glib::Variant<guint64>::create(sample_rate));
471 } catch (Error error) {
472 qDebug() << "Failed to configure samplerate.";
475 updating_sample_rate_ = false;
478 void MainBar::on_device_selected()
480 if (updating_device_selector_)
483 shared_ptr<Device> device = get_selected_device();
487 main_window_.select_device(device);
489 update_device_config_widgets();
492 void MainBar::on_sample_count_changed()
494 commit_sample_count();
497 void MainBar::on_sample_rate_changed()
499 commit_sample_rate();
502 void MainBar::on_run_stop()
504 commit_sample_count();
505 commit_sample_rate();
506 main_window_.run_stop();
509 void MainBar::on_config_changed()
511 commit_sample_count();
512 update_sample_count_selector();
513 commit_sample_rate();
514 update_sample_rate_selector();
517 bool MainBar::eventFilter(QObject *watched, QEvent *event)
519 if ((watched == &sample_count_ || watched == &sample_rate_) &&
520 (event->type() == QEvent::ToolTip)) {
521 double sec = (double)sample_count_.value() / sample_rate_.value();
522 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
524 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
525 QToolTip::showText(help_event->globalPos(), str);
533 } // namespace toolbars