]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
Fix #957 and #874 by implementing the pane splitter
[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
d375140b
SA
117 setContextMenuPolicy(Qt::PreventContextMenu);
118
0f8f8c18 119 // Actions
c9da5118
SA
120 action_new_view_->setText(tr("New &View"));
121 action_new_view_->setIcon(QIcon::fromTheme("window-new",
122 QIcon(":/icons/window-new.png")));
123 connect(action_new_view_, SIGNAL(triggered(bool)),
124 this, SLOT(on_actionNewView_triggered()));
125
0f8f8c18
SA
126 action_open_->setText(tr("&Open..."));
127 action_open_->setIcon(QIcon::fromTheme("document-open",
128 QIcon(":/icons/document-open.png")));
129 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
d552c5c7
SA
130 connect(action_open_, SIGNAL(triggered(bool)),
131 this, SLOT(on_actionOpen_triggered()));
0f8f8c18
SA
132
133 action_save_as_->setText(tr("&Save As..."));
134 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
135 QIcon(":/icons/document-save-as.png")));
136 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
d552c5c7
SA
137 connect(action_save_as_, SIGNAL(triggered(bool)),
138 this, SLOT(on_actionSaveAs_triggered()));
0f8f8c18
SA
139
140 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
141 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
142 QIcon(":/icons/document-save-as.png")));
143 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
d552c5c7
SA
144 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
145 this, SLOT(on_actionSaveSelectionAs_triggered()));
0f8f8c18
SA
146
147 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
148 session.device_manager().context());
149 menu_file_export->setTitle(tr("&Export"));
150 connect(menu_file_export,
6f925ba9
UH
151 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
152 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
0f8f8c18
SA
153
154 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
155 session.device_manager().context());
156 menu_file_import->setTitle(tr("&Import"));
157 connect(menu_file_import,
6f925ba9
UH
158 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
159 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
0f8f8c18
SA
160
161 action_connect_->setText(tr("&Connect to Device..."));
d552c5c7
SA
162 connect(action_connect_, SIGNAL(triggered(bool)),
163 this, SLOT(on_actionConnect_triggered()));
0f8f8c18 164
ed43ef2e 165 // Open button
ed43ef2e 166 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
0f8f8c18 167 session.device_manager().context(), action_open_);
ed43ef2e 168 connect(import_menu,
6f925ba9 169 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
d552c5c7 170 this,
6f925ba9 171 SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
ed43ef2e 172
e0ba4f6f
SA
173 open_button_->setMenu(import_menu);
174 open_button_->setDefaultAction(action_open_);
175 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
ed43ef2e 176
998b89fd 177 // Save button
686d9151 178 vector<QAction *> open_actions;
0f8f8c18
SA
179 open_actions.push_back(action_save_as_);
180 open_actions.push_back(action_save_selection_as_);
686d9151 181
998b89fd
JH
182 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
183 session.device_manager().context(),
686d9151 184 open_actions);
998b89fd 185 connect(export_menu,
6f925ba9 186 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
d552c5c7 187 this,
6f925ba9 188 SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
998b89fd 189
e0ba4f6f
SA
190 save_button_->setMenu(export_menu);
191 save_button_->setDefaultAction(action_save_as_);
192 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
998b89fd 193
079d39ea
JH
194 // Device selector menu
195 connect(&device_selector_, SIGNAL(device_selected()),
196 this, SLOT(on_device_selected()));
197
168888e2
JH
198 // Setup the decoder button
199#ifdef ENABLE_DECODE
0f8f8c18
SA
200 menu_decoders_add_->setTitle(tr("&Add"));
201 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
202 this, SLOT(add_decoder(srd_decoder*)));
203
4d8130bb 204 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
e0ba4f6f
SA
205 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
206 add_decoder_button_->setMenu(menu_decoders_add_);
ae95fe58 207 add_decoder_button_->setToolTip(tr("Add low-level, non-stacked protocol decoder"));
168888e2
JH
208#endif
209
8dbbc7f0 210 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 211 this, SLOT(on_sample_count_changed()));
8dbbc7f0 212 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 213 this, SLOT(on_sample_rate_changed()));
dde1a563 214
8dbbc7f0 215 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 216
2b81ae46 217 set_capture_state(pv::Session::Stopped);
274d4f13 218
5f66b56e 219 configure_button_.setToolTip(tr("Configure Device"));
3432032f
UH
220 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
221 QIcon(":/icons/preferences-system.png")));
b7b659aa 222
5f66b56e 223 channels_button_.setToolTip(tr("Configure Channels"));
4d8130bb 224 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
cdb50f67 225
e0ba4f6f 226 add_toolbar_widgets();
40065ab6 227
8dbbc7f0
JH
228 sample_count_.installEventFilter(this);
229 sample_rate_.installEventFilter(this);
0f8f8c18 230
0f8f8c18 231 // Setup session_ events
d552c5c7 232 connect(&session_, SIGNAL(capture_state_changed(int)),
f4ce3e77 233 this, SLOT(on_capture_state_changed(int)));
d552c5c7
SA
234 connect(&session, SIGNAL(device_changed()),
235 this, SLOT(on_device_changed()));
0f8f8c18 236
101e7a9b 237 update_device_list();
0f8f8c18
SA
238}
239
079d39ea 240void MainBar::update_device_list()
18203d86 241{
079d39ea 242 DeviceManager &mgr = session_.device_manager();
da30ecb7
JH
243 shared_ptr<devices::Device> selected_device = session_.device();
244 list< shared_ptr<devices::Device> > devs;
e8d00928 245
079d39ea 246 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
53a7cce4 247
079d39ea
JH
248 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
249 devs.push_back(selected_device);
e95e8563 250
079d39ea 251 device_selector_.set_device_list(devs, selected_device);
e95e8563 252 update_device_config_widgets();
18203d86
JH
253}
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;
6f925ba9 297 map< const ConfigKey*, 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;
348f9cec 407 bool default_count_set = false;
3cbffdcf 408
348f9cec 409 if (sample_count == 0) {
3e7636f9 410 sample_count = DefaultSampleCount;
348f9cec
SA
411 default_count_set = true;
412 }
b50ef520 413
7bb0fbf4
ML
414 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
415 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
416 if (gvar.gobj())
417 g_variant_get(gvar.gobj(), "(tt)",
418 &min_sample_count, &max_sample_count);
ed1d9d81 419 }
1d04852f 420
3e7636f9
JH
421 min_sample_count = min(max(min_sample_count, MinSampleCount),
422 max_sample_count);
b50ef520 423
c063290a 424 sample_count_.show_125_list(min_sample_count, max_sample_count);
b50ef520 425
7bb0fbf4 426 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
da30ecb7 427 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
3e7636f9 428 sample_count = g_variant_get_uint64(gvar.gobj());
348f9cec 429 if (sample_count == 0) {
3e7636f9 430 sample_count = DefaultSampleCount;
348f9cec
SA
431 default_count_set = true;
432 }
3e7636f9
JH
433 sample_count = min(max(sample_count, MinSampleCount),
434 max_sample_count);
7bb0fbf4 435 }
3e7636f9
JH
436
437 sample_count_.set_value(sample_count);
df3c1aa3 438
8dbbc7f0 439 updating_sample_count_ = false;
348f9cec
SA
440
441 // If we show the default rate then make sure the device uses the same
442 if (default_count_set)
443 commit_sample_count();
85756012
ML
444}
445
7c657094 446void MainBar::update_device_config_widgets()
e95e8563 447{
e95e8563
JH
448 using namespace pv::popups;
449
da30ecb7
JH
450 const shared_ptr<devices::Device> device =
451 device_selector_.selected_device();
9dd88889
JH
452
453 // Hide the widgets if no device is selected
454 channels_button_action_->setVisible(!!device);
9dd88889
JH
455 if (!device) {
456 configure_button_action_->setVisible(false);
457 sample_count_.show_none();
458 sample_rate_.show_none();
e95e8563 459 return;
9dd88889 460 }
e95e8563 461
da30ecb7
JH
462 const shared_ptr<sigrok::Device> sr_dev = device->device();
463 if (!sr_dev)
464 return;
465
e95e8563 466 // Update the configure popup
da30ecb7 467 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
8dbbc7f0 468 configure_button_action_->setVisible(
e95e8563 469 !opts->binding().properties().empty());
8dbbc7f0 470 configure_button_.set_popup(opts);
e95e8563 471
6ac6242b 472 // Update the channels popup
8dbbc7f0
JH
473 Channels *const channels = new Channels(session_, this);
474 channels_button_.set_popup(channels);
e95e8563
JH
475
476 // Update supported options.
8dbbc7f0 477 sample_count_supported_ = false;
e95e8563 478
7bb0fbf4
ML
479 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
480 sample_count_supported_ = true;
481
482 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
483 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
484 Glib::Variant<guint64>::create(1));
485 on_config_changed();
486 }
e95e8563
JH
487
488 // Add notification of reconfigure events
489 disconnect(this, SLOT(on_config_changed()));
e8d00928 490 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
491 this, SLOT(on_config_changed()));
492
493 // Update sweep timing widgets.
494 update_sample_count_selector();
495 update_sample_rate_selector();
496}
497
0f8f8c18
SA
498void MainBar::commit_sample_rate()
499{
500 uint64_t sample_rate = 0;
501
502 const shared_ptr<devices::Device> device =
503 device_selector_.selected_device();
504 if (!device)
505 return;
506
507 const shared_ptr<sigrok::Device> sr_dev = device->device();
508
509 sample_rate = sample_rate_.value();
510 if (sample_rate == 0)
511 return;
512
513 try {
514 sr_dev->config_set(ConfigKey::SAMPLERATE,
515 Glib::Variant<guint64>::create(sample_rate));
516 update_sample_rate_selector();
517 } catch (Error error) {
518 qDebug() << "Failed to configure samplerate.";
519 return;
520 }
521
522 // Devices with built-in memory might impose limits on certain
523 // configurations, so let's check what sample count the driver
524 // lets us use now.
525 update_sample_count_selector();
526}
527
7c657094 528void MainBar::commit_sample_count()
124d97de
ML
529{
530 uint64_t sample_count = 0;
531
da30ecb7
JH
532 const shared_ptr<devices::Device> device =
533 device_selector_.selected_device();
e8d00928 534 if (!device)
19adbc2c
JH
535 return;
536
da30ecb7
JH
537 const shared_ptr<sigrok::Device> sr_dev = device->device();
538
8dbbc7f0 539 sample_count = sample_count_.value();
2ad82c2e 540 if (sample_count_supported_) {
e8d00928 541 try {
da30ecb7 542 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 543 Glib::Variant<guint64>::create(sample_count));
449aec3c 544 update_sample_count_selector();
e8d00928
ML
545 } catch (Error error) {
546 qDebug() << "Failed to configure sample count.";
547 return;
548 }
124d97de 549 }
e13e7879
SA
550
551 // Devices with built-in memory might impose limits on certain
552 // configurations, so let's check what sample rate the driver
553 // lets us use now.
554 update_sample_rate_selector();
124d97de
ML
555}
556
0f8f8c18 557void MainBar::session_error(const QString text, const QString info_text)
48888313 558{
0f8f8c18
SA
559 QMetaObject::invokeMethod(this, "show_session_error",
560 Qt::QueuedConnection, Q_ARG(QString, text),
561 Q_ARG(QString, info_text));
562}
48888313 563
0f8f8c18
SA
564void MainBar::show_session_error(const QString text, const QString info_text)
565{
566 QMessageBox msg(this);
567 msg.setText(text);
568 msg.setInformativeText(info_text);
569 msg.setStandardButtons(QMessageBox::Ok);
570 msg.setIcon(QMessageBox::Warning);
571 msg.exec();
572}
19adbc2c 573
0f8f8c18
SA
574void MainBar::add_decoder(srd_decoder *decoder)
575{
576#ifdef ENABLE_DECODE
577 assert(decoder);
578 session_.add_decoder(decoder);
579#else
580 (void)decoder;
581#endif
582}
583
c063290a 584void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
0f8f8c18
SA
585{
586 using pv::dialogs::StoreProgress;
587
588 // Stop any currently running capture session
589 session_.stop_capture();
590
591 QSettings settings;
592 const QString dir = settings.value(SettingSaveDirectory).toString();
593
6f925ba9 594 pair<uint64_t, uint64_t> sample_range;
0f8f8c18
SA
595
596 // Selection only? Verify that the cursors are active and fetch their values
597 if (selection_only) {
f4e57597
SA
598 views::TraceView::View *trace_view =
599 qobject_cast<views::TraceView::View*>(session_.main_view().get());
600
601 if (!trace_view->cursors()->enabled()) {
0f8f8c18
SA
602 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
603 "cursors before you can save the data enclosed by them " \
e0ddc2a4 604 "to a session file (e.g. using the Show Cursors button)."));
0f8f8c18
SA
605 return;
606 }
607
608 const double samplerate = session_.get_samplerate();
609
f4e57597
SA
610 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
611 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
0f8f8c18 612
6f925ba9 613 const uint64_t start_sample = (uint64_t)max(
49ec9e07 614 (double)0, start_time.convert_to<double>() * samplerate);
6f925ba9 615 const uint64_t end_sample = (uint64_t)max(
49ec9e07 616 (double)0, end_time.convert_to<double>() * samplerate);
0f8f8c18 617
6f925ba9 618 sample_range = make_pair(start_sample, end_sample);
0f8f8c18 619 } else {
6f925ba9 620 sample_range = make_pair(0, 0);
0f8f8c18
SA
621 }
622
623 // Construct the filter
624 const vector<string> exts = format->extensions();
625 QString filter = tr("%1 files ").arg(
626 QString::fromStdString(format->description()));
627
628 if (exts.empty())
629 filter += "(*.*)";
630 else
631 filter += QString("(*.%1);;%2 (*.*)").arg(
632 QString::fromStdString(join(exts, ", *.")),
633 tr("All Files"));
634
635 // Show the file dialog
636 const QString file_name = QFileDialog::getSaveFileName(
637 this, tr("Save File"), dir, filter);
638
639 if (file_name.isEmpty())
f9541bde
JH
640 return;
641
0f8f8c18
SA
642 const QString abs_path = QFileInfo(file_name).absolutePath();
643 settings.setValue(SettingSaveDirectory, abs_path);
644
645 // Show the options dialog
646 map<string, Glib::VariantBase> options;
647 if (!format->options().empty()) {
648 dialogs::InputOutputOptions dlg(
649 tr("Export %1").arg(QString::fromStdString(
650 format->description())),
651 format->options(), this);
652 if (!dlg.exec())
653 return;
654 options = dlg.options();
655 }
656
c32bf63a
SA
657 if (!selection_only)
658 session_.set_name(QFileInfo(file_name).fileName());
101e7a9b 659
0f8f8c18
SA
660 StoreProgress *dlg = new StoreProgress(file_name, format, options,
661 sample_range, session_, this);
662 dlg->run();
663}
664
665void MainBar::import_file(shared_ptr<InputFormat> format)
666{
667 assert(format);
668
669 QSettings settings;
670 const QString dir = settings.value(SettingOpenDirectory).toString();
671
672 // Construct the filter
673 const vector<string> exts = format->extensions();
674 const QString filter = exts.empty() ? "" :
675 tr("%1 files (*.%2)").arg(
676 QString::fromStdString(format->description()),
677 QString::fromStdString(join(exts, ", *.")));
678
679 // Show the file dialog
680 const QString file_name = QFileDialog::getOpenFileName(
681 this, tr("Import File"), dir, tr(
682 "%1 files (*.*);;All Files (*.*)").arg(
683 QString::fromStdString(format->description())));
684
685 if (file_name.isEmpty())
48888313 686 return;
0f8f8c18
SA
687
688 // Show the options dialog
689 map<string, Glib::VariantBase> options;
690 if (!format->options().empty()) {
691 dialogs::InputOutputOptions dlg(
692 tr("Import %1").arg(QString::fromStdString(
693 format->description())),
694 format->options(), this);
695 if (!dlg.exec())
696 return;
697 options = dlg.options();
48888313 698 }
e13e7879 699
fd22c71c 700 session_.load_file(file_name, format, options);
0f8f8c18
SA
701
702 const QString abs_path = QFileInfo(file_name).absolutePath();
703 settings.setValue(SettingOpenDirectory, abs_path);
dde1a563
JH
704}
705
7c657094 706void MainBar::on_device_selected()
dde1a563 707{
da30ecb7 708 shared_ptr<devices::Device> device = device_selector_.selected_device();
0f8f8c18
SA
709 if (!device) {
710 reset_device_selector();
19adbc2c 711 return;
0f8f8c18 712 }
19adbc2c 713
fd22c71c 714 session_.select_device(device);
91f8fe8c 715}
51d4a9ab 716
91f8fe8c
SA
717void MainBar::on_device_changed()
718{
719 update_device_list();
e95e8563 720 update_device_config_widgets();
dde1a563 721}
51e77110 722
f4ce3e77
SA
723void MainBar::on_capture_state_changed(int state)
724{
725 set_capture_state((pv::Session::capture_state)state);
726}
727
7c657094 728void MainBar::on_sample_count_changed()
124d97de 729{
449aec3c
SA
730 if (!updating_sample_count_)
731 commit_sample_count();
124d97de
ML
732}
733
7c657094 734void MainBar::on_sample_rate_changed()
48888313 735{
0e0b6b3e
SA
736 if (!updating_sample_rate_)
737 commit_sample_rate();
48888313
JH
738}
739
7c657094 740void MainBar::on_config_changed()
82afd5e3
JH
741{
742 commit_sample_count();
c063290a 743 commit_sample_rate();
82afd5e3
JH
744}
745
c9da5118
SA
746void MainBar::on_actionNewView_triggered()
747{
748 new_view(&session_);
749}
750
0f8f8c18
SA
751void MainBar::on_actionOpen_triggered()
752{
753 QSettings settings;
754 const QString dir = settings.value(SettingOpenDirectory).toString();
755
756 // Show the dialog
757 const QString file_name = QFileDialog::getOpenFileName(
758 this, tr("Open File"), dir, tr(
759 "Sigrok Sessions (*.sr);;"
760 "All Files (*.*)"));
761
762 if (!file_name.isEmpty()) {
fd22c71c 763 session_.load_file(file_name);
0f8f8c18
SA
764
765 const QString abs_path = QFileInfo(file_name).absolutePath();
766 settings.setValue(SettingOpenDirectory, abs_path);
767 }
768}
769
770void MainBar::on_actionSaveAs_triggered()
771{
772 export_file(session_.device_manager().context()->output_formats()["srzip"]);
773}
774
775void MainBar::on_actionSaveSelectionAs_triggered()
776{
777 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
778}
779
780void MainBar::on_actionConnect_triggered()
781{
782 // Stop any currently running capture session
783 session_.stop_capture();
784
785 dialogs::Connect dlg(this, session_.device_manager());
786
787 // If the user selected a device, select it in the device list. Select the
788 // current device otherwise.
789 if (dlg.exec())
fd22c71c 790 session_.select_device(dlg.get_selected_device());
0f8f8c18
SA
791
792 update_device_list();
793}
794
e0ba4f6f 795void MainBar::add_toolbar_widgets()
0f8f8c18 796{
e0ba4f6f
SA
797 addAction(action_new_view_);
798 addSeparator();
799 addWidget(open_button_);
800 addWidget(save_button_);
801 addSeparator();
0f8f8c18 802
e0ba4f6f 803 StandardBar::add_toolbar_widgets();
0f8f8c18 804
e0ba4f6f
SA
805 addWidget(&device_selector_);
806 configure_button_action_ = addWidget(&configure_button_);
807 channels_button_action_ = addWidget(&channels_button_);
808 addWidget(&sample_count_);
809 addWidget(&sample_rate_);
810#ifdef ENABLE_DECODE
811 addSeparator();
812 addWidget(add_decoder_button_);
813#endif
d552c5c7
SA
814}
815
7c657094 816bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 817{
2ad82c2e
UH
818 if (sample_count_supported_ && (watched == &sample_count_ ||
819 watched == &sample_rate_) &&
820 (event->type() == QEvent::ToolTip)) {
60d9b99a 821 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
40065ab6 822 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45 823
3ccf0f7f
JS
824 QString str = tr("Total sampling time: %1").arg(
825 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
40065ab6
JS
826 QToolTip::showText(help_event->globalPos(), str);
827
828 return true;
829 }
830
831 return false;
832}
833
f4c92e1c 834} // namespace toolbars
51e77110 835} // namespace pv