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