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),
114 icon_red_(":/icons/status-red.svg"),
115 icon_green_(":/icons/status-green.svg"),
116 icon_grey_(":/icons/status-grey.svg"),
117 run_stop_button_(this),
118 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_new_view_->setText(tr("New &View"));
131 action_new_view_->setIcon(QIcon::fromTheme("window-new",
132 QIcon(":/icons/window-new.png")));
133 connect(action_new_view_, SIGNAL(triggered(bool)),
134 this, SLOT(on_actionNewView_triggered()));
136 action_open_->setText(tr("&Open..."));
137 action_open_->setIcon(QIcon::fromTheme("document-open",
138 QIcon(":/icons/document-open.png")));
139 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
140 connect(action_open_, SIGNAL(triggered(bool)),
141 this, SLOT(on_actionOpen_triggered()));
143 action_save_as_->setText(tr("&Save As..."));
144 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
145 QIcon(":/icons/document-save-as.png")));
146 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
147 connect(action_save_as_, SIGNAL(triggered(bool)),
148 this, SLOT(on_actionSaveAs_triggered()));
150 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
151 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
152 QIcon(":/icons/document-save-as.png")));
153 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
154 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
155 this, SLOT(on_actionSaveSelectionAs_triggered()));
157 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
158 session.device_manager().context());
159 menu_file_export->setTitle(tr("&Export"));
160 connect(menu_file_export,
161 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
162 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
164 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
165 session.device_manager().context());
166 menu_file_import->setTitle(tr("&Import"));
167 connect(menu_file_import,
168 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
169 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
171 action_connect_->setText(tr("&Connect to Device..."));
172 connect(action_connect_, SIGNAL(triggered(bool)),
173 this, SLOT(on_actionConnect_triggered()));
175 action_view_zoom_in_->setText(tr("Zoom &In"));
176 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
177 QIcon(":/icons/zoom-in.png")));
178 // simply using Qt::Key_Plus shows no + in the menu
179 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
180 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
181 this, SLOT(on_actionViewZoomIn_triggered()));
183 action_view_zoom_out_->setText(tr("Zoom &Out"));
184 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
185 QIcon(":/icons/zoom-out.png")));
186 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
187 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
188 this, SLOT(on_actionViewZoomOut_triggered()));
190 action_view_zoom_fit_->setCheckable(true);
191 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
192 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
193 QIcon(":/icons/zoom-fit.png")));
194 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
195 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
196 this, SLOT(on_actionViewZoomFit_triggered()));
198 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
199 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
200 QIcon(":/icons/zoom-original.png")));
201 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
202 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
203 this, SLOT(on_actionViewZoomOneToOne_triggered()));
205 action_view_show_cursors_->setCheckable(true);
206 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
207 QIcon(":/icons/show-cursors.svg")));
208 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
209 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
210 this, SLOT(on_actionViewShowCursors_triggered()));
211 action_view_show_cursors_->setText(tr("Show &Cursors"));
214 QToolButton *const open_button = new QToolButton(this);
216 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
217 session.device_manager().context(), action_open_);
219 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
221 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
223 open_button->setMenu(import_menu);
224 open_button->setDefaultAction(action_open_);
225 open_button->setPopupMode(QToolButton::MenuButtonPopup);
228 QToolButton *const save_button = new QToolButton(this);
230 vector<QAction *> open_actions;
231 open_actions.push_back(action_save_as_);
232 open_actions.push_back(action_save_selection_as_);
234 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
235 session.device_manager().context(),
238 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
240 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
242 save_button->setMenu(export_menu);
243 save_button->setDefaultAction(action_save_as_);
244 save_button->setPopupMode(QToolButton::MenuButtonPopup);
246 // Device selector menu
247 connect(&device_selector_, SIGNAL(device_selected()),
248 this, SLOT(on_device_selected()));
250 // Setup the decoder button
252 menu_decoders_add_->setTitle(tr("&Add"));
253 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
254 this, SLOT(add_decoder(srd_decoder*)));
256 QToolButton *add_decoder_button = new QToolButton(this);
257 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
258 QIcon(":/icons/add-decoder.svg")));
259 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
260 add_decoder_button->setMenu(menu_decoders_add_);
264 addAction(action_new_view_);
266 addWidget(open_button);
267 addWidget(save_button);
269 addAction(action_view_zoom_in_);
270 addAction(action_view_zoom_out_);
271 addAction(action_view_zoom_fit_);
272 addAction(action_view_zoom_one_to_one_);
274 addAction(action_view_show_cursors_);
277 connect(&run_stop_button_, SIGNAL(clicked()),
278 this, SLOT(on_run_stop()));
279 connect(&sample_count_, SIGNAL(value_changed()),
280 this, SLOT(on_sample_count_changed()));
281 connect(&sample_rate_, SIGNAL(value_changed()),
282 this, SLOT(on_sample_rate_changed()));
284 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
286 set_capture_state(pv::Session::Stopped);
288 configure_button_.setIcon(QIcon::fromTheme("configure",
289 QIcon(":/icons/configure.png")));
291 channels_button_.setIcon(QIcon::fromTheme("channels",
292 QIcon(":/icons/channels.svg")));
294 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
296 addWidget(&device_selector_);
297 configure_button_action_ = addWidget(&configure_button_);
298 channels_button_action_ = addWidget(&channels_button_);
299 addWidget(&sample_count_);
300 addWidget(&sample_rate_);
301 run_stop_button_action_ = addWidget(&run_stop_button_);
304 addWidget(add_decoder_button);
307 sample_count_.installEventFilter(this);
308 sample_rate_.installEventFilter(this);
310 // Setup session_ events
311 connect(&session_, SIGNAL(capture_state_changed(int)),
312 this, SLOT(capture_state_changed(int)));
313 connect(&session, SIGNAL(device_changed()),
314 this, SLOT(on_device_changed()));
316 update_device_list();
319 Session &MainBar::session(void) const
324 void MainBar::update_device_list()
326 DeviceManager &mgr = session_.device_manager();
327 shared_ptr<devices::Device> selected_device = session_.device();
328 list< shared_ptr<devices::Device> > devs;
330 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
332 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
333 devs.push_back(selected_device);
335 device_selector_.set_device_list(devs, selected_device);
336 update_device_config_widgets();
340 void MainBar::set_capture_state(pv::Session::capture_state state)
342 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
343 run_stop_button_.setIcon(*icons[state]);
344 run_stop_button_.setText((state == pv::Session::Stopped) ?
345 tr("Run") : tr("Stop"));
346 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
348 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
350 device_selector_.setEnabled(ui_enabled);
351 configure_button_.setEnabled(ui_enabled);
352 channels_button_.setEnabled(ui_enabled);
353 sample_count_.setEnabled(ui_enabled);
354 sample_rate_.setEnabled(ui_enabled);
357 void MainBar::reset_device_selector()
359 device_selector_.reset();
362 void MainBar::select_device(shared_ptr<devices::Device> device)
366 session_.set_device(device);
368 session_.set_default_device();
369 } catch (const QString &e) {
370 QMessageBox msg(this);
372 msg.setInformativeText(tr("Failed to Select Device"));
373 msg.setStandardButtons(QMessageBox::Ok);
374 msg.setIcon(QMessageBox::Warning);
379 void MainBar::load_init_file(const std::string &file_name,
380 const std::string &format)
382 shared_ptr<InputFormat> input_format;
384 DeviceManager& device_manager = session_.device_manager();
386 if (!format.empty()) {
387 const map<string, shared_ptr<InputFormat> > formats =
388 device_manager.context()->input_formats();
389 const auto iter = find_if(formats.begin(), formats.end(),
390 [&](const pair<string, shared_ptr<InputFormat> > f) {
391 return f.first == format; });
392 if (iter == formats.end()) {
393 cerr << "Unexpected input format: " << format << endl;
397 input_format = (*iter).second;
400 load_file(QString::fromStdString(file_name), input_format);
403 QAction* MainBar::action_open() const
408 QAction* MainBar::action_save_as() const
410 return action_save_as_;
413 QAction* MainBar::action_save_selection_as() const
415 return action_save_selection_as_;
418 QAction* MainBar::action_connect() const
420 return action_connect_;
423 QAction* MainBar::action_view_zoom_in() const
425 return action_view_zoom_in_;
428 QAction* MainBar::action_view_zoom_out() const
430 return action_view_zoom_out_;
433 QAction* MainBar::action_view_zoom_fit() const
435 return action_view_zoom_fit_;
438 QAction* MainBar::action_view_zoom_one_to_one() const
440 return action_view_zoom_one_to_one_;
443 QAction* MainBar::action_view_show_cursors() const
445 return action_view_show_cursors_;
448 void MainBar::run_stop()
450 switch (session_.get_capture_state()) {
451 case Session::Stopped:
452 session_.start_capture([&](QString message) {
453 session_error("Capture failed", message); });
455 case Session::AwaitingTrigger:
456 case Session::Running:
457 session_.stop_capture();
462 void MainBar::load_file(QString file_name,
463 std::shared_ptr<sigrok::InputFormat> format,
464 const std::map<std::string, Glib::VariantBase> &options)
466 DeviceManager& device_manager = session_.device_manager();
468 const QString errorMessage(
469 QString("Failed to load file %1").arg(file_name));
473 session_.set_device(shared_ptr<devices::Device>(
474 new devices::InputFile(
475 device_manager.context(),
476 file_name.toStdString(),
479 session_.set_device(shared_ptr<devices::Device>(
480 new devices::SessionFile(
481 device_manager.context(),
482 file_name.toStdString())));
484 show_session_error(tr("Failed to load ") + file_name, e.what());
485 session_.set_default_device();
486 update_device_list();
490 update_device_list();
492 session_.start_capture([&, errorMessage](QString infoMessage) {
493 session_error(errorMessage, infoMessage); });
495 session_.set_name(QFileInfo(file_name).fileName());
498 void MainBar::update_sample_rate_selector()
500 Glib::VariantContainerBase gvar_dict;
502 const uint64_t *elements = nullptr;
504 map< const ConfigKey*, std::set<Capability> > keys;
506 if (updating_sample_rate_) {
507 sample_rate_.show_none();
511 const shared_ptr<devices::Device> device =
512 device_selector_.selected_device();
516 assert(!updating_sample_rate_);
517 updating_sample_rate_ = true;
519 const shared_ptr<sigrok::Device> sr_dev = device->device();
521 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
522 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
524 sample_rate_.show_none();
525 updating_sample_rate_ = false;
529 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
530 "samplerate-steps", G_VARIANT_TYPE("at")))) {
531 elements = (const uint64_t *)g_variant_get_fixed_array(
532 gvar_list, &num_elements, sizeof(uint64_t));
534 const uint64_t min = elements[0];
535 const uint64_t max = elements[1];
536 const uint64_t step = elements[2];
538 g_variant_unref(gvar_list);
546 sample_rate_.show_125_list(min, max);
548 // When the step is not 1, we cam't make a 1-2-5-10
549 // list of sample rates, because we may not be able to
550 // make round numbers. Therefore in this case, show a
552 sample_rate_.show_min_max_step(min, max, step);
554 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
555 "samplerates", G_VARIANT_TYPE("at")))) {
556 elements = (const uint64_t *)g_variant_get_fixed_array(
557 gvar_list, &num_elements, sizeof(uint64_t));
558 sample_rate_.show_list(elements, num_elements);
559 g_variant_unref(gvar_list);
561 updating_sample_rate_ = false;
563 update_sample_rate_selector_value();
566 void MainBar::update_sample_rate_selector_value()
568 if (updating_sample_rate_)
571 const shared_ptr<devices::Device> device =
572 device_selector_.selected_device();
577 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
578 uint64_t samplerate =
579 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
580 assert(!updating_sample_rate_);
581 updating_sample_rate_ = true;
582 sample_rate_.set_value(samplerate);
583 updating_sample_rate_ = false;
584 } catch (Error error) {
585 qDebug() << "WARNING: Failed to get value of sample rate";
590 void MainBar::update_sample_count_selector()
592 if (updating_sample_count_)
595 const shared_ptr<devices::Device> device =
596 device_selector_.selected_device();
600 const shared_ptr<sigrok::Device> sr_dev = device->device();
602 assert(!updating_sample_count_);
603 updating_sample_count_ = true;
605 if (!sample_count_supported_) {
606 sample_count_.show_none();
607 updating_sample_count_ = false;
611 uint64_t sample_count = sample_count_.value();
612 uint64_t min_sample_count = 0;
613 uint64_t max_sample_count = MaxSampleCount;
615 if (sample_count == 0)
616 sample_count = DefaultSampleCount;
618 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
619 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
621 g_variant_get(gvar.gobj(), "(tt)",
622 &min_sample_count, &max_sample_count);
625 min_sample_count = min(max(min_sample_count, MinSampleCount),
628 sample_count_.show_125_list(
629 min_sample_count, max_sample_count);
631 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
632 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
633 sample_count = g_variant_get_uint64(gvar.gobj());
634 if (sample_count == 0)
635 sample_count = DefaultSampleCount;
636 sample_count = min(max(sample_count, MinSampleCount),
640 sample_count_.set_value(sample_count);
642 updating_sample_count_ = false;
645 void MainBar::update_device_config_widgets()
647 using namespace pv::popups;
649 const shared_ptr<devices::Device> device =
650 device_selector_.selected_device();
652 // Hide the widgets if no device is selected
653 channels_button_action_->setVisible(!!device);
654 run_stop_button_action_->setVisible(!!device);
656 configure_button_action_->setVisible(false);
657 sample_count_.show_none();
658 sample_rate_.show_none();
662 const shared_ptr<sigrok::Device> sr_dev = device->device();
666 // Update the configure popup
667 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
668 configure_button_action_->setVisible(
669 !opts->binding().properties().empty());
670 configure_button_.set_popup(opts);
672 // Update the channels popup
673 Channels *const channels = new Channels(session_, this);
674 channels_button_.set_popup(channels);
676 // Update supported options.
677 sample_count_supported_ = false;
679 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
680 sample_count_supported_ = true;
682 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
683 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
684 Glib::Variant<guint64>::create(1));
688 // Add notification of reconfigure events
689 disconnect(this, SLOT(on_config_changed()));
690 connect(&opts->binding(), SIGNAL(config_changed()),
691 this, SLOT(on_config_changed()));
693 // Update sweep timing widgets.
694 update_sample_count_selector();
695 update_sample_rate_selector();
698 void MainBar::commit_sample_rate()
700 uint64_t sample_rate = 0;
702 const shared_ptr<devices::Device> device =
703 device_selector_.selected_device();
707 const shared_ptr<sigrok::Device> sr_dev = device->device();
709 sample_rate = sample_rate_.value();
710 if (sample_rate == 0)
714 sr_dev->config_set(ConfigKey::SAMPLERATE,
715 Glib::Variant<guint64>::create(sample_rate));
716 update_sample_rate_selector();
717 } catch (Error error) {
718 qDebug() << "Failed to configure samplerate.";
722 // Devices with built-in memory might impose limits on certain
723 // configurations, so let's check what sample count the driver
725 update_sample_count_selector();
728 void MainBar::commit_sample_count()
730 uint64_t sample_count = 0;
732 const shared_ptr<devices::Device> device =
733 device_selector_.selected_device();
737 const shared_ptr<sigrok::Device> sr_dev = device->device();
739 sample_count = sample_count_.value();
740 if (sample_count_supported_) {
742 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
743 Glib::Variant<guint64>::create(sample_count));
744 update_sample_count_selector();
745 } catch (Error error) {
746 qDebug() << "Failed to configure sample count.";
751 // Devices with built-in memory might impose limits on certain
752 // configurations, so let's check what sample rate the driver
754 update_sample_rate_selector();
757 void MainBar::session_error(const QString text, const QString info_text)
759 QMetaObject::invokeMethod(this, "show_session_error",
760 Qt::QueuedConnection, Q_ARG(QString, text),
761 Q_ARG(QString, info_text));
764 void MainBar::show_session_error(const QString text, const QString info_text)
766 QMessageBox msg(this);
768 msg.setInformativeText(info_text);
769 msg.setStandardButtons(QMessageBox::Ok);
770 msg.setIcon(QMessageBox::Warning);
774 void MainBar::capture_state_changed(int state)
776 set_capture_state((pv::Session::capture_state)state);
779 void MainBar::add_decoder(srd_decoder *decoder)
783 session_.add_decoder(decoder);
789 void MainBar::export_file(shared_ptr<OutputFormat> format,
792 using pv::dialogs::StoreProgress;
794 // Stop any currently running capture session
795 session_.stop_capture();
798 const QString dir = settings.value(SettingSaveDirectory).toString();
800 std::pair<uint64_t, uint64_t> sample_range;
802 // Selection only? Verify that the cursors are active and fetch their values
803 if (selection_only) {
804 views::TraceView::View *trace_view =
805 qobject_cast<views::TraceView::View*>(session_.main_view().get());
807 if (!trace_view->cursors()->enabled()) {
808 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
809 "cursors before you can save the data enclosed by them " \
810 "to a session file (e.g. using ALT-V - Show Cursors)."));
814 const double samplerate = session_.get_samplerate();
816 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
817 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
819 const uint64_t start_sample =
820 std::max((double)0, start_time.convert_to<double>() * samplerate);
821 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
823 sample_range = std::make_pair(start_sample, end_sample);
825 sample_range = std::make_pair(0, 0);
828 // Construct the filter
829 const vector<string> exts = format->extensions();
830 QString filter = tr("%1 files ").arg(
831 QString::fromStdString(format->description()));
836 filter += QString("(*.%1);;%2 (*.*)").arg(
837 QString::fromStdString(join(exts, ", *.")),
840 // Show the file dialog
841 const QString file_name = QFileDialog::getSaveFileName(
842 this, tr("Save File"), dir, filter);
844 if (file_name.isEmpty())
847 const QString abs_path = QFileInfo(file_name).absolutePath();
848 settings.setValue(SettingSaveDirectory, abs_path);
850 // Show the options dialog
851 map<string, Glib::VariantBase> options;
852 if (!format->options().empty()) {
853 dialogs::InputOutputOptions dlg(
854 tr("Export %1").arg(QString::fromStdString(
855 format->description())),
856 format->options(), this);
859 options = dlg.options();
862 session_.set_name(QFileInfo(file_name).fileName());
864 StoreProgress *dlg = new StoreProgress(file_name, format, options,
865 sample_range, session_, this);
869 void MainBar::import_file(shared_ptr<InputFormat> format)
874 const QString dir = settings.value(SettingOpenDirectory).toString();
876 // Construct the filter
877 const vector<string> exts = format->extensions();
878 const QString filter = exts.empty() ? "" :
879 tr("%1 files (*.%2)").arg(
880 QString::fromStdString(format->description()),
881 QString::fromStdString(join(exts, ", *.")));
883 // Show the file dialog
884 const QString file_name = QFileDialog::getOpenFileName(
885 this, tr("Import File"), dir, tr(
886 "%1 files (*.*);;All Files (*.*)").arg(
887 QString::fromStdString(format->description())));
889 if (file_name.isEmpty())
892 // Show the options dialog
893 map<string, Glib::VariantBase> options;
894 if (!format->options().empty()) {
895 dialogs::InputOutputOptions dlg(
896 tr("Import %1").arg(QString::fromStdString(
897 format->description())),
898 format->options(), this);
901 options = dlg.options();
904 load_file(file_name, format, options);
906 const QString abs_path = QFileInfo(file_name).absolutePath();
907 settings.setValue(SettingOpenDirectory, abs_path);
910 void MainBar::on_device_selected()
912 shared_ptr<devices::Device> device = device_selector_.selected_device();
914 reset_device_selector();
918 select_device(device);
921 void MainBar::on_device_changed()
923 update_device_list();
924 update_device_config_widgets();
927 void MainBar::on_sample_count_changed()
929 if (!updating_sample_count_)
930 commit_sample_count();
933 void MainBar::on_sample_rate_changed()
935 if (!updating_sample_rate_)
936 commit_sample_rate();
939 void MainBar::on_run_stop()
941 commit_sample_count();
942 commit_sample_rate();
946 void MainBar::on_config_changed()
948 commit_sample_count();
949 commit_sample_rate();
952 void MainBar::on_actionNewView_triggered()
957 void MainBar::on_actionOpen_triggered()
960 const QString dir = settings.value(SettingOpenDirectory).toString();
963 const QString file_name = QFileDialog::getOpenFileName(
964 this, tr("Open File"), dir, tr(
965 "Sigrok Sessions (*.sr);;"
968 if (!file_name.isEmpty()) {
969 load_file(file_name);
971 const QString abs_path = QFileInfo(file_name).absolutePath();
972 settings.setValue(SettingOpenDirectory, abs_path);
976 void MainBar::on_actionSaveAs_triggered()
978 export_file(session_.device_manager().context()->output_formats()["srzip"]);
981 void MainBar::on_actionSaveSelectionAs_triggered()
983 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
986 void MainBar::on_actionConnect_triggered()
988 // Stop any currently running capture session
989 session_.stop_capture();
991 dialogs::Connect dlg(this, session_.device_manager());
993 // If the user selected a device, select it in the device list. Select the
994 // current device otherwise.
996 select_device(dlg.get_selected_device());
998 update_device_list();
1001 void MainBar::on_actionViewZoomIn_triggered()
1003 views::TraceView::View *trace_view =
1004 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1006 trace_view->zoom(1);
1009 void MainBar::on_actionViewZoomOut_triggered()
1011 views::TraceView::View *trace_view =
1012 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1014 trace_view->zoom(-1);
1017 void MainBar::on_actionViewZoomFit_triggered()
1019 views::TraceView::View *trace_view =
1020 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1022 trace_view->zoom_fit(action_view_zoom_fit_->isChecked());
1025 void MainBar::on_actionViewZoomOneToOne_triggered()
1027 views::TraceView::View *trace_view =
1028 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1030 trace_view->zoom_one_to_one();
1033 void MainBar::on_actionViewShowCursors_triggered()
1035 views::TraceView::View *trace_view =
1036 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1038 const bool show = !trace_view->cursors_shown();
1040 trace_view->centre_cursors();
1042 trace_view->show_cursors(show);
1045 void MainBar::on_always_zoom_to_fit_changed(bool state)
1047 action_view_zoom_fit_->setChecked(state);
1050 bool MainBar::eventFilter(QObject *watched, QEvent *event)
1052 if (sample_count_supported_ && (watched == &sample_count_ ||
1053 watched == &sample_rate_) &&
1054 (event->type() == QEvent::ToolTip)) {
1055 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
1056 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
1058 QString str = tr("Total sampling time: %1").arg(
1059 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
1060 QToolTip::showText(help_event->globalPos(), str);
1068 } // namespace toolbars