X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Ftoolbars%2Fmainbar.cpp;h=a0f190f7eea64d9eb1c38d3956139c8113f9d7fc;hp=61a467281520de949664fb15b41746e7b8a54c8a;hb=c9da51187f8db0c9822d544a0253e0e7a58945d7;hpb=bdf57963233028b4a01d87e706a575b85a45cbf5 diff --git a/pv/toolbars/mainbar.cpp b/pv/toolbars/mainbar.cpp index 61a46728..a0f190f7 100644 --- a/pv/toolbars/mainbar.cpp +++ b/pv/toolbars/mainbar.cpp @@ -1,7 +1,7 @@ /* * This file is part of the PulseView project. * - * Copyright (C) 2012 Joel Holdsworth + * Copyright (C) 2012-2015 Joel Holdsworth * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,35 +20,62 @@ #include -#include +#include +#include #include #include +#include #include #include +#include +#include #include #include "mainbar.hpp" +#include + #include +#include +#include +#include +#include +#include +#include #include #include #include #include - -#include - +#include +#include +#include +#ifdef ENABLE_DECODE +#include +#endif + +#include + +using std::back_inserter; +using std::cerr; +using std::copy; +using std::endl; +using std::list; using std::map; -using std::vector; using std::max; using std::min; +using std::pair; using std::shared_ptr; using std::string; +using std::vector; using sigrok::Capability; using sigrok::ConfigKey; -using sigrok::Device; using sigrok::Error; +using sigrok::InputFormat; +using sigrok::OutputFormat; + +using boost::algorithm::join; namespace pv { namespace toolbars { @@ -57,15 +84,29 @@ const uint64_t MainBar::MinSampleCount = 100ULL; const uint64_t MainBar::MaxSampleCount = 1000000000000ULL; const uint64_t MainBar::DefaultSampleCount = 1000000; +const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory"; +const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory"; + MainBar::MainBar(Session &session, MainWindow &main_window) : QToolBar("Sampling Bar", &main_window), + action_new_session_(new QAction(this)), + action_new_view_(new QAction(this)), + action_open_(new QAction(this)), + action_save_as_(new QAction(this)), + action_save_selection_as_(new QAction(this)), + action_connect_(new QAction(this)), + action_view_zoom_in_(new QAction(this)), + action_view_zoom_out_(new QAction(this)), + action_view_zoom_fit_(new QAction(this)), + action_view_zoom_one_to_one_(new QAction(this)), + action_view_show_cursors_(new QAction(this)), session_(session), - main_window_(main_window), - device_selector_(this), - updating_device_selector_(false), + device_selector_(&main_window, session.device_manager(), + action_connect_), configure_button_(this), - configure_button_action_(NULL), + configure_button_action_(nullptr), channels_button_(this), + channels_button_action_(nullptr), sample_count_(" samples", this), sample_rate_("Hz", this), updating_sample_rate_(false), @@ -75,45 +116,175 @@ MainBar::MainBar(Session &session, MainWindow &main_window) : icon_green_(":/icons/status-green.svg"), icon_grey_(":/icons/status-grey.svg"), run_stop_button_(this), + run_stop_button_action_(nullptr), menu_button_(this) +#ifdef ENABLE_DECODE + , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) +#endif { setObjectName(QString::fromUtf8("MainBar")); setMovable(false); setFloatable(false); + setContextMenuPolicy(Qt::PreventContextMenu); + + // Actions + action_new_session_->setText(tr("New &Session")); + action_new_session_->setIcon(QIcon::fromTheme("document-new", + QIcon(":/icons/document-new.png"))); + connect(action_new_session_, SIGNAL(triggered(bool)), + this, SLOT(on_actionNewSession_triggered())); + + action_new_view_->setText(tr("New &View")); + action_new_view_->setIcon(QIcon::fromTheme("window-new", + QIcon(":/icons/window-new.png"))); + connect(action_new_view_, SIGNAL(triggered(bool)), + this, SLOT(on_actionNewView_triggered())); + + action_open_->setText(tr("&Open...")); + action_open_->setIcon(QIcon::fromTheme("document-open", + QIcon(":/icons/document-open.png"))); + action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); + connect(action_open_, SIGNAL(triggered(bool)), + this, SLOT(on_actionOpen_triggered())); + + action_save_as_->setText(tr("&Save As...")); + action_save_as_->setIcon(QIcon::fromTheme("document-save-as", + QIcon(":/icons/document-save-as.png"))); + action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + connect(action_save_as_, SIGNAL(triggered(bool)), + this, SLOT(on_actionSaveAs_triggered())); + + action_save_selection_as_->setText(tr("Save Selected &Range As...")); + action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as", + QIcon(":/icons/document-save-as.png"))); + action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); + connect(action_save_selection_as_, SIGNAL(triggered(bool)), + this, SLOT(on_actionSaveSelectionAs_triggered())); + + widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, + session.device_manager().context()); + menu_file_export->setTitle(tr("&Export")); + connect(menu_file_export, + SIGNAL(format_selected(std::shared_ptr)), + this, SLOT(export_file(std::shared_ptr))); + + widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this, + session.device_manager().context()); + menu_file_import->setTitle(tr("&Import")); + connect(menu_file_import, + SIGNAL(format_selected(std::shared_ptr)), + this, SLOT(import_file(std::shared_ptr))); + + action_connect_->setText(tr("&Connect to Device...")); + connect(action_connect_, SIGNAL(triggered(bool)), + this, SLOT(on_actionConnect_triggered())); + + action_view_zoom_in_->setText(tr("Zoom &In")); + action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", + QIcon(":/icons/zoom-in.png"))); + // simply using Qt::Key_Plus shows no + in the menu + action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); + connect(action_view_zoom_in_, SIGNAL(triggered(bool)), + this, SLOT(on_actionViewZoomIn_triggered())); + + action_view_zoom_out_->setText(tr("Zoom &Out")); + action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", + QIcon(":/icons/zoom-out.png"))); + action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); + connect(action_view_zoom_out_, SIGNAL(triggered(bool)), + this, SLOT(on_actionViewZoomOut_triggered())); + + action_view_zoom_fit_->setCheckable(true); + action_view_zoom_fit_->setText(tr("Zoom to &Fit")); + action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", + QIcon(":/icons/zoom-fit.png"))); + action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); + connect(action_view_zoom_fit_, SIGNAL(triggered(bool)), + this, SLOT(on_actionViewZoomFit_triggered())); + + action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); + action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", + QIcon(":/icons/zoom-original.png"))); + action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); + connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)), + this, SLOT(on_actionViewZoomOneToOne_triggered())); + + action_view_show_cursors_->setCheckable(true); + action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", + QIcon(":/icons/show-cursors.svg"))); + action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); + connect(action_view_show_cursors_, SIGNAL(triggered(bool)), + this, SLOT(on_actionViewShowCursors_triggered())); + action_view_show_cursors_->setText(tr("Show &Cursors")); + + // Open button + QToolButton *const open_button = new QToolButton(this); + + widgets::ImportMenu *import_menu = new widgets::ImportMenu(this, + session.device_manager().context(), action_open_); + connect(import_menu, + SIGNAL(format_selected(std::shared_ptr)), + this, + SLOT(import_file(std::shared_ptr))); + + open_button->setMenu(import_menu); + open_button->setDefaultAction(action_open_); + open_button->setPopupMode(QToolButton::MenuButtonPopup); + + // Save button + QToolButton *const save_button = new QToolButton(this); + + vector open_actions; + open_actions.push_back(action_save_as_); + open_actions.push_back(action_save_selection_as_); + + widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, + session.device_manager().context(), + open_actions); + connect(export_menu, + SIGNAL(format_selected(std::shared_ptr)), + this, + SLOT(export_file(std::shared_ptr))); + + save_button->setMenu(export_menu); + save_button->setDefaultAction(action_save_as_); + save_button->setPopupMode(QToolButton::MenuButtonPopup); + + // Device selector menu + connect(&device_selector_, SIGNAL(device_selected()), + this, SLOT(on_device_selected())); - // Setup the menu - QMenu *const menu = new QMenu(this); - - QMenu *const menu_help = new QMenu; - menu_help->setTitle(tr("&Help")); - menu_help->addAction(main_window.action_about()); - - menu->addAction(menu_help->menuAction()); - menu->addSeparator(); - menu->addAction(main_window.action_quit()); + // Setup the decoder button +#ifdef ENABLE_DECODE + menu_decoders_add_->setTitle(tr("&Add")); + connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), + this, SLOT(add_decoder(srd_decoder*))); - menu_button_.setMenu(menu); - menu_button_.setPopupMode(QToolButton::InstantPopup); - menu_button_.setIcon(QIcon::fromTheme("menu", - QIcon(":/icons/menu.svg"))); + QToolButton *add_decoder_button = new QToolButton(this); + add_decoder_button->setIcon(QIcon::fromTheme("add-decoder", + QIcon(":/icons/add-decoder.svg"))); + add_decoder_button->setPopupMode(QToolButton::InstantPopup); + add_decoder_button->setMenu(menu_decoders_add_); +#endif // Setup the toolbar - addAction(main_window.action_open()); - addAction(main_window.action_save_as()); + addAction(action_new_session_); + addAction(action_new_view_); addSeparator(); - addAction(main_window.action_view_zoom_in()); - addAction(main_window.action_view_zoom_out()); - addAction(main_window.action_view_zoom_fit()); - addAction(main_window.action_view_zoom_one_to_one()); + addWidget(open_button); + addWidget(save_button); addSeparator(); - addAction(main_window.action_view_show_cursors()); + addAction(action_view_zoom_in_); + addAction(action_view_zoom_out_); + addAction(action_view_zoom_fit_); + addAction(action_view_zoom_one_to_one_); + addSeparator(); + addAction(action_view_show_cursors_); addSeparator(); connect(&run_stop_button_, SIGNAL(clicked()), this, SLOT(on_run_stop())); - connect(&device_selector_, SIGNAL(currentIndexChanged (int)), - this, SLOT(on_device_selected())); connect(&sample_count_, SIGNAL(value_changed()), this, SLOT(on_sample_count_changed())); connect(&sample_rate_, SIGNAL(value_changed()), @@ -133,10 +304,14 @@ MainBar::MainBar(Session &session, MainWindow &main_window) : addWidget(&device_selector_); configure_button_action_ = addWidget(&configure_button_); - addWidget(&channels_button_); + channels_button_action_ = addWidget(&channels_button_); addWidget(&sample_count_); addWidget(&sample_rate_); - addWidget(&run_stop_button_); + run_stop_button_action_ = addWidget(&run_stop_button_); +#ifdef ENABLE_DECODE + addSeparator(); + addWidget(add_decoder_button); +#endif QWidget *const spacer = new QWidget(); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); @@ -146,100 +321,228 @@ MainBar::MainBar(Session &session, MainWindow &main_window) : sample_count_.installEventFilter(this); sample_rate_.installEventFilter(this); + + // Setup session_ events + connect(&session_, SIGNAL(capture_state_changed(int)), + this, SLOT(capture_state_changed(int))); + connect(&session, SIGNAL(device_changed()), + this, SLOT(on_device_changed())); + + update_device_list(); } -void MainBar::set_device_list( - const std::list< std::shared_ptr > &devices, - shared_ptr selected) +Session &MainBar::session(void) const { - int selected_index = -1; + return session_; +} - assert(selected); +void MainBar::update_device_list() +{ + DeviceManager &mgr = session_.device_manager(); + shared_ptr selected_device = session_.device(); + list< shared_ptr > devs; + + copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); + + if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) + devs.push_back(selected_device); + + device_selector_.set_device_list(devs, selected_device); + update_device_config_widgets(); +} + + +void MainBar::set_capture_state(pv::Session::capture_state state) +{ + const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; + run_stop_button_.setIcon(*icons[state]); + run_stop_button_.setText((state == pv::Session::Stopped) ? + tr("Run") : tr("Stop")); + run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); - updating_device_selector_ = true; + bool ui_enabled = (state == pv::Session::Stopped) ? true : false; - device_selector_.clear(); + device_selector_.setEnabled(ui_enabled); + configure_button_.setEnabled(ui_enabled); + channels_button_.setEnabled(ui_enabled); + sample_count_.setEnabled(ui_enabled); + sample_rate_.setEnabled(ui_enabled); +} - for (auto device : devices) { - assert(device); +void MainBar::reset_device_selector() +{ + device_selector_.reset(); +} - string display_name = - session_.device_manager().get_display_name(device); +void MainBar::select_device(shared_ptr device) +{ + try { + if (device) + session_.set_device(device); + else + session_.set_default_device(); + } catch (const QString &e) { + QMessageBox msg(this); + msg.setText(e); + msg.setInformativeText(tr("Failed to Select Device")); + msg.setStandardButtons(QMessageBox::Ok); + msg.setIcon(QMessageBox::Warning); + msg.exec(); + } +} - if (selected == device) - selected_index = device_selector_.count(); +void MainBar::load_init_file(const std::string &file_name, + const std::string &format) +{ + shared_ptr input_format; + + DeviceManager& device_manager = session_.device_manager(); + + if (!format.empty()) { + const map > formats = + device_manager.context()->input_formats(); + const auto iter = find_if(formats.begin(), formats.end(), + [&](const pair > f) { + return f.first == format; }); + if (iter == formats.end()) { + cerr << "Unexpected input format: " << format << endl; + return; + } - device_selector_.addItem(display_name.c_str(), - qVariantFromValue(device)); + input_format = (*iter).second; } - // The selected device should have been in the list - assert(selected_index != -1); - device_selector_.setCurrentIndex(selected_index); + load_file(QString::fromStdString(file_name), input_format); +} - update_device_config_widgets(); +QAction* MainBar::action_open() const +{ + return action_open_; +} - updating_device_selector_ = false; +QAction* MainBar::action_save_as() const +{ + return action_save_as_; } -shared_ptr MainBar::get_selected_device() const +QAction* MainBar::action_save_selection_as() const { - const int index = device_selector_.currentIndex(); - if (index < 0) - return shared_ptr(); + return action_save_selection_as_; +} - return device_selector_.itemData(index).value>(); +QAction* MainBar::action_connect() const +{ + return action_connect_; } -void MainBar::set_capture_state(pv::Session::capture_state state) +QAction* MainBar::action_view_zoom_in() const { - const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; - run_stop_button_.setIcon(*icons[state]); - run_stop_button_.setText((state == pv::Session::Stopped) ? - tr("Run") : tr("Stop")); - run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); + return action_view_zoom_in_; +} + +QAction* MainBar::action_view_zoom_out() const +{ + return action_view_zoom_out_; +} + +QAction* MainBar::action_view_zoom_fit() const +{ + return action_view_zoom_fit_; +} + +QAction* MainBar::action_view_zoom_one_to_one() const +{ + return action_view_zoom_one_to_one_; +} + +QAction* MainBar::action_view_show_cursors() const +{ + return action_view_show_cursors_; +} + +void MainBar::run_stop() +{ + switch (session_.get_capture_state()) { + case Session::Stopped: + session_.start_capture([&](QString message) { + session_error("Capture failed", message); }); + break; + case Session::AwaitingTrigger: + case Session::Running: + session_.stop_capture(); + break; + } +} + +void MainBar::load_file(QString file_name, + std::shared_ptr format, + const std::map &options) +{ + DeviceManager& device_manager = session_.device_manager(); + + const QString errorMessage( + QString("Failed to load file %1").arg(file_name)); + + try { + if (format) + session_.set_device(shared_ptr( + new devices::InputFile( + device_manager.context(), + file_name.toStdString(), + format, options))); + else + session_.set_device(shared_ptr( + new devices::SessionFile( + device_manager.context(), + file_name.toStdString()))); + } catch (Error e) { + show_session_error(tr("Failed to load ") + file_name, e.what()); + session_.set_default_device(); + update_device_list(); + return; + } + + session_.set_name(QFileInfo(file_name).fileName()); + + update_device_list(); + + session_.start_capture([&, errorMessage](QString infoMessage) { + session_error(errorMessage, infoMessage); }); } void MainBar::update_sample_rate_selector() { Glib::VariantContainerBase gvar_dict; GVariant *gvar_list; - const uint64_t *elements = NULL; + const uint64_t *elements = nullptr; gsize num_elements; + map< const ConfigKey*, std::set > keys; - if (updating_sample_rate_) + if (updating_sample_rate_) { + sample_rate_.show_none(); return; + } - const shared_ptr device = get_selected_device(); + const shared_ptr device = + device_selector_.selected_device(); if (!device) return; assert(!updating_sample_rate_); updating_sample_rate_ = true; - const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS); - const auto iter = keys.find(ConfigKey::SAMPLERATE); - if (iter != keys.end() && - (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { - const auto keys = device->config_keys( - ConfigKey::DEVICE_OPTIONS); - try { - gvar_dict = device->config_list(ConfigKey::SAMPLERATE); - } catch(const sigrok::Error &e) { - // Failed to enunmerate samplerate - (void)e; - } - } + const shared_ptr sr_dev = device->device(); - if (!gvar_dict) { + if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) { + gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); + } else { sample_rate_.show_none(); updating_sample_rate_ = false; return; } if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), - "samplerate-steps", G_VARIANT_TYPE("at")))) - { + "samplerate-steps", G_VARIANT_TYPE("at")))) { elements = (const uint64_t *)g_variant_get_fixed_array( gvar_list, &num_elements, sizeof(uint64_t)); @@ -256,18 +559,15 @@ void MainBar::update_sample_rate_selector() if (step == 1) sample_rate_.show_125_list(min, max); - else - { + else { // When the step is not 1, we cam't make a 1-2-5-10 // list of sample rates, because we may not be able to // make round numbers. Therefore in this case, show a // spin box. sample_rate_.show_min_max_step(min, max, step); } - } - else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), - "samplerates", G_VARIANT_TYPE("at")))) - { + } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), + "samplerates", G_VARIANT_TYPE("at")))) { elements = (const uint64_t *)g_variant_get_fixed_array( gvar_list, &num_elements, sizeof(uint64_t)); sample_rate_.show_list(elements, num_elements); @@ -283,12 +583,13 @@ void MainBar::update_sample_rate_selector_value() if (updating_sample_rate_) return; - const shared_ptr device = get_selected_device(); + const shared_ptr device = + device_selector_.selected_device(); if (!device) return; try { - auto gvar = device->config_get(ConfigKey::SAMPLERATE); + auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); uint64_t samplerate = Glib::VariantBase::cast_dynamic>(gvar).get(); assert(!updating_sample_rate_); @@ -306,15 +607,17 @@ void MainBar::update_sample_count_selector() if (updating_sample_count_) return; - const shared_ptr device = get_selected_device(); + const shared_ptr device = + device_selector_.selected_device(); if (!device) return; + const shared_ptr sr_dev = device->device(); + assert(!updating_sample_count_); updating_sample_count_ = true; - if (!sample_count_supported_) - { + if (!sample_count_supported_) { sample_count_.show_none(); updating_sample_count_ = false; return; @@ -327,20 +630,11 @@ void MainBar::update_sample_count_selector() if (sample_count == 0) sample_count = DefaultSampleCount; - const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS); - const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES); - if (iter != keys.end() && - (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { - try { - auto gvar = - device->config_list(ConfigKey::LIMIT_SAMPLES); - if (gvar) - g_variant_get(gvar.gobj(), "(tt)", - &min_sample_count, &max_sample_count); - } catch(const sigrok::Error &e) { - // Failed to query sample limit - (void)e; - } + if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) { + auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); + if (gvar.gobj()) + g_variant_get(gvar.gobj(), "(tt)", + &min_sample_count, &max_sample_count); } min_sample_count = min(max(min_sample_count, MinSampleCount), @@ -349,14 +643,14 @@ void MainBar::update_sample_count_selector() sample_count_.show_125_list( min_sample_count, max_sample_count); - try { - auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES); + if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) { + auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); sample_count = g_variant_get_uint64(gvar.gobj()); if (sample_count == 0) sample_count = DefaultSampleCount; sample_count = min(max(sample_count, MinSampleCount), max_sample_count); - } catch (Error error) {} + } sample_count_.set_value(sample_count); @@ -367,12 +661,25 @@ void MainBar::update_device_config_widgets() { using namespace pv::popups; - const shared_ptr device = get_selected_device(); - if (!device) + const shared_ptr device = + device_selector_.selected_device(); + + // Hide the widgets if no device is selected + channels_button_action_->setVisible(!!device); + run_stop_button_action_->setVisible(!!device); + if (!device) { + configure_button_action_->setVisible(false); + sample_count_.show_none(); + sample_rate_.show_none(); + return; + } + + const shared_ptr sr_dev = device->device(); + if (!sr_dev) return; // Update the configure popup - DeviceOptions *const opts = new DeviceOptions(device, this); + DeviceOptions *const opts = new DeviceOptions(sr_dev, this); configure_button_action_->setVisible( !opts->binding().properties().empty()); configure_button_.set_popup(opts); @@ -384,29 +691,14 @@ void MainBar::update_device_config_widgets() // Update supported options. sample_count_supported_ = false; - try { - for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS)) - { - auto key = entry.first; - auto capabilities = entry.second; - switch (key->id()) { - case SR_CONF_LIMIT_SAMPLES: - if (capabilities.count(Capability::SET)) - sample_count_supported_ = true; - break; - case SR_CONF_LIMIT_FRAMES: - if (capabilities.count(Capability::SET)) - { - device->config_set(ConfigKey::LIMIT_FRAMES, - Glib::Variant::create(1)); - on_config_changed(); - } - break; - default: - break; - } - } - } catch (Error error) {} + if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET)) + sample_count_supported_ = true; + + if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) { + sr_dev->config_set(ConfigKey::LIMIT_FRAMES, + Glib::Variant::create(1)); + on_config_changed(); + } // Add notification of reconfigure events disconnect(this, SLOT(on_config_changed())); @@ -418,112 +710,355 @@ void MainBar::update_device_config_widgets() update_sample_rate_selector(); } -void MainBar::commit_sample_count() +void MainBar::commit_sample_rate() { - uint64_t sample_count = 0; + uint64_t sample_rate = 0; - if (updating_sample_count_) + const shared_ptr device = + device_selector_.selected_device(); + if (!device) return; - const shared_ptr device = get_selected_device(); + const shared_ptr sr_dev = device->device(); + + sample_rate = sample_rate_.value(); + if (sample_rate == 0) + return; + + try { + sr_dev->config_set(ConfigKey::SAMPLERATE, + Glib::Variant::create(sample_rate)); + update_sample_rate_selector(); + } catch (Error error) { + qDebug() << "Failed to configure samplerate."; + return; + } + + // Devices with built-in memory might impose limits on certain + // configurations, so let's check what sample count the driver + // lets us use now. + update_sample_count_selector(); +} + +void MainBar::commit_sample_count() +{ + uint64_t sample_count = 0; + + const shared_ptr device = + device_selector_.selected_device(); if (!device) return; - sample_count = sample_count_.value(); + const shared_ptr sr_dev = device->device(); - // Set the sample count - assert(!updating_sample_count_); - updating_sample_count_ = true; - if (sample_count_supported_) - { + sample_count = sample_count_.value(); + if (sample_count_supported_) { try { - device->config_set(ConfigKey::LIMIT_SAMPLES, + sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, Glib::Variant::create(sample_count)); - on_config_changed(); + update_sample_count_selector(); } catch (Error error) { qDebug() << "Failed to configure sample count."; return; } } - updating_sample_count_ = false; + + // Devices with built-in memory might impose limits on certain + // configurations, so let's check what sample rate the driver + // lets us use now. + update_sample_rate_selector(); } -void MainBar::commit_sample_rate() +void MainBar::session_error(const QString text, const QString info_text) { - uint64_t sample_rate = 0; + QMetaObject::invokeMethod(this, "show_session_error", + Qt::QueuedConnection, Q_ARG(QString, text), + Q_ARG(QString, info_text)); +} - if (updating_sample_rate_) - return; +void MainBar::show_session_error(const QString text, const QString info_text) +{ + QMessageBox msg(this); + msg.setText(text); + msg.setInformativeText(info_text); + msg.setStandardButtons(QMessageBox::Ok); + msg.setIcon(QMessageBox::Warning); + msg.exec(); +} - const shared_ptr device = get_selected_device(); - if (!device) - return; +void MainBar::capture_state_changed(int state) +{ + set_capture_state((pv::Session::capture_state)state); +} - sample_rate = sample_rate_.value(); - if (sample_rate == 0) - return; +void MainBar::add_decoder(srd_decoder *decoder) +{ +#ifdef ENABLE_DECODE + assert(decoder); + session_.add_decoder(decoder); +#else + (void)decoder; +#endif +} - // Set the samplerate - assert(!updating_sample_rate_); - updating_sample_rate_ = true; - try { - device->config_set(ConfigKey::SAMPLERATE, - Glib::Variant::create(sample_rate)); - on_config_changed(); - } catch (Error error) { - qDebug() << "Failed to configure samplerate."; +void MainBar::export_file(shared_ptr format, + bool selection_only) +{ + using pv::dialogs::StoreProgress; + + // Stop any currently running capture session + session_.stop_capture(); + + QSettings settings; + const QString dir = settings.value(SettingSaveDirectory).toString(); + + std::pair sample_range; + + // Selection only? Verify that the cursors are active and fetch their values + if (selection_only) { + if (!session_.main_view()->cursors()->enabled()) { + show_session_error(tr("Missing Cursors"), tr("You need to set the " \ + "cursors before you can save the data enclosed by them " \ + "to a session file (e.g. using ALT-V - Show Cursors).")); + return; + } + + const double samplerate = session_.get_samplerate(); + + const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time(); + const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time(); + + const uint64_t start_sample = + std::max((double)0, start_time.convert_to() * samplerate); + const uint64_t end_sample = end_time.convert_to() * samplerate; + + sample_range = std::make_pair(start_sample, end_sample); + } else { + sample_range = std::make_pair(0, 0); + } + + // Construct the filter + const vector exts = format->extensions(); + QString filter = tr("%1 files ").arg( + QString::fromStdString(format->description())); + + if (exts.empty()) + filter += "(*.*)"; + else + filter += QString("(*.%1);;%2 (*.*)").arg( + QString::fromStdString(join(exts, ", *.")), + tr("All Files")); + + // Show the file dialog + const QString file_name = QFileDialog::getSaveFileName( + this, tr("Save File"), dir, filter); + + if (file_name.isEmpty()) return; + + const QString abs_path = QFileInfo(file_name).absolutePath(); + settings.setValue(SettingSaveDirectory, abs_path); + + // Show the options dialog + map options; + if (!format->options().empty()) { + dialogs::InputOutputOptions dlg( + tr("Export %1").arg(QString::fromStdString( + format->description())), + format->options(), this); + if (!dlg.exec()) + return; + options = dlg.options(); } - updating_sample_rate_ = false; + + session_.set_name(QFileInfo(file_name).fileName()); + + StoreProgress *dlg = new StoreProgress(file_name, format, options, + sample_range, session_, this); + dlg->run(); } -void MainBar::on_device_selected() +void MainBar::import_file(shared_ptr format) { - if (updating_device_selector_) + assert(format); + + QSettings settings; + const QString dir = settings.value(SettingOpenDirectory).toString(); + + // Construct the filter + const vector exts = format->extensions(); + const QString filter = exts.empty() ? "" : + tr("%1 files (*.%2)").arg( + QString::fromStdString(format->description()), + QString::fromStdString(join(exts, ", *."))); + + // Show the file dialog + const QString file_name = QFileDialog::getOpenFileName( + this, tr("Import File"), dir, tr( + "%1 files (*.*);;All Files (*.*)").arg( + QString::fromStdString(format->description()))); + + if (file_name.isEmpty()) return; - shared_ptr device = get_selected_device(); - if (!device) + // Show the options dialog + map options; + if (!format->options().empty()) { + dialogs::InputOutputOptions dlg( + tr("Import %1").arg(QString::fromStdString( + format->description())), + format->options(), this); + if (!dlg.exec()) + return; + options = dlg.options(); + } + + load_file(file_name, format, options); + + const QString abs_path = QFileInfo(file_name).absolutePath(); + settings.setValue(SettingOpenDirectory, abs_path); +} + +void MainBar::on_device_selected() +{ + shared_ptr device = device_selector_.selected_device(); + if (!device) { + reset_device_selector(); return; + } - main_window_.select_device(device); + select_device(device); +} +void MainBar::on_device_changed() +{ + update_device_list(); update_device_config_widgets(); } void MainBar::on_sample_count_changed() { - commit_sample_count(); + if (!updating_sample_count_) + commit_sample_count(); } void MainBar::on_sample_rate_changed() { - commit_sample_rate(); + if (!updating_sample_rate_) + commit_sample_rate(); } void MainBar::on_run_stop() { commit_sample_count(); commit_sample_rate(); - main_window_.run_stop(); + run_stop(); } void MainBar::on_config_changed() { commit_sample_count(); - update_sample_count_selector(); commit_sample_rate(); - update_sample_rate_selector(); +} + +void MainBar::on_actionNewSession_triggered() +{ + new_session(); +} + +void MainBar::on_actionNewView_triggered() +{ + new_view(&session_); +} + +void MainBar::on_actionOpen_triggered() +{ + QSettings settings; + const QString dir = settings.value(SettingOpenDirectory).toString(); + + // Show the dialog + const QString file_name = QFileDialog::getOpenFileName( + this, tr("Open File"), dir, tr( + "Sigrok Sessions (*.sr);;" + "All Files (*.*)")); + + if (!file_name.isEmpty()) { + load_file(file_name); + + const QString abs_path = QFileInfo(file_name).absolutePath(); + settings.setValue(SettingOpenDirectory, abs_path); + } +} + +void MainBar::on_actionSaveAs_triggered() +{ + export_file(session_.device_manager().context()->output_formats()["srzip"]); +} + +void MainBar::on_actionSaveSelectionAs_triggered() +{ + export_file(session_.device_manager().context()->output_formats()["srzip"], true); +} + +void MainBar::on_actionConnect_triggered() +{ + // Stop any currently running capture session + session_.stop_capture(); + + dialogs::Connect dlg(this, session_.device_manager()); + + // If the user selected a device, select it in the device list. Select the + // current device otherwise. + if (dlg.exec()) + select_device(dlg.get_selected_device()); + + update_device_list(); +} + +void MainBar::on_actionViewZoomIn_triggered() +{ + session_.main_view()->zoom(1); +} + +void MainBar::on_actionViewZoomOut_triggered() +{ + session_.main_view()->zoom(-1); +} + +void MainBar::on_actionViewZoomFit_triggered() +{ + session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked()); +} + +void MainBar::on_actionViewZoomOneToOne_triggered() +{ + session_.main_view()->zoom_one_to_one(); +} + +void MainBar::on_actionViewShowCursors_triggered() +{ + const bool show = !session_.main_view()->cursors_shown(); + if (show) + session_.main_view()->centre_cursors(); + + session_.main_view()->show_cursors(show); +} + +void MainBar::on_always_zoom_to_fit_changed(bool state) +{ + action_view_zoom_fit_->setChecked(state); } bool MainBar::eventFilter(QObject *watched, QEvent *event) { - if ((watched == &sample_count_ || watched == &sample_rate_) && - (event->type() == QEvent::ToolTip)) { - double sec = (double)sample_count_.value() / sample_rate_.value(); + if (sample_count_supported_ && (watched == &sample_count_ || + watched == &sample_rate_) && + (event->type() == QEvent::ToolTip)) { + auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value(); QHelpEvent *help_event = static_cast(event); - QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); + QString str = tr("Total sampling time: %1").arg( + pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false)); QToolTip::showText(help_event->globalPos(), str); return true;