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/devicemanager.hpp>
39 #include <pv/devices/hardwaredevice.hpp>
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>
45 #include <pv/mainwindow.hpp>
46 #include <pv/popups/channels.hpp>
47 #include <pv/popups/deviceoptions.hpp>
48 #include <pv/util.hpp>
49 #include <pv/views/trace/view.hpp>
50 #include <pv/widgets/exportmenu.hpp>
51 #include <pv/widgets/importmenu.hpp>
53 #include <pv/data/decodesignal.hpp>
56 #include <libsigrokcxx/libsigrokcxx.hpp>
58 using std::back_inserter;
67 using std::shared_ptr;
71 using sigrok::Capability;
72 using sigrok::ConfigKey;
74 using sigrok::InputFormat;
75 using sigrok::OutputFormat;
77 using boost::algorithm::join;
82 const uint64_t MainBar::MinSampleCount = 100ULL;
83 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
84 const uint64_t MainBar::DefaultSampleCount = 1000000;
86 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
87 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
89 MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) :
90 StandardBar(session, parent, view, false),
91 action_new_view_(new QAction(this)),
92 action_open_(new QAction(this)),
93 action_save_(new QAction(this)),
94 action_save_as_(new QAction(this)),
95 action_save_selection_as_(new QAction(this)),
96 action_restore_setup_(new QAction(this)),
97 action_save_setup_(new QAction(this)),
98 action_connect_(new QAction(this)),
99 new_view_button_(new QToolButton()),
100 open_button_(new QToolButton()),
101 save_button_(new QToolButton()),
102 device_selector_(parent, session.device_manager(), action_connect_),
103 configure_button_(this),
104 configure_button_action_(nullptr),
105 channels_button_(this),
106 channels_button_action_(nullptr),
107 sample_count_(" samples", this),
108 sample_rate_("Hz", this),
109 updating_sample_rate_(false),
110 updating_sample_count_(false),
111 sample_count_supported_(false)
113 , add_decoder_button_(new QToolButton())
116 setObjectName(QString::fromUtf8("MainBar"));
118 setContextMenuPolicy(Qt::PreventContextMenu);
121 action_new_view_->setText(tr("New &View"));
122 action_new_view_->setIcon(QIcon::fromTheme("window-new",
123 QIcon(":/icons/window-new.png")));
124 connect(action_new_view_, SIGNAL(triggered(bool)),
125 this, SLOT(on_actionNewView_triggered()));
127 action_open_->setText(tr("&Open..."));
128 action_open_->setIcon(QIcon::fromTheme("document-open",
129 QIcon(":/icons/document-open.png")));
130 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
131 connect(action_open_, SIGNAL(triggered(bool)),
132 this, SLOT(on_actionOpen_triggered()));
134 action_restore_setup_->setText(tr("Restore Session Setu&p..."));
135 connect(action_restore_setup_, SIGNAL(triggered(bool)),
136 this, SLOT(on_actionRestoreSetup_triggered()));
138 action_save_->setText(tr("&Save..."));
139 action_save_->setIcon(QIcon::fromTheme("document-save-as",
140 QIcon(":/icons/document-save-as.png")));
141 action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
142 connect(action_save_, SIGNAL(triggered(bool)),
143 this, SLOT(on_actionSave_triggered()));
145 action_save_as_->setText(tr("Save &As..."));
146 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
147 QIcon(":/icons/document-save-as.png")));
148 connect(action_save_as_, SIGNAL(triggered(bool)),
149 this, SLOT(on_actionSaveAs_triggered()));
151 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
152 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
153 QIcon(":/icons/document-save-as.png")));
154 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
155 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
156 this, SLOT(on_actionSaveSelectionAs_triggered()));
158 action_save_setup_->setText(tr("Save Session Setu&p..."));
159 connect(action_save_setup_, SIGNAL(triggered(bool)),
160 this, SLOT(on_actionSaveSetup_triggered()));
162 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
163 session.device_manager().context());
164 menu_file_export->setTitle(tr("&Export"));
165 connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
166 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
168 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
169 session.device_manager().context());
170 menu_file_import->setTitle(tr("&Import"));
171 connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
172 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
174 action_connect_->setText(tr("&Connect to Device..."));
175 connect(action_connect_, SIGNAL(triggered(bool)),
176 this, SLOT(on_actionConnect_triggered()));
179 QMenu *menu_new_view = new QMenu();
180 connect(menu_new_view, SIGNAL(triggered(QAction*)),
181 this, SLOT(on_actionNewView_triggered(QAction*)));
183 for (int i = 0; i < views::ViewTypeCount; i++) {
184 QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i]));
185 action->setData(QVariant::fromValue(i));
188 new_view_button_->setMenu(menu_new_view);
189 new_view_button_->setDefaultAction(action_new_view_);
190 new_view_button_->setPopupMode(QToolButton::MenuButtonPopup);
193 vector<QAction*> open_actions;
194 open_actions.push_back(action_open_);
195 QAction* separator_o = new QAction(this);
196 separator_o->setSeparator(true);
197 open_actions.push_back(separator_o);
198 open_actions.push_back(action_restore_setup_);
200 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
201 session.device_manager().context(), open_actions);
202 connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
203 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
205 open_button_->setMenu(import_menu);
206 open_button_->setDefaultAction(action_open_);
207 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
210 vector<QAction*> save_actions;
211 save_actions.push_back(action_save_);
212 save_actions.push_back(action_save_as_);
213 save_actions.push_back(action_save_selection_as_);
214 QAction* separator_s = new QAction(this);
215 separator_s->setSeparator(true);
216 save_actions.push_back(separator_s);
217 save_actions.push_back(action_save_setup_);
219 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
220 session.device_manager().context(), save_actions);
221 connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
222 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
224 save_button_->setMenu(export_menu);
225 save_button_->setDefaultAction(action_save_);
226 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
228 // Device selector menu
229 connect(&device_selector_, SIGNAL(device_selected()),
230 this, SLOT(on_device_selected()));
232 // Setup the decoder button
234 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
235 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
236 add_decoder_button_->setToolTip(tr("Add protocol decoder"));
237 add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D));
239 connect(add_decoder_button_, SIGNAL(clicked()),
240 this, SLOT(on_add_decoder_clicked()));
243 connect(&sample_count_, SIGNAL(value_changed()),
244 this, SLOT(on_sample_count_changed()));
245 connect(&sample_rate_, SIGNAL(value_changed()),
246 this, SLOT(on_sample_rate_changed()));
248 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
250 set_capture_state(pv::Session::Stopped);
252 configure_button_.setToolTip(tr("Configure Device"));
253 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
254 QIcon(":/icons/preferences-system.png")));
256 channels_button_.setToolTip(tr("Configure Channels"));
257 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
259 add_toolbar_widgets();
261 sample_count_.installEventFilter(this);
262 sample_rate_.installEventFilter(this);
264 // Setup session_ events
265 connect(&session_, SIGNAL(capture_state_changed(int)),
266 this, SLOT(on_capture_state_changed(int)));
267 connect(&session, SIGNAL(device_changed()),
268 this, SLOT(on_device_changed()));
270 update_device_list();
273 void MainBar::update_device_list()
275 DeviceManager &mgr = session_.device_manager();
276 shared_ptr<devices::Device> selected_device = session_.device();
277 list< shared_ptr<devices::Device> > devs;
279 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
281 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
282 devs.push_back(selected_device);
284 device_selector_.set_device_list(devs, selected_device);
285 update_device_config_widgets();
288 void MainBar::set_capture_state(pv::Session::capture_state state)
290 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
292 device_selector_.setEnabled(ui_enabled);
293 configure_button_.setEnabled(ui_enabled);
294 channels_button_.setEnabled(ui_enabled);
295 sample_count_.setEnabled(ui_enabled);
296 sample_rate_.setEnabled(ui_enabled);
299 void MainBar::reset_device_selector()
301 device_selector_.reset();
304 QAction* MainBar::action_open() const
309 QAction* MainBar::action_save() const
314 QAction* MainBar::action_save_as() const
316 return action_save_as_;
319 QAction* MainBar::action_save_selection_as() const
321 return action_save_selection_as_;
324 QAction* MainBar::action_connect() const
326 return action_connect_;
329 void MainBar::update_sample_rate_selector()
331 Glib::VariantContainerBase gvar_dict;
333 const uint64_t *elements = nullptr;
335 map< const ConfigKey*, set<Capability> > keys;
337 if (updating_sample_rate_) {
338 sample_rate_.show_none();
342 const shared_ptr<devices::Device> device = device_selector_.selected_device();
346 assert(!updating_sample_rate_);
347 updating_sample_rate_ = true;
349 const shared_ptr<sigrok::Device> sr_dev = device->device();
351 sample_rate_.allow_user_entered_values(false);
352 if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
354 auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
356 bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
358 sample_rate_.allow_user_entered_values(value);
360 } catch (Error& error) {
366 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
368 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
369 } catch (Error& error) {
370 qDebug() << tr("Failed to get sample rate list:") << error.what();
373 sample_rate_.show_none();
374 updating_sample_rate_ = false;
378 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
379 "samplerate-steps", G_VARIANT_TYPE("at")))) {
380 elements = (const uint64_t *)g_variant_get_fixed_array(
381 gvar_list, &num_elements, sizeof(uint64_t));
383 const uint64_t min = elements[0];
384 const uint64_t max = elements[1];
385 const uint64_t step = elements[2];
387 g_variant_unref(gvar_list);
395 sample_rate_.show_125_list(min, max);
397 // When the step is not 1, we cam't make a 1-2-5-10
398 // list of sample rates, because we may not be able to
399 // make round numbers. Therefore in this case, show a
401 sample_rate_.show_min_max_step(min, max, step);
403 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
404 "samplerates", G_VARIANT_TYPE("at")))) {
405 elements = (const uint64_t *)g_variant_get_fixed_array(
406 gvar_list, &num_elements, sizeof(uint64_t));
407 sample_rate_.show_list(elements, num_elements);
408 g_variant_unref(gvar_list);
410 updating_sample_rate_ = false;
412 update_sample_rate_selector_value();
415 void MainBar::update_sample_rate_selector_value()
417 if (updating_sample_rate_)
420 const shared_ptr<devices::Device> device = device_selector_.selected_device();
425 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
426 uint64_t samplerate =
427 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
428 assert(!updating_sample_rate_);
429 updating_sample_rate_ = true;
430 sample_rate_.set_value(samplerate);
431 updating_sample_rate_ = false;
432 } catch (Error& error) {
433 qDebug() << tr("Failed to get sample rate:") << error.what();
437 void MainBar::update_sample_count_selector()
439 if (updating_sample_count_)
442 const shared_ptr<devices::Device> device = device_selector_.selected_device();
446 const shared_ptr<sigrok::Device> sr_dev = device->device();
448 assert(!updating_sample_count_);
449 updating_sample_count_ = true;
451 if (!sample_count_supported_) {
452 sample_count_.show_none();
453 updating_sample_count_ = false;
457 uint64_t sample_count = sample_count_.value();
458 uint64_t min_sample_count = 0;
459 uint64_t max_sample_count = MaxSampleCount;
460 bool default_count_set = false;
462 if (sample_count == 0) {
463 sample_count = DefaultSampleCount;
464 default_count_set = true;
467 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
469 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
471 g_variant_get(gvar.gobj(), "(tt)",
472 &min_sample_count, &max_sample_count);
473 } catch (Error& error) {
474 qDebug() << tr("Failed to get sample limit list:") << error.what();
478 min_sample_count = min(max(min_sample_count, MinSampleCount),
481 sample_count_.show_125_list(min_sample_count, max_sample_count);
483 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
484 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
485 sample_count = g_variant_get_uint64(gvar.gobj());
486 if (sample_count == 0) {
487 sample_count = DefaultSampleCount;
488 default_count_set = true;
490 sample_count = min(max(sample_count, MinSampleCount),
494 sample_count_.set_value(sample_count);
496 updating_sample_count_ = false;
498 // If we show the default rate then make sure the device uses the same
499 if (default_count_set)
500 commit_sample_count();
503 void MainBar::update_device_config_widgets()
505 using namespace pv::popups;
507 const shared_ptr<devices::Device> device = device_selector_.selected_device();
509 // Hide the widgets if no device is selected
510 channels_button_action_->setVisible(!!device);
512 configure_button_action_->setVisible(false);
513 sample_count_.show_none();
514 sample_rate_.show_none();
518 const shared_ptr<sigrok::Device> sr_dev = device->device();
522 // Update the configure popup
523 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
524 configure_button_action_->setVisible(!opts->binding().properties().empty());
525 configure_button_.set_popup(opts);
527 // Update the channels popup
528 Channels *const channels = new Channels(session_, this);
529 channels_button_.set_popup(channels);
531 // Update supported options.
532 sample_count_supported_ = false;
534 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
535 sample_count_supported_ = true;
537 // Add notification of reconfigure events
538 disconnect(this, SLOT(on_config_changed()));
539 connect(&opts->binding(), SIGNAL(config_changed()),
540 this, SLOT(on_config_changed()));
542 // Update sweep timing widgets.
543 update_sample_count_selector();
544 update_sample_rate_selector();
547 void MainBar::commit_sample_rate()
549 uint64_t sample_rate = 0;
551 const shared_ptr<devices::Device> device = device_selector_.selected_device();
555 const shared_ptr<sigrok::Device> sr_dev = device->device();
557 sample_rate = sample_rate_.value();
560 sr_dev->config_set(ConfigKey::SAMPLERATE,
561 Glib::Variant<guint64>::create(sample_rate));
562 update_sample_rate_selector();
563 } catch (Error& error) {
564 qDebug() << tr("Failed to configure samplerate:") << error.what();
568 // Devices with built-in memory might impose limits on certain
569 // configurations, so let's check what sample count the driver
571 update_sample_count_selector();
574 void MainBar::commit_sample_count()
576 uint64_t sample_count = 0;
578 const shared_ptr<devices::Device> device = device_selector_.selected_device();
582 const shared_ptr<sigrok::Device> sr_dev = device->device();
584 sample_count = sample_count_.value();
585 if (sample_count_supported_) {
587 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
588 Glib::Variant<guint64>::create(sample_count));
589 update_sample_count_selector();
590 } catch (Error& error) {
591 qDebug() << tr("Failed to configure sample count:") << error.what();
596 // Devices with built-in memory might impose limits on certain
597 // configurations, so let's check what sample rate the driver
599 update_sample_rate_selector();
602 void MainBar::show_session_error(const QString text, const QString info_text)
604 QMessageBox msg(this);
605 msg.setText(text + "\n\n" + info_text);
606 msg.setStandardButtons(QMessageBox::Ok);
607 msg.setIcon(QMessageBox::Warning);
611 void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name)
613 using pv::dialogs::StoreProgress;
615 // Stop any currently running capture session
616 session_.stop_capture();
619 const QString dir = settings.value(SettingSaveDirectory).toString();
621 pair<uint64_t, uint64_t> sample_range;
623 // Selection only? Verify that the cursors are active and fetch their values
624 if (selection_only) {
625 views::trace::View *trace_view =
626 qobject_cast<views::trace::View*>(session_.main_view().get());
628 if (!trace_view->cursors()->enabled()) {
629 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
630 "cursors before you can save the data enclosed by them " \
631 "to a session file (e.g. using the Show Cursors button)."));
635 const double samplerate = session_.get_samplerate();
637 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
638 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
640 const uint64_t start_sample = (uint64_t)max(
641 0.0, start_time.convert_to<double>() * samplerate);
642 const uint64_t end_sample = (uint64_t)max(
643 0.0, end_time.convert_to<double>() * samplerate);
645 if ((start_sample == 0) && (end_sample == 0)) {
646 // Both cursors are negative and were clamped to 0
647 show_session_error(tr("Invalid Range"), tr("The cursors don't " \
648 "define a valid range of samples."));
652 sample_range = make_pair(start_sample, end_sample);
654 sample_range = make_pair(0, 0);
657 // Construct the filter
658 const vector<string> exts = format->extensions();
659 QString filter = tr("%1 files ").arg(
660 QString::fromStdString(format->description()));
665 filter += QString("(*.%1);;%2 (*)").arg(
666 QString::fromStdString(join(exts, ", *.")),
669 // Show the file dialog
670 if (file_name.isEmpty())
671 file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter);
673 if (file_name.isEmpty())
676 const QString abs_path = QFileInfo(file_name).absolutePath();
677 settings.setValue(SettingSaveDirectory, abs_path);
679 // Show the options dialog
680 map<string, Glib::VariantBase> options;
681 if (!format->options().empty()) {
682 dialogs::InputOutputOptions dlg(
683 tr("Export %1").arg(QString::fromStdString(
684 format->description())),
685 format->options(), this);
688 options = dlg.options();
691 if (!selection_only) {
692 if (format == session_.device_manager().context()->output_formats()["srzip"]) {
693 session_.set_save_path(QFileInfo(file_name).absolutePath());
694 session_.set_name(QFileInfo(file_name).fileName());
696 session_.set_save_path("");
699 StoreProgress *dlg = new StoreProgress(file_name, format, options,
700 sample_range, session_, this);
704 void MainBar::import_file(shared_ptr<InputFormat> format)
709 const QString dir = settings.value(SettingOpenDirectory).toString();
711 // Construct the filter
712 const vector<string> exts = format->extensions();
713 const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
714 tr("%1 files").arg(QString::fromStdString(format->description())),
715 QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
716 const QString filter_all = QString::fromStdString("%1 (%2)").arg(
717 tr("All Files"), QString::fromStdString("*"));
718 const QString filter = QString::fromStdString("%1%2%3").arg(
719 exts.empty() ? "" : filter_exts,
720 exts.empty() ? "" : ";;",
723 // Show the file dialog
724 const QString file_name = QFileDialog::getOpenFileName(
725 this, tr("Import File"), dir, filter);
727 if (file_name.isEmpty())
730 // Show the options dialog
731 map<string, Glib::VariantBase> options;
732 if (!format->options().empty()) {
733 dialogs::InputOutputOptions dlg(
734 tr("Import %1").arg(QString::fromStdString(
735 format->description())),
736 format->options(), this);
739 options = dlg.options();
742 session_.load_file(file_name, "", format, options);
744 const QString abs_path = QFileInfo(file_name).absolutePath();
745 settings.setValue(SettingOpenDirectory, abs_path);
748 void MainBar::on_device_selected()
750 shared_ptr<devices::Device> device = device_selector_.selected_device();
753 session_.select_device(device);
755 reset_device_selector();
758 void MainBar::on_device_changed()
760 update_device_list();
761 update_device_config_widgets();
764 void MainBar::on_capture_state_changed(int state)
766 set_capture_state((pv::Session::capture_state)state);
769 void MainBar::on_sample_count_changed()
771 if (!updating_sample_count_)
772 commit_sample_count();
775 void MainBar::on_sample_rate_changed()
777 if (!updating_sample_rate_)
778 commit_sample_rate();
781 void MainBar::on_config_changed()
783 // We want to also call update_sample_rate_selector() here in case
784 // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
785 // commit_sample_rate() does this already, so we don't call it here
787 commit_sample_count();
788 commit_sample_rate();
791 void MainBar::on_actionNewView_triggered(QAction* action)
794 new_view(&session_, action->data().toInt());
796 // When the icon of the button is clicked, we create a trace view
797 new_view(&session_, views::ViewTypeTrace);
800 void MainBar::on_actionOpen_triggered()
803 const QString dir = settings.value(SettingOpenDirectory).toString();
806 const QString file_name = QFileDialog::getOpenFileName(
807 this, tr("Open File"), dir, tr(
808 "sigrok Sessions (*.sr);;"
811 if (!file_name.isEmpty()) {
812 session_.load_file(file_name);
814 const QString abs_path = QFileInfo(file_name).absolutePath();
815 settings.setValue(SettingOpenDirectory, abs_path);
819 void MainBar::on_actionSave_triggered()
821 // A path is only set if we loaded/saved an srzip file before
822 if (session_.save_path().isEmpty()) {
823 on_actionSaveAs_triggered();
827 QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name());
828 export_file(session_.device_manager().context()->output_formats()["srzip"], false,
829 fi.absoluteFilePath());
832 void MainBar::on_actionSaveAs_triggered()
834 export_file(session_.device_manager().context()->output_formats()["srzip"]);
837 void MainBar::on_actionSaveSelectionAs_triggered()
839 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
842 void MainBar::on_actionSaveSetup_triggered()
845 const QString dir = settings.value(SettingSaveDirectory).toString();
847 const QString file_name = QFileDialog::getSaveFileName(
848 this, tr("Save File"), dir, tr(
849 "PulseView Session Setups (*.pvs);;"
852 if (file_name.isEmpty())
855 QSettings settings_storage(file_name, QSettings::IniFormat);
856 session_.save_setup(settings_storage);
859 void MainBar::on_actionRestoreSetup_triggered()
862 const QString dir = settings.value(SettingSaveDirectory).toString();
864 const QString file_name = QFileDialog::getOpenFileName(
865 this, tr("Open File"), dir, tr(
866 "PulseView Session Setups (*.pvs);;"
869 if (file_name.isEmpty())
872 QSettings settings_storage(file_name, QSettings::IniFormat);
873 session_.restore_setup(settings_storage);
876 void MainBar::on_actionConnect_triggered()
878 // Stop any currently running capture session
879 session_.stop_capture();
881 dialogs::Connect dlg(this, session_.device_manager());
883 // If the user selected a device, select it in the device list. Select the
884 // current device otherwise.
886 session_.select_device(dlg.get_selected_device());
888 update_device_list();
891 void MainBar::on_add_decoder_clicked()
893 show_decoder_selector(&session_);
896 void MainBar::add_toolbar_widgets()
898 addWidget(new_view_button_);
900 addWidget(open_button_);
901 addWidget(save_button_);
904 StandardBar::add_toolbar_widgets();
906 addWidget(&device_selector_);
907 configure_button_action_ = addWidget(&configure_button_);
908 channels_button_action_ = addWidget(&channels_button_);
909 addWidget(&sample_count_);
910 addWidget(&sample_rate_);
913 addWidget(add_decoder_button_);
917 bool MainBar::eventFilter(QObject *watched, QEvent *event)
919 if (sample_count_supported_ && (watched == &sample_count_ ||
920 watched == &sample_rate_) &&
921 (event->type() == QEvent::ToolTip)) {
923 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
924 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
926 QString str = tr("Total sampling time: %1").arg(
927 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
928 QToolTip::showText(help_event->globalPos(), str);
936 } // namespace toolbars