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