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