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