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