]> sigrok.org Git - pulseview.git/blobdiff - pv/toolbars/mainbar.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / toolbars / mainbar.cpp
index 666c28d3049f7bd6b8a9ff7ccc35dc902b69814b..cd46c063bdc8a84d78d1e42e1c616c2fc8a3b270 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <boost/algorithm/string/join.hpp>
 
+#include <pv/data/mathsignal.hpp>
 #include <pv/devicemanager.hpp>
 #include <pv/devices/hardwaredevice.hpp>
 #include <pv/devices/inputfile.hpp>
@@ -59,6 +60,7 @@ using std::back_inserter;
 using std::copy;
 using std::list;
 using std::make_pair;
+using std::make_shared;
 using std::map;
 using std::max;
 using std::min;
@@ -90,11 +92,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_),
@@ -106,10 +110,11 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        sample_rate_("Hz", this),
        updating_sample_rate_(false),
        updating_sample_count_(false),
-       sample_count_supported_(false)
+       sample_count_supported_(false),
 #ifdef ENABLE_DECODE
-       , add_decoder_button_(new QToolButton())
+       add_decoder_button_(new QToolButton()),
 #endif
+       add_math_signal_button_(new QToolButton())
 {
        setObjectName(QString::fromUtf8("MainBar"));
 
@@ -125,7 +130,11 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        action_open_->setText(tr("&Open..."));
        action_open_->setIcon(QIcon::fromTheme("document-open",
                QIcon(":/icons/document-open.png")));
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+       action_open_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O));
+#else
        action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
+#endif
        connect(action_open_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionOpen_triggered()));
 
@@ -133,17 +142,31 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        connect(action_restore_setup_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionRestoreSetup_triggered()));
 
-       action_save_as_->setText(tr("&Save As..."));
+       action_save_->setText(tr("&Save..."));
+       action_save_->setIcon(QIcon::fromTheme("document-save-as",
+               QIcon(":/icons/document-save-as.png")));
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+       action_save_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
+#else
+       action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
+#endif
+       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()));
 
        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")));
+#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+       action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_R));
+#else
        action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
+#endif
        connect(action_save_selection_as_, SIGNAL(triggered(bool)),
                this, SLOT(on_actionSaveSelectionAs_triggered()));
 
@@ -167,6 +190,20 @@ 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_);
@@ -186,6 +223,7 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
 
        // Save button
        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);
@@ -199,7 +237,7 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
                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
@@ -211,11 +249,22 @@ MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view
        add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
        add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
        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
 
+       // Setup the math signal button
+       add_math_signal_button_->setIcon(QIcon(":/icons/add-math-signal.svg"));
+       add_math_signal_button_->setPopupMode(QToolButton::InstantPopup);
+       add_math_signal_button_->setToolTip(tr("Add math signal"));
+       add_math_signal_button_->setShortcut(QKeySequence(Qt::Key_M));
+
+       connect(add_math_signal_button_, SIGNAL(clicked()),
+               this, SLOT(on_add_math_signal_clicked()));
+
+
        connect(&sample_count_, SIGNAL(value_changed()),
                this, SLOT(on_sample_count_changed()));
        connect(&sample_rate_, SIGNAL(value_changed()),
@@ -282,6 +331,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_;
@@ -506,7 +560,7 @@ void MainBar::update_device_config_widgets()
                sample_count_supported_ = true;
 
        // Add notification of reconfigure events
-       disconnect(this, SLOT(on_config_changed()));
+       // Note: No need to disconnect the previous signal as that QObject instance is destroyed
        connect(&opts->binding(), SIGNAL(config_changed()),
                this, SLOT(on_config_changed()));
 
@@ -579,7 +633,7 @@ void MainBar::show_session_error(const QString text, const QString info_text)
        msg.exec();
 }
 
-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;
 
@@ -638,8 +692,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;
@@ -659,8 +713,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);
@@ -754,9 +813,13 @@ void MainBar::on_config_changed()
        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()
@@ -778,6 +841,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"]);
@@ -842,9 +918,15 @@ void MainBar::on_add_decoder_clicked()
        show_decoder_selector(&session_);
 }
 
+void MainBar::on_add_math_signal_clicked()
+{
+       shared_ptr<data::SignalBase> signal = make_shared<data::MathSignal>(session_);
+       session_.add_generated_signal(signal);
+}
+
 void MainBar::add_toolbar_widgets()
 {
-       addAction(action_new_view_);
+       addWidget(new_view_button_);
        addSeparator();
        addWidget(open_button_);
        addWidget(save_button_);
@@ -861,6 +943,7 @@ void MainBar::add_toolbar_widgets()
        addSeparator();
        addWidget(add_decoder_button_);
 #endif
+       addWidget(add_math_signal_button_);
 }
 
 bool MainBar::eventFilter(QObject *watched, QEvent *event)