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