CMakeLists.txt: Remove -Wunqualified-std-cast-call
[pulseview.git] / pv / toolbars / mainbar.cpp
blobcd46c063bdc8a84d78d1e42e1c616c2fc8a3b270
1 /*
2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <extdef.h>
22 #include <algorithm>
23 #include <cassert>
25 #include <QAction>
26 #include <QDebug>
27 #include <QFileDialog>
28 #include <QHelpEvent>
29 #include <QMenu>
30 #include <QMessageBox>
31 #include <QSettings>
32 #include <QToolTip>
34 #include "mainbar.hpp"
36 #include <boost/algorithm/string/join.hpp>
38 #include <pv/data/mathsignal.hpp>
39 #include <pv/devicemanager.hpp>
40 #include <pv/devices/hardwaredevice.hpp>
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>
46 #include <pv/mainwindow.hpp>
47 #include <pv/popups/channels.hpp>
48 #include <pv/popups/deviceoptions.hpp>
49 #include <pv/util.hpp>
50 #include <pv/views/trace/view.hpp>
51 #include <pv/widgets/exportmenu.hpp>
52 #include <pv/widgets/importmenu.hpp>
53 #ifdef ENABLE_DECODE
54 #include <pv/data/decodesignal.hpp>
55 #endif
57 #include <libsigrokcxx/libsigrokcxx.hpp>
59 using std::back_inserter;
60 using std::copy;
61 using std::list;
62 using std::make_pair;
63 using std::make_shared;
64 using std::map;
65 using std::max;
66 using std::min;
67 using std::pair;
68 using std::set;
69 using std::shared_ptr;
70 using std::string;
71 using std::vector;
73 using sigrok::Capability;
74 using sigrok::ConfigKey;
75 using sigrok::Error;
76 using sigrok::InputFormat;
77 using sigrok::OutputFormat;
79 using boost::algorithm::join;
81 namespace pv {
82 namespace toolbars {
84 const uint64_t MainBar::MinSampleCount = 100ULL;
85 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
86 const uint64_t MainBar::DefaultSampleCount = 1000000;
88 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
89 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
91 MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) :
92 StandardBar(session, parent, view, false),
93 action_new_view_(new QAction(this)),
94 action_open_(new QAction(this)),
95 action_save_(new QAction(this)),
96 action_save_as_(new QAction(this)),
97 action_save_selection_as_(new QAction(this)),
98 action_restore_setup_(new QAction(this)),
99 action_save_setup_(new QAction(this)),
100 action_connect_(new QAction(this)),
101 new_view_button_(new QToolButton()),
102 open_button_(new QToolButton()),
103 save_button_(new QToolButton()),
104 device_selector_(parent, session.device_manager(), action_connect_),
105 configure_button_(this),
106 configure_button_action_(nullptr),
107 channels_button_(this),
108 channels_button_action_(nullptr),
109 sample_count_(" samples", this),
110 sample_rate_("Hz", this),
111 updating_sample_rate_(false),
112 updating_sample_count_(false),
113 sample_count_supported_(false),
114 #ifdef ENABLE_DECODE
115 add_decoder_button_(new QToolButton()),
116 #endif
117 add_math_signal_button_(new QToolButton())
119 setObjectName(QString::fromUtf8("MainBar"));
121 setContextMenuPolicy(Qt::PreventContextMenu);
123 // Actions
124 action_new_view_->setText(tr("New &View"));
125 action_new_view_->setIcon(QIcon::fromTheme("window-new",
126 QIcon(":/icons/window-new.png")));
127 connect(action_new_view_, SIGNAL(triggered(bool)),
128 this, SLOT(on_actionNewView_triggered()));
130 action_open_->setText(tr("&Open..."));
131 action_open_->setIcon(QIcon::fromTheme("document-open",
132 QIcon(":/icons/document-open.png")));
133 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
134 action_open_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O));
135 #else
136 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
137 #endif
138 connect(action_open_, SIGNAL(triggered(bool)),
139 this, SLOT(on_actionOpen_triggered()));
141 action_restore_setup_->setText(tr("Restore Session Setu&p..."));
142 connect(action_restore_setup_, SIGNAL(triggered(bool)),
143 this, SLOT(on_actionRestoreSetup_triggered()));
145 action_save_->setText(tr("&Save..."));
146 action_save_->setIcon(QIcon::fromTheme("document-save-as",
147 QIcon(":/icons/document-save-as.png")));
148 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
149 action_save_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
150 #else
151 action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
152 #endif
153 connect(action_save_, SIGNAL(triggered(bool)),
154 this, SLOT(on_actionSave_triggered()));
156 action_save_as_->setText(tr("Save &As..."));
157 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
158 QIcon(":/icons/document-save-as.png")));
159 connect(action_save_as_, SIGNAL(triggered(bool)),
160 this, SLOT(on_actionSaveAs_triggered()));
162 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
163 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
164 QIcon(":/icons/document-save-as.png")));
165 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
166 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_R));
167 #else
168 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
169 #endif
170 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
171 this, SLOT(on_actionSaveSelectionAs_triggered()));
173 action_save_setup_->setText(tr("Save Session Setu&p..."));
174 connect(action_save_setup_, SIGNAL(triggered(bool)),
175 this, SLOT(on_actionSaveSetup_triggered()));
177 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
178 session.device_manager().context());
179 menu_file_export->setTitle(tr("&Export"));
180 connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
181 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
183 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
184 session.device_manager().context());
185 menu_file_import->setTitle(tr("&Import"));
186 connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
187 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
189 action_connect_->setText(tr("&Connect to Device..."));
190 connect(action_connect_, SIGNAL(triggered(bool)),
191 this, SLOT(on_actionConnect_triggered()));
193 // New view button
194 QMenu *menu_new_view = new QMenu();
195 connect(menu_new_view, SIGNAL(triggered(QAction*)),
196 this, SLOT(on_actionNewView_triggered(QAction*)));
198 for (int i = 0; i < views::ViewTypeCount; i++) {
199 QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i]));
200 action->setData(QVariant::fromValue(i));
203 new_view_button_->setMenu(menu_new_view);
204 new_view_button_->setDefaultAction(action_new_view_);
205 new_view_button_->setPopupMode(QToolButton::MenuButtonPopup);
207 // Open button
208 vector<QAction*> open_actions;
209 open_actions.push_back(action_open_);
210 QAction* separator_o = new QAction(this);
211 separator_o->setSeparator(true);
212 open_actions.push_back(separator_o);
213 open_actions.push_back(action_restore_setup_);
215 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
216 session.device_manager().context(), open_actions);
217 connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
218 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
220 open_button_->setMenu(import_menu);
221 open_button_->setDefaultAction(action_open_);
222 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
224 // Save button
225 vector<QAction*> save_actions;
226 save_actions.push_back(action_save_);
227 save_actions.push_back(action_save_as_);
228 save_actions.push_back(action_save_selection_as_);
229 QAction* separator_s = new QAction(this);
230 separator_s->setSeparator(true);
231 save_actions.push_back(separator_s);
232 save_actions.push_back(action_save_setup_);
234 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
235 session.device_manager().context(), save_actions);
236 connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
237 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
239 save_button_->setMenu(export_menu);
240 save_button_->setDefaultAction(action_save_);
241 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
243 // Device selector menu
244 connect(&device_selector_, SIGNAL(device_selected()),
245 this, SLOT(on_device_selected()));
247 // Setup the decoder button
248 #ifdef ENABLE_DECODE
249 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
250 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
251 add_decoder_button_->setToolTip(tr("Add protocol decoder"));
252 add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D));
254 connect(add_decoder_button_, SIGNAL(clicked()),
255 this, SLOT(on_add_decoder_clicked()));
256 #endif
258 // Setup the math signal button
259 add_math_signal_button_->setIcon(QIcon(":/icons/add-math-signal.svg"));
260 add_math_signal_button_->setPopupMode(QToolButton::InstantPopup);
261 add_math_signal_button_->setToolTip(tr("Add math signal"));
262 add_math_signal_button_->setShortcut(QKeySequence(Qt::Key_M));
264 connect(add_math_signal_button_, SIGNAL(clicked()),
265 this, SLOT(on_add_math_signal_clicked()));
268 connect(&sample_count_, SIGNAL(value_changed()),
269 this, SLOT(on_sample_count_changed()));
270 connect(&sample_rate_, SIGNAL(value_changed()),
271 this, SLOT(on_sample_rate_changed()));
273 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
275 set_capture_state(pv::Session::Stopped);
277 configure_button_.setToolTip(tr("Configure Device"));
278 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
279 QIcon(":/icons/preferences-system.png")));
281 channels_button_.setToolTip(tr("Configure Channels"));
282 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
284 add_toolbar_widgets();
286 sample_count_.installEventFilter(this);
287 sample_rate_.installEventFilter(this);
289 // Setup session_ events
290 connect(&session_, SIGNAL(capture_state_changed(int)),
291 this, SLOT(on_capture_state_changed(int)));
292 connect(&session, SIGNAL(device_changed()),
293 this, SLOT(on_device_changed()));
295 update_device_list();
298 void MainBar::update_device_list()
300 DeviceManager &mgr = session_.device_manager();
301 shared_ptr<devices::Device> selected_device = session_.device();
302 list< shared_ptr<devices::Device> > devs;
304 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
306 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
307 devs.push_back(selected_device);
309 device_selector_.set_device_list(devs, selected_device);
310 update_device_config_widgets();
313 void MainBar::set_capture_state(pv::Session::capture_state state)
315 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
317 device_selector_.setEnabled(ui_enabled);
318 configure_button_.setEnabled(ui_enabled);
319 channels_button_.setEnabled(ui_enabled);
320 sample_count_.setEnabled(ui_enabled);
321 sample_rate_.setEnabled(ui_enabled);
324 void MainBar::reset_device_selector()
326 device_selector_.reset();
329 QAction* MainBar::action_open() const
331 return action_open_;
334 QAction* MainBar::action_save() const
336 return action_save_;
339 QAction* MainBar::action_save_as() const
341 return action_save_as_;
344 QAction* MainBar::action_save_selection_as() const
346 return action_save_selection_as_;
349 QAction* MainBar::action_connect() const
351 return action_connect_;
354 void MainBar::update_sample_rate_selector()
356 Glib::VariantContainerBase gvar_dict;
357 GVariant *gvar_list;
358 const uint64_t *elements = nullptr;
359 gsize num_elements;
360 map< const ConfigKey*, set<Capability> > keys;
362 if (updating_sample_rate_) {
363 sample_rate_.show_none();
364 return;
367 const shared_ptr<devices::Device> device = device_selector_.selected_device();
368 if (!device)
369 return;
371 assert(!updating_sample_rate_);
372 updating_sample_rate_ = true;
374 const shared_ptr<sigrok::Device> sr_dev = device->device();
376 sample_rate_.allow_user_entered_values(false);
377 if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
378 try {
379 auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
380 if (gvar.gobj()) {
381 bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
382 gvar).get();
383 sample_rate_.allow_user_entered_values(value);
385 } catch (Error& error) {
386 // Do nothing
391 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
392 try {
393 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
394 } catch (Error& error) {
395 qDebug() << tr("Failed to get sample rate list:") << error.what();
397 } else {
398 sample_rate_.show_none();
399 updating_sample_rate_ = false;
400 return;
403 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
404 "samplerate-steps", G_VARIANT_TYPE("at")))) {
405 elements = (const uint64_t *)g_variant_get_fixed_array(
406 gvar_list, &num_elements, sizeof(uint64_t));
408 const uint64_t min = elements[0];
409 const uint64_t max = elements[1];
410 const uint64_t step = elements[2];
412 g_variant_unref(gvar_list);
414 assert(min > 0);
415 assert(max > 0);
416 assert(max > min);
417 assert(step > 0);
419 if (step == 1)
420 sample_rate_.show_125_list(min, max);
421 else {
422 // When the step is not 1, we cam't make a 1-2-5-10
423 // list of sample rates, because we may not be able to
424 // make round numbers. Therefore in this case, show a
425 // spin box.
426 sample_rate_.show_min_max_step(min, max, step);
428 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
429 "samplerates", G_VARIANT_TYPE("at")))) {
430 elements = (const uint64_t *)g_variant_get_fixed_array(
431 gvar_list, &num_elements, sizeof(uint64_t));
432 sample_rate_.show_list(elements, num_elements);
433 g_variant_unref(gvar_list);
435 updating_sample_rate_ = false;
437 update_sample_rate_selector_value();
440 void MainBar::update_sample_rate_selector_value()
442 if (updating_sample_rate_)
443 return;
445 const shared_ptr<devices::Device> device = device_selector_.selected_device();
446 if (!device)
447 return;
449 try {
450 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
451 uint64_t samplerate =
452 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
453 assert(!updating_sample_rate_);
454 updating_sample_rate_ = true;
455 sample_rate_.set_value(samplerate);
456 updating_sample_rate_ = false;
457 } catch (Error& error) {
458 qDebug() << tr("Failed to get sample rate:") << error.what();
462 void MainBar::update_sample_count_selector()
464 if (updating_sample_count_)
465 return;
467 const shared_ptr<devices::Device> device = device_selector_.selected_device();
468 if (!device)
469 return;
471 const shared_ptr<sigrok::Device> sr_dev = device->device();
473 assert(!updating_sample_count_);
474 updating_sample_count_ = true;
476 if (!sample_count_supported_) {
477 sample_count_.show_none();
478 updating_sample_count_ = false;
479 return;
482 uint64_t sample_count = sample_count_.value();
483 uint64_t min_sample_count = 0;
484 uint64_t max_sample_count = MaxSampleCount;
485 bool default_count_set = false;
487 if (sample_count == 0) {
488 sample_count = DefaultSampleCount;
489 default_count_set = true;
492 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
493 try {
494 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
495 if (gvar.gobj())
496 g_variant_get(gvar.gobj(), "(tt)",
497 &min_sample_count, &max_sample_count);
498 } catch (Error& error) {
499 qDebug() << tr("Failed to get sample limit list:") << error.what();
503 min_sample_count = min(max(min_sample_count, MinSampleCount),
504 max_sample_count);
506 sample_count_.show_125_list(min_sample_count, max_sample_count);
508 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
509 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
510 sample_count = g_variant_get_uint64(gvar.gobj());
511 if (sample_count == 0) {
512 sample_count = DefaultSampleCount;
513 default_count_set = true;
515 sample_count = min(max(sample_count, MinSampleCount),
516 max_sample_count);
519 sample_count_.set_value(sample_count);
521 updating_sample_count_ = false;
523 // If we show the default rate then make sure the device uses the same
524 if (default_count_set)
525 commit_sample_count();
528 void MainBar::update_device_config_widgets()
530 using namespace pv::popups;
532 const shared_ptr<devices::Device> device = device_selector_.selected_device();
534 // Hide the widgets if no device is selected
535 channels_button_action_->setVisible(!!device);
536 if (!device) {
537 configure_button_action_->setVisible(false);
538 sample_count_.show_none();
539 sample_rate_.show_none();
540 return;
543 const shared_ptr<sigrok::Device> sr_dev = device->device();
544 if (!sr_dev)
545 return;
547 // Update the configure popup
548 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
549 configure_button_action_->setVisible(!opts->binding().properties().empty());
550 configure_button_.set_popup(opts);
552 // Update the channels popup
553 Channels *const channels = new Channels(session_, this);
554 channels_button_.set_popup(channels);
556 // Update supported options.
557 sample_count_supported_ = false;
559 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
560 sample_count_supported_ = true;
562 // Add notification of reconfigure events
563 // Note: No need to disconnect the previous signal as that QObject instance is destroyed
564 connect(&opts->binding(), SIGNAL(config_changed()),
565 this, SLOT(on_config_changed()));
567 // Update sweep timing widgets.
568 update_sample_count_selector();
569 update_sample_rate_selector();
572 void MainBar::commit_sample_rate()
574 uint64_t sample_rate = 0;
576 const shared_ptr<devices::Device> device = device_selector_.selected_device();
577 if (!device)
578 return;
580 const shared_ptr<sigrok::Device> sr_dev = device->device();
582 sample_rate = sample_rate_.value();
584 try {
585 sr_dev->config_set(ConfigKey::SAMPLERATE,
586 Glib::Variant<guint64>::create(sample_rate));
587 update_sample_rate_selector();
588 } catch (Error& error) {
589 qDebug() << tr("Failed to configure samplerate:") << error.what();
590 return;
593 // Devices with built-in memory might impose limits on certain
594 // configurations, so let's check what sample count the driver
595 // lets us use now.
596 update_sample_count_selector();
599 void MainBar::commit_sample_count()
601 uint64_t sample_count = 0;
603 const shared_ptr<devices::Device> device = device_selector_.selected_device();
604 if (!device)
605 return;
607 const shared_ptr<sigrok::Device> sr_dev = device->device();
609 sample_count = sample_count_.value();
610 if (sample_count_supported_) {
611 try {
612 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
613 Glib::Variant<guint64>::create(sample_count));
614 update_sample_count_selector();
615 } catch (Error& error) {
616 qDebug() << tr("Failed to configure sample count:") << error.what();
617 return;
621 // Devices with built-in memory might impose limits on certain
622 // configurations, so let's check what sample rate the driver
623 // lets us use now.
624 update_sample_rate_selector();
627 void MainBar::show_session_error(const QString text, const QString info_text)
629 QMessageBox msg(this);
630 msg.setText(text + "\n\n" + info_text);
631 msg.setStandardButtons(QMessageBox::Ok);
632 msg.setIcon(QMessageBox::Warning);
633 msg.exec();
636 void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name)
638 using pv::dialogs::StoreProgress;
640 // Stop any currently running capture session
641 session_.stop_capture();
643 QSettings settings;
644 const QString dir = settings.value(SettingSaveDirectory).toString();
646 pair<uint64_t, uint64_t> sample_range;
648 // Selection only? Verify that the cursors are active and fetch their values
649 if (selection_only) {
650 views::trace::View *trace_view =
651 qobject_cast<views::trace::View*>(session_.main_view().get());
653 if (!trace_view->cursors()->enabled()) {
654 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
655 "cursors before you can save the data enclosed by them " \
656 "to a session file (e.g. using the Show Cursors button)."));
657 return;
660 const double samplerate = session_.get_samplerate();
662 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
663 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
665 const uint64_t start_sample = (uint64_t)max(
666 0.0, start_time.convert_to<double>() * samplerate);
667 const uint64_t end_sample = (uint64_t)max(
668 0.0, end_time.convert_to<double>() * samplerate);
670 if ((start_sample == 0) && (end_sample == 0)) {
671 // Both cursors are negative and were clamped to 0
672 show_session_error(tr("Invalid Range"), tr("The cursors don't " \
673 "define a valid range of samples."));
674 return;
677 sample_range = make_pair(start_sample, end_sample);
678 } else {
679 sample_range = make_pair(0, 0);
682 // Construct the filter
683 const vector<string> exts = format->extensions();
684 QString filter = tr("%1 files ").arg(
685 QString::fromStdString(format->description()));
687 if (exts.empty())
688 filter += "(*)";
689 else
690 filter += QString("(*.%1);;%2 (*)").arg(
691 QString::fromStdString(join(exts, ", *.")),
692 tr("All Files"));
694 // Show the file dialog
695 if (file_name.isEmpty())
696 file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter);
698 if (file_name.isEmpty())
699 return;
701 const QString abs_path = QFileInfo(file_name).absolutePath();
702 settings.setValue(SettingSaveDirectory, abs_path);
704 // Show the options dialog
705 map<string, Glib::VariantBase> options;
706 if (!format->options().empty()) {
707 dialogs::InputOutputOptions dlg(
708 tr("Export %1").arg(QString::fromStdString(
709 format->description())),
710 format->options(), this);
711 if (!dlg.exec())
712 return;
713 options = dlg.options();
716 if (!selection_only) {
717 if (format == session_.device_manager().context()->output_formats()["srzip"]) {
718 session_.set_save_path(QFileInfo(file_name).absolutePath());
719 session_.set_name(QFileInfo(file_name).fileName());
720 } else
721 session_.set_save_path("");
724 StoreProgress *dlg = new StoreProgress(file_name, format, options,
725 sample_range, session_, this);
726 dlg->run();
729 void MainBar::import_file(shared_ptr<InputFormat> format)
731 assert(format);
733 QSettings settings;
734 const QString dir = settings.value(SettingOpenDirectory).toString();
736 // Construct the filter
737 const vector<string> exts = format->extensions();
738 const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
739 tr("%1 files").arg(QString::fromStdString(format->description())),
740 QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
741 const QString filter_all = QString::fromStdString("%1 (%2)").arg(
742 tr("All Files"), QString::fromStdString("*"));
743 const QString filter = QString::fromStdString("%1%2%3").arg(
744 exts.empty() ? "" : filter_exts,
745 exts.empty() ? "" : ";;",
746 filter_all);
748 // Show the file dialog
749 const QString file_name = QFileDialog::getOpenFileName(
750 this, tr("Import File"), dir, filter);
752 if (file_name.isEmpty())
753 return;
755 // Show the options dialog
756 map<string, Glib::VariantBase> options;
757 if (!format->options().empty()) {
758 dialogs::InputOutputOptions dlg(
759 tr("Import %1").arg(QString::fromStdString(
760 format->description())),
761 format->options(), this);
762 if (!dlg.exec())
763 return;
764 options = dlg.options();
767 session_.load_file(file_name, "", format, options);
769 const QString abs_path = QFileInfo(file_name).absolutePath();
770 settings.setValue(SettingOpenDirectory, abs_path);
773 void MainBar::on_device_selected()
775 shared_ptr<devices::Device> device = device_selector_.selected_device();
777 if (device)
778 session_.select_device(device);
779 else
780 reset_device_selector();
783 void MainBar::on_device_changed()
785 update_device_list();
786 update_device_config_widgets();
789 void MainBar::on_capture_state_changed(int state)
791 set_capture_state((pv::Session::capture_state)state);
794 void MainBar::on_sample_count_changed()
796 if (!updating_sample_count_)
797 commit_sample_count();
800 void MainBar::on_sample_rate_changed()
802 if (!updating_sample_rate_)
803 commit_sample_rate();
806 void MainBar::on_config_changed()
808 // We want to also call update_sample_rate_selector() here in case
809 // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
810 // commit_sample_rate() does this already, so we don't call it here
812 commit_sample_count();
813 commit_sample_rate();
816 void MainBar::on_actionNewView_triggered(QAction* action)
818 if (action)
819 new_view(&session_, action->data().toInt());
820 else
821 // When the icon of the button is clicked, we create a trace view
822 new_view(&session_, views::ViewTypeTrace);
825 void MainBar::on_actionOpen_triggered()
827 QSettings settings;
828 const QString dir = settings.value(SettingOpenDirectory).toString();
830 // Show the dialog
831 const QString file_name = QFileDialog::getOpenFileName(
832 this, tr("Open File"), dir, tr(
833 "sigrok Sessions (*.sr);;"
834 "All Files (*)"));
836 if (!file_name.isEmpty()) {
837 session_.load_file(file_name);
839 const QString abs_path = QFileInfo(file_name).absolutePath();
840 settings.setValue(SettingOpenDirectory, abs_path);
844 void MainBar::on_actionSave_triggered()
846 // A path is only set if we loaded/saved an srzip file before
847 if (session_.save_path().isEmpty()) {
848 on_actionSaveAs_triggered();
849 return;
852 QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name());
853 export_file(session_.device_manager().context()->output_formats()["srzip"], false,
854 fi.absoluteFilePath());
857 void MainBar::on_actionSaveAs_triggered()
859 export_file(session_.device_manager().context()->output_formats()["srzip"]);
862 void MainBar::on_actionSaveSelectionAs_triggered()
864 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
867 void MainBar::on_actionSaveSetup_triggered()
869 QSettings settings;
870 const QString dir = settings.value(SettingSaveDirectory).toString();
872 const QString file_name = QFileDialog::getSaveFileName(
873 this, tr("Save File"), dir, tr(
874 "PulseView Session Setups (*.pvs);;"
875 "All Files (*)"));
877 if (file_name.isEmpty())
878 return;
880 QSettings settings_storage(file_name, QSettings::IniFormat);
881 session_.save_setup(settings_storage);
884 void MainBar::on_actionRestoreSetup_triggered()
886 QSettings settings;
887 const QString dir = settings.value(SettingSaveDirectory).toString();
889 const QString file_name = QFileDialog::getOpenFileName(
890 this, tr("Open File"), dir, tr(
891 "PulseView Session Setups (*.pvs);;"
892 "All Files (*)"));
894 if (file_name.isEmpty())
895 return;
897 QSettings settings_storage(file_name, QSettings::IniFormat);
898 session_.restore_setup(settings_storage);
901 void MainBar::on_actionConnect_triggered()
903 // Stop any currently running capture session
904 session_.stop_capture();
906 dialogs::Connect dlg(this, session_.device_manager());
908 // If the user selected a device, select it in the device list. Select the
909 // current device otherwise.
910 if (dlg.exec())
911 session_.select_device(dlg.get_selected_device());
913 update_device_list();
916 void MainBar::on_add_decoder_clicked()
918 show_decoder_selector(&session_);
921 void MainBar::on_add_math_signal_clicked()
923 shared_ptr<data::SignalBase> signal = make_shared<data::MathSignal>(session_);
924 session_.add_generated_signal(signal);
927 void MainBar::add_toolbar_widgets()
929 addWidget(new_view_button_);
930 addSeparator();
931 addWidget(open_button_);
932 addWidget(save_button_);
933 addSeparator();
935 StandardBar::add_toolbar_widgets();
937 addWidget(&device_selector_);
938 configure_button_action_ = addWidget(&configure_button_);
939 channels_button_action_ = addWidget(&channels_button_);
940 addWidget(&sample_count_);
941 addWidget(&sample_rate_);
942 #ifdef ENABLE_DECODE
943 addSeparator();
944 addWidget(add_decoder_button_);
945 #endif
946 addWidget(add_math_signal_button_);
949 bool MainBar::eventFilter(QObject *watched, QEvent *event)
951 if (sample_count_supported_ && (watched == &sample_count_ ||
952 watched == &sample_rate_) &&
953 (event->type() == QEvent::ToolTip)) {
955 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
956 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
958 QString str = tr("Total sampling time: %1").arg(
959 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
960 QToolTip::showText(help_event->globalPos(), str);
962 return true;
965 return false;
968 } // namespace toolbars
969 } // namespace pv