]> sigrok.org Git - pulseview.git/commitdiff
Introduce "save selection range as..." feature
authorSoeren Apel <redacted>
Wed, 4 Nov 2015 15:33:07 +0000 (16:33 +0100)
committerSoeren Apel <redacted>
Wed, 4 Nov 2015 16:56:17 +0000 (17:56 +0100)
pv/dialogs/storeprogress.cpp
pv/dialogs/storeprogress.hpp
pv/mainwindow.cpp
pv/mainwindow.hpp
pv/storesession.cpp
pv/storesession.hpp

index a7f889e07940573c775bfbed361ec5b0823b161a..5aeafd141fbc86453a00070eb7a94c5a060777b5 100644 (file)
@@ -35,9 +35,11 @@ namespace dialogs {
 StoreProgress::StoreProgress(const QString &file_name,
        const std::shared_ptr<sigrok::OutputFormat> output_format,
        const map<string, VariantBase> &options,
 StoreProgress::StoreProgress(const QString &file_name,
        const std::shared_ptr<sigrok::OutputFormat> output_format,
        const map<string, VariantBase> &options,
+       const std::pair<uint64_t, uint64_t> sample_range,
        const Session &session, QWidget *parent) :
        QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent),
        const Session &session, QWidget *parent) :
        QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent),
-       session_(file_name.toStdString(), output_format, options, session)
+       session_(file_name.toStdString(), output_format, options, sample_range,
+               session)
 {
        connect(&session_, SIGNAL(progress_updated()),
                this, SLOT(on_progress_updated()));
 {
        connect(&session_, SIGNAL(progress_updated()),
                this, SLOT(on_progress_updated()));
index 19aef14a0e5bcb504813ccee9134b434ec73a217..37916420674aa9a43b7c1680e0258cf1d5f70d24 100644 (file)
@@ -42,7 +42,9 @@ public:
        StoreProgress(const QString &file_name,
                const std::shared_ptr<sigrok::OutputFormat> output_format,
                const std::map<std::string, Glib::VariantBase> &options,
        StoreProgress(const QString &file_name,
                const std::shared_ptr<sigrok::OutputFormat> output_format,
                const std::map<std::string, Glib::VariantBase> &options,
-               const Session &session, QWidget *parent = 0);
+               const std::pair<uint64_t, uint64_t> sample_range,
+               const Session &session,
+               QWidget *parent = 0);
 
        virtual ~StoreProgress();
 
 
        virtual ~StoreProgress();
 
index 8b32046a6e8d3db5aa3ccdfce967bf7022fc3a25..195c460a1de90f3aea20b3fb4af9745a6a34103b 100644 (file)
@@ -45,6 +45,8 @@
 #include "mainwindow.hpp"
 
 #include "devicemanager.hpp"
 #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"
 #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)),
        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)),
        action_connect_(new QAction(this)),
        action_quit_(new QAction(this)),
        action_view_zoom_in_(new QAction(this)),
@@ -259,8 +262,10 @@ void MainWindow::export_file(shared_ptr<OutputFormat> format)
                options = dlg.options();
        }
 
                options = dlg.options();
        }
 
+       const std::pair<uint64_t, uint64_t> sample_range = std::make_pair(0, 0);
+
        StoreProgress *dlg = new StoreProgress(file_name, format, options,
        StoreProgress *dlg = new StoreProgress(file_name, format, options,
-               session_, this);
+               sample_range, session_, this);
        dlg->run();
 }
 
        dlg->run();
 }
 
@@ -347,6 +352,13 @@ void MainWindow::setup_ui()
        action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
        menu_file->addAction(action_save_as_);
 
        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,
        menu_file->addSeparator();
 
        widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
@@ -649,6 +661,87 @@ void MainWindow::load_file(QString file_name,
                session_error(errorMessage, infoMessage); });
 }
 
                session_error(errorMessage, infoMessage); });
 }
 
+void MainWindow::save_selection_to_file()
+{
+       // Stop any currently running capture session
+       session_.stop_capture();
+
+       // Get sample rate
+       double samplerate = 0.0;
+
+       for (const shared_ptr<pv::data::SignalData> d : session_.get_data()) {
+               assert(d);
+               const vector< shared_ptr<pv::data::Segment> > segments =
+                       d->segments();
+               for (const shared_ptr<pv::data::Segment> &s : segments)
+                       samplerate = std::max(samplerate, s->samplerate());
+       }
+
+       if (samplerate == 0.0)
+               samplerate = 1;
+
+       // Verify that the cursors are active and fetch their values
+       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 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;
+
+       const std::pair<uint64_t, uint64_t> sample_range =
+                       std::make_pair(start_sample, end_sample);
+
+       // Ask for output file name
+       QSettings settings;
+       const QString dir = settings.value(SettingSaveDirectory).toString();
+
+       shared_ptr<OutputFormat> format =
+                       device_manager_.context()->output_formats()["srzip"];
+
+       const vector<string> 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, ", *."))).arg(
+                       tr("All Files"));
+
+       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<string, Glib::VariantBase> 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();
+       }
+
+       // Save
+       pv::dialogs::StoreProgress *dlg = new pv::dialogs::StoreProgress(file_name,
+               format, options, sample_range, session_, this);
+       dlg->run();
+}
+
 void MainWindow::closeEvent(QCloseEvent *event)
 {
        save_ui_settings();
 void MainWindow::closeEvent(QCloseEvent *event)
 {
        save_ui_settings();
@@ -699,6 +792,11 @@ void MainWindow::on_actionSaveAs_triggered()
        export_file(device_manager_.context()->output_formats()["srzip"]);
 }
 
        export_file(device_manager_.context()->output_formats()["srzip"]);
 }
 
+void MainWindow::on_actionSaveSelectionAs_triggered()
+{
+       save_selection_to_file();
+}
+
 void MainWindow::on_actionConnect_triggered()
 {
        // Stop any currently running capture session
 void MainWindow::on_actionConnect_triggered()
 {
        // Stop any currently running capture session
index e86dd183bbee32192a6511b58bf29441c585be9c..94fc092a940c278783629c77c41099fb7ed0fd7d 100644 (file)
@@ -84,6 +84,7 @@ public:
 
        QAction* action_open() const;
        QAction* action_save_as() const;
 
        QAction* action_open() const;
        QAction* action_save_as() const;
+       QAction* action_save_selection_as() const;
        QAction* action_connect() const;
        QAction* action_quit() const;
        QAction* action_view_zoom_in() const;
        QAction* action_connect() const;
        QAction* action_quit() const;
        QAction* action_view_zoom_in() const;
@@ -130,6 +131,8 @@ private:
                const std::map<std::string, Glib::VariantBase> &options =
                        std::map<std::string, Glib::VariantBase>());
 
                const std::map<std::string, Glib::VariantBase> &options =
                        std::map<std::string, Glib::VariantBase>());
 
+       void save_selection_to_file();
+
 private:
        void closeEvent(QCloseEvent *event);
 
 private:
        void closeEvent(QCloseEvent *event);
 
@@ -141,6 +144,7 @@ private Q_SLOTS:
 
        void on_actionOpen_triggered();
        void on_actionSaveAs_triggered();
 
        void on_actionOpen_triggered();
        void on_actionSaveAs_triggered();
+       void on_actionSaveSelectionAs_triggered();
        void on_actionQuit_triggered();
 
        void on_actionConnect_triggered();
        void on_actionQuit_triggered();
 
        void on_actionConnect_triggered();
@@ -182,6 +186,7 @@ private:
 
        QAction *const action_open_;
        QAction *const action_save_as_;
 
        QAction *const action_open_;
        QAction *const action_save_as_;
+       QAction *const action_save_selection_as_;
        QAction *const action_connect_;
        QAction *const action_quit_;
        QAction *const action_view_zoom_in_;
        QAction *const action_connect_;
        QAction *const action_quit_;
        QAction *const action_view_zoom_in_;
index c7c2edb0b4eace1e1fcf290770527b35ac6d12fb..309fa1b37062c8facf922bccbc5f433418cd23bb 100644 (file)
@@ -71,10 +71,13 @@ const size_t StoreSession::BlockSize = 1024 * 1024;
 
 StoreSession::StoreSession(const std::string &file_name,
        const shared_ptr<OutputFormat> &output_format,
 
 StoreSession::StoreSession(const std::string &file_name,
        const shared_ptr<OutputFormat> &output_format,
-       const map<string, VariantBase> &options, const Session &session) :
+       const map<string, VariantBase> &options,
+       const std::pair<uint64_t, uint64_t> sample_range,
+       const Session &session) :
        file_name_(file_name),
        output_format_(output_format),
        options_(options),
        file_name_(file_name),
        output_format_(output_format),
        options_(options),
+       sample_range_(sample_range),
        session_(session),
        interrupt_(false),
        units_stored_(0),
        session_(session),
        interrupt_(false),
        units_stored_(0),
@@ -141,6 +144,15 @@ bool StoreSession::start()
        const shared_ptr<data::LogicSegment> segment(segments.front());
        assert(segment);
 
        const shared_ptr<data::LogicSegment> segment(segments.front());
        assert(segment);
 
+       // Check whether the user wants to export a certain sample range
+       if (sample_range_.first == sample_range_.second) {
+               start_sample_ = 0;
+               sample_count_ = segment->get_sample_count();
+       } else {
+               start_sample_ = std::min(sample_range_.first, sample_range_.second);
+               sample_count_ = std::abs(sample_range_.second - sample_range_.first);
+       }
+
        // Begin storing
        try {
                const auto context = session_.device_manager().context();
        // Begin storing
        try {
                const auto context = session_.device_manager().context();
@@ -181,7 +193,6 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
 {
        assert(segment);
 
 {
        assert(segment);
 
-       uint64_t start_sample = 0, sample_count;
        unsigned progress_scale = 0;
 
        /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
        unsigned progress_scale = 0;
 
        /// TODO: Wrap this in a std::unique_ptr when we transition to C++11
@@ -191,26 +202,25 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
        const int unit_size = segment->unit_size();
        assert(unit_size != 0);
 
        const int unit_size = segment->unit_size();
        assert(unit_size != 0);
 
-       sample_count = segment->get_sample_count();
-
        // Qt needs the progress values to fit inside an int.  If they would
        // not, scale the current and max values down until they do.
        // Qt needs the progress values to fit inside an int.  If they would
        // not, scale the current and max values down until they do.
-       while ((sample_count >> progress_scale) > INT_MAX)
+       while ((sample_count_ >> progress_scale) > INT_MAX)
                progress_scale ++;
 
                progress_scale ++;
 
-       unit_count_ = sample_count >> progress_scale;
+       unit_count_ = sample_count_ >> progress_scale;
 
        const unsigned int samples_per_block = BlockSize / unit_size;
 
 
        const unsigned int samples_per_block = BlockSize / unit_size;
 
-       while (!interrupt_ && start_sample < sample_count)
+       while (!interrupt_ && sample_count_)
        {
                progress_updated();
 
        {
                progress_updated();
 
-               const uint64_t end_sample = min(
-                       start_sample + samples_per_block, sample_count);
-               segment->get_samples(data, start_sample, end_sample);
+               const uint64_t packet_len =
+                       std::min((uint64_t)samples_per_block, sample_count_);
+
+               segment->get_samples(data, start_sample_, start_sample_ + packet_len);
 
 
-               size_t length = (end_sample - start_sample) * unit_size;
+               size_t length = packet_len * unit_size;
 
                try {
                        const auto context = session_.device_manager().context();
 
                try {
                        const auto context = session_.device_manager().context();
@@ -223,8 +233,9 @@ void StoreSession::store_proc(shared_ptr<data::LogicSegment> segment)
                        break;
                }
 
                        break;
                }
 
-               start_sample = end_sample;
-               units_stored_ = start_sample >> progress_scale;
+               sample_count_ -= packet_len;
+               start_sample_ += packet_len;
+               units_stored_ = unit_count_ - (sample_count_ >> progress_scale);
        }
 
        // Zeroing the progress variables indicates completion
        }
 
        // Zeroing the progress variables indicates completion
index 9f31d59a7920c93d6252dba98acf0eee8fcef0d9..6a081801573491e8a626b9a90107ef68bff90a89 100644 (file)
@@ -59,6 +59,7 @@ public:
        StoreSession(const std::string &file_name,
                const std::shared_ptr<sigrok::OutputFormat> &output_format,
                const std::map<std::string, Glib::VariantBase> &options,
        StoreSession(const std::string &file_name,
                const std::shared_ptr<sigrok::OutputFormat> &output_format,
                const std::map<std::string, Glib::VariantBase> &options,
+               const std::pair<uint64_t, uint64_t> sample_range,
                const Session &session);
 
        ~StoreSession();
                const Session &session);
 
        ~StoreSession();
@@ -83,6 +84,7 @@ private:
        const std::string file_name_;
        const std::shared_ptr<sigrok::OutputFormat> output_format_;
        const std::map<std::string, Glib::VariantBase> options_;
        const std::string file_name_;
        const std::shared_ptr<sigrok::OutputFormat> output_format_;
        const std::map<std::string, Glib::VariantBase> options_;
+       const std::pair<uint64_t, uint64_t> sample_range_;
        const Session &session_;
 
        std::shared_ptr<sigrok::Output> output_;
        const Session &session_;
 
        std::shared_ptr<sigrok::Output> output_;
@@ -96,6 +98,8 @@ private:
 
        mutable std::mutex mutex_;
        QString error_;
 
        mutable std::mutex mutex_;
        QString error_;
+
+       uint64_t start_sample_, sample_count_;
 };
 
 } // pv
 };
 
 } // pv