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