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