]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
MainBar: Prevent context menu from appearing
[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);
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 const auto keys = device->config_keys(
219 ConfigKey::DEVICE_OPTIONS);
220 try {
221 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
222 } catch(const sigrok::Error &e) {
223 // Failed to enunmerate samplerate
224 (void)e;
225 }
226 }
227
228 if (!gvar_dict) {
8dbbc7f0
JH
229 sample_rate_.show_none();
230 updating_sample_rate_ = false;
dde1a563 231 return;
1198b887 232 }
dde1a563 233
e8d00928 234 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
1198b887
JH
235 "samplerate-steps", G_VARIANT_TYPE("at"))))
236 {
488f5d3f
BV
237 elements = (const uint64_t *)g_variant_get_fixed_array(
238 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
239
240 const uint64_t min = elements[0];
241 const uint64_t max = elements[1];
242 const uint64_t step = elements[2];
243
488f5d3f 244 g_variant_unref(gvar_list);
aafe53af
JH
245
246 assert(min > 0);
247 assert(max > 0);
248 assert(max > min);
249 assert(step > 0);
250
251 if (step == 1)
8dbbc7f0 252 sample_rate_.show_125_list(min, max);
aafe53af
JH
253 else
254 {
255 // When the step is not 1, we cam't make a 1-2-5-10
256 // list of sample rates, because we may not be able to
257 // make round numbers. Therefore in this case, show a
258 // spin box.
8dbbc7f0 259 sample_rate_.show_min_max_step(min, max, step);
aafe53af 260 }
dde1a563 261 }
e8d00928 262 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
488f5d3f 263 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 264 {
488f5d3f
BV
265 elements = (const uint64_t *)g_variant_get_fixed_array(
266 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 267 sample_rate_.show_list(elements, num_elements);
488f5d3f 268 g_variant_unref(gvar_list);
dde1a563 269 }
8dbbc7f0 270 updating_sample_rate_ = false;
48888313
JH
271
272 update_sample_rate_selector_value();
273}
274
7c657094 275void MainBar::update_sample_rate_selector_value()
48888313 276{
8dbbc7f0 277 if (updating_sample_rate_)
3d4f16af
JH
278 return;
279
079d39ea 280 const shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 281 if (!device)
19adbc2c
JH
282 return;
283
e8d00928
ML
284 try {
285 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
286 uint64_t samplerate =
15289d5c 287 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
288 assert(!updating_sample_rate_);
289 updating_sample_rate_ = true;
290 sample_rate_.set_value(samplerate);
291 updating_sample_rate_ = false;
e8d00928 292 } catch (Error error) {
8dd44190 293 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
294 return;
295 }
48888313
JH
296}
297
7c657094 298void MainBar::update_sample_count_selector()
85756012 299{
8dbbc7f0 300 if (updating_sample_count_)
3d4f16af
JH
301 return;
302
079d39ea 303 const shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 304 if (!device)
19adbc2c
JH
305 return;
306
8dbbc7f0
JH
307 assert(!updating_sample_count_);
308 updating_sample_count_ = true;
85756012 309
3e7636f9 310 if (!sample_count_supported_)
df3c1aa3 311 {
3e7636f9
JH
312 sample_count_.show_none();
313 updating_sample_count_ = false;
314 return;
315 }
b50ef520 316
3e7636f9
JH
317 uint64_t sample_count = sample_count_.value();
318 uint64_t min_sample_count = 0;
319 uint64_t max_sample_count = MaxSampleCount;
3cbffdcf 320
3e7636f9
JH
321 if (sample_count == 0)
322 sample_count = DefaultSampleCount;
b50ef520 323
ed1d9d81
JH
324 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
325 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
326 if (iter != keys.end() &&
327 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
6454b1e9
JH
328 try {
329 auto gvar =
330 device->config_list(ConfigKey::LIMIT_SAMPLES);
331 if (gvar)
332 g_variant_get(gvar.gobj(), "(tt)",
333 &min_sample_count, &max_sample_count);
334 } catch(const sigrok::Error &e) {
335 // Failed to query sample limit
336 (void)e;
337 }
ed1d9d81 338 }
1d04852f 339
3e7636f9
JH
340 min_sample_count = min(max(min_sample_count, MinSampleCount),
341 max_sample_count);
b50ef520 342
3e7636f9
JH
343 sample_count_.show_125_list(
344 min_sample_count, max_sample_count);
b50ef520 345
3e7636f9
JH
346 try {
347 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
348 sample_count = g_variant_get_uint64(gvar.gobj());
349 if (sample_count == 0)
350 sample_count = DefaultSampleCount;
351 sample_count = min(max(sample_count, MinSampleCount),
352 max_sample_count);
353 } catch (Error error) {}
354
355 sample_count_.set_value(sample_count);
df3c1aa3 356
8dbbc7f0 357 updating_sample_count_ = false;
85756012
ML
358}
359
7c657094 360void MainBar::update_device_config_widgets()
e95e8563 361{
e95e8563
JH
362 using namespace pv::popups;
363
079d39ea 364 const shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 365 if (!device)
e95e8563
JH
366 return;
367
368 // Update the configure popup
e8d00928 369 DeviceOptions *const opts = new DeviceOptions(device, this);
8dbbc7f0 370 configure_button_action_->setVisible(
e95e8563 371 !opts->binding().properties().empty());
8dbbc7f0 372 configure_button_.set_popup(opts);
e95e8563 373
6ac6242b 374 // Update the channels popup
8dbbc7f0
JH
375 Channels *const channels = new Channels(session_, this);
376 channels_button_.set_popup(channels);
e95e8563
JH
377
378 // Update supported options.
8dbbc7f0 379 sample_count_supported_ = false;
e95e8563 380
e8d00928
ML
381 try {
382 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
e95e8563 383 {
e8d00928
ML
384 auto key = entry.first;
385 auto capabilities = entry.second;
386 switch (key->id()) {
e95e8563 387 case SR_CONF_LIMIT_SAMPLES:
e8d00928 388 if (capabilities.count(Capability::SET))
8dbbc7f0 389 sample_count_supported_ = true;
e95e8563
JH
390 break;
391 case SR_CONF_LIMIT_FRAMES:
e8d00928
ML
392 if (capabilities.count(Capability::SET))
393 {
394 device->config_set(ConfigKey::LIMIT_FRAMES,
15289d5c 395 Glib::Variant<guint64>::create(1));
e8d00928
ML
396 on_config_changed();
397 }
398 break;
399 default:
e95e8563
JH
400 break;
401 }
402 }
e8d00928 403 } catch (Error error) {}
e95e8563
JH
404
405 // Add notification of reconfigure events
406 disconnect(this, SLOT(on_config_changed()));
e8d00928 407 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
408 this, SLOT(on_config_changed()));
409
410 // Update sweep timing widgets.
411 update_sample_count_selector();
412 update_sample_rate_selector();
413}
414
7c657094 415void MainBar::commit_sample_count()
124d97de
ML
416{
417 uint64_t sample_count = 0;
418
8dbbc7f0 419 if (updating_sample_count_)
3d4f16af
JH
420 return;
421
079d39ea 422 const shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 423 if (!device)
19adbc2c
JH
424 return;
425
8dbbc7f0 426 sample_count = sample_count_.value();
124d97de
ML
427
428 // Set the sample count
8dbbc7f0
JH
429 assert(!updating_sample_count_);
430 updating_sample_count_ = true;
431 if (sample_count_supported_)
e8d00928
ML
432 {
433 try {
434 device->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 435 Glib::Variant<guint64>::create(sample_count));
e8d00928
ML
436 on_config_changed();
437 } catch (Error error) {
438 qDebug() << "Failed to configure sample count.";
439 return;
440 }
124d97de 441 }
8dbbc7f0 442 updating_sample_count_ = false;
124d97de
ML
443}
444
7c657094 445void MainBar::commit_sample_rate()
48888313
JH
446{
447 uint64_t sample_rate = 0;
448
8dbbc7f0 449 if (updating_sample_rate_)
3d4f16af
JH
450 return;
451
079d39ea 452 const shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 453 if (!device)
19adbc2c
JH
454 return;
455
8dbbc7f0 456 sample_rate = sample_rate_.value();
f9541bde
JH
457 if (sample_rate == 0)
458 return;
459
48888313 460 // Set the samplerate
8dbbc7f0
JH
461 assert(!updating_sample_rate_);
462 updating_sample_rate_ = true;
e8d00928
ML
463 try {
464 device->config_set(ConfigKey::SAMPLERATE,
15289d5c 465 Glib::Variant<guint64>::create(sample_rate));
e8d00928
ML
466 on_config_changed();
467 } catch (Error error) {
48888313
JH
468 qDebug() << "Failed to configure samplerate.";
469 return;
470 }
8dbbc7f0 471 updating_sample_rate_ = false;
dde1a563
JH
472}
473
7c657094 474void MainBar::on_device_selected()
dde1a563 475{
079d39ea 476 shared_ptr<Device> device = device_selector_.selected_device();
e8d00928 477 if (!device)
19adbc2c
JH
478 return;
479
51cf49fe 480 main_window_.select_device(device);
51d4a9ab 481
e95e8563 482 update_device_config_widgets();
dde1a563 483}
51e77110 484
7c657094 485void MainBar::on_sample_count_changed()
124d97de 486{
3d4f16af 487 commit_sample_count();
124d97de
ML
488}
489
7c657094 490void MainBar::on_sample_rate_changed()
48888313 491{
3d4f16af 492 commit_sample_rate();
48888313
JH
493}
494
7c657094 495void MainBar::on_run_stop()
9f3d12f3 496{
b50ef520 497 commit_sample_count();
9f3d12f3 498 commit_sample_rate();
4e7f5ba8 499 main_window_.run_stop();
9f3d12f3
JH
500}
501
7c657094 502void MainBar::on_config_changed()
82afd5e3
JH
503{
504 commit_sample_count();
505 update_sample_count_selector();
506 commit_sample_rate();
507 update_sample_rate_selector();
508}
509
7c657094 510bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 511{
8dbbc7f0 512 if ((watched == &sample_count_ || watched == &sample_rate_) &&
40065ab6 513 (event->type() == QEvent::ToolTip)) {
8dbbc7f0 514 double sec = (double)sample_count_.value() / sample_rate_.value();
40065ab6 515 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45
JS
516
517 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
40065ab6
JS
518 QToolTip::showText(help_event->globalPos(), str);
519
520 return true;
521 }
522
523 return false;
524}
525
f4c92e1c 526} // namespace toolbars
51e77110 527} // namespace pv