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_open_(new QAction(this)),
93 action_save_as_(new QAction(this)),
94 action_save_selection_as_(new QAction(this)),
95 action_connect_(new QAction(this)),
96 action_view_zoom_in_(new QAction(this)),
97 action_view_zoom_out_(new QAction(this)),
98 action_view_zoom_fit_(new QAction(this)),
99 action_view_zoom_one_to_one_(new QAction(this)),
100 action_view_show_cursors_(new QAction(this)),
102 device_selector_(&main_window, session.device_manager(),
104 configure_button_(this),
105 configure_button_action_(nullptr),
106 channels_button_(this),
107 channels_button_action_(nullptr),
108 sample_count_(" samples", this),
109 sample_rate_("Hz", this),
110 updating_sample_rate_(false),
111 updating_sample_count_(false),
112 sample_count_supported_(false),
113 icon_red_(":/icons/status-red.svg"),
114 icon_green_(":/icons/status-green.svg"),
115 icon_grey_(":/icons/status-grey.svg"),
116 run_stop_button_(this),
117 run_stop_button_action_(nullptr),
120 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
123 setObjectName(QString::fromUtf8("MainBar"));
127 setContextMenuPolicy(Qt::PreventContextMenu);
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 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
136 action_save_as_->setText(tr("&Save As..."));
137 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
138 QIcon(":/icons/document-save-as.png")));
139 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
140 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
142 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
143 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
144 QIcon(":/icons/document-save-as.png")));
145 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
146 action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
148 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
149 session.device_manager().context());
150 menu_file_export->setTitle(tr("&Export"));
151 connect(menu_file_export,
152 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
153 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
155 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
156 session.device_manager().context());
157 menu_file_import->setTitle(tr("&Import"));
158 connect(menu_file_import,
159 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
160 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
162 action_connect_->setText(tr("&Connect to Device..."));
163 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
165 action_view_zoom_in_->setText(tr("Zoom &In"));
166 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
167 QIcon(":/icons/zoom-in.png")));
168 // simply using Qt::Key_Plus shows no + in the menu
169 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
170 action_view_zoom_in_->setObjectName(
171 QString::fromUtf8("actionViewZoomIn"));
173 action_view_zoom_out_->setText(tr("Zoom &Out"));
174 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
175 QIcon(":/icons/zoom-out.png")));
176 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
177 action_view_zoom_out_->setObjectName(
178 QString::fromUtf8("actionViewZoomOut"));
180 action_view_zoom_fit_->setCheckable(true);
181 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
182 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
183 QIcon(":/icons/zoom-fit.png")));
184 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
185 action_view_zoom_fit_->setObjectName(
186 QString::fromUtf8("actionViewZoomFit"));
188 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
189 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
190 QIcon(":/icons/zoom-original.png")));
191 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
192 action_view_zoom_one_to_one_->setObjectName(
193 QString::fromUtf8("actionViewZoomOneToOne"));
195 action_view_show_cursors_->setCheckable(true);
196 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
197 QIcon(":/icons/show-cursors.svg")));
198 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
199 action_view_show_cursors_->setObjectName(
200 QString::fromUtf8("actionViewShowCursors"));
201 action_view_show_cursors_->setText(tr("Show &Cursors"));
204 QToolButton *const open_button = new QToolButton(this);
206 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
207 session.device_manager().context(), action_open_);
209 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
211 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
213 open_button->setMenu(import_menu);
214 open_button->setDefaultAction(action_open_);
215 open_button->setPopupMode(QToolButton::MenuButtonPopup);
218 QToolButton *const save_button = new QToolButton(this);
220 vector<QAction *> open_actions;
221 open_actions.push_back(action_save_as_);
222 open_actions.push_back(action_save_selection_as_);
224 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
225 session.device_manager().context(),
228 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
230 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
232 save_button->setMenu(export_menu);
233 save_button->setDefaultAction(action_save_as_);
234 save_button->setPopupMode(QToolButton::MenuButtonPopup);
236 // Device selector menu
237 connect(&device_selector_, SIGNAL(device_selected()),
238 this, SLOT(on_device_selected()));
240 // Setup the decoder button
242 menu_decoders_add_->setTitle(tr("&Add"));
243 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
244 this, SLOT(add_decoder(srd_decoder*)));
246 QToolButton *add_decoder_button = new QToolButton(this);
247 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
248 QIcon(":/icons/add-decoder.svg")));
249 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
250 add_decoder_button->setMenu(menu_decoders_add_);
254 addWidget(open_button);
255 addWidget(save_button);
257 addAction(action_view_zoom_in_);
258 addAction(action_view_zoom_out_);
259 addAction(action_view_zoom_fit_);
260 addAction(action_view_zoom_one_to_one_);
262 addAction(action_view_show_cursors_);
265 connect(&run_stop_button_, SIGNAL(clicked()),
266 this, SLOT(on_run_stop()));
267 connect(&sample_count_, SIGNAL(value_changed()),
268 this, SLOT(on_sample_count_changed()));
269 connect(&sample_rate_, SIGNAL(value_changed()),
270 this, SLOT(on_sample_rate_changed()));
272 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
274 set_capture_state(pv::Session::Stopped);
276 configure_button_.setIcon(QIcon::fromTheme("configure",
277 QIcon(":/icons/configure.png")));
279 channels_button_.setIcon(QIcon::fromTheme("channels",
280 QIcon(":/icons/channels.svg")));
282 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
284 addWidget(&device_selector_);
285 configure_button_action_ = addWidget(&configure_button_);
286 channels_button_action_ = addWidget(&channels_button_);
287 addWidget(&sample_count_);
288 addWidget(&sample_rate_);
289 run_stop_button_action_ = addWidget(&run_stop_button_);
292 addWidget(add_decoder_button);
295 QWidget *const spacer = new QWidget();
296 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
299 addWidget(&menu_button_);
301 sample_count_.installEventFilter(this);
302 sample_rate_.installEventFilter(this);
304 QMetaObject::connectSlotsByName(this);
306 // Setup session_ events
307 connect(&session_, SIGNAL(capture_state_changed(int)), this,
308 SLOT(capture_state_changed(int)));
309 connect(&session_, SIGNAL(device_selected()), this,
310 SLOT(device_selected()));
312 update_device_list();
315 Session &MainBar::session(void) const
320 void MainBar::update_device_list()
322 DeviceManager &mgr = session_.device_manager();
323 shared_ptr<devices::Device> selected_device = session_.device();
324 list< shared_ptr<devices::Device> > devs;
326 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
328 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
329 devs.push_back(selected_device);
331 device_selector_.set_device_list(devs, selected_device);
332 update_device_config_widgets();
336 void MainBar::set_capture_state(pv::Session::capture_state state)
338 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
339 run_stop_button_.setIcon(*icons[state]);
340 run_stop_button_.setText((state == pv::Session::Stopped) ?
341 tr("Run") : tr("Stop"));
342 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
344 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
346 device_selector_.setEnabled(ui_enabled);
347 configure_button_.setEnabled(ui_enabled);
348 channels_button_.setEnabled(ui_enabled);
349 sample_count_.setEnabled(ui_enabled);
350 sample_rate_.setEnabled(ui_enabled);
353 void MainBar::reset_device_selector()
355 device_selector_.reset();
358 void MainBar::select_device(shared_ptr<devices::Device> device)
362 session_.set_device(device);
364 session_.set_default_device();
365 } catch (const QString &e) {
366 QMessageBox msg(this);
368 msg.setInformativeText(tr("Failed to Select Device"));
369 msg.setStandardButtons(QMessageBox::Ok);
370 msg.setIcon(QMessageBox::Warning);
375 void MainBar::load_init_file(const std::string &file_name,
376 const std::string &format)
378 shared_ptr<InputFormat> input_format;
380 DeviceManager& device_manager = session_.device_manager();
382 if (!format.empty()) {
383 const map<string, shared_ptr<InputFormat> > formats =
384 device_manager.context()->input_formats();
385 const auto iter = find_if(formats.begin(), formats.end(),
386 [&](const pair<string, shared_ptr<InputFormat> > f) {
387 return f.first == format; });
388 if (iter == formats.end()) {
389 cerr << "Unexpected input format: " << format << endl;
393 input_format = (*iter).second;
396 load_file(QString::fromStdString(file_name), input_format);
399 QAction* MainBar::action_open() const
404 QAction* MainBar::action_save_as() const
406 return action_save_as_;
409 QAction* MainBar::action_save_selection_as() const
411 return action_save_selection_as_;
414 QAction* MainBar::action_connect() const
416 return action_connect_;
419 QAction* MainBar::action_view_zoom_in() const
421 return action_view_zoom_in_;
424 QAction* MainBar::action_view_zoom_out() const
426 return action_view_zoom_out_;
429 QAction* MainBar::action_view_zoom_fit() const
431 return action_view_zoom_fit_;
434 QAction* MainBar::action_view_zoom_one_to_one() const
436 return action_view_zoom_one_to_one_;
439 QAction* MainBar::action_view_show_cursors() const
441 return action_view_show_cursors_;
444 void MainBar::run_stop()
446 switch (session_.get_capture_state()) {
447 case Session::Stopped:
448 session_.start_capture([&](QString message) {
449 session_error("Capture failed", message); });
451 case Session::AwaitingTrigger:
452 case Session::Running:
453 session_.stop_capture();
458 void MainBar::load_file(QString file_name,
459 std::shared_ptr<sigrok::InputFormat> format,
460 const std::map<std::string, Glib::VariantBase> &options)
462 DeviceManager& device_manager = session_.device_manager();
464 const QString errorMessage(
465 QString("Failed to load file %1").arg(file_name));
469 session_.set_device(shared_ptr<devices::Device>(
470 new devices::InputFile(
471 device_manager.context(),
472 file_name.toStdString(),
475 session_.set_device(shared_ptr<devices::Device>(
476 new devices::SessionFile(
477 device_manager.context(),
478 file_name.toStdString())));
480 show_session_error(tr("Failed to load ") + file_name, e.what());
481 session_.set_default_device();
482 update_device_list();
486 session_.set_name(QFileInfo(file_name).fileName());
488 update_device_list();
490 session_.start_capture([&, errorMessage](QString infoMessage) {
491 session_error(errorMessage, infoMessage); });
494 void MainBar::update_sample_rate_selector()
496 Glib::VariantContainerBase gvar_dict;
498 const uint64_t *elements = nullptr;
500 map< const ConfigKey*, std::set<Capability> > keys;
502 if (updating_sample_rate_) {
503 sample_rate_.show_none();
507 const shared_ptr<devices::Device> device =
508 device_selector_.selected_device();
512 assert(!updating_sample_rate_);
513 updating_sample_rate_ = true;
515 const shared_ptr<sigrok::Device> sr_dev = device->device();
517 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
518 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
520 sample_rate_.show_none();
521 updating_sample_rate_ = false;
525 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
526 "samplerate-steps", G_VARIANT_TYPE("at")))) {
527 elements = (const uint64_t *)g_variant_get_fixed_array(
528 gvar_list, &num_elements, sizeof(uint64_t));
530 const uint64_t min = elements[0];
531 const uint64_t max = elements[1];
532 const uint64_t step = elements[2];
534 g_variant_unref(gvar_list);
542 sample_rate_.show_125_list(min, max);
544 // When the step is not 1, we cam't make a 1-2-5-10
545 // list of sample rates, because we may not be able to
546 // make round numbers. Therefore in this case, show a
548 sample_rate_.show_min_max_step(min, max, step);
550 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
551 "samplerates", G_VARIANT_TYPE("at")))) {
552 elements = (const uint64_t *)g_variant_get_fixed_array(
553 gvar_list, &num_elements, sizeof(uint64_t));
554 sample_rate_.show_list(elements, num_elements);
555 g_variant_unref(gvar_list);
557 updating_sample_rate_ = false;
559 update_sample_rate_selector_value();
562 void MainBar::update_sample_rate_selector_value()
564 if (updating_sample_rate_)
567 const shared_ptr<devices::Device> device =
568 device_selector_.selected_device();
573 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
574 uint64_t samplerate =
575 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
576 assert(!updating_sample_rate_);
577 updating_sample_rate_ = true;
578 sample_rate_.set_value(samplerate);
579 updating_sample_rate_ = false;
580 } catch (Error error) {
581 qDebug() << "WARNING: Failed to get value of sample rate";
586 void MainBar::update_sample_count_selector()
588 if (updating_sample_count_)
591 const shared_ptr<devices::Device> device =
592 device_selector_.selected_device();
596 const shared_ptr<sigrok::Device> sr_dev = device->device();
598 assert(!updating_sample_count_);
599 updating_sample_count_ = true;
601 if (!sample_count_supported_) {
602 sample_count_.show_none();
603 updating_sample_count_ = false;
607 uint64_t sample_count = sample_count_.value();
608 uint64_t min_sample_count = 0;
609 uint64_t max_sample_count = MaxSampleCount;
611 if (sample_count == 0)
612 sample_count = DefaultSampleCount;
614 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
615 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
617 g_variant_get(gvar.gobj(), "(tt)",
618 &min_sample_count, &max_sample_count);
621 min_sample_count = min(max(min_sample_count, MinSampleCount),
624 sample_count_.show_125_list(
625 min_sample_count, max_sample_count);
627 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
628 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
629 sample_count = g_variant_get_uint64(gvar.gobj());
630 if (sample_count == 0)
631 sample_count = DefaultSampleCount;
632 sample_count = min(max(sample_count, MinSampleCount),
636 sample_count_.set_value(sample_count);
638 updating_sample_count_ = false;
641 void MainBar::update_device_config_widgets()
643 using namespace pv::popups;
645 const shared_ptr<devices::Device> device =
646 device_selector_.selected_device();
648 // Hide the widgets if no device is selected
649 channels_button_action_->setVisible(!!device);
650 run_stop_button_action_->setVisible(!!device);
652 configure_button_action_->setVisible(false);
653 sample_count_.show_none();
654 sample_rate_.show_none();
658 const shared_ptr<sigrok::Device> sr_dev = device->device();
662 // Update the configure popup
663 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
664 configure_button_action_->setVisible(
665 !opts->binding().properties().empty());
666 configure_button_.set_popup(opts);
668 // Update the channels popup
669 Channels *const channels = new Channels(session_, this);
670 channels_button_.set_popup(channels);
672 // Update supported options.
673 sample_count_supported_ = false;
675 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
676 sample_count_supported_ = true;
678 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
679 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
680 Glib::Variant<guint64>::create(1));
684 // Add notification of reconfigure events
685 disconnect(this, SLOT(on_config_changed()));
686 connect(&opts->binding(), SIGNAL(config_changed()),
687 this, SLOT(on_config_changed()));
689 // Update sweep timing widgets.
690 update_sample_count_selector();
691 update_sample_rate_selector();
694 void MainBar::commit_sample_rate()
696 uint64_t sample_rate = 0;
698 const shared_ptr<devices::Device> device =
699 device_selector_.selected_device();
703 const shared_ptr<sigrok::Device> sr_dev = device->device();
705 sample_rate = sample_rate_.value();
706 if (sample_rate == 0)
710 sr_dev->config_set(ConfigKey::SAMPLERATE,
711 Glib::Variant<guint64>::create(sample_rate));
712 update_sample_rate_selector();
713 } catch (Error error) {
714 qDebug() << "Failed to configure samplerate.";
718 // Devices with built-in memory might impose limits on certain
719 // configurations, so let's check what sample count the driver
721 update_sample_count_selector();
724 void MainBar::commit_sample_count()
726 uint64_t sample_count = 0;
728 const shared_ptr<devices::Device> device =
729 device_selector_.selected_device();
733 const shared_ptr<sigrok::Device> sr_dev = device->device();
735 sample_count = sample_count_.value();
736 if (sample_count_supported_) {
738 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
739 Glib::Variant<guint64>::create(sample_count));
740 update_sample_count_selector();
741 } catch (Error error) {
742 qDebug() << "Failed to configure sample count.";
747 // Devices with built-in memory might impose limits on certain
748 // configurations, so let's check what sample rate the driver
750 update_sample_rate_selector();
753 void MainBar::session_error(const QString text, const QString info_text)
755 QMetaObject::invokeMethod(this, "show_session_error",
756 Qt::QueuedConnection, Q_ARG(QString, text),
757 Q_ARG(QString, info_text));
760 void MainBar::show_session_error(const QString text, const QString info_text)
762 QMessageBox msg(this);
764 msg.setInformativeText(info_text);
765 msg.setStandardButtons(QMessageBox::Ok);
766 msg.setIcon(QMessageBox::Warning);
770 void MainBar::capture_state_changed(int state)
772 set_capture_state((pv::Session::capture_state)state);
775 void MainBar::add_decoder(srd_decoder *decoder)
779 session_.add_decoder(decoder);
785 void MainBar::export_file(shared_ptr<OutputFormat> format,
788 using pv::dialogs::StoreProgress;
790 // Stop any currently running capture session
791 session_.stop_capture();
794 const QString dir = settings.value(SettingSaveDirectory).toString();
796 std::pair<uint64_t, uint64_t> sample_range;
798 // Selection only? Verify that the cursors are active and fetch their values
799 if (selection_only) {
800 if (!session_.main_view()->cursors()->enabled()) {
801 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
802 "cursors before you can save the data enclosed by them " \
803 "to a session file (e.g. using ALT-V - Show Cursors)."));
807 const double samplerate = session_.get_samplerate();
809 const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time();
810 const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time();
812 const uint64_t start_sample =
813 std::max((double)0, start_time.convert_to<double>() * samplerate);
814 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
816 sample_range = std::make_pair(start_sample, end_sample);
818 sample_range = std::make_pair(0, 0);
821 // Construct the filter
822 const vector<string> exts = format->extensions();
823 QString filter = tr("%1 files ").arg(
824 QString::fromStdString(format->description()));
829 filter += QString("(*.%1);;%2 (*.*)").arg(
830 QString::fromStdString(join(exts, ", *.")),
833 // Show the file dialog
834 const QString file_name = QFileDialog::getSaveFileName(
835 this, tr("Save File"), dir, filter);
837 if (file_name.isEmpty())
840 const QString abs_path = QFileInfo(file_name).absolutePath();
841 settings.setValue(SettingSaveDirectory, abs_path);
843 // Show the options dialog
844 map<string, Glib::VariantBase> options;
845 if (!format->options().empty()) {
846 dialogs::InputOutputOptions dlg(
847 tr("Export %1").arg(QString::fromStdString(
848 format->description())),
849 format->options(), this);
852 options = dlg.options();
855 session_.set_name(QFileInfo(file_name).fileName());
857 StoreProgress *dlg = new StoreProgress(file_name, format, options,
858 sample_range, session_, this);
862 void MainBar::import_file(shared_ptr<InputFormat> format)
867 const QString dir = settings.value(SettingOpenDirectory).toString();
869 // Construct the filter
870 const vector<string> exts = format->extensions();
871 const QString filter = exts.empty() ? "" :
872 tr("%1 files (*.%2)").arg(
873 QString::fromStdString(format->description()),
874 QString::fromStdString(join(exts, ", *.")));
876 // Show the file dialog
877 const QString file_name = QFileDialog::getOpenFileName(
878 this, tr("Import File"), dir, tr(
879 "%1 files (*.*);;All Files (*.*)").arg(
880 QString::fromStdString(format->description())));
882 if (file_name.isEmpty())
885 // Show the options dialog
886 map<string, Glib::VariantBase> options;
887 if (!format->options().empty()) {
888 dialogs::InputOutputOptions dlg(
889 tr("Import %1").arg(QString::fromStdString(
890 format->description())),
891 format->options(), this);
894 options = dlg.options();
897 load_file(file_name, format, options);
899 const QString abs_path = QFileInfo(file_name).absolutePath();
900 settings.setValue(SettingOpenDirectory, abs_path);
903 void MainBar::on_device_selected()
905 shared_ptr<devices::Device> device = device_selector_.selected_device();
907 reset_device_selector();
911 select_device(device);
913 update_device_config_widgets();
916 void MainBar::on_sample_count_changed()
918 if (!updating_sample_count_)
919 commit_sample_count();
922 void MainBar::on_sample_rate_changed()
924 if (!updating_sample_rate_)
925 commit_sample_rate();
928 void MainBar::on_run_stop()
930 commit_sample_count();
931 commit_sample_rate();
935 void MainBar::on_config_changed()
937 commit_sample_count();
938 commit_sample_rate();
941 void MainBar::on_actionOpen_triggered()
944 const QString dir = settings.value(SettingOpenDirectory).toString();
947 const QString file_name = QFileDialog::getOpenFileName(
948 this, tr("Open File"), dir, tr(
949 "Sigrok Sessions (*.sr);;"
952 if (!file_name.isEmpty()) {
953 load_file(file_name);
955 const QString abs_path = QFileInfo(file_name).absolutePath();
956 settings.setValue(SettingOpenDirectory, abs_path);
960 void MainBar::on_actionSaveAs_triggered()
962 export_file(session_.device_manager().context()->output_formats()["srzip"]);
965 void MainBar::on_actionSaveSelectionAs_triggered()
967 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
970 void MainBar::on_actionConnect_triggered()
972 // Stop any currently running capture session
973 session_.stop_capture();
975 dialogs::Connect dlg(this, session_.device_manager());
977 // If the user selected a device, select it in the device list. Select the
978 // current device otherwise.
980 select_device(dlg.get_selected_device());
982 update_device_list();
985 void MainBar::on_actionViewZoomIn_triggered()
987 session_.main_view()->zoom(1);
990 void MainBar::on_actionViewZoomOut_triggered()
992 session_.main_view()->zoom(-1);
995 void MainBar::on_actionViewZoomFit_triggered()
997 session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked());
1000 void MainBar::on_actionViewZoomOneToOne_triggered()
1002 session_.main_view()->zoom_one_to_one();
1005 void MainBar::on_actionViewShowCursors_triggered()
1007 const bool show = !session_.main_view()->cursors_shown();
1009 session_.main_view()->centre_cursors();
1011 session_.main_view()->show_cursors(show);
1014 bool MainBar::eventFilter(QObject *watched, QEvent *event)
1016 if (sample_count_supported_ && (watched == &sample_count_ ||
1017 watched == &sample_rate_) &&
1018 (event->type() == QEvent::ToolTip)) {
1019 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
1020 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
1022 QString str = tr("Total sampling time: %1").arg(
1023 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
1024 QToolTip::showText(help_event->globalPos(), str);
1032 } // namespace toolbars