]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
Introduce math signals
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
d4984fe7
JH
18 */
19
09f5d123 20#include <extdef.h>
dde1a563 21
079d39ea
JH
22#include <algorithm>
23#include <cassert>
215f9499 24
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
0f8f8c18 27#include <QFileDialog>
40065ab6 28#include <QHelpEvent>
87f0df9b 29#include <QMenu>
0f8f8c18
SA
30#include <QMessageBox>
31#include <QSettings>
40065ab6 32#include <QToolTip>
dde1a563 33
7c657094 34#include "mainbar.hpp"
d4984fe7 35
0f8f8c18
SA
36#include <boost/algorithm/string/join.hpp>
37
b0773a8a 38#include <pv/data/mathsignal.hpp>
2acdb232 39#include <pv/devicemanager.hpp>
da30ecb7 40#include <pv/devices/hardwaredevice.hpp>
0f8f8c18
SA
41#include <pv/devices/inputfile.hpp>
42#include <pv/devices/sessionfile.hpp>
43#include <pv/dialogs/connect.hpp>
44#include <pv/dialogs/inputoutputoptions.hpp>
45#include <pv/dialogs/storeprogress.hpp>
4e7f5ba8 46#include <pv/mainwindow.hpp>
2acdb232 47#include <pv/popups/channels.hpp>
aca9aa83 48#include <pv/popups/deviceoptions.hpp>
2acdb232 49#include <pv/util.hpp>
1573bf16 50#include <pv/views/trace/view.hpp>
998b89fd 51#include <pv/widgets/exportmenu.hpp>
ed43ef2e 52#include <pv/widgets/importmenu.hpp>
0f8f8c18 53#ifdef ENABLE_DECODE
13fe60f5 54#include <pv/data/decodesignal.hpp>
0f8f8c18 55#endif
cdb50f67 56
fe3a1c21 57#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 58
079d39ea
JH
59using std::back_inserter;
60using std::copy;
61using std::list;
6f925ba9 62using std::make_pair;
b0773a8a 63using std::make_shared;
19adbc2c 64using std::map;
b50ef520
JH
65using std::max;
66using std::min;
6f925ba9
UH
67using std::pair;
68using std::set;
f9abf97e 69using std::shared_ptr;
819f4c25 70using std::string;
079d39ea 71using std::vector;
dd63af74 72
e8d00928
ML
73using sigrok::Capability;
74using sigrok::ConfigKey;
e8d00928 75using sigrok::Error;
ed43ef2e 76using sigrok::InputFormat;
0f8f8c18
SA
77using sigrok::OutputFormat;
78
79using boost::algorithm::join;
e8d00928 80
51e77110 81namespace pv {
f4c92e1c 82namespace toolbars {
51e77110 83
7c657094
JH
84const uint64_t MainBar::MinSampleCount = 100ULL;
85const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
86const uint64_t MainBar::DefaultSampleCount = 1000000;
09f5d123 87
0f8f8c18
SA
88const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
89const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
90
83af3ec1 91MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) :
e0ba4f6f 92 StandardBar(session, parent, view, false),
c9da5118 93 action_new_view_(new QAction(this)),
0f8f8c18 94 action_open_(new QAction(this)),
2cd6be62 95 action_save_(new QAction(this)),
0f8f8c18
SA
96 action_save_as_(new QAction(this)),
97 action_save_selection_as_(new QAction(this)),
323715c4
SA
98 action_restore_setup_(new QAction(this)),
99 action_save_setup_(new QAction(this)),
0f8f8c18 100 action_connect_(new QAction(this)),
baf867ed 101 new_view_button_(new QToolButton()),
e0ba4f6f
SA
102 open_button_(new QToolButton()),
103 save_button_(new QToolButton()),
83af3ec1 104 device_selector_(parent, session.device_manager(), action_connect_),
8dbbc7f0 105 configure_button_(this),
4c60462b 106 configure_button_action_(nullptr),
8dbbc7f0 107 channels_button_(this),
9dd88889 108 channels_button_action_(nullptr),
8dbbc7f0
JH
109 sample_count_(" samples", this),
110 sample_rate_("Hz", this),
111 updating_sample_rate_(false),
112 updating_sample_count_(false),
b0773a8a 113 sample_count_supported_(false),
0f8f8c18 114#ifdef ENABLE_DECODE
b0773a8a 115 add_decoder_button_(new QToolButton()),
0f8f8c18 116#endif
b0773a8a 117 add_math_signal_button_(new QToolButton())
6fb67b27 118{
7c657094 119 setObjectName(QString::fromUtf8("MainBar"));
0c7cdea2 120
d375140b
SA
121 setContextMenuPolicy(Qt::PreventContextMenu);
122
0f8f8c18 123 // Actions
c9da5118
SA
124 action_new_view_->setText(tr("New &View"));
125 action_new_view_->setIcon(QIcon::fromTheme("window-new",
126 QIcon(":/icons/window-new.png")));
127 connect(action_new_view_, SIGNAL(triggered(bool)),
128 this, SLOT(on_actionNewView_triggered()));
129
0f8f8c18
SA
130 action_open_->setText(tr("&Open..."));
131 action_open_->setIcon(QIcon::fromTheme("document-open",
132 QIcon(":/icons/document-open.png")));
133 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
d552c5c7
SA
134 connect(action_open_, SIGNAL(triggered(bool)),
135 this, SLOT(on_actionOpen_triggered()));
0f8f8c18 136
323715c4
SA
137 action_restore_setup_->setText(tr("Restore Session Setu&p..."));
138 connect(action_restore_setup_, SIGNAL(triggered(bool)),
139 this, SLOT(on_actionRestoreSetup_triggered()));
140
2cd6be62
VO
141 action_save_->setText(tr("&Save..."));
142 action_save_->setIcon(QIcon::fromTheme("document-save-as",
143 QIcon(":/icons/document-save-as.png")));
144 action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
145 connect(action_save_, SIGNAL(triggered(bool)),
146 this, SLOT(on_actionSave_triggered()));
147
148 action_save_as_->setText(tr("Save &As..."));
0f8f8c18
SA
149 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
150 QIcon(":/icons/document-save-as.png")));
d552c5c7
SA
151 connect(action_save_as_, SIGNAL(triggered(bool)),
152 this, SLOT(on_actionSaveAs_triggered()));
0f8f8c18
SA
153
154 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
155 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
156 QIcon(":/icons/document-save-as.png")));
157 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
d552c5c7
SA
158 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
159 this, SLOT(on_actionSaveSelectionAs_triggered()));
0f8f8c18 160
323715c4
SA
161 action_save_setup_->setText(tr("Save Session Setu&p..."));
162 connect(action_save_setup_, SIGNAL(triggered(bool)),
163 this, SLOT(on_actionSaveSetup_triggered()));
164
0f8f8c18
SA
165 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
166 session.device_manager().context());
167 menu_file_export->setTitle(tr("&Export"));
83af3ec1 168 connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
6f925ba9 169 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
0f8f8c18
SA
170
171 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
172 session.device_manager().context());
173 menu_file_import->setTitle(tr("&Import"));
83af3ec1 174 connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
6f925ba9 175 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
0f8f8c18
SA
176
177 action_connect_->setText(tr("&Connect to Device..."));
d552c5c7
SA
178 connect(action_connect_, SIGNAL(triggered(bool)),
179 this, SLOT(on_actionConnect_triggered()));
0f8f8c18 180
baf867ed
SA
181 // New view button
182 QMenu *menu_new_view = new QMenu();
183 connect(menu_new_view, SIGNAL(triggered(QAction*)),
184 this, SLOT(on_actionNewView_triggered(QAction*)));
185
186 for (int i = 0; i < views::ViewTypeCount; i++) {
187 QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i]));
009fc9ae 188 action->setData(QVariant::fromValue(i));
baf867ed
SA
189 }
190
191 new_view_button_->setMenu(menu_new_view);
192 new_view_button_->setDefaultAction(action_new_view_);
193 new_view_button_->setPopupMode(QToolButton::MenuButtonPopup);
194
ed43ef2e 195 // Open button
323715c4
SA
196 vector<QAction*> open_actions;
197 open_actions.push_back(action_open_);
198 QAction* separator_o = new QAction(this);
199 separator_o->setSeparator(true);
200 open_actions.push_back(separator_o);
201 open_actions.push_back(action_restore_setup_);
202
ed43ef2e 203 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
323715c4 204 session.device_manager().context(), open_actions);
83af3ec1
SA
205 connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
206 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
ed43ef2e 207
e0ba4f6f
SA
208 open_button_->setMenu(import_menu);
209 open_button_->setDefaultAction(action_open_);
210 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
ed43ef2e 211
998b89fd 212 // Save button
323715c4 213 vector<QAction*> save_actions;
2cd6be62 214 save_actions.push_back(action_save_);
323715c4
SA
215 save_actions.push_back(action_save_as_);
216 save_actions.push_back(action_save_selection_as_);
217 QAction* separator_s = new QAction(this);
218 separator_s->setSeparator(true);
219 save_actions.push_back(separator_s);
220 save_actions.push_back(action_save_setup_);
686d9151 221
998b89fd 222 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
323715c4 223 session.device_manager().context(), save_actions);
83af3ec1
SA
224 connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
225 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
998b89fd 226
e0ba4f6f 227 save_button_->setMenu(export_menu);
2cd6be62 228 save_button_->setDefaultAction(action_save_);
e0ba4f6f 229 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
998b89fd 230
079d39ea
JH
231 // Device selector menu
232 connect(&device_selector_, SIGNAL(device_selected()),
233 this, SLOT(on_device_selected()));
234
168888e2
JH
235 // Setup the decoder button
236#ifdef ENABLE_DECODE
4d8130bb 237 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
e0ba4f6f 238 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
97378470 239 add_decoder_button_->setToolTip(tr("Add protocol decoder"));
6b8b739d 240 add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D));
97378470
SA
241
242 connect(add_decoder_button_, SIGNAL(clicked()),
243 this, SLOT(on_add_decoder_clicked()));
168888e2
JH
244#endif
245
b0773a8a
SA
246 // Setup the math signal button
247 add_math_signal_button_->setIcon(QIcon(":/icons/add-math-signal.svg"));
248 add_math_signal_button_->setPopupMode(QToolButton::InstantPopup);
249 add_math_signal_button_->setToolTip(tr("Add math signal"));
250 add_math_signal_button_->setShortcut(QKeySequence(Qt::Key_M));
251
252 connect(add_math_signal_button_, SIGNAL(clicked()),
253 this, SLOT(on_add_math_signal_clicked()));
254
255
8dbbc7f0 256 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 257 this, SLOT(on_sample_count_changed()));
8dbbc7f0 258 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 259 this, SLOT(on_sample_rate_changed()));
dde1a563 260
8dbbc7f0 261 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 262
2b81ae46 263 set_capture_state(pv::Session::Stopped);
274d4f13 264
5f66b56e 265 configure_button_.setToolTip(tr("Configure Device"));
3432032f
UH
266 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
267 QIcon(":/icons/preferences-system.png")));
b7b659aa 268
5f66b56e 269 channels_button_.setToolTip(tr("Configure Channels"));
4d8130bb 270 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
cdb50f67 271
e0ba4f6f 272 add_toolbar_widgets();
40065ab6 273
8dbbc7f0
JH
274 sample_count_.installEventFilter(this);
275 sample_rate_.installEventFilter(this);
0f8f8c18 276
0f8f8c18 277 // Setup session_ events
d552c5c7 278 connect(&session_, SIGNAL(capture_state_changed(int)),
f4ce3e77 279 this, SLOT(on_capture_state_changed(int)));
d552c5c7
SA
280 connect(&session, SIGNAL(device_changed()),
281 this, SLOT(on_device_changed()));
0f8f8c18 282
101e7a9b 283 update_device_list();
0f8f8c18
SA
284}
285
079d39ea 286void MainBar::update_device_list()
18203d86 287{
079d39ea 288 DeviceManager &mgr = session_.device_manager();
da30ecb7
JH
289 shared_ptr<devices::Device> selected_device = session_.device();
290 list< shared_ptr<devices::Device> > devs;
e8d00928 291
079d39ea 292 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
53a7cce4 293
079d39ea
JH
294 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
295 devs.push_back(selected_device);
e95e8563 296
079d39ea 297 device_selector_.set_device_list(devs, selected_device);
e95e8563 298 update_device_config_widgets();
18203d86
JH
299}
300
7c657094 301void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 302{
73e170f9
SA
303 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
304
305 device_selector_.setEnabled(ui_enabled);
306 configure_button_.setEnabled(ui_enabled);
307 channels_button_.setEnabled(ui_enabled);
308 sample_count_.setEnabled(ui_enabled);
309 sample_rate_.setEnabled(ui_enabled);
6ac96c2e
JH
310}
311
7e0c99bf
SA
312void MainBar::reset_device_selector()
313{
314 device_selector_.reset();
315}
316
0f8f8c18
SA
317QAction* MainBar::action_open() const
318{
319 return action_open_;
320}
321
2cd6be62
VO
322QAction* MainBar::action_save() const
323{
324 return action_save_;
325}
326
0f8f8c18
SA
327QAction* MainBar::action_save_as() const
328{
329 return action_save_as_;
330}
331
332QAction* MainBar::action_save_selection_as() const
333{
334 return action_save_selection_as_;
335}
336
337QAction* MainBar::action_connect() const
338{
339 return action_connect_;
340}
341
7c657094 342void MainBar::update_sample_rate_selector()
dde1a563 343{
e8d00928
ML
344 Glib::VariantContainerBase gvar_dict;
345 GVariant *gvar_list;
4c60462b 346 const uint64_t *elements = nullptr;
488f5d3f 347 gsize num_elements;
6f925ba9 348 map< const ConfigKey*, set<Capability> > keys;
dde1a563 349
9dd88889
JH
350 if (updating_sample_rate_) {
351 sample_rate_.show_none();
3d4f16af 352 return;
9dd88889 353 }
3d4f16af 354
83af3ec1 355 const shared_ptr<devices::Device> device = device_selector_.selected_device();
e8d00928 356 if (!device)
ef4d0201
JH
357 return;
358
8dbbc7f0
JH
359 assert(!updating_sample_rate_);
360 updating_sample_rate_ = true;
1198b887 361
da30ecb7 362 const shared_ptr<sigrok::Device> sr_dev = device->device();
a6ed12bf 363
220801e9 364 sample_rate_.allow_user_entered_values(false);
c7808100
SA
365 if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
366 try {
367 auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
368 if (gvar.gobj()) {
369 bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
370 gvar).get();
371 sample_rate_.allow_user_entered_values(value);
372 }
373 } catch (Error& error) {
374 // Do nothing
bc4b9ccf 375 }
bc4b9ccf
SA
376 }
377
c7808100 378
7bb0fbf4 379 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
519a6ed8
WS
380 try {
381 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
30677c13 382 } catch (Error& error) {
519a6ed8
WS
383 qDebug() << tr("Failed to get sample rate list:") << error.what();
384 }
7bb0fbf4 385 } else {
8dbbc7f0
JH
386 sample_rate_.show_none();
387 updating_sample_rate_ = false;
dde1a563 388 return;
1198b887 389 }
dde1a563 390
e8d00928 391 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
2ad82c2e 392 "samplerate-steps", G_VARIANT_TYPE("at")))) {
488f5d3f 393 elements = (const uint64_t *)g_variant_get_fixed_array(
83af3ec1 394 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
395
396 const uint64_t min = elements[0];
397 const uint64_t max = elements[1];
398 const uint64_t step = elements[2];
399
488f5d3f 400 g_variant_unref(gvar_list);
aafe53af
JH
401
402 assert(min > 0);
403 assert(max > 0);
404 assert(max > min);
405 assert(step > 0);
406
407 if (step == 1)
8dbbc7f0 408 sample_rate_.show_125_list(min, max);
2ad82c2e 409 else {
aafe53af
JH
410 // When the step is not 1, we cam't make a 1-2-5-10
411 // list of sample rates, because we may not be able to
412 // make round numbers. Therefore in this case, show a
413 // spin box.
8dbbc7f0 414 sample_rate_.show_min_max_step(min, max, step);
aafe53af 415 }
2ad82c2e
UH
416 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
417 "samplerates", G_VARIANT_TYPE("at")))) {
488f5d3f 418 elements = (const uint64_t *)g_variant_get_fixed_array(
83af3ec1 419 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 420 sample_rate_.show_list(elements, num_elements);
488f5d3f 421 g_variant_unref(gvar_list);
dde1a563 422 }
8dbbc7f0 423 updating_sample_rate_ = false;
48888313
JH
424
425 update_sample_rate_selector_value();
426}
427
7c657094 428void MainBar::update_sample_rate_selector_value()
48888313 429{
8dbbc7f0 430 if (updating_sample_rate_)
3d4f16af
JH
431 return;
432
83af3ec1 433 const shared_ptr<devices::Device> device = device_selector_.selected_device();
e8d00928 434 if (!device)
19adbc2c
JH
435 return;
436
e8d00928 437 try {
da30ecb7 438 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
e8d00928 439 uint64_t samplerate =
15289d5c 440 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
441 assert(!updating_sample_rate_);
442 updating_sample_rate_ = true;
443 sample_rate_.set_value(samplerate);
444 updating_sample_rate_ = false;
30677c13 445 } catch (Error& error) {
46ebcd3f 446 qDebug() << tr("Failed to get sample rate:") << error.what();
eb4008a6 447 }
48888313
JH
448}
449
7c657094 450void MainBar::update_sample_count_selector()
85756012 451{
8dbbc7f0 452 if (updating_sample_count_)
3d4f16af
JH
453 return;
454
83af3ec1 455 const shared_ptr<devices::Device> device = device_selector_.selected_device();
e8d00928 456 if (!device)
19adbc2c
JH
457 return;
458
da30ecb7
JH
459 const shared_ptr<sigrok::Device> sr_dev = device->device();
460
8dbbc7f0
JH
461 assert(!updating_sample_count_);
462 updating_sample_count_ = true;
85756012 463
2ad82c2e 464 if (!sample_count_supported_) {
3e7636f9
JH
465 sample_count_.show_none();
466 updating_sample_count_ = false;
467 return;
468 }
b50ef520 469
3e7636f9
JH
470 uint64_t sample_count = sample_count_.value();
471 uint64_t min_sample_count = 0;
472 uint64_t max_sample_count = MaxSampleCount;
348f9cec 473 bool default_count_set = false;
3cbffdcf 474
348f9cec 475 if (sample_count == 0) {
3e7636f9 476 sample_count = DefaultSampleCount;
348f9cec
SA
477 default_count_set = true;
478 }
b50ef520 479
7bb0fbf4 480 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
519a6ed8
WS
481 try {
482 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
483 if (gvar.gobj())
484 g_variant_get(gvar.gobj(), "(tt)",
485 &min_sample_count, &max_sample_count);
30677c13 486 } catch (Error& error) {
519a6ed8
WS
487 qDebug() << tr("Failed to get sample limit list:") << error.what();
488 }
ed1d9d81 489 }
1d04852f 490
3e7636f9
JH
491 min_sample_count = min(max(min_sample_count, MinSampleCount),
492 max_sample_count);
b50ef520 493
c063290a 494 sample_count_.show_125_list(min_sample_count, max_sample_count);
b50ef520 495
7bb0fbf4 496 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
da30ecb7 497 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
3e7636f9 498 sample_count = g_variant_get_uint64(gvar.gobj());
348f9cec 499 if (sample_count == 0) {
3e7636f9 500 sample_count = DefaultSampleCount;
348f9cec
SA
501 default_count_set = true;
502 }
3e7636f9
JH
503 sample_count = min(max(sample_count, MinSampleCount),
504 max_sample_count);
7bb0fbf4 505 }
3e7636f9
JH
506
507 sample_count_.set_value(sample_count);
df3c1aa3 508
8dbbc7f0 509 updating_sample_count_ = false;
348f9cec
SA
510
511 // If we show the default rate then make sure the device uses the same
512 if (default_count_set)
513 commit_sample_count();
85756012
ML
514}
515
7c657094 516void MainBar::update_device_config_widgets()
e95e8563 517{
e95e8563
JH
518 using namespace pv::popups;
519
83af3ec1 520 const shared_ptr<devices::Device> device = device_selector_.selected_device();
9dd88889
JH
521
522 // Hide the widgets if no device is selected
523 channels_button_action_->setVisible(!!device);
9dd88889
JH
524 if (!device) {
525 configure_button_action_->setVisible(false);
526 sample_count_.show_none();
527 sample_rate_.show_none();
e95e8563 528 return;
9dd88889 529 }
e95e8563 530
da30ecb7
JH
531 const shared_ptr<sigrok::Device> sr_dev = device->device();
532 if (!sr_dev)
533 return;
534
e95e8563 535 // Update the configure popup
da30ecb7 536 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
83af3ec1 537 configure_button_action_->setVisible(!opts->binding().properties().empty());
8dbbc7f0 538 configure_button_.set_popup(opts);
e95e8563 539
6ac6242b 540 // Update the channels popup
8dbbc7f0
JH
541 Channels *const channels = new Channels(session_, this);
542 channels_button_.set_popup(channels);
e95e8563
JH
543
544 // Update supported options.
8dbbc7f0 545 sample_count_supported_ = false;
e95e8563 546
7bb0fbf4
ML
547 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
548 sample_count_supported_ = true;
549
e95e8563
JH
550 // Add notification of reconfigure events
551 disconnect(this, SLOT(on_config_changed()));
e8d00928 552 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
553 this, SLOT(on_config_changed()));
554
555 // Update sweep timing widgets.
556 update_sample_count_selector();
557 update_sample_rate_selector();
558}
559
0f8f8c18
SA
560void MainBar::commit_sample_rate()
561{
562 uint64_t sample_rate = 0;
563
83af3ec1 564 const shared_ptr<devices::Device> device = device_selector_.selected_device();
0f8f8c18
SA
565 if (!device)
566 return;
567
568 const shared_ptr<sigrok::Device> sr_dev = device->device();
569
570 sample_rate = sample_rate_.value();
0f8f8c18
SA
571
572 try {
573 sr_dev->config_set(ConfigKey::SAMPLERATE,
574 Glib::Variant<guint64>::create(sample_rate));
575 update_sample_rate_selector();
30677c13 576 } catch (Error& error) {
83af3ec1 577 qDebug() << tr("Failed to configure samplerate:") << error.what();
0f8f8c18
SA
578 return;
579 }
580
581 // Devices with built-in memory might impose limits on certain
582 // configurations, so let's check what sample count the driver
583 // lets us use now.
584 update_sample_count_selector();
585}
586
7c657094 587void MainBar::commit_sample_count()
124d97de
ML
588{
589 uint64_t sample_count = 0;
590
83af3ec1 591 const shared_ptr<devices::Device> device = device_selector_.selected_device();
e8d00928 592 if (!device)
19adbc2c
JH
593 return;
594
da30ecb7
JH
595 const shared_ptr<sigrok::Device> sr_dev = device->device();
596
8dbbc7f0 597 sample_count = sample_count_.value();
2ad82c2e 598 if (sample_count_supported_) {
e8d00928 599 try {
da30ecb7 600 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 601 Glib::Variant<guint64>::create(sample_count));
449aec3c 602 update_sample_count_selector();
30677c13 603 } catch (Error& error) {
83af3ec1 604 qDebug() << tr("Failed to configure sample count:") << error.what();
e8d00928
ML
605 return;
606 }
124d97de 607 }
e13e7879
SA
608
609 // Devices with built-in memory might impose limits on certain
610 // configurations, so let's check what sample rate the driver
611 // lets us use now.
612 update_sample_rate_selector();
124d97de
ML
613}
614
0f8f8c18
SA
615void MainBar::show_session_error(const QString text, const QString info_text)
616{
617 QMessageBox msg(this);
970fca0d 618 msg.setText(text + "\n\n" + info_text);
0f8f8c18
SA
619 msg.setStandardButtons(QMessageBox::Ok);
620 msg.setIcon(QMessageBox::Warning);
621 msg.exec();
622}
19adbc2c 623
e6d85b89 624void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name)
0f8f8c18
SA
625{
626 using pv::dialogs::StoreProgress;
627
628 // Stop any currently running capture session
629 session_.stop_capture();
630
631 QSettings settings;
632 const QString dir = settings.value(SettingSaveDirectory).toString();
633
6f925ba9 634 pair<uint64_t, uint64_t> sample_range;
0f8f8c18
SA
635
636 // Selection only? Verify that the cursors are active and fetch their values
637 if (selection_only) {
f23c4692
SA
638 views::trace::View *trace_view =
639 qobject_cast<views::trace::View*>(session_.main_view().get());
f4e57597
SA
640
641 if (!trace_view->cursors()->enabled()) {
0f8f8c18 642 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
83af3ec1
SA
643 "cursors before you can save the data enclosed by them " \
644 "to a session file (e.g. using the Show Cursors button)."));
0f8f8c18
SA
645 return;
646 }
647
648 const double samplerate = session_.get_samplerate();
649
f4e57597
SA
650 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
651 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
0f8f8c18 652
6f925ba9 653 const uint64_t start_sample = (uint64_t)max(
a20c1fcc 654 0.0, start_time.convert_to<double>() * samplerate);
6f925ba9 655 const uint64_t end_sample = (uint64_t)max(
a20c1fcc 656 0.0, end_time.convert_to<double>() * samplerate);
0f8f8c18 657
7b7ab1f5
SA
658 if ((start_sample == 0) && (end_sample == 0)) {
659 // Both cursors are negative and were clamped to 0
660 show_session_error(tr("Invalid Range"), tr("The cursors don't " \
83af3ec1 661 "define a valid range of samples."));
7b7ab1f5
SA
662 return;
663 }
664
6f925ba9 665 sample_range = make_pair(start_sample, end_sample);
0f8f8c18 666 } else {
6f925ba9 667 sample_range = make_pair(0, 0);
0f8f8c18
SA
668 }
669
670 // Construct the filter
671 const vector<string> exts = format->extensions();
672 QString filter = tr("%1 files ").arg(
673 QString::fromStdString(format->description()));
674
675 if (exts.empty())
75ab6b87 676 filter += "(*)";
0f8f8c18 677 else
75ab6b87 678 filter += QString("(*.%1);;%2 (*)").arg(
0f8f8c18
SA
679 QString::fromStdString(join(exts, ", *.")),
680 tr("All Files"));
681
682 // Show the file dialog
e6d85b89
SA
683 if (file_name.isEmpty())
684 file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter);
0f8f8c18
SA
685
686 if (file_name.isEmpty())
f9541bde
JH
687 return;
688
0f8f8c18
SA
689 const QString abs_path = QFileInfo(file_name).absolutePath();
690 settings.setValue(SettingSaveDirectory, abs_path);
691
692 // Show the options dialog
693 map<string, Glib::VariantBase> options;
694 if (!format->options().empty()) {
695 dialogs::InputOutputOptions dlg(
696 tr("Export %1").arg(QString::fromStdString(
697 format->description())),
698 format->options(), this);
699 if (!dlg.exec())
700 return;
701 options = dlg.options();
702 }
703
e6d85b89 704 if (!selection_only) {
73d5a9bb 705 if (format == session_.device_manager().context()->output_formats()["srzip"]) {
e6d85b89 706 session_.set_save_path(QFileInfo(file_name).absolutePath());
73d5a9bb
SA
707 session_.set_name(QFileInfo(file_name).fileName());
708 } else
e6d85b89
SA
709 session_.set_save_path("");
710 }
101e7a9b 711
0f8f8c18
SA
712 StoreProgress *dlg = new StoreProgress(file_name, format, options,
713 sample_range, session_, this);
714 dlg->run();
715}
716
717void MainBar::import_file(shared_ptr<InputFormat> format)
718{
719 assert(format);
720
721 QSettings settings;
722 const QString dir = settings.value(SettingOpenDirectory).toString();
723
724 // Construct the filter
725 const vector<string> exts = format->extensions();
ca1d6d5f
GS
726 const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
727 tr("%1 files").arg(QString::fromStdString(format->description())),
728 QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
729 const QString filter_all = QString::fromStdString("%1 (%2)").arg(
730 tr("All Files"), QString::fromStdString("*"));
731 const QString filter = QString::fromStdString("%1%2%3").arg(
732 exts.empty() ? "" : filter_exts,
733 exts.empty() ? "" : ";;",
734 filter_all);
0f8f8c18
SA
735
736 // Show the file dialog
737 const QString file_name = QFileDialog::getOpenFileName(
ca1d6d5f 738 this, tr("Import File"), dir, filter);
0f8f8c18
SA
739
740 if (file_name.isEmpty())
48888313 741 return;
0f8f8c18
SA
742
743 // Show the options dialog
744 map<string, Glib::VariantBase> options;
745 if (!format->options().empty()) {
746 dialogs::InputOutputOptions dlg(
747 tr("Import %1").arg(QString::fromStdString(
748 format->description())),
749 format->options(), this);
750 if (!dlg.exec())
751 return;
752 options = dlg.options();
48888313 753 }
e13e7879 754
96dbf014 755 session_.load_file(file_name, "", format, options);
0f8f8c18
SA
756
757 const QString abs_path = QFileInfo(file_name).absolutePath();
758 settings.setValue(SettingOpenDirectory, abs_path);
dde1a563
JH
759}
760
7c657094 761void MainBar::on_device_selected()
dde1a563 762{
da30ecb7 763 shared_ptr<devices::Device> device = device_selector_.selected_device();
19adbc2c 764
83af3ec1
SA
765 if (device)
766 session_.select_device(device);
767 else
768 reset_device_selector();
91f8fe8c 769}
51d4a9ab 770
91f8fe8c
SA
771void MainBar::on_device_changed()
772{
773 update_device_list();
e95e8563 774 update_device_config_widgets();
dde1a563 775}
51e77110 776
f4ce3e77
SA
777void MainBar::on_capture_state_changed(int state)
778{
779 set_capture_state((pv::Session::capture_state)state);
780}
781
7c657094 782void MainBar::on_sample_count_changed()
124d97de 783{
449aec3c
SA
784 if (!updating_sample_count_)
785 commit_sample_count();
124d97de
ML
786}
787
7c657094 788void MainBar::on_sample_rate_changed()
48888313 789{
0e0b6b3e
SA
790 if (!updating_sample_rate_)
791 commit_sample_rate();
48888313
JH
792}
793
7c657094 794void MainBar::on_config_changed()
82afd5e3 795{
bc4b9ccf
SA
796 // We want to also call update_sample_rate_selector() here in case
797 // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
798 // commit_sample_rate() does this already, so we don't call it here
799
82afd5e3 800 commit_sample_count();
c063290a 801 commit_sample_rate();
82afd5e3
JH
802}
803
baf867ed 804void MainBar::on_actionNewView_triggered(QAction* action)
c9da5118 805{
baf867ed
SA
806 if (action)
807 new_view(&session_, action->data().toInt());
808 else
809 // When the icon of the button is clicked, we create a trace view
810 new_view(&session_, views::ViewTypeTrace);
c9da5118
SA
811}
812
0f8f8c18
SA
813void MainBar::on_actionOpen_triggered()
814{
815 QSettings settings;
816 const QString dir = settings.value(SettingOpenDirectory).toString();
817
818 // Show the dialog
819 const QString file_name = QFileDialog::getOpenFileName(
820 this, tr("Open File"), dir, tr(
b0e15aa2 821 "sigrok Sessions (*.sr);;"
75ab6b87 822 "All Files (*)"));
0f8f8c18
SA
823
824 if (!file_name.isEmpty()) {
fd22c71c 825 session_.load_file(file_name);
0f8f8c18
SA
826
827 const QString abs_path = QFileInfo(file_name).absolutePath();
828 settings.setValue(SettingOpenDirectory, abs_path);
829 }
830}
831
2cd6be62
VO
832void MainBar::on_actionSave_triggered()
833{
e6d85b89
SA
834 // A path is only set if we loaded/saved an srzip file before
835 if (session_.save_path().isEmpty()) {
836 on_actionSaveAs_triggered();
837 return;
838 }
839
73d5a9bb 840 QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name());
e6d85b89
SA
841 export_file(session_.device_manager().context()->output_formats()["srzip"], false,
842 fi.absoluteFilePath());
2cd6be62
VO
843}
844
0f8f8c18
SA
845void MainBar::on_actionSaveAs_triggered()
846{
847 export_file(session_.device_manager().context()->output_formats()["srzip"]);
848}
849
850void MainBar::on_actionSaveSelectionAs_triggered()
851{
852 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
853}
854
323715c4
SA
855void MainBar::on_actionSaveSetup_triggered()
856{
857 QSettings settings;
858 const QString dir = settings.value(SettingSaveDirectory).toString();
859
860 const QString file_name = QFileDialog::getSaveFileName(
861 this, tr("Save File"), dir, tr(
862 "PulseView Session Setups (*.pvs);;"
863 "All Files (*)"));
864
865 if (file_name.isEmpty())
866 return;
867
868 QSettings settings_storage(file_name, QSettings::IniFormat);
869 session_.save_setup(settings_storage);
870}
871
872void MainBar::on_actionRestoreSetup_triggered()
873{
874 QSettings settings;
875 const QString dir = settings.value(SettingSaveDirectory).toString();
876
877 const QString file_name = QFileDialog::getOpenFileName(
878 this, tr("Open File"), dir, tr(
879 "PulseView Session Setups (*.pvs);;"
880 "All Files (*)"));
881
882 if (file_name.isEmpty())
883 return;
884
885 QSettings settings_storage(file_name, QSettings::IniFormat);
886 session_.restore_setup(settings_storage);
887}
888
0f8f8c18
SA
889void MainBar::on_actionConnect_triggered()
890{
891 // Stop any currently running capture session
892 session_.stop_capture();
893
894 dialogs::Connect dlg(this, session_.device_manager());
895
896 // If the user selected a device, select it in the device list. Select the
897 // current device otherwise.
898 if (dlg.exec())
fd22c71c 899 session_.select_device(dlg.get_selected_device());
0f8f8c18
SA
900
901 update_device_list();
902}
903
97378470
SA
904void MainBar::on_add_decoder_clicked()
905{
906 show_decoder_selector(&session_);
907}
908
b0773a8a
SA
909void MainBar::on_add_math_signal_clicked()
910{
911 shared_ptr<data::SignalBase> signal = make_shared<data::MathSignal>(session_);
912 session_.add_generated_signal(signal);
913}
914
e0ba4f6f 915void MainBar::add_toolbar_widgets()
0f8f8c18 916{
baf867ed 917 addWidget(new_view_button_);
e0ba4f6f
SA
918 addSeparator();
919 addWidget(open_button_);
920 addWidget(save_button_);
921 addSeparator();
0f8f8c18 922
e0ba4f6f 923 StandardBar::add_toolbar_widgets();
0f8f8c18 924
e0ba4f6f
SA
925 addWidget(&device_selector_);
926 configure_button_action_ = addWidget(&configure_button_);
927 channels_button_action_ = addWidget(&channels_button_);
928 addWidget(&sample_count_);
929 addWidget(&sample_rate_);
930#ifdef ENABLE_DECODE
931 addSeparator();
932 addWidget(add_decoder_button_);
933#endif
b0773a8a 934 addWidget(add_math_signal_button_);
d552c5c7
SA
935}
936
7c657094 937bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 938{
2ad82c2e 939 if (sample_count_supported_ && (watched == &sample_count_ ||
83af3ec1
SA
940 watched == &sample_rate_) &&
941 (event->type() == QEvent::ToolTip)) {
942
60d9b99a 943 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
40065ab6 944 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45 945
3ccf0f7f
JS
946 QString str = tr("Total sampling time: %1").arg(
947 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
40065ab6
JS
948 QToolTip::showText(help_event->globalPos(), str);
949
950 return true;
951 }
952
953 return false;
954}
955
f4c92e1c 956} // namespace toolbars
51e77110 957} // namespace pv