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