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