]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
Improve icon/theme handling.
[pulseview.git] / pv / toolbars / mainbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7 3 *
079d39ea 4 * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
d4984fe7
JH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
d4984fe7
JH
18 */
19
09f5d123 20#include <extdef.h>
dde1a563 21
079d39ea
JH
22#include <algorithm>
23#include <cassert>
215f9499 24
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
0f8f8c18 27#include <QFileDialog>
40065ab6 28#include <QHelpEvent>
87f0df9b 29#include <QMenu>
0f8f8c18
SA
30#include <QMessageBox>
31#include <QSettings>
40065ab6 32#include <QToolTip>
dde1a563 33
7c657094 34#include "mainbar.hpp"
d4984fe7 35
0f8f8c18
SA
36#include <boost/algorithm/string/join.hpp>
37
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>
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
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
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,
6f925ba9
UH
149 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
150 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
0f8f8c18
SA
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,
6f925ba9
UH
156 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
157 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
0f8f8c18
SA
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 166 connect(import_menu,
6f925ba9 167 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
d552c5c7 168 this,
6f925ba9 169 SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
ed43ef2e 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 183 connect(export_menu,
6f925ba9 184 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
d552c5c7 185 this,
6f925ba9 186 SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
998b89fd 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
4d8130bb 202 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
e0ba4f6f
SA
203 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
204 add_decoder_button_->setMenu(menu_decoders_add_);
ae95fe58 205 add_decoder_button_->setToolTip(tr("Add low-level, non-stacked protocol decoder"));
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"));
3432032f
UH
218 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
219 QIcon(":/icons/preferences-system.png")));
b7b659aa 220
5f66b56e 221 channels_button_.setToolTip(tr("Configure Channels"));
4d8130bb 222 channels_button_.setIcon(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
7c657094 253void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 254{
73e170f9
SA
255 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
256
257 device_selector_.setEnabled(ui_enabled);
258 configure_button_.setEnabled(ui_enabled);
259 channels_button_.setEnabled(ui_enabled);
260 sample_count_.setEnabled(ui_enabled);
261 sample_rate_.setEnabled(ui_enabled);
6ac96c2e
JH
262}
263
7e0c99bf
SA
264void MainBar::reset_device_selector()
265{
266 device_selector_.reset();
267}
268
0f8f8c18
SA
269QAction* MainBar::action_open() const
270{
271 return action_open_;
272}
273
274QAction* MainBar::action_save_as() const
275{
276 return action_save_as_;
277}
278
279QAction* MainBar::action_save_selection_as() const
280{
281 return action_save_selection_as_;
282}
283
284QAction* MainBar::action_connect() const
285{
286 return action_connect_;
287}
288
7c657094 289void MainBar::update_sample_rate_selector()
dde1a563 290{
e8d00928
ML
291 Glib::VariantContainerBase gvar_dict;
292 GVariant *gvar_list;
4c60462b 293 const uint64_t *elements = nullptr;
488f5d3f 294 gsize num_elements;
6f925ba9 295 map< const ConfigKey*, set<Capability> > keys;
dde1a563 296
9dd88889
JH
297 if (updating_sample_rate_) {
298 sample_rate_.show_none();
3d4f16af 299 return;
9dd88889 300 }
3d4f16af 301
da30ecb7
JH
302 const shared_ptr<devices::Device> device =
303 device_selector_.selected_device();
e8d00928 304 if (!device)
ef4d0201
JH
305 return;
306
8dbbc7f0
JH
307 assert(!updating_sample_rate_);
308 updating_sample_rate_ = true;
1198b887 309
da30ecb7 310 const shared_ptr<sigrok::Device> sr_dev = device->device();
a6ed12bf 311
7bb0fbf4
ML
312 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
313 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
314 } else {
8dbbc7f0
JH
315 sample_rate_.show_none();
316 updating_sample_rate_ = false;
dde1a563 317 return;
1198b887 318 }
dde1a563 319
e8d00928 320 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
2ad82c2e 321 "samplerate-steps", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
322 elements = (const uint64_t *)g_variant_get_fixed_array(
323 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
324
325 const uint64_t min = elements[0];
326 const uint64_t max = elements[1];
327 const uint64_t step = elements[2];
328
488f5d3f 329 g_variant_unref(gvar_list);
aafe53af
JH
330
331 assert(min > 0);
332 assert(max > 0);
333 assert(max > min);
334 assert(step > 0);
335
336 if (step == 1)
8dbbc7f0 337 sample_rate_.show_125_list(min, max);
2ad82c2e 338 else {
aafe53af
JH
339 // When the step is not 1, we cam't make a 1-2-5-10
340 // list of sample rates, because we may not be able to
341 // make round numbers. Therefore in this case, show a
342 // spin box.
8dbbc7f0 343 sample_rate_.show_min_max_step(min, max, step);
aafe53af 344 }
2ad82c2e
UH
345 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
346 "samplerates", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
347 elements = (const uint64_t *)g_variant_get_fixed_array(
348 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 349 sample_rate_.show_list(elements, num_elements);
488f5d3f 350 g_variant_unref(gvar_list);
dde1a563 351 }
8dbbc7f0 352 updating_sample_rate_ = false;
48888313
JH
353
354 update_sample_rate_selector_value();
355}
356
7c657094 357void MainBar::update_sample_rate_selector_value()
48888313 358{
8dbbc7f0 359 if (updating_sample_rate_)
3d4f16af
JH
360 return;
361
da30ecb7
JH
362 const shared_ptr<devices::Device> device =
363 device_selector_.selected_device();
e8d00928 364 if (!device)
19adbc2c
JH
365 return;
366
e8d00928 367 try {
da30ecb7 368 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
e8d00928 369 uint64_t samplerate =
15289d5c 370 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
371 assert(!updating_sample_rate_);
372 updating_sample_rate_ = true;
373 sample_rate_.set_value(samplerate);
374 updating_sample_rate_ = false;
e8d00928 375 } catch (Error error) {
8dd44190 376 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
377 return;
378 }
48888313
JH
379}
380
7c657094 381void MainBar::update_sample_count_selector()
85756012 382{
8dbbc7f0 383 if (updating_sample_count_)
3d4f16af
JH
384 return;
385
da30ecb7
JH
386 const shared_ptr<devices::Device> device =
387 device_selector_.selected_device();
e8d00928 388 if (!device)
19adbc2c
JH
389 return;
390
da30ecb7
JH
391 const shared_ptr<sigrok::Device> sr_dev = device->device();
392
8dbbc7f0
JH
393 assert(!updating_sample_count_);
394 updating_sample_count_ = true;
85756012 395
2ad82c2e 396 if (!sample_count_supported_) {
3e7636f9
JH
397 sample_count_.show_none();
398 updating_sample_count_ = false;
399 return;
400 }
b50ef520 401
3e7636f9
JH
402 uint64_t sample_count = sample_count_.value();
403 uint64_t min_sample_count = 0;
404 uint64_t max_sample_count = MaxSampleCount;
348f9cec 405 bool default_count_set = false;
3cbffdcf 406
348f9cec 407 if (sample_count == 0) {
3e7636f9 408 sample_count = DefaultSampleCount;
348f9cec
SA
409 default_count_set = true;
410 }
b50ef520 411
7bb0fbf4
ML
412 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
413 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
414 if (gvar.gobj())
415 g_variant_get(gvar.gobj(), "(tt)",
416 &min_sample_count, &max_sample_count);
ed1d9d81 417 }
1d04852f 418
3e7636f9
JH
419 min_sample_count = min(max(min_sample_count, MinSampleCount),
420 max_sample_count);
b50ef520 421
c063290a 422 sample_count_.show_125_list(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 426 sample_count = g_variant_get_uint64(gvar.gobj());
348f9cec 427 if (sample_count == 0) {
3e7636f9 428 sample_count = DefaultSampleCount;
348f9cec
SA
429 default_count_set = true;
430 }
3e7636f9
JH
431 sample_count = min(max(sample_count, MinSampleCount),
432 max_sample_count);
7bb0fbf4 433 }
3e7636f9
JH
434
435 sample_count_.set_value(sample_count);
df3c1aa3 436
8dbbc7f0 437 updating_sample_count_ = false;
348f9cec
SA
438
439 // If we show the default rate then make sure the device uses the same
440 if (default_count_set)
441 commit_sample_count();
85756012
ML
442}
443
7c657094 444void MainBar::update_device_config_widgets()
e95e8563 445{
e95e8563
JH
446 using namespace pv::popups;
447
da30ecb7
JH
448 const shared_ptr<devices::Device> device =
449 device_selector_.selected_device();
9dd88889
JH
450
451 // Hide the widgets if no device is selected
452 channels_button_action_->setVisible(!!device);
9dd88889
JH
453 if (!device) {
454 configure_button_action_->setVisible(false);
455 sample_count_.show_none();
456 sample_rate_.show_none();
e95e8563 457 return;
9dd88889 458 }
e95e8563 459
da30ecb7
JH
460 const shared_ptr<sigrok::Device> sr_dev = device->device();
461 if (!sr_dev)
462 return;
463
e95e8563 464 // Update the configure popup
da30ecb7 465 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
8dbbc7f0 466 configure_button_action_->setVisible(
e95e8563 467 !opts->binding().properties().empty());
8dbbc7f0 468 configure_button_.set_popup(opts);
e95e8563 469
6ac6242b 470 // Update the channels popup
8dbbc7f0
JH
471 Channels *const channels = new Channels(session_, this);
472 channels_button_.set_popup(channels);
e95e8563
JH
473
474 // Update supported options.
8dbbc7f0 475 sample_count_supported_ = false;
e95e8563 476
7bb0fbf4
ML
477 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
478 sample_count_supported_ = true;
479
480 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
481 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
482 Glib::Variant<guint64>::create(1));
483 on_config_changed();
484 }
e95e8563
JH
485
486 // Add notification of reconfigure events
487 disconnect(this, SLOT(on_config_changed()));
e8d00928 488 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
489 this, SLOT(on_config_changed()));
490
491 // Update sweep timing widgets.
492 update_sample_count_selector();
493 update_sample_rate_selector();
494}
495
0f8f8c18
SA
496void MainBar::commit_sample_rate()
497{
498 uint64_t sample_rate = 0;
499
500 const shared_ptr<devices::Device> device =
501 device_selector_.selected_device();
502 if (!device)
503 return;
504
505 const shared_ptr<sigrok::Device> sr_dev = device->device();
506
507 sample_rate = sample_rate_.value();
508 if (sample_rate == 0)
509 return;
510
511 try {
512 sr_dev->config_set(ConfigKey::SAMPLERATE,
513 Glib::Variant<guint64>::create(sample_rate));
514 update_sample_rate_selector();
515 } catch (Error error) {
516 qDebug() << "Failed to configure samplerate.";
517 return;
518 }
519
520 // Devices with built-in memory might impose limits on certain
521 // configurations, so let's check what sample count the driver
522 // lets us use now.
523 update_sample_count_selector();
524}
525
7c657094 526void MainBar::commit_sample_count()
124d97de
ML
527{
528 uint64_t sample_count = 0;
529
da30ecb7
JH
530 const shared_ptr<devices::Device> device =
531 device_selector_.selected_device();
e8d00928 532 if (!device)
19adbc2c
JH
533 return;
534
da30ecb7
JH
535 const shared_ptr<sigrok::Device> sr_dev = device->device();
536
8dbbc7f0 537 sample_count = sample_count_.value();
2ad82c2e 538 if (sample_count_supported_) {
e8d00928 539 try {
da30ecb7 540 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 541 Glib::Variant<guint64>::create(sample_count));
449aec3c 542 update_sample_count_selector();
e8d00928
ML
543 } catch (Error error) {
544 qDebug() << "Failed to configure sample count.";
545 return;
546 }
124d97de 547 }
e13e7879
SA
548
549 // Devices with built-in memory might impose limits on certain
550 // configurations, so let's check what sample rate the driver
551 // lets us use now.
552 update_sample_rate_selector();
124d97de
ML
553}
554
0f8f8c18 555void MainBar::session_error(const QString text, const QString info_text)
48888313 556{
0f8f8c18
SA
557 QMetaObject::invokeMethod(this, "show_session_error",
558 Qt::QueuedConnection, Q_ARG(QString, text),
559 Q_ARG(QString, info_text));
560}
48888313 561
0f8f8c18
SA
562void MainBar::show_session_error(const QString text, const QString info_text)
563{
564 QMessageBox msg(this);
565 msg.setText(text);
566 msg.setInformativeText(info_text);
567 msg.setStandardButtons(QMessageBox::Ok);
568 msg.setIcon(QMessageBox::Warning);
569 msg.exec();
570}
19adbc2c 571
0f8f8c18
SA
572void MainBar::add_decoder(srd_decoder *decoder)
573{
574#ifdef ENABLE_DECODE
575 assert(decoder);
576 session_.add_decoder(decoder);
577#else
578 (void)decoder;
579#endif
580}
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) {
f4e57597
SA
596 views::TraceView::View *trace_view =
597 qobject_cast<views::TraceView::View*>(session_.main_view().get());
598
599 if (!trace_view->cursors()->enabled()) {
0f8f8c18
SA
600 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
601 "cursors before you can save the data enclosed by them " \
602 "to a session file (e.g. using ALT-V - Show Cursors)."));
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(
49ec9e07 612 (double)0, start_time.convert_to<double>() * samplerate);
6f925ba9 613 const uint64_t end_sample = (uint64_t)max(
49ec9e07 614 (double)0, end_time.convert_to<double>() * samplerate);
0f8f8c18 615
6f925ba9 616 sample_range = make_pair(start_sample, end_sample);
0f8f8c18 617 } else {
6f925ba9 618 sample_range = make_pair(0, 0);
0f8f8c18
SA
619 }
620
621 // Construct the filter
622 const vector<string> exts = format->extensions();
623 QString filter = tr("%1 files ").arg(
624 QString::fromStdString(format->description()));
625
626 if (exts.empty())
627 filter += "(*.*)";
628 else
629 filter += QString("(*.%1);;%2 (*.*)").arg(
630 QString::fromStdString(join(exts, ", *.")),
631 tr("All Files"));
632
633 // Show the file dialog
634 const QString file_name = QFileDialog::getSaveFileName(
635 this, tr("Save File"), dir, filter);
636
637 if (file_name.isEmpty())
f9541bde
JH
638 return;
639
0f8f8c18
SA
640 const QString abs_path = QFileInfo(file_name).absolutePath();
641 settings.setValue(SettingSaveDirectory, abs_path);
642
643 // Show the options dialog
644 map<string, Glib::VariantBase> options;
645 if (!format->options().empty()) {
646 dialogs::InputOutputOptions dlg(
647 tr("Export %1").arg(QString::fromStdString(
648 format->description())),
649 format->options(), this);
650 if (!dlg.exec())
651 return;
652 options = dlg.options();
653 }
654
c32bf63a
SA
655 if (!selection_only)
656 session_.set_name(QFileInfo(file_name).fileName());
101e7a9b 657
0f8f8c18
SA
658 StoreProgress *dlg = new StoreProgress(file_name, format, options,
659 sample_range, session_, this);
660 dlg->run();
661}
662
663void MainBar::import_file(shared_ptr<InputFormat> format)
664{
665 assert(format);
666
667 QSettings settings;
668 const QString dir = settings.value(SettingOpenDirectory).toString();
669
670 // Construct the filter
671 const vector<string> exts = format->extensions();
672 const QString filter = exts.empty() ? "" :
673 tr("%1 files (*.%2)").arg(
674 QString::fromStdString(format->description()),
675 QString::fromStdString(join(exts, ", *.")));
676
677 // Show the file dialog
678 const QString file_name = QFileDialog::getOpenFileName(
679 this, tr("Import File"), dir, tr(
680 "%1 files (*.*);;All Files (*.*)").arg(
681 QString::fromStdString(format->description())));
682
683 if (file_name.isEmpty())
48888313 684 return;
0f8f8c18
SA
685
686 // Show the options dialog
687 map<string, Glib::VariantBase> options;
688 if (!format->options().empty()) {
689 dialogs::InputOutputOptions dlg(
690 tr("Import %1").arg(QString::fromStdString(
691 format->description())),
692 format->options(), this);
693 if (!dlg.exec())
694 return;
695 options = dlg.options();
48888313 696 }
e13e7879 697
fd22c71c 698 session_.load_file(file_name, format, options);
0f8f8c18
SA
699
700 const QString abs_path = QFileInfo(file_name).absolutePath();
701 settings.setValue(SettingOpenDirectory, abs_path);
dde1a563
JH
702}
703
7c657094 704void MainBar::on_device_selected()
dde1a563 705{
da30ecb7 706 shared_ptr<devices::Device> device = device_selector_.selected_device();
0f8f8c18
SA
707 if (!device) {
708 reset_device_selector();
19adbc2c 709 return;
0f8f8c18 710 }
19adbc2c 711
fd22c71c 712 session_.select_device(device);
91f8fe8c 713}
51d4a9ab 714
91f8fe8c
SA
715void MainBar::on_device_changed()
716{
717 update_device_list();
e95e8563 718 update_device_config_widgets();
dde1a563 719}
51e77110 720
f4ce3e77
SA
721void MainBar::on_capture_state_changed(int state)
722{
723 set_capture_state((pv::Session::capture_state)state);
724}
725
7c657094 726void MainBar::on_sample_count_changed()
124d97de 727{
449aec3c
SA
728 if (!updating_sample_count_)
729 commit_sample_count();
124d97de
ML
730}
731
7c657094 732void MainBar::on_sample_rate_changed()
48888313 733{
0e0b6b3e
SA
734 if (!updating_sample_rate_)
735 commit_sample_rate();
48888313
JH
736}
737
7c657094 738void MainBar::on_config_changed()
82afd5e3
JH
739{
740 commit_sample_count();
c063290a 741 commit_sample_rate();
82afd5e3
JH
742}
743
c9da5118
SA
744void MainBar::on_actionNewView_triggered()
745{
746 new_view(&session_);
747}
748
0f8f8c18
SA
749void MainBar::on_actionOpen_triggered()
750{
751 QSettings settings;
752 const QString dir = settings.value(SettingOpenDirectory).toString();
753
754 // Show the dialog
755 const QString file_name = QFileDialog::getOpenFileName(
756 this, tr("Open File"), dir, tr(
757 "Sigrok Sessions (*.sr);;"
758 "All Files (*.*)"));
759
760 if (!file_name.isEmpty()) {
fd22c71c 761 session_.load_file(file_name);
0f8f8c18
SA
762
763 const QString abs_path = QFileInfo(file_name).absolutePath();
764 settings.setValue(SettingOpenDirectory, abs_path);
765 }
766}
767
768void MainBar::on_actionSaveAs_triggered()
769{
770 export_file(session_.device_manager().context()->output_formats()["srzip"]);
771}
772
773void MainBar::on_actionSaveSelectionAs_triggered()
774{
775 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
776}
777
778void MainBar::on_actionConnect_triggered()
779{
780 // Stop any currently running capture session
781 session_.stop_capture();
782
783 dialogs::Connect dlg(this, session_.device_manager());
784
785 // If the user selected a device, select it in the device list. Select the
786 // current device otherwise.
787 if (dlg.exec())
fd22c71c 788 session_.select_device(dlg.get_selected_device());
0f8f8c18
SA
789
790 update_device_list();
791}
792
e0ba4f6f 793void MainBar::add_toolbar_widgets()
0f8f8c18 794{
e0ba4f6f
SA
795 addAction(action_new_view_);
796 addSeparator();
797 addWidget(open_button_);
798 addWidget(save_button_);
799 addSeparator();
0f8f8c18 800
e0ba4f6f 801 StandardBar::add_toolbar_widgets();
0f8f8c18 802
e0ba4f6f
SA
803 addWidget(&device_selector_);
804 configure_button_action_ = addWidget(&configure_button_);
805 channels_button_action_ = addWidget(&channels_button_);
806 addWidget(&sample_count_);
807 addWidget(&sample_rate_);
808#ifdef ENABLE_DECODE
809 addSeparator();
810 addWidget(add_decoder_button_);
811#endif
d552c5c7
SA
812}
813
7c657094 814bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 815{
2ad82c2e
UH
816 if (sample_count_supported_ && (watched == &sample_count_ ||
817 watched == &sample_rate_) &&
818 (event->type() == QEvent::ToolTip)) {
60d9b99a 819 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
40065ab6 820 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45 821
3ccf0f7f
JS
822 QString str = tr("Total sampling time: %1").arg(
823 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
40065ab6
JS
824 QToolTip::showText(help_event->globalPos(), str);
825
826 return true;
827 }
828
829 return false;
830}
831
f4c92e1c 832} // namespace toolbars
51e77110 833} // namespace pv