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