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