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