]> sigrok.org Git - pulseview.git/blobdiff - pv/toolbars/mainbar.cpp
Fix #1183 by adding a workaround for srzip handling
[pulseview.git] / pv / toolbars / mainbar.cpp
index 1797e0a43e7de836b3695cfde24cf09647b6d150..28ea0042227a6119890329208bdeedacc5277def 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>
@@ -91,9 +90,13 @@ 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_(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)),
+       new_view_button_(new QToolButton()),
        open_button_(new QToolButton()),
        save_button_(new QToolButton()),
        device_selector_(parent, session.device_manager(), action_connect_),
@@ -107,8 +110,7 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        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"));
@@ -129,10 +131,20 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        connect(action_open_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionOpen_triggered()));
 
-       action_save_as_->setText(tr("&Save As..."));
+       action_restore_setup_->setText(tr("Restore Session Setu&p..."));
+       connect(action_restore_setup_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionRestoreSetup_triggered()));
+
+       action_save_->setText(tr("&Save..."));
+       action_save_->setIcon(QIcon::fromTheme("document-save-as",
+               QIcon(":/icons/document-save-as.png")));
+       action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
+       connect(action_save_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionSave_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()));
 
@@ -143,6 +155,10 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        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"));
@@ -159,9 +175,30 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        connect(action_connect_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionConnect_triggered()));
 
+       // New view button
+       QMenu *menu_new_view = new QMenu();
+       connect(menu_new_view, SIGNAL(triggered(QAction*)),
+               this, SLOT(on_actionNewView_triggered(QAction*)));
+
+       for (int i = 0; i < views::ViewTypeCount; i++) {
+               QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i]));
+               action->setData(QVariant::fromValue(i));
+       }
+
+       new_view_button_->setMenu(menu_new_view);
+       new_view_button_->setDefaultAction(action_new_view_);
+       new_view_button_->setPopupMode(QToolButton::MenuButtonPopup);
+
        // 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_);
+               session.device_manager().context(), open_actions);
        connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
                this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
 
@@ -170,17 +207,22 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        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_);
+       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);
+               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_);
+       save_button_->setDefaultAction(action_save_);
        save_button_->setPopupMode(QToolButton::MenuButtonPopup);
 
        // Device selector menu
@@ -189,14 +231,13 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
 
        // 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"));
+       add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D));
+
+       connect(add_decoder_button_, SIGNAL(clicked()),
+               this, SLOT(on_add_decoder_clicked()));
 #endif
 
        connect(&sample_count_, SIGNAL(value_changed()),
@@ -265,6 +306,11 @@ QAction* MainBar::action_open() const
        return action_open_;
 }
 
+QAction* MainBar::action_save() const
+{
+       return action_save_;
+}
+
 QAction* MainBar::action_save_as() const
 {
        return action_save_as_;
@@ -302,6 +348,21 @@ 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);
@@ -369,7 +430,7 @@ void MainBar::update_sample_rate_selector_value()
                sample_rate_.set_value(samplerate);
                updating_sample_rate_ = false;
        } catch (Error& error) {
-               qDebug() << tr("Failed to get value of sample rate:") << error.what();
+               qDebug() << tr("Failed to get sample rate:") << error.what();
        }
 }
 
@@ -494,8 +555,6 @@ void MainBar::commit_sample_rate()
        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,
@@ -540,36 +599,16 @@ 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)
+void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name)
 {
        using pv::dialogs::StoreProgress;
 
@@ -599,9 +638,9 @@ 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
@@ -628,8 +667,8 @@ void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
                        tr("All Files"));
 
        // Show the file dialog
-       const QString file_name = QFileDialog::getSaveFileName(
-               this, tr("Save File"), dir, filter);
+       if (file_name.isEmpty())
+               file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter);
 
        if (file_name.isEmpty())
                return;
@@ -649,8 +688,13 @@ void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
                options = dlg.options();
        }
 
-       if (!selection_only)
-               session_.set_name(QFileInfo(file_name).fileName());
+       if (!selection_only) {
+               if (format == session_.device_manager().context()->output_formats()["srzip"]) {
+                       session_.set_save_path(QFileInfo(file_name).absolutePath());
+                       session_.set_name(QFileInfo(file_name).fileName());
+               } else
+                       session_.set_save_path("");
+       }
 
        StoreProgress *dlg = new StoreProgress(file_name, format, options,
                sample_range, session_, this);
@@ -695,7 +739,7 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
                options = dlg.options();
        }
 
-       session_.load_file(file_name, format, options);
+       session_.load_file(file_name, "", format, options);
 
        const QString abs_path = QFileInfo(file_name).absolutePath();
        settings.setValue(SettingOpenDirectory, abs_path);
@@ -736,13 +780,21 @@ 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();
 }
 
-void MainBar::on_actionNewView_triggered()
+void MainBar::on_actionNewView_triggered(QAction* action)
 {
-       new_view(&session_);
+       if (action)
+               new_view(&session_, action->data().toInt());
+       else
+               // When the icon of the button is clicked, we create a trace view
+               new_view(&session_, views::ViewTypeTrace);
 }
 
 void MainBar::on_actionOpen_triggered()
@@ -764,6 +816,19 @@ void MainBar::on_actionOpen_triggered()
        }
 }
 
+void MainBar::on_actionSave_triggered()
+{
+       // A path is only set if we loaded/saved an srzip file before
+       if (session_.save_path().isEmpty()) {
+               on_actionSaveAs_triggered();
+               return;
+       }
+
+       QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name());
+       export_file(session_.device_manager().context()->output_formats()["srzip"], false,
+               fi.absoluteFilePath());
+}
+
 void MainBar::on_actionSaveAs_triggered()
 {
        export_file(session_.device_manager().context()->output_formats()["srzip"]);
@@ -774,6 +839,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
@@ -789,9 +888,14 @@ 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_);
+       addWidget(new_view_button_);
        addSeparator();
        addWidget(open_button_);
        addWidget(save_button_);