]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
MainWindow: Made QActions into member variables
[pulseview.git] / pv / toolbars / mainbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
40065ab6
JS
27#include <QHelpEvent>
28#include <QToolTip>
dde1a563 29
7c657094 30#include "mainbar.hpp"
d4984fe7 31
2acdb232 32#include <pv/devicemanager.hpp>
4e7f5ba8 33#include <pv/mainwindow.hpp>
2acdb232
JH
34#include <pv/popups/deviceoptions.hpp>
35#include <pv/popups/channels.hpp>
36#include <pv/util.hpp>
cdb50f67 37
e8d00928
ML
38#include <libsigrok/libsigrok.hpp>
39
19adbc2c 40using std::map;
e8d00928 41using std::vector;
b50ef520
JH
42using std::max;
43using std::min;
f9abf97e 44using std::shared_ptr;
819f4c25 45using std::string;
dd63af74 46
e8d00928
ML
47using sigrok::Capability;
48using sigrok::ConfigKey;
49using sigrok::Device;
50using sigrok::Error;
51
51e77110 52namespace pv {
f4c92e1c 53namespace toolbars {
51e77110 54
7c657094
JH
55const uint64_t MainBar::MinSampleCount = 100ULL;
56const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
57const uint64_t MainBar::DefaultSampleCount = 1000000;
09f5d123 58
7c657094 59MainBar::MainBar(Session &session, MainWindow &main_window) :
4e7f5ba8 60 QToolBar("Sampling Bar", &main_window),
8dbbc7f0 61 session_(session),
4e7f5ba8 62 main_window_(main_window),
8dbbc7f0
JH
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)
6fb67b27 77{
7c657094 78 setObjectName(QString::fromUtf8("MainBar"));
0c7cdea2 79
8dbbc7f0 80 connect(&run_stop_button_, SIGNAL(clicked()),
9f3d12f3 81 this, SLOT(on_run_stop()));
8dbbc7f0 82 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
dde1a563 83 this, SLOT(on_device_selected()));
8dbbc7f0 84 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 85 this, SLOT(on_sample_count_changed()));
8dbbc7f0 86 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 87 this, SLOT(on_sample_rate_changed()));
dde1a563 88
8dbbc7f0 89 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 90
2b81ae46 91 set_capture_state(pv::Session::Stopped);
274d4f13 92
8dbbc7f0 93 configure_button_.setIcon(QIcon::fromTheme("configure",
688ef645 94 QIcon(":/icons/configure.png")));
b7b659aa 95
8dbbc7f0 96 channels_button_.setIcon(QIcon::fromTheme("channels",
6ac6242b 97 QIcon(":/icons/channels.svg")));
cdb50f67 98
8dbbc7f0 99 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
f5798068 100
8dbbc7f0
JH
101 addWidget(&device_selector_);
102 configure_button_action_ = addWidget(&configure_button_);
103 addWidget(&channels_button_);
104 addWidget(&sample_count_);
105 addWidget(&sample_rate_);
6fb67b27 106
8dbbc7f0 107 addWidget(&run_stop_button_);
40065ab6 108
8dbbc7f0
JH
109 sample_count_.installEventFilter(this);
110 sample_rate_.installEventFilter(this);
6fb67b27
JH
111}
112
7c657094 113void MainBar::set_device_list(
53a7cce4 114 const std::list< std::shared_ptr<sigrok::Device> > &devices,
e8d00928 115 shared_ptr<Device> selected)
18203d86 116{
e95e8563
JH
117 int selected_index = -1;
118
119 assert(selected);
120
8dbbc7f0 121 updating_device_selector_ = true;
95237c18 122
8dbbc7f0 123 device_selector_.clear();
18203d86 124
53a7cce4 125 for (auto device : devices) {
e8d00928
ML
126 assert(device);
127
53a7cce4
SA
128 string display_name =
129 session_.device_manager().get_display_name(device);
130
e8d00928 131 if (selected == device)
8dbbc7f0 132 selected_index = device_selector_.count();
e95e8563 133
8dbbc7f0 134 device_selector_.addItem(display_name.c_str(),
e8d00928 135 qVariantFromValue(device));
18203d86
JH
136 }
137
e95e8563
JH
138 // The selected device should have been in the list
139 assert(selected_index != -1);
8dbbc7f0 140 device_selector_.setCurrentIndex(selected_index);
e95e8563
JH
141
142 update_device_config_widgets();
143
8dbbc7f0 144 updating_device_selector_ = false;
18203d86
JH
145}
146
7c657094 147shared_ptr<Device> MainBar::get_selected_device() const
d4984fe7 148{
8dbbc7f0 149 const int index = device_selector_.currentIndex();
333d5bbc 150 if (index < 0)
e8d00928 151 return shared_ptr<Device>();
6fb67b27 152
8dbbc7f0 153 return device_selector_.itemData(index).value<shared_ptr<Device>>();
6fb67b27
JH
154}
155
7c657094 156void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 157{
8dbbc7f0
JH
158 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
159 run_stop_button_.setIcon(*icons[state]);
2b81ae46 160 run_stop_button_.setText((state == pv::Session::Stopped) ?
2b49eeb0 161 tr("Run") : tr("Stop"));
8dbbc7f0 162 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
6ac96c2e
JH
163}
164
7c657094 165void MainBar::update_sample_rate_selector()
dde1a563 166{
e8d00928
ML
167 Glib::VariantContainerBase gvar_dict;
168 GVariant *gvar_list;
488f5d3f
BV
169 const uint64_t *elements = NULL;
170 gsize num_elements;
dde1a563 171
8dbbc7f0 172 if (updating_sample_rate_)
3d4f16af
JH
173 return;
174
e8d00928
ML
175 const shared_ptr<Device> device = get_selected_device();
176 if (!device)
ef4d0201
JH
177 return;
178
8dbbc7f0
JH
179 assert(!updating_sample_rate_);
180 updating_sample_rate_ = true;
1198b887 181
ed1d9d81
JH
182 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
183 const auto iter = keys.find(ConfigKey::SAMPLERATE);
184 if (iter != keys.end() &&
185 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
c2db8ec3
JH
186 const auto keys = device->config_keys(
187 ConfigKey::DEVICE_OPTIONS);
188 try {
189 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
190 } catch(const sigrok::Error &e) {
191 // Failed to enunmerate samplerate
192 (void)e;
193 }
194 }
195
196 if (!gvar_dict) {
8dbbc7f0
JH
197 sample_rate_.show_none();
198 updating_sample_rate_ = false;
dde1a563 199 return;
1198b887 200 }
dde1a563 201
e8d00928 202 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
1198b887
JH
203 "samplerate-steps", G_VARIANT_TYPE("at"))))
204 {
488f5d3f
BV
205 elements = (const uint64_t *)g_variant_get_fixed_array(
206 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
207
208 const uint64_t min = elements[0];
209 const uint64_t max = elements[1];
210 const uint64_t step = elements[2];
211
488f5d3f 212 g_variant_unref(gvar_list);
aafe53af
JH
213
214 assert(min > 0);
215 assert(max > 0);
216 assert(max > min);
217 assert(step > 0);
218
219 if (step == 1)
8dbbc7f0 220 sample_rate_.show_125_list(min, max);
aafe53af
JH
221 else
222 {
223 // When the step is not 1, we cam't make a 1-2-5-10
224 // list of sample rates, because we may not be able to
225 // make round numbers. Therefore in this case, show a
226 // spin box.
8dbbc7f0 227 sample_rate_.show_min_max_step(min, max, step);
aafe53af 228 }
dde1a563 229 }
e8d00928 230 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
488f5d3f 231 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 232 {
488f5d3f
BV
233 elements = (const uint64_t *)g_variant_get_fixed_array(
234 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 235 sample_rate_.show_list(elements, num_elements);
488f5d3f 236 g_variant_unref(gvar_list);
dde1a563 237 }
8dbbc7f0 238 updating_sample_rate_ = false;
48888313
JH
239
240 update_sample_rate_selector_value();
241}
242
7c657094 243void MainBar::update_sample_rate_selector_value()
48888313 244{
8dbbc7f0 245 if (updating_sample_rate_)
3d4f16af
JH
246 return;
247
e8d00928
ML
248 const shared_ptr<Device> device = get_selected_device();
249 if (!device)
19adbc2c
JH
250 return;
251
e8d00928
ML
252 try {
253 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
254 uint64_t samplerate =
15289d5c 255 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
256 assert(!updating_sample_rate_);
257 updating_sample_rate_ = true;
258 sample_rate_.set_value(samplerate);
259 updating_sample_rate_ = false;
e8d00928 260 } catch (Error error) {
8dd44190 261 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
262 return;
263 }
48888313
JH
264}
265
7c657094 266void MainBar::update_sample_count_selector()
85756012 267{
8dbbc7f0 268 if (updating_sample_count_)
3d4f16af
JH
269 return;
270
e8d00928
ML
271 const shared_ptr<Device> device = get_selected_device();
272 if (!device)
19adbc2c
JH
273 return;
274
8dbbc7f0
JH
275 assert(!updating_sample_count_);
276 updating_sample_count_ = true;
85756012 277
3e7636f9 278 if (!sample_count_supported_)
df3c1aa3 279 {
3e7636f9
JH
280 sample_count_.show_none();
281 updating_sample_count_ = false;
282 return;
283 }
b50ef520 284
3e7636f9
JH
285 uint64_t sample_count = sample_count_.value();
286 uint64_t min_sample_count = 0;
287 uint64_t max_sample_count = MaxSampleCount;
3cbffdcf 288
3e7636f9
JH
289 if (sample_count == 0)
290 sample_count = DefaultSampleCount;
b50ef520 291
ed1d9d81
JH
292 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
293 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
294 if (iter != keys.end() &&
295 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
6454b1e9
JH
296 try {
297 auto gvar =
298 device->config_list(ConfigKey::LIMIT_SAMPLES);
299 if (gvar)
300 g_variant_get(gvar.gobj(), "(tt)",
301 &min_sample_count, &max_sample_count);
302 } catch(const sigrok::Error &e) {
303 // Failed to query sample limit
304 (void)e;
305 }
ed1d9d81 306 }
1d04852f 307
3e7636f9
JH
308 min_sample_count = min(max(min_sample_count, MinSampleCount),
309 max_sample_count);
b50ef520 310
3e7636f9
JH
311 sample_count_.show_125_list(
312 min_sample_count, max_sample_count);
b50ef520 313
3e7636f9
JH
314 try {
315 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
316 sample_count = g_variant_get_uint64(gvar.gobj());
317 if (sample_count == 0)
318 sample_count = DefaultSampleCount;
319 sample_count = min(max(sample_count, MinSampleCount),
320 max_sample_count);
321 } catch (Error error) {}
322
323 sample_count_.set_value(sample_count);
df3c1aa3 324
8dbbc7f0 325 updating_sample_count_ = false;
85756012
ML
326}
327
7c657094 328void MainBar::update_device_config_widgets()
e95e8563 329{
e95e8563
JH
330 using namespace pv::popups;
331
e8d00928
ML
332 const shared_ptr<Device> device = get_selected_device();
333 if (!device)
e95e8563
JH
334 return;
335
336 // Update the configure popup
e8d00928 337 DeviceOptions *const opts = new DeviceOptions(device, this);
8dbbc7f0 338 configure_button_action_->setVisible(
e95e8563 339 !opts->binding().properties().empty());
8dbbc7f0 340 configure_button_.set_popup(opts);
e95e8563 341
6ac6242b 342 // Update the channels popup
8dbbc7f0
JH
343 Channels *const channels = new Channels(session_, this);
344 channels_button_.set_popup(channels);
e95e8563
JH
345
346 // Update supported options.
8dbbc7f0 347 sample_count_supported_ = false;
e95e8563 348
e8d00928
ML
349 try {
350 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
e95e8563 351 {
e8d00928
ML
352 auto key = entry.first;
353 auto capabilities = entry.second;
354 switch (key->id()) {
e95e8563 355 case SR_CONF_LIMIT_SAMPLES:
e8d00928 356 if (capabilities.count(Capability::SET))
8dbbc7f0 357 sample_count_supported_ = true;
e95e8563
JH
358 break;
359 case SR_CONF_LIMIT_FRAMES:
e8d00928
ML
360 if (capabilities.count(Capability::SET))
361 {
362 device->config_set(ConfigKey::LIMIT_FRAMES,
15289d5c 363 Glib::Variant<guint64>::create(1));
e8d00928
ML
364 on_config_changed();
365 }
366 break;
367 default:
e95e8563
JH
368 break;
369 }
370 }
e8d00928 371 } catch (Error error) {}
e95e8563
JH
372
373 // Add notification of reconfigure events
374 disconnect(this, SLOT(on_config_changed()));
e8d00928 375 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
376 this, SLOT(on_config_changed()));
377
378 // Update sweep timing widgets.
379 update_sample_count_selector();
380 update_sample_rate_selector();
381}
382
7c657094 383void MainBar::commit_sample_count()
124d97de
ML
384{
385 uint64_t sample_count = 0;
386
8dbbc7f0 387 if (updating_sample_count_)
3d4f16af
JH
388 return;
389
e8d00928
ML
390 const shared_ptr<Device> device = get_selected_device();
391 if (!device)
19adbc2c
JH
392 return;
393
8dbbc7f0 394 sample_count = sample_count_.value();
124d97de
ML
395
396 // Set the sample count
8dbbc7f0
JH
397 assert(!updating_sample_count_);
398 updating_sample_count_ = true;
399 if (sample_count_supported_)
e8d00928
ML
400 {
401 try {
402 device->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 403 Glib::Variant<guint64>::create(sample_count));
e8d00928
ML
404 on_config_changed();
405 } catch (Error error) {
406 qDebug() << "Failed to configure sample count.";
407 return;
408 }
124d97de 409 }
8dbbc7f0 410 updating_sample_count_ = false;
124d97de
ML
411}
412
7c657094 413void MainBar::commit_sample_rate()
48888313
JH
414{
415 uint64_t sample_rate = 0;
416
8dbbc7f0 417 if (updating_sample_rate_)
3d4f16af
JH
418 return;
419
e8d00928
ML
420 const shared_ptr<Device> device = get_selected_device();
421 if (!device)
19adbc2c
JH
422 return;
423
8dbbc7f0 424 sample_rate = sample_rate_.value();
f9541bde
JH
425 if (sample_rate == 0)
426 return;
427
48888313 428 // Set the samplerate
8dbbc7f0
JH
429 assert(!updating_sample_rate_);
430 updating_sample_rate_ = true;
e8d00928
ML
431 try {
432 device->config_set(ConfigKey::SAMPLERATE,
15289d5c 433 Glib::Variant<guint64>::create(sample_rate));
e8d00928
ML
434 on_config_changed();
435 } catch (Error error) {
48888313
JH
436 qDebug() << "Failed to configure samplerate.";
437 return;
438 }
8dbbc7f0 439 updating_sample_rate_ = false;
dde1a563
JH
440}
441
7c657094 442void MainBar::on_device_selected()
dde1a563 443{
8dbbc7f0 444 if (updating_device_selector_)
95237c18
JH
445 return;
446
e8d00928
ML
447 shared_ptr<Device> device = get_selected_device();
448 if (!device)
19adbc2c
JH
449 return;
450
51cf49fe 451 main_window_.select_device(device);
51d4a9ab 452
e95e8563 453 update_device_config_widgets();
dde1a563 454}
51e77110 455
7c657094 456void MainBar::on_sample_count_changed()
124d97de 457{
3d4f16af 458 commit_sample_count();
124d97de
ML
459}
460
7c657094 461void MainBar::on_sample_rate_changed()
48888313 462{
3d4f16af 463 commit_sample_rate();
48888313
JH
464}
465
7c657094 466void MainBar::on_run_stop()
9f3d12f3 467{
b50ef520 468 commit_sample_count();
9f3d12f3 469 commit_sample_rate();
4e7f5ba8 470 main_window_.run_stop();
9f3d12f3
JH
471}
472
7c657094 473void MainBar::on_config_changed()
82afd5e3
JH
474{
475 commit_sample_count();
476 update_sample_count_selector();
477 commit_sample_rate();
478 update_sample_rate_selector();
479}
480
7c657094 481bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 482{
8dbbc7f0 483 if ((watched == &sample_count_ || watched == &sample_rate_) &&
40065ab6 484 (event->type() == QEvent::ToolTip)) {
8dbbc7f0 485 double sec = (double)sample_count_.value() / sample_rate_.value();
40065ab6 486 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45
JS
487
488 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
40065ab6
JS
489 QToolTip::showText(help_event->globalPos(), str);
490
491 return true;
492 }
493
494 return false;
495}
496
f4c92e1c 497} // namespace toolbars
51e77110 498} // namespace pv