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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <QFileDialog>
31 #include <QMessageBox>
35 #include "mainbar.hpp"
37 #include <boost/algorithm/string/join.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/deviceoptions.hpp>
48 #include <pv/popups/channels.hpp>
49 #include <pv/util.hpp>
50 #include <pv/view/view.hpp>
51 #include <pv/widgets/exportmenu.hpp>
52 #include <pv/widgets/importmenu.hpp>
54 #include <pv/widgets/decodermenu.hpp>
57 #include <libsigrokcxx/libsigrokcxx.hpp>
59 using std::back_inserter;
68 using std::shared_ptr;
72 using sigrok::Capability;
73 using sigrok::ConfigKey;
75 using sigrok::InputFormat;
76 using sigrok::OutputFormat;
78 using boost::algorithm::join;
83 const uint64_t MainBar::MinSampleCount = 100ULL;
84 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
85 const uint64_t MainBar::DefaultSampleCount = 1000000;
87 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
88 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
90 MainBar::MainBar(Session &session, MainWindow &main_window) :
91 QToolBar("Sampling Bar", &main_window),
92 action_new_view_(new QAction(this)),
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)),
97 action_view_zoom_in_(new QAction(this)),
98 action_view_zoom_out_(new QAction(this)),
99 action_view_zoom_fit_(new QAction(this)),
100 action_view_zoom_one_to_one_(new QAction(this)),
101 action_view_show_cursors_(new QAction(this)),
103 device_selector_(&main_window, session.device_manager(),
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 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
118 setObjectName(QString::fromUtf8("MainBar"));
122 setContextMenuPolicy(Qt::PreventContextMenu);
125 action_new_view_->setText(tr("New &View"));
126 action_new_view_->setIcon(QIcon::fromTheme("window-new",
127 QIcon(":/icons/window-new.png")));
128 connect(action_new_view_, SIGNAL(triggered(bool)),
129 this, SLOT(on_actionNewView_triggered()));
131 action_open_->setText(tr("&Open..."));
132 action_open_->setIcon(QIcon::fromTheme("document-open",
133 QIcon(":/icons/document-open.png")));
134 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
135 connect(action_open_, SIGNAL(triggered(bool)),
136 this, SLOT(on_actionOpen_triggered()));
138 action_save_as_->setText(tr("&Save As..."));
139 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
140 QIcon(":/icons/document-save-as.png")));
141 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
142 connect(action_save_as_, SIGNAL(triggered(bool)),
143 this, SLOT(on_actionSaveAs_triggered()));
145 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
146 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
147 QIcon(":/icons/document-save-as.png")));
148 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
149 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
150 this, SLOT(on_actionSaveSelectionAs_triggered()));
152 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
153 session.device_manager().context());
154 menu_file_export->setTitle(tr("&Export"));
155 connect(menu_file_export,
156 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
157 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
159 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
160 session.device_manager().context());
161 menu_file_import->setTitle(tr("&Import"));
162 connect(menu_file_import,
163 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
164 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
166 action_connect_->setText(tr("&Connect to Device..."));
167 connect(action_connect_, SIGNAL(triggered(bool)),
168 this, SLOT(on_actionConnect_triggered()));
170 action_view_zoom_in_->setText(tr("Zoom &In"));
171 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
172 QIcon(":/icons/zoom-in.png")));
173 // simply using Qt::Key_Plus shows no + in the menu
174 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
175 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
176 this, SLOT(on_actionViewZoomIn_triggered()));
178 action_view_zoom_out_->setText(tr("Zoom &Out"));
179 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
180 QIcon(":/icons/zoom-out.png")));
181 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
182 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
183 this, SLOT(on_actionViewZoomOut_triggered()));
185 action_view_zoom_fit_->setCheckable(true);
186 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
187 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
188 QIcon(":/icons/zoom-fit.png")));
189 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
190 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
191 this, SLOT(on_actionViewZoomFit_triggered()));
193 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
194 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
195 QIcon(":/icons/zoom-original.png")));
196 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
197 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
198 this, SLOT(on_actionViewZoomOneToOne_triggered()));
200 action_view_show_cursors_->setCheckable(true);
201 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
202 QIcon(":/icons/show-cursors.svg")));
203 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
204 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
205 this, SLOT(on_actionViewShowCursors_triggered()));
206 action_view_show_cursors_->setText(tr("Show &Cursors"));
209 QToolButton *const open_button = new QToolButton(this);
211 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
212 session.device_manager().context(), action_open_);
214 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
216 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
218 open_button->setMenu(import_menu);
219 open_button->setDefaultAction(action_open_);
220 open_button->setPopupMode(QToolButton::MenuButtonPopup);
223 QToolButton *const save_button = new QToolButton(this);
225 vector<QAction *> open_actions;
226 open_actions.push_back(action_save_as_);
227 open_actions.push_back(action_save_selection_as_);
229 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
230 session.device_manager().context(),
233 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
235 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
237 save_button->setMenu(export_menu);
238 save_button->setDefaultAction(action_save_as_);
239 save_button->setPopupMode(QToolButton::MenuButtonPopup);
241 // Device selector menu
242 connect(&device_selector_, SIGNAL(device_selected()),
243 this, SLOT(on_device_selected()));
245 // Setup the decoder button
247 menu_decoders_add_->setTitle(tr("&Add"));
248 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
249 this, SLOT(add_decoder(srd_decoder*)));
251 QToolButton *add_decoder_button = new QToolButton(this);
252 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
253 QIcon(":/icons/add-decoder.svg")));
254 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
255 add_decoder_button->setMenu(menu_decoders_add_);
259 addAction(action_new_view_);
261 addWidget(open_button);
262 addWidget(save_button);
264 addAction(action_view_zoom_in_);
265 addAction(action_view_zoom_out_);
266 addAction(action_view_zoom_fit_);
267 addAction(action_view_zoom_one_to_one_);
269 addAction(action_view_show_cursors_);
272 connect(&sample_count_, SIGNAL(value_changed()),
273 this, SLOT(on_sample_count_changed()));
274 connect(&sample_rate_, SIGNAL(value_changed()),
275 this, SLOT(on_sample_rate_changed()));
277 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
279 set_capture_state(pv::Session::Stopped);
281 configure_button_.setIcon(QIcon::fromTheme("configure",
282 QIcon(":/icons/configure.png")));
284 channels_button_.setIcon(QIcon::fromTheme("channels",
285 QIcon(":/icons/channels.svg")));
287 addWidget(&device_selector_);
288 configure_button_action_ = addWidget(&configure_button_);
289 channels_button_action_ = addWidget(&channels_button_);
290 addWidget(&sample_count_);
291 addWidget(&sample_rate_);
294 addWidget(add_decoder_button);
297 sample_count_.installEventFilter(this);
298 sample_rate_.installEventFilter(this);
300 // Setup session_ events
301 connect(&session_, SIGNAL(capture_state_changed(int)),
302 this, SLOT(capture_state_changed(int)));
303 connect(&session, SIGNAL(device_changed()),
304 this, SLOT(on_device_changed()));
306 update_device_list();
309 Session &MainBar::session(void) const
314 void MainBar::update_device_list()
316 DeviceManager &mgr = session_.device_manager();
317 shared_ptr<devices::Device> selected_device = session_.device();
318 list< shared_ptr<devices::Device> > devs;
320 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
322 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
323 devs.push_back(selected_device);
325 device_selector_.set_device_list(devs, selected_device);
326 update_device_config_widgets();
330 void MainBar::set_capture_state(pv::Session::capture_state state)
332 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
334 device_selector_.setEnabled(ui_enabled);
335 configure_button_.setEnabled(ui_enabled);
336 channels_button_.setEnabled(ui_enabled);
337 sample_count_.setEnabled(ui_enabled);
338 sample_rate_.setEnabled(ui_enabled);
341 void MainBar::reset_device_selector()
343 device_selector_.reset();
346 void MainBar::select_device(shared_ptr<devices::Device> device)
350 session_.set_device(device);
352 session_.set_default_device();
353 } catch (const QString &e) {
354 QMessageBox msg(this);
356 msg.setInformativeText(tr("Failed to Select Device"));
357 msg.setStandardButtons(QMessageBox::Ok);
358 msg.setIcon(QMessageBox::Warning);
363 void MainBar::load_init_file(const std::string &file_name,
364 const std::string &format)
366 shared_ptr<InputFormat> input_format;
368 DeviceManager& device_manager = session_.device_manager();
370 if (!format.empty()) {
371 const map<string, shared_ptr<InputFormat> > formats =
372 device_manager.context()->input_formats();
373 const auto iter = find_if(formats.begin(), formats.end(),
374 [&](const pair<string, shared_ptr<InputFormat> > f) {
375 return f.first == format; });
376 if (iter == formats.end()) {
377 cerr << "Unexpected input format: " << format << endl;
381 input_format = (*iter).second;
384 load_file(QString::fromStdString(file_name), input_format);
387 QAction* MainBar::action_open() const
392 QAction* MainBar::action_save_as() const
394 return action_save_as_;
397 QAction* MainBar::action_save_selection_as() const
399 return action_save_selection_as_;
402 QAction* MainBar::action_connect() const
404 return action_connect_;
407 QAction* MainBar::action_view_zoom_in() const
409 return action_view_zoom_in_;
412 QAction* MainBar::action_view_zoom_out() const
414 return action_view_zoom_out_;
417 QAction* MainBar::action_view_zoom_fit() const
419 return action_view_zoom_fit_;
422 QAction* MainBar::action_view_zoom_one_to_one() const
424 return action_view_zoom_one_to_one_;
427 QAction* MainBar::action_view_show_cursors() const
429 return action_view_show_cursors_;
432 void MainBar::load_file(QString file_name,
433 std::shared_ptr<sigrok::InputFormat> format,
434 const std::map<std::string, Glib::VariantBase> &options)
436 DeviceManager& device_manager = session_.device_manager();
438 const QString errorMessage(
439 QString("Failed to load file %1").arg(file_name));
443 session_.set_device(shared_ptr<devices::Device>(
444 new devices::InputFile(
445 device_manager.context(),
446 file_name.toStdString(),
449 session_.set_device(shared_ptr<devices::Device>(
450 new devices::SessionFile(
451 device_manager.context(),
452 file_name.toStdString())));
454 show_session_error(tr("Failed to load ") + file_name, e.what());
455 session_.set_default_device();
456 update_device_list();
460 update_device_list();
462 session_.start_capture([&, errorMessage](QString infoMessage) {
463 session_error(errorMessage, infoMessage); });
465 session_.set_name(QFileInfo(file_name).fileName());
468 void MainBar::update_sample_rate_selector()
470 Glib::VariantContainerBase gvar_dict;
472 const uint64_t *elements = nullptr;
474 map< const ConfigKey*, std::set<Capability> > keys;
476 if (updating_sample_rate_) {
477 sample_rate_.show_none();
481 const shared_ptr<devices::Device> device =
482 device_selector_.selected_device();
486 assert(!updating_sample_rate_);
487 updating_sample_rate_ = true;
489 const shared_ptr<sigrok::Device> sr_dev = device->device();
491 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
492 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
494 sample_rate_.show_none();
495 updating_sample_rate_ = false;
499 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
500 "samplerate-steps", G_VARIANT_TYPE("at")))) {
501 elements = (const uint64_t *)g_variant_get_fixed_array(
502 gvar_list, &num_elements, sizeof(uint64_t));
504 const uint64_t min = elements[0];
505 const uint64_t max = elements[1];
506 const uint64_t step = elements[2];
508 g_variant_unref(gvar_list);
516 sample_rate_.show_125_list(min, max);
518 // When the step is not 1, we cam't make a 1-2-5-10
519 // list of sample rates, because we may not be able to
520 // make round numbers. Therefore in this case, show a
522 sample_rate_.show_min_max_step(min, max, step);
524 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
525 "samplerates", G_VARIANT_TYPE("at")))) {
526 elements = (const uint64_t *)g_variant_get_fixed_array(
527 gvar_list, &num_elements, sizeof(uint64_t));
528 sample_rate_.show_list(elements, num_elements);
529 g_variant_unref(gvar_list);
531 updating_sample_rate_ = false;
533 update_sample_rate_selector_value();
536 void MainBar::update_sample_rate_selector_value()
538 if (updating_sample_rate_)
541 const shared_ptr<devices::Device> device =
542 device_selector_.selected_device();
547 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
548 uint64_t samplerate =
549 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
550 assert(!updating_sample_rate_);
551 updating_sample_rate_ = true;
552 sample_rate_.set_value(samplerate);
553 updating_sample_rate_ = false;
554 } catch (Error error) {
555 qDebug() << "WARNING: Failed to get value of sample rate";
560 void MainBar::update_sample_count_selector()
562 if (updating_sample_count_)
565 const shared_ptr<devices::Device> device =
566 device_selector_.selected_device();
570 const shared_ptr<sigrok::Device> sr_dev = device->device();
572 assert(!updating_sample_count_);
573 updating_sample_count_ = true;
575 if (!sample_count_supported_) {
576 sample_count_.show_none();
577 updating_sample_count_ = false;
581 uint64_t sample_count = sample_count_.value();
582 uint64_t min_sample_count = 0;
583 uint64_t max_sample_count = MaxSampleCount;
585 if (sample_count == 0)
586 sample_count = DefaultSampleCount;
588 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
589 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
591 g_variant_get(gvar.gobj(), "(tt)",
592 &min_sample_count, &max_sample_count);
595 min_sample_count = min(max(min_sample_count, MinSampleCount),
598 sample_count_.show_125_list(
599 min_sample_count, max_sample_count);
601 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
602 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
603 sample_count = g_variant_get_uint64(gvar.gobj());
604 if (sample_count == 0)
605 sample_count = DefaultSampleCount;
606 sample_count = min(max(sample_count, MinSampleCount),
610 sample_count_.set_value(sample_count);
612 updating_sample_count_ = false;
615 void MainBar::update_device_config_widgets()
617 using namespace pv::popups;
619 const shared_ptr<devices::Device> device =
620 device_selector_.selected_device();
622 // Hide the widgets if no device is selected
623 channels_button_action_->setVisible(!!device);
625 configure_button_action_->setVisible(false);
626 sample_count_.show_none();
627 sample_rate_.show_none();
631 const shared_ptr<sigrok::Device> sr_dev = device->device();
635 // Update the configure popup
636 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
637 configure_button_action_->setVisible(
638 !opts->binding().properties().empty());
639 configure_button_.set_popup(opts);
641 // Update the channels popup
642 Channels *const channels = new Channels(session_, this);
643 channels_button_.set_popup(channels);
645 // Update supported options.
646 sample_count_supported_ = false;
648 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
649 sample_count_supported_ = true;
651 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
652 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
653 Glib::Variant<guint64>::create(1));
657 // Add notification of reconfigure events
658 disconnect(this, SLOT(on_config_changed()));
659 connect(&opts->binding(), SIGNAL(config_changed()),
660 this, SLOT(on_config_changed()));
662 // Update sweep timing widgets.
663 update_sample_count_selector();
664 update_sample_rate_selector();
667 void MainBar::commit_sample_rate()
669 uint64_t sample_rate = 0;
671 const shared_ptr<devices::Device> device =
672 device_selector_.selected_device();
676 const shared_ptr<sigrok::Device> sr_dev = device->device();
678 sample_rate = sample_rate_.value();
679 if (sample_rate == 0)
683 sr_dev->config_set(ConfigKey::SAMPLERATE,
684 Glib::Variant<guint64>::create(sample_rate));
685 update_sample_rate_selector();
686 } catch (Error error) {
687 qDebug() << "Failed to configure samplerate.";
691 // Devices with built-in memory might impose limits on certain
692 // configurations, so let's check what sample count the driver
694 update_sample_count_selector();
697 void MainBar::commit_sample_count()
699 uint64_t sample_count = 0;
701 const shared_ptr<devices::Device> device =
702 device_selector_.selected_device();
706 const shared_ptr<sigrok::Device> sr_dev = device->device();
708 sample_count = sample_count_.value();
709 if (sample_count_supported_) {
711 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
712 Glib::Variant<guint64>::create(sample_count));
713 update_sample_count_selector();
714 } catch (Error error) {
715 qDebug() << "Failed to configure sample count.";
720 // Devices with built-in memory might impose limits on certain
721 // configurations, so let's check what sample rate the driver
723 update_sample_rate_selector();
726 void MainBar::session_error(const QString text, const QString info_text)
728 QMetaObject::invokeMethod(this, "show_session_error",
729 Qt::QueuedConnection, Q_ARG(QString, text),
730 Q_ARG(QString, info_text));
733 void MainBar::show_session_error(const QString text, const QString info_text)
735 QMessageBox msg(this);
737 msg.setInformativeText(info_text);
738 msg.setStandardButtons(QMessageBox::Ok);
739 msg.setIcon(QMessageBox::Warning);
743 void MainBar::capture_state_changed(int state)
745 set_capture_state((pv::Session::capture_state)state);
748 void MainBar::add_decoder(srd_decoder *decoder)
752 session_.add_decoder(decoder);
758 void MainBar::export_file(shared_ptr<OutputFormat> format,
761 using pv::dialogs::StoreProgress;
763 // Stop any currently running capture session
764 session_.stop_capture();
767 const QString dir = settings.value(SettingSaveDirectory).toString();
769 std::pair<uint64_t, uint64_t> sample_range;
771 // Selection only? Verify that the cursors are active and fetch their values
772 if (selection_only) {
773 views::TraceView::View *trace_view =
774 qobject_cast<views::TraceView::View*>(session_.main_view().get());
776 if (!trace_view->cursors()->enabled()) {
777 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
778 "cursors before you can save the data enclosed by them " \
779 "to a session file (e.g. using ALT-V - Show Cursors)."));
783 const double samplerate = session_.get_samplerate();
785 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
786 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
788 const uint64_t start_sample =
789 std::max((double)0, start_time.convert_to<double>() * samplerate);
790 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
792 sample_range = std::make_pair(start_sample, end_sample);
794 sample_range = std::make_pair(0, 0);
797 // Construct the filter
798 const vector<string> exts = format->extensions();
799 QString filter = tr("%1 files ").arg(
800 QString::fromStdString(format->description()));
805 filter += QString("(*.%1);;%2 (*.*)").arg(
806 QString::fromStdString(join(exts, ", *.")),
809 // Show the file dialog
810 const QString file_name = QFileDialog::getSaveFileName(
811 this, tr("Save File"), dir, filter);
813 if (file_name.isEmpty())
816 const QString abs_path = QFileInfo(file_name).absolutePath();
817 settings.setValue(SettingSaveDirectory, abs_path);
819 // Show the options dialog
820 map<string, Glib::VariantBase> options;
821 if (!format->options().empty()) {
822 dialogs::InputOutputOptions dlg(
823 tr("Export %1").arg(QString::fromStdString(
824 format->description())),
825 format->options(), this);
828 options = dlg.options();
831 session_.set_name(QFileInfo(file_name).fileName());
833 StoreProgress *dlg = new StoreProgress(file_name, format, options,
834 sample_range, session_, this);
838 void MainBar::import_file(shared_ptr<InputFormat> format)
843 const QString dir = settings.value(SettingOpenDirectory).toString();
845 // Construct the filter
846 const vector<string> exts = format->extensions();
847 const QString filter = exts.empty() ? "" :
848 tr("%1 files (*.%2)").arg(
849 QString::fromStdString(format->description()),
850 QString::fromStdString(join(exts, ", *.")));
852 // Show the file dialog
853 const QString file_name = QFileDialog::getOpenFileName(
854 this, tr("Import File"), dir, tr(
855 "%1 files (*.*);;All Files (*.*)").arg(
856 QString::fromStdString(format->description())));
858 if (file_name.isEmpty())
861 // Show the options dialog
862 map<string, Glib::VariantBase> options;
863 if (!format->options().empty()) {
864 dialogs::InputOutputOptions dlg(
865 tr("Import %1").arg(QString::fromStdString(
866 format->description())),
867 format->options(), this);
870 options = dlg.options();
873 load_file(file_name, format, options);
875 const QString abs_path = QFileInfo(file_name).absolutePath();
876 settings.setValue(SettingOpenDirectory, abs_path);
879 void MainBar::on_device_selected()
881 shared_ptr<devices::Device> device = device_selector_.selected_device();
883 reset_device_selector();
887 select_device(device);
890 void MainBar::on_device_changed()
892 update_device_list();
893 update_device_config_widgets();
896 void MainBar::on_sample_count_changed()
898 if (!updating_sample_count_)
899 commit_sample_count();
902 void MainBar::on_sample_rate_changed()
904 if (!updating_sample_rate_)
905 commit_sample_rate();
908 void MainBar::on_config_changed()
910 commit_sample_count();
911 commit_sample_rate();
914 void MainBar::on_actionNewView_triggered()
919 void MainBar::on_actionOpen_triggered()
922 const QString dir = settings.value(SettingOpenDirectory).toString();
925 const QString file_name = QFileDialog::getOpenFileName(
926 this, tr("Open File"), dir, tr(
927 "Sigrok Sessions (*.sr);;"
930 if (!file_name.isEmpty()) {
931 load_file(file_name);
933 const QString abs_path = QFileInfo(file_name).absolutePath();
934 settings.setValue(SettingOpenDirectory, abs_path);
938 void MainBar::on_actionSaveAs_triggered()
940 export_file(session_.device_manager().context()->output_formats()["srzip"]);
943 void MainBar::on_actionSaveSelectionAs_triggered()
945 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
948 void MainBar::on_actionConnect_triggered()
950 // Stop any currently running capture session
951 session_.stop_capture();
953 dialogs::Connect dlg(this, session_.device_manager());
955 // If the user selected a device, select it in the device list. Select the
956 // current device otherwise.
958 select_device(dlg.get_selected_device());
960 update_device_list();
963 void MainBar::on_actionViewZoomIn_triggered()
965 views::TraceView::View *trace_view =
966 qobject_cast<views::TraceView::View*>(session_.main_view().get());
971 void MainBar::on_actionViewZoomOut_triggered()
973 views::TraceView::View *trace_view =
974 qobject_cast<views::TraceView::View*>(session_.main_view().get());
976 trace_view->zoom(-1);
979 void MainBar::on_actionViewZoomFit_triggered()
981 views::TraceView::View *trace_view =
982 qobject_cast<views::TraceView::View*>(session_.main_view().get());
984 trace_view->zoom_fit(action_view_zoom_fit_->isChecked());
987 void MainBar::on_actionViewZoomOneToOne_triggered()
989 views::TraceView::View *trace_view =
990 qobject_cast<views::TraceView::View*>(session_.main_view().get());
992 trace_view->zoom_one_to_one();
995 void MainBar::on_actionViewShowCursors_triggered()
997 views::TraceView::View *trace_view =
998 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1000 const bool show = !trace_view->cursors_shown();
1002 trace_view->centre_cursors();
1004 trace_view->show_cursors(show);
1007 void MainBar::on_always_zoom_to_fit_changed(bool state)
1009 action_view_zoom_fit_->setChecked(state);
1012 bool MainBar::eventFilter(QObject *watched, QEvent *event)
1014 if (sample_count_supported_ && (watched == &sample_count_ ||
1015 watched == &sample_rate_) &&
1016 (event->type() == QEvent::ToolTip)) {
1017 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
1018 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
1020 QString str = tr("Total sampling time: %1").arg(
1021 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
1022 QToolTip::showText(help_event->globalPos(), str);
1030 } // namespace toolbars