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