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