]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
Fix #685 by adding a special T marker when SR_DT_TRIGGER arrives
[pulseview.git] / pv / mainwindow.cpp
index 6d28194bb87cbed491e00a3aa2bc21436e8976b3..dad9d680241ad4d59f4352c6dd80863d13b73cbd 100644 (file)
@@ -45,6 +45,8 @@
 #include "mainwindow.hpp"
 
 #include "devicemanager.hpp"
+#include "util.hpp"
+#include "data/segment.hpp"
 #include "devices/hardwaredevice.hpp"
 #include "devices/inputfile.hpp"
 #include "devices/sessionfile.hpp"
@@ -100,6 +102,7 @@ MainWindow::MainWindow(DeviceManager &device_manager,
        session_(device_manager),
        action_open_(new QAction(this)),
        action_save_as_(new QAction(this)),
+       action_save_selection_as_(new QAction(this)),
        action_connect_(new QAction(this)),
        action_quit_(new QAction(this)),
        action_view_zoom_in_(new QAction(this)),
@@ -113,6 +116,8 @@ MainWindow::MainWindow(DeviceManager &device_manager,
        , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
 #endif
 {
+       qRegisterMetaType<util::Timestamp>("util::Timestamp");
+
        setup_ui();
        restore_ui_settings();
        if (open_file_name.empty())
@@ -131,6 +136,11 @@ QAction* MainWindow::action_save_as() const
        return action_save_as_;
 }
 
+QAction* MainWindow::action_save_selection_as() const
+{
+       return action_save_selection_as_;
+}
+
 QAction* MainWindow::action_connect() const
 {
        return action_connect_;
@@ -215,7 +225,8 @@ void MainWindow::select_device(shared_ptr<devices::Device> device)
        }
 }
 
-void MainWindow::export_file(shared_ptr<OutputFormat> format)
+void MainWindow::export_file(shared_ptr<OutputFormat> format,
+       bool selection_only)
 {
        using pv::dialogs::StoreProgress;
 
@@ -225,6 +236,30 @@ void MainWindow::export_file(shared_ptr<OutputFormat> format)
        QSettings settings;
        const QString dir = settings.value(SettingSaveDirectory).toString();
 
+       std::pair<uint64_t, uint64_t> sample_range;
+
+       // Selection only? Verify that the cursors are active and fetch their values
+       if (selection_only) {
+               if (!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 = view_->cursors()->first()->time();
+               const pv::util::Timestamp& end_time = view_->cursors()->second()->time();
+
+               const uint64_t start_sample = start_time.convert_to<double>() * samplerate;
+               const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
+
+               sample_range = std::make_pair(start_sample, end_sample);
+       } else {
+               sample_range = std::make_pair(0, 0);
+       }
+
        // Construct the filter
        const vector<string> exts = format->extensions();
        QString filter = tr("%1 files ").arg(
@@ -260,7 +295,7 @@ void MainWindow::export_file(shared_ptr<OutputFormat> format)
        }
 
        StoreProgress *dlg = new StoreProgress(file_name, format, options,
-               session_, this);
+               sample_range, session_, this);
        dlg->run();
 }
 
@@ -347,6 +382,13 @@ void MainWindow::setup_ui()
        action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
        menu_file->addAction(action_save_as_);
 
+       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));
+       action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
+       menu_file->addAction(action_save_selection_as_);
+
        menu_file->addSeparator();
 
        widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
@@ -401,6 +443,7 @@ void MainWindow::setup_ui()
                QString::fromUtf8("actionViewZoomOut"));
        menu_view->addAction(action_view_zoom_out_);
 
+       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")));
@@ -421,10 +464,10 @@ void MainWindow::setup_ui()
 
        action_view_sticky_scrolling_->setCheckable(true);
        action_view_sticky_scrolling_->setChecked(true);
-       action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_R));
+       action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
        action_view_sticky_scrolling_->setObjectName(
                QString::fromUtf8("actionViewStickyScrolling"));
-       action_view_sticky_scrolling_->setText(tr("Sticky Sc&rolling"));
+       action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
        menu_view->addAction(action_view_sticky_scrolling_);
 
        view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
@@ -491,10 +534,14 @@ void MainWindow::setup_ui()
                SLOT(capture_state_changed(int)));
        connect(&session_, SIGNAL(device_selected()), this,
                SLOT(device_selected()));
+       connect(&session_, SIGNAL(trigger_event(util::Timestamp)), view_,
+               SLOT(trigger_event(util::Timestamp)));
 
        // Setup view_ events
        connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this,
                SLOT(sticky_scrolling_changed(bool)));
+       connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this,
+               SLOT(always_zoom_to_fit_changed(bool)));
 
 }
 
@@ -621,7 +668,6 @@ void MainWindow::load_file(QString file_name,
 {
        const QString errorMessage(
                QString("Failed to load file %1").arg(file_name));
-       const QString infoMessage;
 
        try {
                if (format)
@@ -644,7 +690,7 @@ void MainWindow::load_file(QString file_name,
 
        update_device_list();
 
-       session_.start_capture([&, errorMessage, infoMessage](QString) {
+       session_.start_capture([&, errorMessage](QString infoMessage) {
                session_error(errorMessage, infoMessage); });
 }
 
@@ -698,6 +744,11 @@ void MainWindow::on_actionSaveAs_triggered()
        export_file(device_manager_.context()->output_formats()["srzip"]);
 }
 
+void MainWindow::on_actionSaveSelectionAs_triggered()
+{
+       export_file(device_manager_.context()->output_formats()["srzip"], true);
+}
+
 void MainWindow::on_actionConnect_triggered()
 {
        // Stop any currently running capture session
@@ -730,7 +781,7 @@ void MainWindow::on_actionViewZoomOut_triggered()
 
 void MainWindow::on_actionViewZoomFit_triggered()
 {
-       view_->zoom_fit();
+       view_->zoom_fit(action_view_zoom_fit_->isChecked());
 }
 
 void MainWindow::on_actionViewZoomOneToOne_triggered()
@@ -748,7 +799,7 @@ void MainWindow::on_actionViewShowCursors_triggered()
        assert(view_);
 
        const bool show = !view_->cursors_shown();
-       if(show)
+       if (show)
                view_->centre_cursors();
 
        view_->show_cursors(show);
@@ -765,6 +816,11 @@ void MainWindow::sticky_scrolling_changed(bool state)
        action_view_sticky_scrolling_->setChecked(state);
 }
 
+void MainWindow::always_zoom_to_fit_changed(bool state)
+{
+       action_view_zoom_fit_->setChecked(state);
+}
+
 void MainWindow::add_decoder(srd_decoder *decoder)
 {
 #ifdef ENABLE_DECODE