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