]> sigrok.org Git - pulseview.git/blobdiff - pv/toolbars/mainbar.cpp
Fix #1147 by implementing decoder selector subwindow
[pulseview.git] / pv / toolbars / mainbar.cpp
index 0bb4c4541cc704f50420c06324ec0fa9de83686c..85e55fb3b17aa3194ae481c0b5fde9a1f1c20ceb 100644 (file)
@@ -35,7 +35,6 @@
 
 #include <boost/algorithm/string/join.hpp>
 
-#include <pv/data/decodesignal.hpp>
 #include <pv/devicemanager.hpp>
 #include <pv/devices/hardwaredevice.hpp>
 #include <pv/devices/inputfile.hpp>
@@ -51,7 +50,7 @@
 #include <pv/widgets/exportmenu.hpp>
 #include <pv/widgets/importmenu.hpp>
 #ifdef ENABLE_DECODE
-#include <pv/widgets/decodermenu.hpp>
+#include <pv/data/decodesignal.hpp>
 #endif
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
@@ -87,18 +86,18 @@ const uint64_t MainBar::DefaultSampleCount = 1000000;
 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
 
-MainBar::MainBar(Session &session, QWidget *parent,
-               pv::views::trace::View *view) :
+MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) :
        StandardBar(session, parent, view, false),
        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_restore_setup_(new QAction(this)),
+       action_save_setup_(new QAction(this)),
        action_connect_(new QAction(this)),
        open_button_(new QToolButton()),
        save_button_(new QToolButton()),
-       device_selector_(parent, session.device_manager(),
-               action_connect_),
+       device_selector_(parent, session.device_manager(), action_connect_),
        configure_button_(this),
        configure_button_action_(nullptr),
        channels_button_(this),
@@ -109,8 +108,7 @@ MainBar::MainBar(Session &session, QWidget *parent,
        updating_sample_count_(false),
        sample_count_supported_(false)
 #ifdef ENABLE_DECODE
-       , add_decoder_button_(new QToolButton()),
-       menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
+       , add_decoder_button_(new QToolButton())
 #endif
 {
        setObjectName(QString::fromUtf8("MainBar"));
@@ -131,6 +129,10 @@ MainBar::MainBar(Session &session, QWidget *parent,
        connect(action_open_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionOpen_triggered()));
 
+       action_restore_setup_->setText(tr("Restore Session Setu&p..."));
+       connect(action_restore_setup_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionRestoreSetup_triggered()));
+
        action_save_as_->setText(tr("&Save As..."));
        action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
                QIcon(":/icons/document-save-as.png")));
@@ -145,18 +147,20 @@ MainBar::MainBar(Session &session, QWidget *parent,
        connect(action_save_selection_as_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionSaveSelectionAs_triggered()));
 
+       action_save_setup_->setText(tr("Save Session Setu&p..."));
+       connect(action_save_setup_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionSaveSetup_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(shared_ptr<sigrok::OutputFormat>)),
+       connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
                this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
 
        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(shared_ptr<sigrok::InputFormat>)),
+       connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
                this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
 
        action_connect_->setText(tr("&Connect to Device..."));
@@ -164,29 +168,35 @@ MainBar::MainBar(Session &session, QWidget *parent,
                this, SLOT(on_actionConnect_triggered()));
 
        // Open button
+       vector<QAction*> open_actions;
+       open_actions.push_back(action_open_);
+       QAction* separator_o = new QAction(this);
+       separator_o->setSeparator(true);
+       open_actions.push_back(separator_o);
+       open_actions.push_back(action_restore_setup_);
+
        widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
-               session.device_manager().context(), action_open_);
-       connect(import_menu,
-               SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
-               this,
-               SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
+               session.device_manager().context(), open_actions);
+       connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
+               this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
 
        open_button_->setMenu(import_menu);
        open_button_->setDefaultAction(action_open_);
        open_button_->setPopupMode(QToolButton::MenuButtonPopup);
 
        // Save button
-       vector<QAction *> open_actions;
-       open_actions.push_back(action_save_as_);
-       open_actions.push_back(action_save_selection_as_);
+       vector<QAction*> save_actions;
+       save_actions.push_back(action_save_as_);
+       save_actions.push_back(action_save_selection_as_);
+       QAction* separator_s = new QAction(this);
+       separator_s->setSeparator(true);
+       save_actions.push_back(separator_s);
+       save_actions.push_back(action_save_setup_);
 
        widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
-               session.device_manager().context(),
-               open_actions);
-       connect(export_menu,
-               SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
-               this,
-               SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
+               session.device_manager().context(), save_actions);
+       connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
+               this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
 
        save_button_->setMenu(export_menu);
        save_button_->setDefaultAction(action_save_as_);
@@ -198,14 +208,12 @@ MainBar::MainBar(Session &session, QWidget *parent,
 
        // 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*)));
-
        add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
        add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
-       add_decoder_button_->setMenu(menu_decoders_add_);
-       add_decoder_button_->setToolTip(tr("Add low-level, non-stacked protocol decoder"));
+       add_decoder_button_->setToolTip(tr("Add protocol decoder"));
+
+       connect(add_decoder_button_, SIGNAL(clicked()),
+               this, SLOT(on_add_decoder_clicked()));
 #endif
 
        connect(&sample_count_, SIGNAL(value_changed()),
@@ -302,8 +310,7 @@ void MainBar::update_sample_rate_selector()
                return;
        }
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -312,10 +319,25 @@ void MainBar::update_sample_rate_selector()
 
        const shared_ptr<sigrok::Device> sr_dev = device->device();
 
+       sample_rate_.allow_user_entered_values(false);
+       if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
+               try {
+                       auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
+                       if (gvar.gobj()) {
+                               bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
+                                       gvar).get();
+                               sample_rate_.allow_user_entered_values(value);
+                       }
+               } catch (Error& error) {
+                       // Do nothing
+               }
+       }
+
+
        if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
                try {
                        gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
-               } catch (Error error) {
+               } catch (Error& error) {
                        qDebug() << tr("Failed to get sample rate list:") << error.what();
                }
        } else {
@@ -327,7 +349,7 @@ void MainBar::update_sample_rate_selector()
        if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
                        "samplerate-steps", G_VARIANT_TYPE("at")))) {
                elements = (const uint64_t *)g_variant_get_fixed_array(
-                               gvar_list, &num_elements, sizeof(uint64_t));
+                       gvar_list, &num_elements, sizeof(uint64_t));
 
                const uint64_t min = elements[0];
                const uint64_t max = elements[1];
@@ -352,7 +374,7 @@ void MainBar::update_sample_rate_selector()
        } 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));
+                       gvar_list, &num_elements, sizeof(uint64_t));
                sample_rate_.show_list(elements, num_elements);
                g_variant_unref(gvar_list);
        }
@@ -366,8 +388,7 @@ void MainBar::update_sample_rate_selector_value()
        if (updating_sample_rate_)
                return;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -379,9 +400,8 @@ void MainBar::update_sample_rate_selector_value()
                updating_sample_rate_ = true;
                sample_rate_.set_value(samplerate);
                updating_sample_rate_ = false;
-       } catch (Error error) {
-               qDebug() << "WARNING: Failed to get value of sample rate";
-               return;
+       } catch (Error& error) {
+               qDebug() << tr("Failed to get sample rate:") << error.what();
        }
 }
 
@@ -390,8 +410,7 @@ void MainBar::update_sample_count_selector()
        if (updating_sample_count_)
                return;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -422,7 +441,7 @@ void MainBar::update_sample_count_selector()
                        if (gvar.gobj())
                                g_variant_get(gvar.gobj(), "(tt)",
                                        &min_sample_count, &max_sample_count);
-               } catch (Error error) {
+               } catch (Error& error) {
                        qDebug() << tr("Failed to get sample limit list:") << error.what();
                }
        }
@@ -456,8 +475,7 @@ void MainBar::update_device_config_widgets()
 {
        using namespace pv::popups;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
 
        // Hide the widgets if no device is selected
        channels_button_action_->setVisible(!!device);
@@ -474,8 +492,7 @@ void MainBar::update_device_config_widgets()
 
        // Update the configure popup
        DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
-       configure_button_action_->setVisible(
-               !opts->binding().properties().empty());
+       configure_button_action_->setVisible(!opts->binding().properties().empty());
        configure_button_.set_popup(opts);
 
        // Update the channels popup
@@ -502,23 +519,20 @@ void MainBar::commit_sample_rate()
 {
        uint64_t sample_rate = 0;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
        const shared_ptr<sigrok::Device> sr_dev = device->device();
 
        sample_rate = sample_rate_.value();
-       if (sample_rate == 0)
-               return;
 
        try {
                sr_dev->config_set(ConfigKey::SAMPLERATE,
                        Glib::Variant<guint64>::create(sample_rate));
                update_sample_rate_selector();
-       } catch (Error error) {
-               qDebug() << "Failed to configure samplerate.";
+       } catch (Error& error) {
+               qDebug() << tr("Failed to configure samplerate:") << error.what();
                return;
        }
 
@@ -532,8 +546,7 @@ void MainBar::commit_sample_count()
 {
        uint64_t sample_count = 0;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -545,8 +558,8 @@ void MainBar::commit_sample_count()
                        sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
                                Glib::Variant<guint64>::create(sample_count));
                        update_sample_count_selector();
-               } catch (Error error) {
-                       qDebug() << "Failed to configure sample count.";
+               } catch (Error& error) {
+                       qDebug() << tr("Failed to configure sample count:") << error.what();
                        return;
                }
        }
@@ -557,35 +570,15 @@ void MainBar::commit_sample_count()
        update_sample_rate_selector();
 }
 
-void MainBar::session_error(const QString text, const QString info_text)
-{
-       QMetaObject::invokeMethod(this, "show_session_error",
-               Qt::QueuedConnection, Q_ARG(QString, text),
-               Q_ARG(QString, info_text));
-}
-
 void MainBar::show_session_error(const QString text, const QString info_text)
 {
        QMessageBox msg(this);
-       msg.setText(text);
-       msg.setInformativeText(info_text);
+       msg.setText(text + "\n\n" + info_text);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.setIcon(QMessageBox::Warning);
        msg.exec();
 }
 
-void MainBar::add_decoder(srd_decoder *decoder)
-{
-#ifdef ENABLE_DECODE
-       assert(decoder);
-       shared_ptr<data::DecodeSignal> signal = session_.add_decode_signal();
-       if (signal)
-               signal->stack_decoder(decoder);
-#else
-       (void)decoder;
-#endif
-}
-
 void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
 {
        using pv::dialogs::StoreProgress;
@@ -605,8 +598,8 @@ void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
 
                if (!trace_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 the Show Cursors button)."));
+                               "cursors before you can save the data enclosed by them " \
+                               "to a session file (e.g. using the Show Cursors button)."));
                        return;
                }
 
@@ -616,14 +609,14 @@ void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
                const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
 
                const uint64_t start_sample = (uint64_t)max(
-                       (double)0, start_time.convert_to<double>() * samplerate);
+                       0.0, start_time.convert_to<double>() * samplerate);
                const uint64_t end_sample = (uint64_t)max(
-                       (double)0, end_time.convert_to<double>() * samplerate);
+                       0.0, end_time.convert_to<double>() * samplerate);
 
                if ((start_sample == 0) && (end_sample == 0)) {
                        // Both cursors are negative and were clamped to 0
                        show_session_error(tr("Invalid Range"), tr("The cursors don't " \
-                                       "define a valid range of samples."));
+                               "define a valid range of samples."));
                        return;
                }
 
@@ -721,12 +714,11 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
 void MainBar::on_device_selected()
 {
        shared_ptr<devices::Device> device = device_selector_.selected_device();
-       if (!device) {
-               reset_device_selector();
-               return;
-       }
 
-       session_.select_device(device);
+       if (device)
+               session_.select_device(device);
+       else
+               reset_device_selector();
 }
 
 void MainBar::on_device_changed()
@@ -754,6 +746,10 @@ void MainBar::on_sample_rate_changed()
 
 void MainBar::on_config_changed()
 {
+       // We want to also call update_sample_rate_selector() here in case
+       // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
+       // commit_sample_rate() does this already, so we don't call it here
+
        commit_sample_count();
        commit_sample_rate();
 }
@@ -792,6 +788,40 @@ void MainBar::on_actionSaveSelectionAs_triggered()
        export_file(session_.device_manager().context()->output_formats()["srzip"], true);
 }
 
+void MainBar::on_actionSaveSetup_triggered()
+{
+       QSettings settings;
+       const QString dir = settings.value(SettingSaveDirectory).toString();
+
+       const QString file_name = QFileDialog::getSaveFileName(
+               this, tr("Save File"), dir, tr(
+                               "PulseView Session Setups (*.pvs);;"
+                               "All Files (*)"));
+
+       if (file_name.isEmpty())
+               return;
+
+       QSettings settings_storage(file_name, QSettings::IniFormat);
+       session_.save_setup(settings_storage);
+}
+
+void MainBar::on_actionRestoreSetup_triggered()
+{
+       QSettings settings;
+       const QString dir = settings.value(SettingSaveDirectory).toString();
+
+       const QString file_name = QFileDialog::getOpenFileName(
+               this, tr("Open File"), dir, tr(
+                               "PulseView Session Setups (*.pvs);;"
+                               "All Files (*)"));
+
+       if (file_name.isEmpty())
+               return;
+
+       QSettings settings_storage(file_name, QSettings::IniFormat);
+       session_.restore_setup(settings_storage);
+}
+
 void MainBar::on_actionConnect_triggered()
 {
        // Stop any currently running capture session
@@ -807,6 +837,11 @@ void MainBar::on_actionConnect_triggered()
        update_device_list();
 }
 
+void MainBar::on_add_decoder_clicked()
+{
+       show_decoder_selector(&session_);
+}
+
 void MainBar::add_toolbar_widgets()
 {
        addAction(action_new_view_);
@@ -831,8 +866,9 @@ void MainBar::add_toolbar_widgets()
 bool MainBar::eventFilter(QObject *watched, QEvent *event)
 {
        if (sample_count_supported_ && (watched == &sample_count_ ||
-                       watched == &sample_rate_) &&
-                       (event->type() == QEvent::ToolTip)) {
+               watched == &sample_rate_) &&
+               (event->type() == QEvent::ToolTip)) {
+
                auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
                QHelpEvent *help_event = static_cast<QHelpEvent*>(event);