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