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_session_(new QAction(this)),
93 action_new_view_(new QAction(this)),
94 action_open_(new QAction(this)),
95 action_save_as_(new QAction(this)),
96 action_save_selection_as_(new QAction(this)),
97 action_connect_(new QAction(this)),
98 action_view_zoom_in_(new QAction(this)),
99 action_view_zoom_out_(new QAction(this)),
100 action_view_zoom_fit_(new QAction(this)),
101 action_view_zoom_one_to_one_(new QAction(this)),
102 action_view_show_cursors_(new QAction(this)),
104 device_selector_(&main_window, session.device_manager(),
106 configure_button_(this),
107 configure_button_action_(nullptr),
108 channels_button_(this),
109 channels_button_action_(nullptr),
110 sample_count_(" samples", this),
111 sample_rate_("Hz", this),
112 updating_sample_rate_(false),
113 updating_sample_count_(false),
114 sample_count_supported_(false),
115 icon_red_(":/icons/status-red.svg"),
116 icon_green_(":/icons/status-green.svg"),
117 icon_grey_(":/icons/status-grey.svg"),
118 run_stop_button_(this),
119 run_stop_button_action_(nullptr),
122 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
125 setObjectName(QString::fromUtf8("MainBar"));
129 setContextMenuPolicy(Qt::PreventContextMenu);
132 action_new_session_->setText(tr("New &Session"));
133 action_new_session_->setIcon(QIcon::fromTheme("document-new",
134 QIcon(":/icons/document-new.png")));
135 connect(action_new_session_, SIGNAL(triggered(bool)),
136 this, SLOT(on_actionNewSession_triggered()));
138 action_new_view_->setText(tr("New &View"));
139 action_new_view_->setIcon(QIcon::fromTheme("window-new",
140 QIcon(":/icons/window-new.png")));
141 connect(action_new_view_, SIGNAL(triggered(bool)),
142 this, SLOT(on_actionNewView_triggered()));
144 action_open_->setText(tr("&Open..."));
145 action_open_->setIcon(QIcon::fromTheme("document-open",
146 QIcon(":/icons/document-open.png")));
147 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
148 connect(action_open_, SIGNAL(triggered(bool)),
149 this, SLOT(on_actionOpen_triggered()));
151 action_save_as_->setText(tr("&Save As..."));
152 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
153 QIcon(":/icons/document-save-as.png")));
154 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
155 connect(action_save_as_, SIGNAL(triggered(bool)),
156 this, SLOT(on_actionSaveAs_triggered()));
158 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
159 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
160 QIcon(":/icons/document-save-as.png")));
161 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
162 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
163 this, SLOT(on_actionSaveSelectionAs_triggered()));
165 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
166 session.device_manager().context());
167 menu_file_export->setTitle(tr("&Export"));
168 connect(menu_file_export,
169 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
170 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
172 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
173 session.device_manager().context());
174 menu_file_import->setTitle(tr("&Import"));
175 connect(menu_file_import,
176 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
177 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
179 action_connect_->setText(tr("&Connect to Device..."));
180 connect(action_connect_, SIGNAL(triggered(bool)),
181 this, SLOT(on_actionConnect_triggered()));
183 action_view_zoom_in_->setText(tr("Zoom &In"));
184 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
185 QIcon(":/icons/zoom-in.png")));
186 // simply using Qt::Key_Plus shows no + in the menu
187 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
188 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
189 this, SLOT(on_actionViewZoomIn_triggered()));
191 action_view_zoom_out_->setText(tr("Zoom &Out"));
192 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
193 QIcon(":/icons/zoom-out.png")));
194 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
195 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
196 this, SLOT(on_actionViewZoomOut_triggered()));
198 action_view_zoom_fit_->setCheckable(true);
199 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
200 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
201 QIcon(":/icons/zoom-fit.png")));
202 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
203 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
204 this, SLOT(on_actionViewZoomFit_triggered()));
206 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
207 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
208 QIcon(":/icons/zoom-original.png")));
209 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
210 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
211 this, SLOT(on_actionViewZoomOneToOne_triggered()));
213 action_view_show_cursors_->setCheckable(true);
214 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
215 QIcon(":/icons/show-cursors.svg")));
216 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
217 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
218 this, SLOT(on_actionViewShowCursors_triggered()));
219 action_view_show_cursors_->setText(tr("Show &Cursors"));
222 QToolButton *const open_button = new QToolButton(this);
224 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
225 session.device_manager().context(), action_open_);
227 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
229 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
231 open_button->setMenu(import_menu);
232 open_button->setDefaultAction(action_open_);
233 open_button->setPopupMode(QToolButton::MenuButtonPopup);
236 QToolButton *const save_button = new QToolButton(this);
238 vector<QAction *> open_actions;
239 open_actions.push_back(action_save_as_);
240 open_actions.push_back(action_save_selection_as_);
242 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
243 session.device_manager().context(),
246 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
248 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
250 save_button->setMenu(export_menu);
251 save_button->setDefaultAction(action_save_as_);
252 save_button->setPopupMode(QToolButton::MenuButtonPopup);
254 // Device selector menu
255 connect(&device_selector_, SIGNAL(device_selected()),
256 this, SLOT(on_device_selected()));
258 // Setup the decoder button
260 menu_decoders_add_->setTitle(tr("&Add"));
261 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
262 this, SLOT(add_decoder(srd_decoder*)));
264 QToolButton *add_decoder_button = new QToolButton(this);
265 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
266 QIcon(":/icons/add-decoder.svg")));
267 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
268 add_decoder_button->setMenu(menu_decoders_add_);
272 addAction(action_new_session_);
273 addAction(action_new_view_);
275 addWidget(open_button);
276 addWidget(save_button);
278 addAction(action_view_zoom_in_);
279 addAction(action_view_zoom_out_);
280 addAction(action_view_zoom_fit_);
281 addAction(action_view_zoom_one_to_one_);
283 addAction(action_view_show_cursors_);
286 connect(&run_stop_button_, SIGNAL(clicked()),
287 this, SLOT(on_run_stop()));
288 connect(&sample_count_, SIGNAL(value_changed()),
289 this, SLOT(on_sample_count_changed()));
290 connect(&sample_rate_, SIGNAL(value_changed()),
291 this, SLOT(on_sample_rate_changed()));
293 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
295 set_capture_state(pv::Session::Stopped);
297 configure_button_.setIcon(QIcon::fromTheme("configure",
298 QIcon(":/icons/configure.png")));
300 channels_button_.setIcon(QIcon::fromTheme("channels",
301 QIcon(":/icons/channels.svg")));
303 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
305 addWidget(&device_selector_);
306 configure_button_action_ = addWidget(&configure_button_);
307 channels_button_action_ = addWidget(&channels_button_);
308 addWidget(&sample_count_);
309 addWidget(&sample_rate_);
310 run_stop_button_action_ = addWidget(&run_stop_button_);
313 addWidget(add_decoder_button);
316 QWidget *const spacer = new QWidget();
317 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
320 addWidget(&menu_button_);
322 sample_count_.installEventFilter(this);
323 sample_rate_.installEventFilter(this);
325 // Setup session_ events
326 connect(&session_, SIGNAL(capture_state_changed(int)),
327 this, SLOT(capture_state_changed(int)));
328 connect(&session, SIGNAL(device_changed()),
329 this, SLOT(on_device_changed()));
331 update_device_list();
334 Session &MainBar::session(void) const
339 void MainBar::update_device_list()
341 DeviceManager &mgr = session_.device_manager();
342 shared_ptr<devices::Device> selected_device = session_.device();
343 list< shared_ptr<devices::Device> > devs;
345 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
347 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
348 devs.push_back(selected_device);
350 device_selector_.set_device_list(devs, selected_device);
351 update_device_config_widgets();
355 void MainBar::set_capture_state(pv::Session::capture_state state)
357 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
358 run_stop_button_.setIcon(*icons[state]);
359 run_stop_button_.setText((state == pv::Session::Stopped) ?
360 tr("Run") : tr("Stop"));
361 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
363 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
365 device_selector_.setEnabled(ui_enabled);
366 configure_button_.setEnabled(ui_enabled);
367 channels_button_.setEnabled(ui_enabled);
368 sample_count_.setEnabled(ui_enabled);
369 sample_rate_.setEnabled(ui_enabled);
372 void MainBar::reset_device_selector()
374 device_selector_.reset();
377 void MainBar::select_device(shared_ptr<devices::Device> device)
381 session_.set_device(device);
383 session_.set_default_device();
384 } catch (const QString &e) {
385 QMessageBox msg(this);
387 msg.setInformativeText(tr("Failed to Select Device"));
388 msg.setStandardButtons(QMessageBox::Ok);
389 msg.setIcon(QMessageBox::Warning);
394 void MainBar::load_init_file(const std::string &file_name,
395 const std::string &format)
397 shared_ptr<InputFormat> input_format;
399 DeviceManager& device_manager = session_.device_manager();
401 if (!format.empty()) {
402 const map<string, shared_ptr<InputFormat> > formats =
403 device_manager.context()->input_formats();
404 const auto iter = find_if(formats.begin(), formats.end(),
405 [&](const pair<string, shared_ptr<InputFormat> > f) {
406 return f.first == format; });
407 if (iter == formats.end()) {
408 cerr << "Unexpected input format: " << format << endl;
412 input_format = (*iter).second;
415 load_file(QString::fromStdString(file_name), input_format);
418 QAction* MainBar::action_open() const
423 QAction* MainBar::action_save_as() const
425 return action_save_as_;
428 QAction* MainBar::action_save_selection_as() const
430 return action_save_selection_as_;
433 QAction* MainBar::action_connect() const
435 return action_connect_;
438 QAction* MainBar::action_view_zoom_in() const
440 return action_view_zoom_in_;
443 QAction* MainBar::action_view_zoom_out() const
445 return action_view_zoom_out_;
448 QAction* MainBar::action_view_zoom_fit() const
450 return action_view_zoom_fit_;
453 QAction* MainBar::action_view_zoom_one_to_one() const
455 return action_view_zoom_one_to_one_;
458 QAction* MainBar::action_view_show_cursors() const
460 return action_view_show_cursors_;
463 void MainBar::run_stop()
465 switch (session_.get_capture_state()) {
466 case Session::Stopped:
467 session_.start_capture([&](QString message) {
468 session_error("Capture failed", message); });
470 case Session::AwaitingTrigger:
471 case Session::Running:
472 session_.stop_capture();
477 void MainBar::load_file(QString file_name,
478 std::shared_ptr<sigrok::InputFormat> format,
479 const std::map<std::string, Glib::VariantBase> &options)
481 DeviceManager& device_manager = session_.device_manager();
483 const QString errorMessage(
484 QString("Failed to load file %1").arg(file_name));
488 session_.set_device(shared_ptr<devices::Device>(
489 new devices::InputFile(
490 device_manager.context(),
491 file_name.toStdString(),
494 session_.set_device(shared_ptr<devices::Device>(
495 new devices::SessionFile(
496 device_manager.context(),
497 file_name.toStdString())));
499 show_session_error(tr("Failed to load ") + file_name, e.what());
500 session_.set_default_device();
501 update_device_list();
505 update_device_list();
507 session_.start_capture([&, errorMessage](QString infoMessage) {
508 session_error(errorMessage, infoMessage); });
510 session_.set_name(QFileInfo(file_name).fileName());
513 void MainBar::update_sample_rate_selector()
515 Glib::VariantContainerBase gvar_dict;
517 const uint64_t *elements = nullptr;
519 map< const ConfigKey*, std::set<Capability> > keys;
521 if (updating_sample_rate_) {
522 sample_rate_.show_none();
526 const shared_ptr<devices::Device> device =
527 device_selector_.selected_device();
531 assert(!updating_sample_rate_);
532 updating_sample_rate_ = true;
534 const shared_ptr<sigrok::Device> sr_dev = device->device();
536 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
537 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
539 sample_rate_.show_none();
540 updating_sample_rate_ = false;
544 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
545 "samplerate-steps", G_VARIANT_TYPE("at")))) {
546 elements = (const uint64_t *)g_variant_get_fixed_array(
547 gvar_list, &num_elements, sizeof(uint64_t));
549 const uint64_t min = elements[0];
550 const uint64_t max = elements[1];
551 const uint64_t step = elements[2];
553 g_variant_unref(gvar_list);
561 sample_rate_.show_125_list(min, max);
563 // When the step is not 1, we cam't make a 1-2-5-10
564 // list of sample rates, because we may not be able to
565 // make round numbers. Therefore in this case, show a
567 sample_rate_.show_min_max_step(min, max, step);
569 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
570 "samplerates", G_VARIANT_TYPE("at")))) {
571 elements = (const uint64_t *)g_variant_get_fixed_array(
572 gvar_list, &num_elements, sizeof(uint64_t));
573 sample_rate_.show_list(elements, num_elements);
574 g_variant_unref(gvar_list);
576 updating_sample_rate_ = false;
578 update_sample_rate_selector_value();
581 void MainBar::update_sample_rate_selector_value()
583 if (updating_sample_rate_)
586 const shared_ptr<devices::Device> device =
587 device_selector_.selected_device();
592 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
593 uint64_t samplerate =
594 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
595 assert(!updating_sample_rate_);
596 updating_sample_rate_ = true;
597 sample_rate_.set_value(samplerate);
598 updating_sample_rate_ = false;
599 } catch (Error error) {
600 qDebug() << "WARNING: Failed to get value of sample rate";
605 void MainBar::update_sample_count_selector()
607 if (updating_sample_count_)
610 const shared_ptr<devices::Device> device =
611 device_selector_.selected_device();
615 const shared_ptr<sigrok::Device> sr_dev = device->device();
617 assert(!updating_sample_count_);
618 updating_sample_count_ = true;
620 if (!sample_count_supported_) {
621 sample_count_.show_none();
622 updating_sample_count_ = false;
626 uint64_t sample_count = sample_count_.value();
627 uint64_t min_sample_count = 0;
628 uint64_t max_sample_count = MaxSampleCount;
630 if (sample_count == 0)
631 sample_count = DefaultSampleCount;
633 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
634 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
636 g_variant_get(gvar.gobj(), "(tt)",
637 &min_sample_count, &max_sample_count);
640 min_sample_count = min(max(min_sample_count, MinSampleCount),
643 sample_count_.show_125_list(
644 min_sample_count, max_sample_count);
646 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
647 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
648 sample_count = g_variant_get_uint64(gvar.gobj());
649 if (sample_count == 0)
650 sample_count = DefaultSampleCount;
651 sample_count = min(max(sample_count, MinSampleCount),
655 sample_count_.set_value(sample_count);
657 updating_sample_count_ = false;
660 void MainBar::update_device_config_widgets()
662 using namespace pv::popups;
664 const shared_ptr<devices::Device> device =
665 device_selector_.selected_device();
667 // Hide the widgets if no device is selected
668 channels_button_action_->setVisible(!!device);
669 run_stop_button_action_->setVisible(!!device);
671 configure_button_action_->setVisible(false);
672 sample_count_.show_none();
673 sample_rate_.show_none();
677 const shared_ptr<sigrok::Device> sr_dev = device->device();
681 // Update the configure popup
682 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
683 configure_button_action_->setVisible(
684 !opts->binding().properties().empty());
685 configure_button_.set_popup(opts);
687 // Update the channels popup
688 Channels *const channels = new Channels(session_, this);
689 channels_button_.set_popup(channels);
691 // Update supported options.
692 sample_count_supported_ = false;
694 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
695 sample_count_supported_ = true;
697 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
698 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
699 Glib::Variant<guint64>::create(1));
703 // Add notification of reconfigure events
704 disconnect(this, SLOT(on_config_changed()));
705 connect(&opts->binding(), SIGNAL(config_changed()),
706 this, SLOT(on_config_changed()));
708 // Update sweep timing widgets.
709 update_sample_count_selector();
710 update_sample_rate_selector();
713 void MainBar::commit_sample_rate()
715 uint64_t sample_rate = 0;
717 const shared_ptr<devices::Device> device =
718 device_selector_.selected_device();
722 const shared_ptr<sigrok::Device> sr_dev = device->device();
724 sample_rate = sample_rate_.value();
725 if (sample_rate == 0)
729 sr_dev->config_set(ConfigKey::SAMPLERATE,
730 Glib::Variant<guint64>::create(sample_rate));
731 update_sample_rate_selector();
732 } catch (Error error) {
733 qDebug() << "Failed to configure samplerate.";
737 // Devices with built-in memory might impose limits on certain
738 // configurations, so let's check what sample count the driver
740 update_sample_count_selector();
743 void MainBar::commit_sample_count()
745 uint64_t sample_count = 0;
747 const shared_ptr<devices::Device> device =
748 device_selector_.selected_device();
752 const shared_ptr<sigrok::Device> sr_dev = device->device();
754 sample_count = sample_count_.value();
755 if (sample_count_supported_) {
757 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
758 Glib::Variant<guint64>::create(sample_count));
759 update_sample_count_selector();
760 } catch (Error error) {
761 qDebug() << "Failed to configure sample count.";
766 // Devices with built-in memory might impose limits on certain
767 // configurations, so let's check what sample rate the driver
769 update_sample_rate_selector();
772 void MainBar::session_error(const QString text, const QString info_text)
774 QMetaObject::invokeMethod(this, "show_session_error",
775 Qt::QueuedConnection, Q_ARG(QString, text),
776 Q_ARG(QString, info_text));
779 void MainBar::show_session_error(const QString text, const QString info_text)
781 QMessageBox msg(this);
783 msg.setInformativeText(info_text);
784 msg.setStandardButtons(QMessageBox::Ok);
785 msg.setIcon(QMessageBox::Warning);
789 void MainBar::capture_state_changed(int state)
791 set_capture_state((pv::Session::capture_state)state);
794 void MainBar::add_decoder(srd_decoder *decoder)
798 session_.add_decoder(decoder);
804 void MainBar::export_file(shared_ptr<OutputFormat> format,
807 using pv::dialogs::StoreProgress;
809 // Stop any currently running capture session
810 session_.stop_capture();
813 const QString dir = settings.value(SettingSaveDirectory).toString();
815 std::pair<uint64_t, uint64_t> sample_range;
817 // Selection only? Verify that the cursors are active and fetch their values
818 if (selection_only) {
819 if (!session_.main_view()->cursors()->enabled()) {
820 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
821 "cursors before you can save the data enclosed by them " \
822 "to a session file (e.g. using ALT-V - Show Cursors)."));
826 const double samplerate = session_.get_samplerate();
828 const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time();
829 const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time();
831 const uint64_t start_sample =
832 std::max((double)0, start_time.convert_to<double>() * samplerate);
833 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
835 sample_range = std::make_pair(start_sample, end_sample);
837 sample_range = std::make_pair(0, 0);
840 // Construct the filter
841 const vector<string> exts = format->extensions();
842 QString filter = tr("%1 files ").arg(
843 QString::fromStdString(format->description()));
848 filter += QString("(*.%1);;%2 (*.*)").arg(
849 QString::fromStdString(join(exts, ", *.")),
852 // Show the file dialog
853 const QString file_name = QFileDialog::getSaveFileName(
854 this, tr("Save File"), dir, filter);
856 if (file_name.isEmpty())
859 const QString abs_path = QFileInfo(file_name).absolutePath();
860 settings.setValue(SettingSaveDirectory, abs_path);
862 // Show the options dialog
863 map<string, Glib::VariantBase> options;
864 if (!format->options().empty()) {
865 dialogs::InputOutputOptions dlg(
866 tr("Export %1").arg(QString::fromStdString(
867 format->description())),
868 format->options(), this);
871 options = dlg.options();
874 session_.set_name(QFileInfo(file_name).fileName());
876 StoreProgress *dlg = new StoreProgress(file_name, format, options,
877 sample_range, session_, this);
881 void MainBar::import_file(shared_ptr<InputFormat> format)
886 const QString dir = settings.value(SettingOpenDirectory).toString();
888 // Construct the filter
889 const vector<string> exts = format->extensions();
890 const QString filter = exts.empty() ? "" :
891 tr("%1 files (*.%2)").arg(
892 QString::fromStdString(format->description()),
893 QString::fromStdString(join(exts, ", *.")));
895 // Show the file dialog
896 const QString file_name = QFileDialog::getOpenFileName(
897 this, tr("Import File"), dir, tr(
898 "%1 files (*.*);;All Files (*.*)").arg(
899 QString::fromStdString(format->description())));
901 if (file_name.isEmpty())
904 // Show the options dialog
905 map<string, Glib::VariantBase> options;
906 if (!format->options().empty()) {
907 dialogs::InputOutputOptions dlg(
908 tr("Import %1").arg(QString::fromStdString(
909 format->description())),
910 format->options(), this);
913 options = dlg.options();
916 load_file(file_name, format, options);
918 const QString abs_path = QFileInfo(file_name).absolutePath();
919 settings.setValue(SettingOpenDirectory, abs_path);
922 void MainBar::on_device_selected()
924 shared_ptr<devices::Device> device = device_selector_.selected_device();
926 reset_device_selector();
930 select_device(device);
933 void MainBar::on_device_changed()
935 update_device_list();
936 update_device_config_widgets();
939 void MainBar::on_sample_count_changed()
941 if (!updating_sample_count_)
942 commit_sample_count();
945 void MainBar::on_sample_rate_changed()
947 if (!updating_sample_rate_)
948 commit_sample_rate();
951 void MainBar::on_run_stop()
953 commit_sample_count();
954 commit_sample_rate();
958 void MainBar::on_config_changed()
960 commit_sample_count();
961 commit_sample_rate();
964 void MainBar::on_actionNewSession_triggered()
969 void MainBar::on_actionNewView_triggered()
974 void MainBar::on_actionOpen_triggered()
977 const QString dir = settings.value(SettingOpenDirectory).toString();
980 const QString file_name = QFileDialog::getOpenFileName(
981 this, tr("Open File"), dir, tr(
982 "Sigrok Sessions (*.sr);;"
985 if (!file_name.isEmpty()) {
986 load_file(file_name);
988 const QString abs_path = QFileInfo(file_name).absolutePath();
989 settings.setValue(SettingOpenDirectory, abs_path);
993 void MainBar::on_actionSaveAs_triggered()
995 export_file(session_.device_manager().context()->output_formats()["srzip"]);
998 void MainBar::on_actionSaveSelectionAs_triggered()
1000 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
1003 void MainBar::on_actionConnect_triggered()
1005 // Stop any currently running capture session
1006 session_.stop_capture();
1008 dialogs::Connect dlg(this, session_.device_manager());
1010 // If the user selected a device, select it in the device list. Select the
1011 // current device otherwise.
1013 select_device(dlg.get_selected_device());
1015 update_device_list();
1018 void MainBar::on_actionViewZoomIn_triggered()
1020 session_.main_view()->zoom(1);
1023 void MainBar::on_actionViewZoomOut_triggered()
1025 session_.main_view()->zoom(-1);
1028 void MainBar::on_actionViewZoomFit_triggered()
1030 session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked());
1033 void MainBar::on_actionViewZoomOneToOne_triggered()
1035 session_.main_view()->zoom_one_to_one();
1038 void MainBar::on_actionViewShowCursors_triggered()
1040 const bool show = !session_.main_view()->cursors_shown();
1042 session_.main_view()->centre_cursors();
1044 session_.main_view()->show_cursors(show);
1047 void MainBar::on_always_zoom_to_fit_changed(bool state)
1049 action_view_zoom_fit_->setChecked(state);
1052 bool MainBar::eventFilter(QObject *watched, QEvent *event)
1054 if (sample_count_supported_ && (watched == &sample_count_ ||
1055 watched == &sample_rate_) &&
1056 (event->type() == QEvent::ToolTip)) {
1057 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
1058 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
1060 QString str = tr("Total sampling time: %1").arg(
1061 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
1062 QToolTip::showText(help_event->globalPos(), str);
1070 } // namespace toolbars