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)
121 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
124 setObjectName(QString::fromUtf8("MainBar"));
128 setContextMenuPolicy(Qt::PreventContextMenu);
131 action_new_session_->setText(tr("New &Session"));
132 action_new_session_->setIcon(QIcon::fromTheme("document-new",
133 QIcon(":/icons/document-new.png")));
134 connect(action_new_session_, SIGNAL(triggered(bool)),
135 this, SLOT(on_actionNewSession_triggered()));
137 action_new_view_->setText(tr("New &View"));
138 action_new_view_->setIcon(QIcon::fromTheme("window-new",
139 QIcon(":/icons/window-new.png")));
140 connect(action_new_view_, SIGNAL(triggered(bool)),
141 this, SLOT(on_actionNewView_triggered()));
143 action_open_->setText(tr("&Open..."));
144 action_open_->setIcon(QIcon::fromTheme("document-open",
145 QIcon(":/icons/document-open.png")));
146 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
147 connect(action_open_, SIGNAL(triggered(bool)),
148 this, SLOT(on_actionOpen_triggered()));
150 action_save_as_->setText(tr("&Save As..."));
151 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
152 QIcon(":/icons/document-save-as.png")));
153 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
154 connect(action_save_as_, SIGNAL(triggered(bool)),
155 this, SLOT(on_actionSaveAs_triggered()));
157 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
158 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
159 QIcon(":/icons/document-save-as.png")));
160 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
161 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
162 this, SLOT(on_actionSaveSelectionAs_triggered()));
164 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
165 session.device_manager().context());
166 menu_file_export->setTitle(tr("&Export"));
167 connect(menu_file_export,
168 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
169 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
171 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
172 session.device_manager().context());
173 menu_file_import->setTitle(tr("&Import"));
174 connect(menu_file_import,
175 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
176 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
178 action_connect_->setText(tr("&Connect to Device..."));
179 connect(action_connect_, SIGNAL(triggered(bool)),
180 this, SLOT(on_actionConnect_triggered()));
182 action_view_zoom_in_->setText(tr("Zoom &In"));
183 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
184 QIcon(":/icons/zoom-in.png")));
185 // simply using Qt::Key_Plus shows no + in the menu
186 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
187 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
188 this, SLOT(on_actionViewZoomIn_triggered()));
190 action_view_zoom_out_->setText(tr("Zoom &Out"));
191 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
192 QIcon(":/icons/zoom-out.png")));
193 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
194 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
195 this, SLOT(on_actionViewZoomOut_triggered()));
197 action_view_zoom_fit_->setCheckable(true);
198 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
199 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
200 QIcon(":/icons/zoom-fit.png")));
201 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
202 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
203 this, SLOT(on_actionViewZoomFit_triggered()));
205 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
206 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
207 QIcon(":/icons/zoom-original.png")));
208 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
209 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
210 this, SLOT(on_actionViewZoomOneToOne_triggered()));
212 action_view_show_cursors_->setCheckable(true);
213 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
214 QIcon(":/icons/show-cursors.svg")));
215 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
216 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
217 this, SLOT(on_actionViewShowCursors_triggered()));
218 action_view_show_cursors_->setText(tr("Show &Cursors"));
221 QToolButton *const open_button = new QToolButton(this);
223 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
224 session.device_manager().context(), action_open_);
226 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
228 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
230 open_button->setMenu(import_menu);
231 open_button->setDefaultAction(action_open_);
232 open_button->setPopupMode(QToolButton::MenuButtonPopup);
235 QToolButton *const save_button = new QToolButton(this);
237 vector<QAction *> open_actions;
238 open_actions.push_back(action_save_as_);
239 open_actions.push_back(action_save_selection_as_);
241 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
242 session.device_manager().context(),
245 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
247 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
249 save_button->setMenu(export_menu);
250 save_button->setDefaultAction(action_save_as_);
251 save_button->setPopupMode(QToolButton::MenuButtonPopup);
253 // Device selector menu
254 connect(&device_selector_, SIGNAL(device_selected()),
255 this, SLOT(on_device_selected()));
257 // Setup the decoder button
259 menu_decoders_add_->setTitle(tr("&Add"));
260 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
261 this, SLOT(add_decoder(srd_decoder*)));
263 QToolButton *add_decoder_button = new QToolButton(this);
264 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
265 QIcon(":/icons/add-decoder.svg")));
266 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
267 add_decoder_button->setMenu(menu_decoders_add_);
271 addAction(action_new_session_);
272 addAction(action_new_view_);
274 addWidget(open_button);
275 addWidget(save_button);
277 addAction(action_view_zoom_in_);
278 addAction(action_view_zoom_out_);
279 addAction(action_view_zoom_fit_);
280 addAction(action_view_zoom_one_to_one_);
282 addAction(action_view_show_cursors_);
285 connect(&run_stop_button_, SIGNAL(clicked()),
286 this, SLOT(on_run_stop()));
287 connect(&sample_count_, SIGNAL(value_changed()),
288 this, SLOT(on_sample_count_changed()));
289 connect(&sample_rate_, SIGNAL(value_changed()),
290 this, SLOT(on_sample_rate_changed()));
292 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
294 set_capture_state(pv::Session::Stopped);
296 configure_button_.setIcon(QIcon::fromTheme("configure",
297 QIcon(":/icons/configure.png")));
299 channels_button_.setIcon(QIcon::fromTheme("channels",
300 QIcon(":/icons/channels.svg")));
302 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
304 addWidget(&device_selector_);
305 configure_button_action_ = addWidget(&configure_button_);
306 channels_button_action_ = addWidget(&channels_button_);
307 addWidget(&sample_count_);
308 addWidget(&sample_rate_);
309 run_stop_button_action_ = addWidget(&run_stop_button_);
312 addWidget(add_decoder_button);
315 sample_count_.installEventFilter(this);
316 sample_rate_.installEventFilter(this);
318 // Setup session_ events
319 connect(&session_, SIGNAL(capture_state_changed(int)),
320 this, SLOT(capture_state_changed(int)));
321 connect(&session, SIGNAL(device_changed()),
322 this, SLOT(on_device_changed()));
324 update_device_list();
327 Session &MainBar::session(void) const
332 void MainBar::update_device_list()
334 DeviceManager &mgr = session_.device_manager();
335 shared_ptr<devices::Device> selected_device = session_.device();
336 list< shared_ptr<devices::Device> > devs;
338 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
340 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
341 devs.push_back(selected_device);
343 device_selector_.set_device_list(devs, selected_device);
344 update_device_config_widgets();
348 void MainBar::set_capture_state(pv::Session::capture_state state)
350 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
351 run_stop_button_.setIcon(*icons[state]);
352 run_stop_button_.setText((state == pv::Session::Stopped) ?
353 tr("Run") : tr("Stop"));
354 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
356 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
358 device_selector_.setEnabled(ui_enabled);
359 configure_button_.setEnabled(ui_enabled);
360 channels_button_.setEnabled(ui_enabled);
361 sample_count_.setEnabled(ui_enabled);
362 sample_rate_.setEnabled(ui_enabled);
365 void MainBar::reset_device_selector()
367 device_selector_.reset();
370 void MainBar::select_device(shared_ptr<devices::Device> device)
374 session_.set_device(device);
376 session_.set_default_device();
377 } catch (const QString &e) {
378 QMessageBox msg(this);
380 msg.setInformativeText(tr("Failed to Select Device"));
381 msg.setStandardButtons(QMessageBox::Ok);
382 msg.setIcon(QMessageBox::Warning);
387 void MainBar::load_init_file(const std::string &file_name,
388 const std::string &format)
390 shared_ptr<InputFormat> input_format;
392 DeviceManager& device_manager = session_.device_manager();
394 if (!format.empty()) {
395 const map<string, shared_ptr<InputFormat> > formats =
396 device_manager.context()->input_formats();
397 const auto iter = find_if(formats.begin(), formats.end(),
398 [&](const pair<string, shared_ptr<InputFormat> > f) {
399 return f.first == format; });
400 if (iter == formats.end()) {
401 cerr << "Unexpected input format: " << format << endl;
405 input_format = (*iter).second;
408 load_file(QString::fromStdString(file_name), input_format);
411 QAction* MainBar::action_open() const
416 QAction* MainBar::action_save_as() const
418 return action_save_as_;
421 QAction* MainBar::action_save_selection_as() const
423 return action_save_selection_as_;
426 QAction* MainBar::action_connect() const
428 return action_connect_;
431 QAction* MainBar::action_view_zoom_in() const
433 return action_view_zoom_in_;
436 QAction* MainBar::action_view_zoom_out() const
438 return action_view_zoom_out_;
441 QAction* MainBar::action_view_zoom_fit() const
443 return action_view_zoom_fit_;
446 QAction* MainBar::action_view_zoom_one_to_one() const
448 return action_view_zoom_one_to_one_;
451 QAction* MainBar::action_view_show_cursors() const
453 return action_view_show_cursors_;
456 void MainBar::run_stop()
458 switch (session_.get_capture_state()) {
459 case Session::Stopped:
460 session_.start_capture([&](QString message) {
461 session_error("Capture failed", message); });
463 case Session::AwaitingTrigger:
464 case Session::Running:
465 session_.stop_capture();
470 void MainBar::load_file(QString file_name,
471 std::shared_ptr<sigrok::InputFormat> format,
472 const std::map<std::string, Glib::VariantBase> &options)
474 DeviceManager& device_manager = session_.device_manager();
476 const QString errorMessage(
477 QString("Failed to load file %1").arg(file_name));
481 session_.set_device(shared_ptr<devices::Device>(
482 new devices::InputFile(
483 device_manager.context(),
484 file_name.toStdString(),
487 session_.set_device(shared_ptr<devices::Device>(
488 new devices::SessionFile(
489 device_manager.context(),
490 file_name.toStdString())));
492 show_session_error(tr("Failed to load ") + file_name, e.what());
493 session_.set_default_device();
494 update_device_list();
498 update_device_list();
500 session_.start_capture([&, errorMessage](QString infoMessage) {
501 session_error(errorMessage, infoMessage); });
503 session_.set_name(QFileInfo(file_name).fileName());
506 void MainBar::update_sample_rate_selector()
508 Glib::VariantContainerBase gvar_dict;
510 const uint64_t *elements = nullptr;
512 map< const ConfigKey*, std::set<Capability> > keys;
514 if (updating_sample_rate_) {
515 sample_rate_.show_none();
519 const shared_ptr<devices::Device> device =
520 device_selector_.selected_device();
524 assert(!updating_sample_rate_);
525 updating_sample_rate_ = true;
527 const shared_ptr<sigrok::Device> sr_dev = device->device();
529 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
530 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
532 sample_rate_.show_none();
533 updating_sample_rate_ = false;
537 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
538 "samplerate-steps", G_VARIANT_TYPE("at")))) {
539 elements = (const uint64_t *)g_variant_get_fixed_array(
540 gvar_list, &num_elements, sizeof(uint64_t));
542 const uint64_t min = elements[0];
543 const uint64_t max = elements[1];
544 const uint64_t step = elements[2];
546 g_variant_unref(gvar_list);
554 sample_rate_.show_125_list(min, max);
556 // When the step is not 1, we cam't make a 1-2-5-10
557 // list of sample rates, because we may not be able to
558 // make round numbers. Therefore in this case, show a
560 sample_rate_.show_min_max_step(min, max, step);
562 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
563 "samplerates", G_VARIANT_TYPE("at")))) {
564 elements = (const uint64_t *)g_variant_get_fixed_array(
565 gvar_list, &num_elements, sizeof(uint64_t));
566 sample_rate_.show_list(elements, num_elements);
567 g_variant_unref(gvar_list);
569 updating_sample_rate_ = false;
571 update_sample_rate_selector_value();
574 void MainBar::update_sample_rate_selector_value()
576 if (updating_sample_rate_)
579 const shared_ptr<devices::Device> device =
580 device_selector_.selected_device();
585 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
586 uint64_t samplerate =
587 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
588 assert(!updating_sample_rate_);
589 updating_sample_rate_ = true;
590 sample_rate_.set_value(samplerate);
591 updating_sample_rate_ = false;
592 } catch (Error error) {
593 qDebug() << "WARNING: Failed to get value of sample rate";
598 void MainBar::update_sample_count_selector()
600 if (updating_sample_count_)
603 const shared_ptr<devices::Device> device =
604 device_selector_.selected_device();
608 const shared_ptr<sigrok::Device> sr_dev = device->device();
610 assert(!updating_sample_count_);
611 updating_sample_count_ = true;
613 if (!sample_count_supported_) {
614 sample_count_.show_none();
615 updating_sample_count_ = false;
619 uint64_t sample_count = sample_count_.value();
620 uint64_t min_sample_count = 0;
621 uint64_t max_sample_count = MaxSampleCount;
623 if (sample_count == 0)
624 sample_count = DefaultSampleCount;
626 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
627 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
629 g_variant_get(gvar.gobj(), "(tt)",
630 &min_sample_count, &max_sample_count);
633 min_sample_count = min(max(min_sample_count, MinSampleCount),
636 sample_count_.show_125_list(
637 min_sample_count, max_sample_count);
639 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
640 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
641 sample_count = g_variant_get_uint64(gvar.gobj());
642 if (sample_count == 0)
643 sample_count = DefaultSampleCount;
644 sample_count = min(max(sample_count, MinSampleCount),
648 sample_count_.set_value(sample_count);
650 updating_sample_count_ = false;
653 void MainBar::update_device_config_widgets()
655 using namespace pv::popups;
657 const shared_ptr<devices::Device> device =
658 device_selector_.selected_device();
660 // Hide the widgets if no device is selected
661 channels_button_action_->setVisible(!!device);
662 run_stop_button_action_->setVisible(!!device);
664 configure_button_action_->setVisible(false);
665 sample_count_.show_none();
666 sample_rate_.show_none();
670 const shared_ptr<sigrok::Device> sr_dev = device->device();
674 // Update the configure popup
675 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
676 configure_button_action_->setVisible(
677 !opts->binding().properties().empty());
678 configure_button_.set_popup(opts);
680 // Update the channels popup
681 Channels *const channels = new Channels(session_, this);
682 channels_button_.set_popup(channels);
684 // Update supported options.
685 sample_count_supported_ = false;
687 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
688 sample_count_supported_ = true;
690 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
691 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
692 Glib::Variant<guint64>::create(1));
696 // Add notification of reconfigure events
697 disconnect(this, SLOT(on_config_changed()));
698 connect(&opts->binding(), SIGNAL(config_changed()),
699 this, SLOT(on_config_changed()));
701 // Update sweep timing widgets.
702 update_sample_count_selector();
703 update_sample_rate_selector();
706 void MainBar::commit_sample_rate()
708 uint64_t sample_rate = 0;
710 const shared_ptr<devices::Device> device =
711 device_selector_.selected_device();
715 const shared_ptr<sigrok::Device> sr_dev = device->device();
717 sample_rate = sample_rate_.value();
718 if (sample_rate == 0)
722 sr_dev->config_set(ConfigKey::SAMPLERATE,
723 Glib::Variant<guint64>::create(sample_rate));
724 update_sample_rate_selector();
725 } catch (Error error) {
726 qDebug() << "Failed to configure samplerate.";
730 // Devices with built-in memory might impose limits on certain
731 // configurations, so let's check what sample count the driver
733 update_sample_count_selector();
736 void MainBar::commit_sample_count()
738 uint64_t sample_count = 0;
740 const shared_ptr<devices::Device> device =
741 device_selector_.selected_device();
745 const shared_ptr<sigrok::Device> sr_dev = device->device();
747 sample_count = sample_count_.value();
748 if (sample_count_supported_) {
750 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
751 Glib::Variant<guint64>::create(sample_count));
752 update_sample_count_selector();
753 } catch (Error error) {
754 qDebug() << "Failed to configure sample count.";
759 // Devices with built-in memory might impose limits on certain
760 // configurations, so let's check what sample rate the driver
762 update_sample_rate_selector();
765 void MainBar::session_error(const QString text, const QString info_text)
767 QMetaObject::invokeMethod(this, "show_session_error",
768 Qt::QueuedConnection, Q_ARG(QString, text),
769 Q_ARG(QString, info_text));
772 void MainBar::show_session_error(const QString text, const QString info_text)
774 QMessageBox msg(this);
776 msg.setInformativeText(info_text);
777 msg.setStandardButtons(QMessageBox::Ok);
778 msg.setIcon(QMessageBox::Warning);
782 void MainBar::capture_state_changed(int state)
784 set_capture_state((pv::Session::capture_state)state);
787 void MainBar::add_decoder(srd_decoder *decoder)
791 session_.add_decoder(decoder);
797 void MainBar::export_file(shared_ptr<OutputFormat> format,
800 using pv::dialogs::StoreProgress;
802 // Stop any currently running capture session
803 session_.stop_capture();
806 const QString dir = settings.value(SettingSaveDirectory).toString();
808 std::pair<uint64_t, uint64_t> sample_range;
810 // Selection only? Verify that the cursors are active and fetch their values
811 if (selection_only) {
812 views::TraceView::View *trace_view =
813 qobject_cast<views::TraceView::View*>(session_.main_view().get());
815 if (!trace_view->cursors()->enabled()) {
816 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
817 "cursors before you can save the data enclosed by them " \
818 "to a session file (e.g. using ALT-V - Show Cursors)."));
822 const double samplerate = session_.get_samplerate();
824 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
825 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
827 const uint64_t start_sample =
828 std::max((double)0, start_time.convert_to<double>() * samplerate);
829 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
831 sample_range = std::make_pair(start_sample, end_sample);
833 sample_range = std::make_pair(0, 0);
836 // Construct the filter
837 const vector<string> exts = format->extensions();
838 QString filter = tr("%1 files ").arg(
839 QString::fromStdString(format->description()));
844 filter += QString("(*.%1);;%2 (*.*)").arg(
845 QString::fromStdString(join(exts, ", *.")),
848 // Show the file dialog
849 const QString file_name = QFileDialog::getSaveFileName(
850 this, tr("Save File"), dir, filter);
852 if (file_name.isEmpty())
855 const QString abs_path = QFileInfo(file_name).absolutePath();
856 settings.setValue(SettingSaveDirectory, abs_path);
858 // Show the options dialog
859 map<string, Glib::VariantBase> options;
860 if (!format->options().empty()) {
861 dialogs::InputOutputOptions dlg(
862 tr("Export %1").arg(QString::fromStdString(
863 format->description())),
864 format->options(), this);
867 options = dlg.options();
870 session_.set_name(QFileInfo(file_name).fileName());
872 StoreProgress *dlg = new StoreProgress(file_name, format, options,
873 sample_range, session_, this);
877 void MainBar::import_file(shared_ptr<InputFormat> format)
882 const QString dir = settings.value(SettingOpenDirectory).toString();
884 // Construct the filter
885 const vector<string> exts = format->extensions();
886 const QString filter = exts.empty() ? "" :
887 tr("%1 files (*.%2)").arg(
888 QString::fromStdString(format->description()),
889 QString::fromStdString(join(exts, ", *.")));
891 // Show the file dialog
892 const QString file_name = QFileDialog::getOpenFileName(
893 this, tr("Import File"), dir, tr(
894 "%1 files (*.*);;All Files (*.*)").arg(
895 QString::fromStdString(format->description())));
897 if (file_name.isEmpty())
900 // Show the options dialog
901 map<string, Glib::VariantBase> options;
902 if (!format->options().empty()) {
903 dialogs::InputOutputOptions dlg(
904 tr("Import %1").arg(QString::fromStdString(
905 format->description())),
906 format->options(), this);
909 options = dlg.options();
912 load_file(file_name, format, options);
914 const QString abs_path = QFileInfo(file_name).absolutePath();
915 settings.setValue(SettingOpenDirectory, abs_path);
918 void MainBar::on_device_selected()
920 shared_ptr<devices::Device> device = device_selector_.selected_device();
922 reset_device_selector();
926 select_device(device);
929 void MainBar::on_device_changed()
931 update_device_list();
932 update_device_config_widgets();
935 void MainBar::on_sample_count_changed()
937 if (!updating_sample_count_)
938 commit_sample_count();
941 void MainBar::on_sample_rate_changed()
943 if (!updating_sample_rate_)
944 commit_sample_rate();
947 void MainBar::on_run_stop()
949 commit_sample_count();
950 commit_sample_rate();
954 void MainBar::on_config_changed()
956 commit_sample_count();
957 commit_sample_rate();
960 void MainBar::on_actionNewSession_triggered()
965 void MainBar::on_actionNewView_triggered()
970 void MainBar::on_actionOpen_triggered()
973 const QString dir = settings.value(SettingOpenDirectory).toString();
976 const QString file_name = QFileDialog::getOpenFileName(
977 this, tr("Open File"), dir, tr(
978 "Sigrok Sessions (*.sr);;"
981 if (!file_name.isEmpty()) {
982 load_file(file_name);
984 const QString abs_path = QFileInfo(file_name).absolutePath();
985 settings.setValue(SettingOpenDirectory, abs_path);
989 void MainBar::on_actionSaveAs_triggered()
991 export_file(session_.device_manager().context()->output_formats()["srzip"]);
994 void MainBar::on_actionSaveSelectionAs_triggered()
996 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
999 void MainBar::on_actionConnect_triggered()
1001 // Stop any currently running capture session
1002 session_.stop_capture();
1004 dialogs::Connect dlg(this, session_.device_manager());
1006 // If the user selected a device, select it in the device list. Select the
1007 // current device otherwise.
1009 select_device(dlg.get_selected_device());
1011 update_device_list();
1014 void MainBar::on_actionViewZoomIn_triggered()
1016 views::TraceView::View *trace_view =
1017 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1019 trace_view->zoom(1);
1022 void MainBar::on_actionViewZoomOut_triggered()
1024 views::TraceView::View *trace_view =
1025 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1027 trace_view->zoom(-1);
1030 void MainBar::on_actionViewZoomFit_triggered()
1032 views::TraceView::View *trace_view =
1033 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1035 trace_view->zoom_fit(action_view_zoom_fit_->isChecked());
1038 void MainBar::on_actionViewZoomOneToOne_triggered()
1040 views::TraceView::View *trace_view =
1041 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1043 trace_view->zoom_one_to_one();
1046 void MainBar::on_actionViewShowCursors_triggered()
1048 views::TraceView::View *trace_view =
1049 qobject_cast<views::TraceView::View*>(session_.main_view().get());
1051 const bool show = !trace_view->cursors_shown();
1053 trace_view->centre_cursors();
1055 trace_view->show_cursors(show);
1058 void MainBar::on_always_zoom_to_fit_changed(bool state)
1060 action_view_zoom_fit_->setChecked(state);
1063 bool MainBar::eventFilter(QObject *watched, QEvent *event)
1065 if (sample_count_supported_ && (watched == &sample_count_ ||
1066 watched == &sample_rate_) &&
1067 (event->type() == QEvent::ToolTip)) {
1068 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
1069 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
1071 QString str = tr("Total sampling time: %1").arg(
1072 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
1073 QToolTip::showText(help_event->globalPos(), str);
1081 } // namespace toolbars