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/>.
27 #include <QFileDialog>
30 #include <QMessageBox>
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>
54 #include <pv/data/decodesignal.hpp>
57 #include <libsigrokcxx/libsigrokcxx.hpp>
59 using std::back_inserter;
63 using std::make_shared;
69 using std::shared_ptr;
73 using sigrok::Capability;
74 using sigrok::ConfigKey;
76 using sigrok::InputFormat;
77 using sigrok::OutputFormat;
79 using boost::algorithm::join;
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),
115 add_decoder_button_(new QToolButton()),
117 add_math_signal_button_(new QToolButton())
119 setObjectName(QString::fromUtf8("MainBar"));
121 setContextMenuPolicy(Qt::PreventContextMenu);
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 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
134 connect(action_open_, SIGNAL(triggered(bool)),
135 this, SLOT(on_actionOpen_triggered()));
137 action_restore_setup_->setText(tr("Restore Session Setu&p..."));
138 connect(action_restore_setup_, SIGNAL(triggered(bool)),
139 this, SLOT(on_actionRestoreSetup_triggered()));
141 action_save_->setText(tr("&Save..."));
142 action_save_->setIcon(QIcon::fromTheme("document-save-as",
143 QIcon(":/icons/document-save-as.png")));
144 action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
145 connect(action_save_, SIGNAL(triggered(bool)),
146 this, SLOT(on_actionSave_triggered()));
148 action_save_as_->setText(tr("Save &As..."));
149 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
150 QIcon(":/icons/document-save-as.png")));
151 connect(action_save_as_, SIGNAL(triggered(bool)),
152 this, SLOT(on_actionSaveAs_triggered()));
154 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
155 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
156 QIcon(":/icons/document-save-as.png")));
157 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
158 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
159 this, SLOT(on_actionSaveSelectionAs_triggered()));
161 action_save_setup_->setText(tr("Save Session Setu&p..."));
162 connect(action_save_setup_, SIGNAL(triggered(bool)),
163 this, SLOT(on_actionSaveSetup_triggered()));
165 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
166 session.device_manager().context());
167 menu_file_export->setTitle(tr("&Export"));
168 connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
169 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
171 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
172 session.device_manager().context());
173 menu_file_import->setTitle(tr("&Import"));
174 connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
175 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
177 action_connect_->setText(tr("&Connect to Device..."));
178 connect(action_connect_, SIGNAL(triggered(bool)),
179 this, SLOT(on_actionConnect_triggered()));
182 QMenu *menu_new_view = new QMenu();
183 connect(menu_new_view, SIGNAL(triggered(QAction*)),
184 this, SLOT(on_actionNewView_triggered(QAction*)));
186 for (int i = 0; i < views::ViewTypeCount; i++) {
187 QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i]));
188 action->setData(QVariant::fromValue(i));
191 new_view_button_->setMenu(menu_new_view);
192 new_view_button_->setDefaultAction(action_new_view_);
193 new_view_button_->setPopupMode(QToolButton::MenuButtonPopup);
196 vector<QAction*> open_actions;
197 open_actions.push_back(action_open_);
198 QAction* separator_o = new QAction(this);
199 separator_o->setSeparator(true);
200 open_actions.push_back(separator_o);
201 open_actions.push_back(action_restore_setup_);
203 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
204 session.device_manager().context(), open_actions);
205 connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
206 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
208 open_button_->setMenu(import_menu);
209 open_button_->setDefaultAction(action_open_);
210 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
213 vector<QAction*> save_actions;
214 save_actions.push_back(action_save_);
215 save_actions.push_back(action_save_as_);
216 save_actions.push_back(action_save_selection_as_);
217 QAction* separator_s = new QAction(this);
218 separator_s->setSeparator(true);
219 save_actions.push_back(separator_s);
220 save_actions.push_back(action_save_setup_);
222 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
223 session.device_manager().context(), save_actions);
224 connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
225 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
227 save_button_->setMenu(export_menu);
228 save_button_->setDefaultAction(action_save_);
229 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
231 // Device selector menu
232 connect(&device_selector_, SIGNAL(device_selected()),
233 this, SLOT(on_device_selected()));
235 // Setup the decoder button
237 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
238 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
239 add_decoder_button_->setToolTip(tr("Add protocol decoder"));
240 add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D));
242 connect(add_decoder_button_, SIGNAL(clicked()),
243 this, SLOT(on_add_decoder_clicked()));
246 // Setup the math signal button
247 add_math_signal_button_->setIcon(QIcon(":/icons/add-math-signal.svg"));
248 add_math_signal_button_->setPopupMode(QToolButton::InstantPopup);
249 add_math_signal_button_->setToolTip(tr("Add math signal"));
250 add_math_signal_button_->setShortcut(QKeySequence(Qt::Key_M));
252 connect(add_math_signal_button_, SIGNAL(clicked()),
253 this, SLOT(on_add_math_signal_clicked()));
256 connect(&sample_count_, SIGNAL(value_changed()),
257 this, SLOT(on_sample_count_changed()));
258 connect(&sample_rate_, SIGNAL(value_changed()),
259 this, SLOT(on_sample_rate_changed()));
261 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
263 set_capture_state(pv::Session::Stopped);
265 configure_button_.setToolTip(tr("Configure Device"));
266 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
267 QIcon(":/icons/preferences-system.png")));
269 channels_button_.setToolTip(tr("Configure Channels"));
270 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
272 add_toolbar_widgets();
274 sample_count_.installEventFilter(this);
275 sample_rate_.installEventFilter(this);
277 // Setup session_ events
278 connect(&session_, SIGNAL(capture_state_changed(int)),
279 this, SLOT(on_capture_state_changed(int)));
280 connect(&session, SIGNAL(device_changed()),
281 this, SLOT(on_device_changed()));
283 update_device_list();
286 void MainBar::update_device_list()
288 DeviceManager &mgr = session_.device_manager();
289 shared_ptr<devices::Device> selected_device = session_.device();
290 list< shared_ptr<devices::Device> > devs;
292 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
294 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
295 devs.push_back(selected_device);
297 device_selector_.set_device_list(devs, selected_device);
298 update_device_config_widgets();
301 void MainBar::set_capture_state(pv::Session::capture_state state)
303 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
305 device_selector_.setEnabled(ui_enabled);
306 configure_button_.setEnabled(ui_enabled);
307 channels_button_.setEnabled(ui_enabled);
308 sample_count_.setEnabled(ui_enabled);
309 sample_rate_.setEnabled(ui_enabled);
312 void MainBar::reset_device_selector()
314 device_selector_.reset();
317 QAction* MainBar::action_open() const
322 QAction* MainBar::action_save() const
327 QAction* MainBar::action_save_as() const
329 return action_save_as_;
332 QAction* MainBar::action_save_selection_as() const
334 return action_save_selection_as_;
337 QAction* MainBar::action_connect() const
339 return action_connect_;
342 void MainBar::update_sample_rate_selector()
344 Glib::VariantContainerBase gvar_dict;
346 const uint64_t *elements = nullptr;
348 map< const ConfigKey*, set<Capability> > keys;
350 if (updating_sample_rate_) {
351 sample_rate_.show_none();
355 const shared_ptr<devices::Device> device = device_selector_.selected_device();
359 assert(!updating_sample_rate_);
360 updating_sample_rate_ = true;
362 const shared_ptr<sigrok::Device> sr_dev = device->device();
364 sample_rate_.allow_user_entered_values(false);
365 if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
367 auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
369 bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
371 sample_rate_.allow_user_entered_values(value);
373 } catch (Error& error) {
379 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
381 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
382 } catch (Error& error) {
383 qDebug() << tr("Failed to get sample rate list:") << error.what();
386 sample_rate_.show_none();
387 updating_sample_rate_ = false;
391 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
392 "samplerate-steps", G_VARIANT_TYPE("at")))) {
393 elements = (const uint64_t *)g_variant_get_fixed_array(
394 gvar_list, &num_elements, sizeof(uint64_t));
396 const uint64_t min = elements[0];
397 const uint64_t max = elements[1];
398 const uint64_t step = elements[2];
400 g_variant_unref(gvar_list);
408 sample_rate_.show_125_list(min, max);
410 // When the step is not 1, we cam't make a 1-2-5-10
411 // list of sample rates, because we may not be able to
412 // make round numbers. Therefore in this case, show a
414 sample_rate_.show_min_max_step(min, max, step);
416 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
417 "samplerates", G_VARIANT_TYPE("at")))) {
418 elements = (const uint64_t *)g_variant_get_fixed_array(
419 gvar_list, &num_elements, sizeof(uint64_t));
420 sample_rate_.show_list(elements, num_elements);
421 g_variant_unref(gvar_list);
423 updating_sample_rate_ = false;
425 update_sample_rate_selector_value();
428 void MainBar::update_sample_rate_selector_value()
430 if (updating_sample_rate_)
433 const shared_ptr<devices::Device> device = device_selector_.selected_device();
438 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
439 uint64_t samplerate =
440 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
441 assert(!updating_sample_rate_);
442 updating_sample_rate_ = true;
443 sample_rate_.set_value(samplerate);
444 updating_sample_rate_ = false;
445 } catch (Error& error) {
446 qDebug() << tr("Failed to get sample rate:") << error.what();
450 void MainBar::update_sample_count_selector()
452 if (updating_sample_count_)
455 const shared_ptr<devices::Device> device = device_selector_.selected_device();
459 const shared_ptr<sigrok::Device> sr_dev = device->device();
461 assert(!updating_sample_count_);
462 updating_sample_count_ = true;
464 if (!sample_count_supported_) {
465 sample_count_.show_none();
466 updating_sample_count_ = false;
470 uint64_t sample_count = sample_count_.value();
471 uint64_t min_sample_count = 0;
472 uint64_t max_sample_count = MaxSampleCount;
473 bool default_count_set = false;
475 if (sample_count == 0) {
476 sample_count = DefaultSampleCount;
477 default_count_set = true;
480 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
482 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
484 g_variant_get(gvar.gobj(), "(tt)",
485 &min_sample_count, &max_sample_count);
486 } catch (Error& error) {
487 qDebug() << tr("Failed to get sample limit list:") << error.what();
491 min_sample_count = min(max(min_sample_count, MinSampleCount),
494 sample_count_.show_125_list(min_sample_count, max_sample_count);
496 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
497 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
498 sample_count = g_variant_get_uint64(gvar.gobj());
499 if (sample_count == 0) {
500 sample_count = DefaultSampleCount;
501 default_count_set = true;
503 sample_count = min(max(sample_count, MinSampleCount),
507 sample_count_.set_value(sample_count);
509 updating_sample_count_ = false;
511 // If we show the default rate then make sure the device uses the same
512 if (default_count_set)
513 commit_sample_count();
516 void MainBar::update_device_config_widgets()
518 using namespace pv::popups;
520 const shared_ptr<devices::Device> device = device_selector_.selected_device();
522 // Hide the widgets if no device is selected
523 channels_button_action_->setVisible(!!device);
525 configure_button_action_->setVisible(false);
526 sample_count_.show_none();
527 sample_rate_.show_none();
531 const shared_ptr<sigrok::Device> sr_dev = device->device();
535 // Update the configure popup
536 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
537 configure_button_action_->setVisible(!opts->binding().properties().empty());
538 configure_button_.set_popup(opts);
540 // Update the channels popup
541 Channels *const channels = new Channels(session_, this);
542 channels_button_.set_popup(channels);
544 // Update supported options.
545 sample_count_supported_ = false;
547 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
548 sample_count_supported_ = true;
550 // Add notification of reconfigure events
551 // Note: No need to disconnect the previous signal as that QObject instance is destroyed
552 connect(&opts->binding(), SIGNAL(config_changed()),
553 this, SLOT(on_config_changed()));
555 // Update sweep timing widgets.
556 update_sample_count_selector();
557 update_sample_rate_selector();
560 void MainBar::commit_sample_rate()
562 uint64_t sample_rate = 0;
564 const shared_ptr<devices::Device> device = device_selector_.selected_device();
568 const shared_ptr<sigrok::Device> sr_dev = device->device();
570 sample_rate = sample_rate_.value();
573 sr_dev->config_set(ConfigKey::SAMPLERATE,
574 Glib::Variant<guint64>::create(sample_rate));
575 update_sample_rate_selector();
576 } catch (Error& error) {
577 qDebug() << tr("Failed to configure samplerate:") << error.what();
581 // Devices with built-in memory might impose limits on certain
582 // configurations, so let's check what sample count the driver
584 update_sample_count_selector();
587 void MainBar::commit_sample_count()
589 uint64_t sample_count = 0;
591 const shared_ptr<devices::Device> device = device_selector_.selected_device();
595 const shared_ptr<sigrok::Device> sr_dev = device->device();
597 sample_count = sample_count_.value();
598 if (sample_count_supported_) {
600 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
601 Glib::Variant<guint64>::create(sample_count));
602 update_sample_count_selector();
603 } catch (Error& error) {
604 qDebug() << tr("Failed to configure sample count:") << error.what();
609 // Devices with built-in memory might impose limits on certain
610 // configurations, so let's check what sample rate the driver
612 update_sample_rate_selector();
615 void MainBar::show_session_error(const QString text, const QString info_text)
617 QMessageBox msg(this);
618 msg.setText(text + "\n\n" + info_text);
619 msg.setStandardButtons(QMessageBox::Ok);
620 msg.setIcon(QMessageBox::Warning);
624 void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name)
626 using pv::dialogs::StoreProgress;
628 // Stop any currently running capture session
629 session_.stop_capture();
632 const QString dir = settings.value(SettingSaveDirectory).toString();
634 pair<uint64_t, uint64_t> sample_range;
636 // Selection only? Verify that the cursors are active and fetch their values
637 if (selection_only) {
638 views::trace::View *trace_view =
639 qobject_cast<views::trace::View*>(session_.main_view().get());
641 if (!trace_view->cursors()->enabled()) {
642 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
643 "cursors before you can save the data enclosed by them " \
644 "to a session file (e.g. using the Show Cursors button)."));
648 const double samplerate = session_.get_samplerate();
650 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
651 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
653 const uint64_t start_sample = (uint64_t)max(
654 0.0, start_time.convert_to<double>() * samplerate);
655 const uint64_t end_sample = (uint64_t)max(
656 0.0, end_time.convert_to<double>() * samplerate);
658 if ((start_sample == 0) && (end_sample == 0)) {
659 // Both cursors are negative and were clamped to 0
660 show_session_error(tr("Invalid Range"), tr("The cursors don't " \
661 "define a valid range of samples."));
665 sample_range = make_pair(start_sample, end_sample);
667 sample_range = make_pair(0, 0);
670 // Construct the filter
671 const vector<string> exts = format->extensions();
672 QString filter = tr("%1 files ").arg(
673 QString::fromStdString(format->description()));
678 filter += QString("(*.%1);;%2 (*)").arg(
679 QString::fromStdString(join(exts, ", *.")),
682 // Show the file dialog
683 if (file_name.isEmpty())
684 file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter);
686 if (file_name.isEmpty())
689 const QString abs_path = QFileInfo(file_name).absolutePath();
690 settings.setValue(SettingSaveDirectory, abs_path);
692 // Show the options dialog
693 map<string, Glib::VariantBase> options;
694 if (!format->options().empty()) {
695 dialogs::InputOutputOptions dlg(
696 tr("Export %1").arg(QString::fromStdString(
697 format->description())),
698 format->options(), this);
701 options = dlg.options();
704 if (!selection_only) {
705 if (format == session_.device_manager().context()->output_formats()["srzip"]) {
706 session_.set_save_path(QFileInfo(file_name).absolutePath());
707 session_.set_name(QFileInfo(file_name).fileName());
709 session_.set_save_path("");
712 StoreProgress *dlg = new StoreProgress(file_name, format, options,
713 sample_range, session_, this);
717 void MainBar::import_file(shared_ptr<InputFormat> format)
722 const QString dir = settings.value(SettingOpenDirectory).toString();
724 // Construct the filter
725 const vector<string> exts = format->extensions();
726 const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
727 tr("%1 files").arg(QString::fromStdString(format->description())),
728 QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
729 const QString filter_all = QString::fromStdString("%1 (%2)").arg(
730 tr("All Files"), QString::fromStdString("*"));
731 const QString filter = QString::fromStdString("%1%2%3").arg(
732 exts.empty() ? "" : filter_exts,
733 exts.empty() ? "" : ";;",
736 // Show the file dialog
737 const QString file_name = QFileDialog::getOpenFileName(
738 this, tr("Import File"), dir, filter);
740 if (file_name.isEmpty())
743 // Show the options dialog
744 map<string, Glib::VariantBase> options;
745 if (!format->options().empty()) {
746 dialogs::InputOutputOptions dlg(
747 tr("Import %1").arg(QString::fromStdString(
748 format->description())),
749 format->options(), this);
752 options = dlg.options();
755 session_.load_file(file_name, "", format, options);
757 const QString abs_path = QFileInfo(file_name).absolutePath();
758 settings.setValue(SettingOpenDirectory, abs_path);
761 void MainBar::on_device_selected()
763 shared_ptr<devices::Device> device = device_selector_.selected_device();
766 session_.select_device(device);
768 reset_device_selector();
771 void MainBar::on_device_changed()
773 update_device_list();
774 update_device_config_widgets();
777 void MainBar::on_capture_state_changed(int state)
779 set_capture_state((pv::Session::capture_state)state);
782 void MainBar::on_sample_count_changed()
784 if (!updating_sample_count_)
785 commit_sample_count();
788 void MainBar::on_sample_rate_changed()
790 if (!updating_sample_rate_)
791 commit_sample_rate();
794 void MainBar::on_config_changed()
796 // We want to also call update_sample_rate_selector() here in case
797 // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
798 // commit_sample_rate() does this already, so we don't call it here
800 commit_sample_count();
801 commit_sample_rate();
804 void MainBar::on_actionNewView_triggered(QAction* action)
807 new_view(&session_, action->data().toInt());
809 // When the icon of the button is clicked, we create a trace view
810 new_view(&session_, views::ViewTypeTrace);
813 void MainBar::on_actionOpen_triggered()
816 const QString dir = settings.value(SettingOpenDirectory).toString();
819 const QString file_name = QFileDialog::getOpenFileName(
820 this, tr("Open File"), dir, tr(
821 "sigrok Sessions (*.sr);;"
824 if (!file_name.isEmpty()) {
825 session_.load_file(file_name);
827 const QString abs_path = QFileInfo(file_name).absolutePath();
828 settings.setValue(SettingOpenDirectory, abs_path);
832 void MainBar::on_actionSave_triggered()
834 // A path is only set if we loaded/saved an srzip file before
835 if (session_.save_path().isEmpty()) {
836 on_actionSaveAs_triggered();
840 QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name());
841 export_file(session_.device_manager().context()->output_formats()["srzip"], false,
842 fi.absoluteFilePath());
845 void MainBar::on_actionSaveAs_triggered()
847 export_file(session_.device_manager().context()->output_formats()["srzip"]);
850 void MainBar::on_actionSaveSelectionAs_triggered()
852 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
855 void MainBar::on_actionSaveSetup_triggered()
858 const QString dir = settings.value(SettingSaveDirectory).toString();
860 const QString file_name = QFileDialog::getSaveFileName(
861 this, tr("Save File"), dir, tr(
862 "PulseView Session Setups (*.pvs);;"
865 if (file_name.isEmpty())
868 QSettings settings_storage(file_name, QSettings::IniFormat);
869 session_.save_setup(settings_storage);
872 void MainBar::on_actionRestoreSetup_triggered()
875 const QString dir = settings.value(SettingSaveDirectory).toString();
877 const QString file_name = QFileDialog::getOpenFileName(
878 this, tr("Open File"), dir, tr(
879 "PulseView Session Setups (*.pvs);;"
882 if (file_name.isEmpty())
885 QSettings settings_storage(file_name, QSettings::IniFormat);
886 session_.restore_setup(settings_storage);
889 void MainBar::on_actionConnect_triggered()
891 // Stop any currently running capture session
892 session_.stop_capture();
894 dialogs::Connect dlg(this, session_.device_manager());
896 // If the user selected a device, select it in the device list. Select the
897 // current device otherwise.
899 session_.select_device(dlg.get_selected_device());
901 update_device_list();
904 void MainBar::on_add_decoder_clicked()
906 show_decoder_selector(&session_);
909 void MainBar::on_add_math_signal_clicked()
911 shared_ptr<data::SignalBase> signal = make_shared<data::MathSignal>(session_);
912 session_.add_generated_signal(signal);
915 void MainBar::add_toolbar_widgets()
917 addWidget(new_view_button_);
919 addWidget(open_button_);
920 addWidget(save_button_);
923 StandardBar::add_toolbar_widgets();
925 addWidget(&device_selector_);
926 configure_button_action_ = addWidget(&configure_button_);
927 channels_button_action_ = addWidget(&channels_button_);
928 addWidget(&sample_count_);
929 addWidget(&sample_rate_);
932 addWidget(add_decoder_button_);
934 addWidget(add_math_signal_button_);
937 bool MainBar::eventFilter(QObject *watched, QEvent *event)
939 if (sample_count_supported_ && (watched == &sample_count_ ||
940 watched == &sample_rate_) &&
941 (event->type() == QEvent::ToolTip)) {
943 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
944 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
946 QString str = tr("Total sampling time: %1").arg(
947 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
948 QToolTip::showText(help_event->globalPos(), str);
956 } // namespace toolbars