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
30 #include "mainbar.hpp"
32 #include <pv/devicemanager.hpp>
33 #include <pv/mainwindow.hpp>
34 #include <pv/popups/deviceoptions.hpp>
35 #include <pv/popups/channels.hpp>
36 #include <pv/util.hpp>
38 #include <libsigrok/libsigrok.hpp>
44 using std::shared_ptr;
47 using sigrok::Capability;
48 using sigrok::ConfigKey;
55 const uint64_t MainBar::MinSampleCount = 100ULL;
56 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
57 const uint64_t MainBar::DefaultSampleCount = 1000000;
59 MainBar::MainBar(Session &session, MainWindow &main_window) :
60 QToolBar("Sampling Bar", &main_window),
62 main_window_(main_window),
63 device_selector_(this),
64 updating_device_selector_(false),
65 configure_button_(this),
66 configure_button_action_(NULL),
67 channels_button_(this),
68 sample_count_(" samples", this),
69 sample_rate_("Hz", this),
70 updating_sample_rate_(false),
71 updating_sample_count_(false),
72 sample_count_supported_(false),
73 icon_red_(":/icons/status-red.svg"),
74 icon_green_(":/icons/status-green.svg"),
75 icon_grey_(":/icons/status-grey.svg"),
76 run_stop_button_(this)
78 setObjectName(QString::fromUtf8("MainBar"));
83 connect(&run_stop_button_, SIGNAL(clicked()),
84 this, SLOT(on_run_stop()));
85 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
86 this, SLOT(on_device_selected()));
87 connect(&sample_count_, SIGNAL(value_changed()),
88 this, SLOT(on_sample_count_changed()));
89 connect(&sample_rate_, SIGNAL(value_changed()),
90 this, SLOT(on_sample_rate_changed()));
92 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
94 set_capture_state(pv::Session::Stopped);
96 configure_button_.setIcon(QIcon::fromTheme("configure",
97 QIcon(":/icons/configure.png")));
99 channels_button_.setIcon(QIcon::fromTheme("channels",
100 QIcon(":/icons/channels.svg")));
102 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
104 addWidget(&device_selector_);
105 configure_button_action_ = addWidget(&configure_button_);
106 addWidget(&channels_button_);
107 addWidget(&sample_count_);
108 addWidget(&sample_rate_);
110 addWidget(&run_stop_button_);
112 sample_count_.installEventFilter(this);
113 sample_rate_.installEventFilter(this);
116 void MainBar::set_device_list(
117 const std::list< std::shared_ptr<sigrok::Device> > &devices,
118 shared_ptr<Device> selected)
120 int selected_index = -1;
124 updating_device_selector_ = true;
126 device_selector_.clear();
128 for (auto device : devices) {
131 string display_name =
132 session_.device_manager().get_display_name(device);
134 if (selected == device)
135 selected_index = device_selector_.count();
137 device_selector_.addItem(display_name.c_str(),
138 qVariantFromValue(device));
141 // The selected device should have been in the list
142 assert(selected_index != -1);
143 device_selector_.setCurrentIndex(selected_index);
145 update_device_config_widgets();
147 updating_device_selector_ = false;
150 shared_ptr<Device> MainBar::get_selected_device() const
152 const int index = device_selector_.currentIndex();
154 return shared_ptr<Device>();
156 return device_selector_.itemData(index).value<shared_ptr<Device>>();
159 void MainBar::set_capture_state(pv::Session::capture_state state)
161 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
162 run_stop_button_.setIcon(*icons[state]);
163 run_stop_button_.setText((state == pv::Session::Stopped) ?
164 tr("Run") : tr("Stop"));
165 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
168 void MainBar::update_sample_rate_selector()
170 Glib::VariantContainerBase gvar_dict;
172 const uint64_t *elements = NULL;
175 if (updating_sample_rate_)
178 const shared_ptr<Device> device = get_selected_device();
182 assert(!updating_sample_rate_);
183 updating_sample_rate_ = true;
185 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
186 const auto iter = keys.find(ConfigKey::SAMPLERATE);
187 if (iter != keys.end() &&
188 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
189 const auto keys = device->config_keys(
190 ConfigKey::DEVICE_OPTIONS);
192 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
193 } catch(const sigrok::Error &e) {
194 // Failed to enunmerate samplerate
200 sample_rate_.show_none();
201 updating_sample_rate_ = false;
205 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
206 "samplerate-steps", G_VARIANT_TYPE("at"))))
208 elements = (const uint64_t *)g_variant_get_fixed_array(
209 gvar_list, &num_elements, sizeof(uint64_t));
211 const uint64_t min = elements[0];
212 const uint64_t max = elements[1];
213 const uint64_t step = elements[2];
215 g_variant_unref(gvar_list);
223 sample_rate_.show_125_list(min, max);
226 // When the step is not 1, we cam't make a 1-2-5-10
227 // list of sample rates, because we may not be able to
228 // make round numbers. Therefore in this case, show a
230 sample_rate_.show_min_max_step(min, max, step);
233 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
234 "samplerates", G_VARIANT_TYPE("at"))))
236 elements = (const uint64_t *)g_variant_get_fixed_array(
237 gvar_list, &num_elements, sizeof(uint64_t));
238 sample_rate_.show_list(elements, num_elements);
239 g_variant_unref(gvar_list);
241 updating_sample_rate_ = false;
243 update_sample_rate_selector_value();
246 void MainBar::update_sample_rate_selector_value()
248 if (updating_sample_rate_)
251 const shared_ptr<Device> device = get_selected_device();
256 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
257 uint64_t samplerate =
258 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
259 assert(!updating_sample_rate_);
260 updating_sample_rate_ = true;
261 sample_rate_.set_value(samplerate);
262 updating_sample_rate_ = false;
263 } catch (Error error) {
264 qDebug() << "WARNING: Failed to get value of sample rate";
269 void MainBar::update_sample_count_selector()
271 if (updating_sample_count_)
274 const shared_ptr<Device> device = get_selected_device();
278 assert(!updating_sample_count_);
279 updating_sample_count_ = true;
281 if (!sample_count_supported_)
283 sample_count_.show_none();
284 updating_sample_count_ = false;
288 uint64_t sample_count = sample_count_.value();
289 uint64_t min_sample_count = 0;
290 uint64_t max_sample_count = MaxSampleCount;
292 if (sample_count == 0)
293 sample_count = DefaultSampleCount;
295 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
296 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
297 if (iter != keys.end() &&
298 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
301 device->config_list(ConfigKey::LIMIT_SAMPLES);
303 g_variant_get(gvar.gobj(), "(tt)",
304 &min_sample_count, &max_sample_count);
305 } catch(const sigrok::Error &e) {
306 // Failed to query sample limit
311 min_sample_count = min(max(min_sample_count, MinSampleCount),
314 sample_count_.show_125_list(
315 min_sample_count, max_sample_count);
318 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
319 sample_count = g_variant_get_uint64(gvar.gobj());
320 if (sample_count == 0)
321 sample_count = DefaultSampleCount;
322 sample_count = min(max(sample_count, MinSampleCount),
324 } catch (Error error) {}
326 sample_count_.set_value(sample_count);
328 updating_sample_count_ = false;
331 void MainBar::update_device_config_widgets()
333 using namespace pv::popups;
335 const shared_ptr<Device> device = get_selected_device();
339 // Update the configure popup
340 DeviceOptions *const opts = new DeviceOptions(device, this);
341 configure_button_action_->setVisible(
342 !opts->binding().properties().empty());
343 configure_button_.set_popup(opts);
345 // Update the channels popup
346 Channels *const channels = new Channels(session_, this);
347 channels_button_.set_popup(channels);
349 // Update supported options.
350 sample_count_supported_ = false;
353 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
355 auto key = entry.first;
356 auto capabilities = entry.second;
358 case SR_CONF_LIMIT_SAMPLES:
359 if (capabilities.count(Capability::SET))
360 sample_count_supported_ = true;
362 case SR_CONF_LIMIT_FRAMES:
363 if (capabilities.count(Capability::SET))
365 device->config_set(ConfigKey::LIMIT_FRAMES,
366 Glib::Variant<guint64>::create(1));
374 } catch (Error error) {}
376 // Add notification of reconfigure events
377 disconnect(this, SLOT(on_config_changed()));
378 connect(&opts->binding(), SIGNAL(config_changed()),
379 this, SLOT(on_config_changed()));
381 // Update sweep timing widgets.
382 update_sample_count_selector();
383 update_sample_rate_selector();
386 void MainBar::commit_sample_count()
388 uint64_t sample_count = 0;
390 if (updating_sample_count_)
393 const shared_ptr<Device> device = get_selected_device();
397 sample_count = sample_count_.value();
399 // Set the sample count
400 assert(!updating_sample_count_);
401 updating_sample_count_ = true;
402 if (sample_count_supported_)
405 device->config_set(ConfigKey::LIMIT_SAMPLES,
406 Glib::Variant<guint64>::create(sample_count));
408 } catch (Error error) {
409 qDebug() << "Failed to configure sample count.";
413 updating_sample_count_ = false;
416 void MainBar::commit_sample_rate()
418 uint64_t sample_rate = 0;
420 if (updating_sample_rate_)
423 const shared_ptr<Device> device = get_selected_device();
427 sample_rate = sample_rate_.value();
428 if (sample_rate == 0)
431 // Set the samplerate
432 assert(!updating_sample_rate_);
433 updating_sample_rate_ = true;
435 device->config_set(ConfigKey::SAMPLERATE,
436 Glib::Variant<guint64>::create(sample_rate));
438 } catch (Error error) {
439 qDebug() << "Failed to configure samplerate.";
442 updating_sample_rate_ = false;
445 void MainBar::on_device_selected()
447 if (updating_device_selector_)
450 shared_ptr<Device> device = get_selected_device();
454 main_window_.select_device(device);
456 update_device_config_widgets();
459 void MainBar::on_sample_count_changed()
461 commit_sample_count();
464 void MainBar::on_sample_rate_changed()
466 commit_sample_rate();
469 void MainBar::on_run_stop()
471 commit_sample_count();
472 commit_sample_rate();
473 main_window_.run_stop();
476 void MainBar::on_config_changed()
478 commit_sample_count();
479 update_sample_count_selector();
480 commit_sample_rate();
481 update_sample_rate_selector();
484 bool MainBar::eventFilter(QObject *watched, QEvent *event)
486 if ((watched == &sample_count_ || watched == &sample_rate_) &&
487 (event->type() == QEvent::ToolTip)) {
488 double sec = (double)sample_count_.value() / sample_rate_.value();
489 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
491 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
492 QToolTip::showText(help_event->globalPos(), str);
500 } // namespace toolbars