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/mainwindow.hpp>
36 #include <pv/popups/deviceoptions.hpp>
37 #include <pv/popups/channels.hpp>
38 #include <pv/util.hpp>
40 #include <libsigrokcxx/libsigrokcxx.hpp>
42 using std::back_inserter;
48 using std::shared_ptr;
52 using sigrok::Capability;
53 using sigrok::ConfigKey;
60 const uint64_t MainBar::MinSampleCount = 100ULL;
61 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
62 const uint64_t MainBar::DefaultSampleCount = 1000000;
64 MainBar::MainBar(Session &session, MainWindow &main_window) :
65 QToolBar("Sampling Bar", &main_window),
67 main_window_(main_window),
68 device_selector_(this, session.device_manager(),
69 main_window.action_connect()),
70 configure_button_(this),
71 configure_button_action_(NULL),
72 channels_button_(this),
73 sample_count_(" samples", this),
74 sample_rate_("Hz", this),
75 updating_sample_rate_(false),
76 updating_sample_count_(false),
77 sample_count_supported_(false),
78 icon_red_(":/icons/status-red.svg"),
79 icon_green_(":/icons/status-green.svg"),
80 icon_grey_(":/icons/status-grey.svg"),
81 run_stop_button_(this),
84 setObjectName(QString::fromUtf8("MainBar"));
88 setContextMenuPolicy(Qt::PreventContextMenu);
90 // Device selector menu
91 connect(&device_selector_, SIGNAL(device_selected()),
92 this, SLOT(on_device_selected()));
94 // Setup the decoder button
96 QToolButton *add_decoder_button = new QToolButton(this);
97 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
98 QIcon(":/icons/add-decoder.svg")));
99 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
100 add_decoder_button->setMenu(main_window_.menu_decoder_add());
104 QMenu *const menu = new QMenu(this);
106 QMenu *const menu_help = new QMenu;
107 menu_help->setTitle(tr("&Help"));
108 menu_help->addAction(main_window.action_about());
110 menu->addAction(menu_help->menuAction());
111 menu->addSeparator();
112 menu->addAction(main_window.action_quit());
114 menu_button_.setMenu(menu);
115 menu_button_.setPopupMode(QToolButton::InstantPopup);
116 menu_button_.setIcon(QIcon::fromTheme("menu",
117 QIcon(":/icons/menu.svg")));
120 addAction(main_window.action_open());
121 addAction(main_window.action_save_as());
123 addAction(main_window.action_view_zoom_in());
124 addAction(main_window.action_view_zoom_out());
125 addAction(main_window.action_view_zoom_fit());
126 addAction(main_window.action_view_zoom_one_to_one());
128 addAction(main_window.action_view_show_cursors());
131 connect(&run_stop_button_, SIGNAL(clicked()),
132 this, SLOT(on_run_stop()));
133 connect(&sample_count_, SIGNAL(value_changed()),
134 this, SLOT(on_sample_count_changed()));
135 connect(&sample_rate_, SIGNAL(value_changed()),
136 this, SLOT(on_sample_rate_changed()));
138 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
140 set_capture_state(pv::Session::Stopped);
142 configure_button_.setIcon(QIcon::fromTheme("configure",
143 QIcon(":/icons/configure.png")));
145 channels_button_.setIcon(QIcon::fromTheme("channels",
146 QIcon(":/icons/channels.svg")));
148 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
150 addWidget(&device_selector_);
151 configure_button_action_ = addWidget(&configure_button_);
152 addWidget(&channels_button_);
153 addWidget(&sample_count_);
154 addWidget(&sample_rate_);
155 addWidget(&run_stop_button_);
158 addWidget(add_decoder_button);
161 QWidget *const spacer = new QWidget();
162 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
165 addWidget(&menu_button_);
167 sample_count_.installEventFilter(this);
168 sample_rate_.installEventFilter(this);
171 void MainBar::update_device_list()
173 DeviceManager &mgr = session_.device_manager();
174 shared_ptr<Device> selected_device = session_.device();
175 list< shared_ptr<Device> > devs;
177 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
179 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
180 devs.push_back(selected_device);
181 assert(selected_device);
183 device_selector_.set_device_list(devs, selected_device);
184 update_device_config_widgets();
188 void MainBar::set_capture_state(pv::Session::capture_state state)
190 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
191 run_stop_button_.setIcon(*icons[state]);
192 run_stop_button_.setText((state == pv::Session::Stopped) ?
193 tr("Run") : tr("Stop"));
194 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
197 void MainBar::update_sample_rate_selector()
199 Glib::VariantContainerBase gvar_dict;
201 const uint64_t *elements = NULL;
204 if (updating_sample_rate_)
207 const shared_ptr<Device> device = device_selector_.selected_device();
211 assert(!updating_sample_rate_);
212 updating_sample_rate_ = true;
214 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
215 const auto iter = keys.find(ConfigKey::SAMPLERATE);
216 if (iter != keys.end() &&
217 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
219 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
220 } catch(const sigrok::Error &e) {
221 // Failed to enunmerate samplerate
227 sample_rate_.show_none();
228 updating_sample_rate_ = false;
232 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
233 "samplerate-steps", G_VARIANT_TYPE("at"))))
235 elements = (const uint64_t *)g_variant_get_fixed_array(
236 gvar_list, &num_elements, sizeof(uint64_t));
238 const uint64_t min = elements[0];
239 const uint64_t max = elements[1];
240 const uint64_t step = elements[2];
242 g_variant_unref(gvar_list);
250 sample_rate_.show_125_list(min, max);
253 // When the step is not 1, we cam't make a 1-2-5-10
254 // list of sample rates, because we may not be able to
255 // make round numbers. Therefore in this case, show a
257 sample_rate_.show_min_max_step(min, max, step);
260 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
261 "samplerates", G_VARIANT_TYPE("at"))))
263 elements = (const uint64_t *)g_variant_get_fixed_array(
264 gvar_list, &num_elements, sizeof(uint64_t));
265 sample_rate_.show_list(elements, num_elements);
266 g_variant_unref(gvar_list);
268 updating_sample_rate_ = false;
270 update_sample_rate_selector_value();
273 void MainBar::update_sample_rate_selector_value()
275 if (updating_sample_rate_)
278 const shared_ptr<Device> device = device_selector_.selected_device();
283 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
284 uint64_t samplerate =
285 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
286 assert(!updating_sample_rate_);
287 updating_sample_rate_ = true;
288 sample_rate_.set_value(samplerate);
289 updating_sample_rate_ = false;
290 } catch (Error error) {
291 qDebug() << "WARNING: Failed to get value of sample rate";
296 void MainBar::update_sample_count_selector()
298 if (updating_sample_count_)
301 const shared_ptr<Device> device = device_selector_.selected_device();
305 assert(!updating_sample_count_);
306 updating_sample_count_ = true;
308 if (!sample_count_supported_)
310 sample_count_.show_none();
311 updating_sample_count_ = false;
315 uint64_t sample_count = sample_count_.value();
316 uint64_t min_sample_count = 0;
317 uint64_t max_sample_count = MaxSampleCount;
319 if (sample_count == 0)
320 sample_count = DefaultSampleCount;
322 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
323 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
324 if (iter != keys.end() &&
325 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
328 device->config_list(ConfigKey::LIMIT_SAMPLES);
330 g_variant_get(gvar.gobj(), "(tt)",
331 &min_sample_count, &max_sample_count);
332 } catch(const sigrok::Error &e) {
333 // Failed to query sample limit
338 min_sample_count = min(max(min_sample_count, MinSampleCount),
341 sample_count_.show_125_list(
342 min_sample_count, max_sample_count);
345 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
346 sample_count = g_variant_get_uint64(gvar.gobj());
347 if (sample_count == 0)
348 sample_count = DefaultSampleCount;
349 sample_count = min(max(sample_count, MinSampleCount),
351 } catch (Error error) {}
353 sample_count_.set_value(sample_count);
355 updating_sample_count_ = false;
358 void MainBar::update_device_config_widgets()
360 using namespace pv::popups;
362 const shared_ptr<Device> device = device_selector_.selected_device();
366 // Update the configure popup
367 DeviceOptions *const opts = new DeviceOptions(device, this);
368 configure_button_action_->setVisible(
369 !opts->binding().properties().empty());
370 configure_button_.set_popup(opts);
372 // Update the channels popup
373 Channels *const channels = new Channels(session_, this);
374 channels_button_.set_popup(channels);
376 // Update supported options.
377 sample_count_supported_ = false;
380 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
382 auto key = entry.first;
383 auto capabilities = entry.second;
385 case SR_CONF_LIMIT_SAMPLES:
386 if (capabilities.count(Capability::SET))
387 sample_count_supported_ = true;
389 case SR_CONF_LIMIT_FRAMES:
390 if (capabilities.count(Capability::SET))
392 device->config_set(ConfigKey::LIMIT_FRAMES,
393 Glib::Variant<guint64>::create(1));
401 } catch (Error error) {}
403 // Add notification of reconfigure events
404 disconnect(this, SLOT(on_config_changed()));
405 connect(&opts->binding(), SIGNAL(config_changed()),
406 this, SLOT(on_config_changed()));
408 // Update sweep timing widgets.
409 update_sample_count_selector();
410 update_sample_rate_selector();
413 void MainBar::commit_sample_count()
415 uint64_t sample_count = 0;
417 if (updating_sample_count_)
420 const shared_ptr<Device> device = device_selector_.selected_device();
424 sample_count = sample_count_.value();
426 // Set the sample count
427 assert(!updating_sample_count_);
428 updating_sample_count_ = true;
429 if (sample_count_supported_)
432 device->config_set(ConfigKey::LIMIT_SAMPLES,
433 Glib::Variant<guint64>::create(sample_count));
435 } catch (Error error) {
436 qDebug() << "Failed to configure sample count.";
440 updating_sample_count_ = false;
443 void MainBar::commit_sample_rate()
445 uint64_t sample_rate = 0;
447 if (updating_sample_rate_)
450 const shared_ptr<Device> device = device_selector_.selected_device();
454 sample_rate = sample_rate_.value();
455 if (sample_rate == 0)
458 // Set the samplerate
459 assert(!updating_sample_rate_);
460 updating_sample_rate_ = true;
462 device->config_set(ConfigKey::SAMPLERATE,
463 Glib::Variant<guint64>::create(sample_rate));
465 } catch (Error error) {
466 qDebug() << "Failed to configure samplerate.";
469 updating_sample_rate_ = false;
472 void MainBar::on_device_selected()
474 shared_ptr<Device> device = device_selector_.selected_device();
478 main_window_.select_device(device);
480 update_device_config_widgets();
483 void MainBar::on_sample_count_changed()
485 commit_sample_count();
488 void MainBar::on_sample_rate_changed()
490 commit_sample_rate();
493 void MainBar::on_run_stop()
495 commit_sample_count();
496 commit_sample_rate();
497 main_window_.run_stop();
500 void MainBar::on_config_changed()
502 commit_sample_count();
503 update_sample_count_selector();
504 commit_sample_rate();
505 update_sample_rate_selector();
508 bool MainBar::eventFilter(QObject *watched, QEvent *event)
510 if ((watched == &sample_count_ || watched == &sample_rate_) &&
511 (event->type() == QEvent::ToolTip)) {
512 double sec = (double)sample_count_.value() / sample_rate_.value();
513 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
515 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
516 QToolTip::showText(help_event->globalPos(), str);
524 } // namespace toolbars