From: Soeren Apel Date: Wed, 4 Nov 2015 15:33:07 +0000 (+0100) Subject: Introduce "save selection range as..." feature X-Git-Tag: pulseview-0.3.0~56 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d2fc6be9af3ba409032da6dcabc4630c657bb56c;p=pulseview.git Introduce "save selection range as..." feature --- diff --git a/pv/dialogs/storeprogress.cpp b/pv/dialogs/storeprogress.cpp index a7f889e0..5aeafd14 100644 --- a/pv/dialogs/storeprogress.cpp +++ b/pv/dialogs/storeprogress.cpp @@ -35,9 +35,11 @@ namespace dialogs { StoreProgress::StoreProgress(const QString &file_name, const std::shared_ptr output_format, const map &options, + const std::pair sample_range, 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())); diff --git a/pv/dialogs/storeprogress.hpp b/pv/dialogs/storeprogress.hpp index 19aef14a..37916420 100644 --- a/pv/dialogs/storeprogress.hpp +++ b/pv/dialogs/storeprogress.hpp @@ -42,7 +42,9 @@ public: StoreProgress(const QString &file_name, const std::shared_ptr output_format, const std::map &options, - const Session &session, QWidget *parent = 0); + const std::pair sample_range, + const Session &session, + QWidget *parent = 0); virtual ~StoreProgress(); diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 8b32046a..195c460a 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -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)), @@ -259,8 +262,10 @@ void MainWindow::export_file(shared_ptr format) options = dlg.options(); } + const std::pair sample_range = std::make_pair(0, 0); + StoreProgress *dlg = new StoreProgress(file_name, format, options, - session_, this); + sample_range, session_, this); 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_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, @@ -649,6 +661,87 @@ void MainWindow::load_file(QString file_name, 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 d : session_.get_data()) { + assert(d); + const vector< shared_ptr > segments = + d->segments(); + for (const shared_ptr &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() * samplerate; + const uint64_t end_sample = end_time.convert_to() * samplerate; + + const std::pair 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 format = + device_manager_.context()->output_formats()["srzip"]; + + const vector 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 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(); @@ -699,6 +792,11 @@ void MainWindow::on_actionSaveAs_triggered() 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 diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index e86dd183..94fc092a 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -84,6 +84,7 @@ public: 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; @@ -130,6 +131,8 @@ private: const std::map &options = std::map()); + void save_selection_to_file(); + private: void closeEvent(QCloseEvent *event); @@ -141,6 +144,7 @@ private Q_SLOTS: void on_actionOpen_triggered(); void on_actionSaveAs_triggered(); + void on_actionSaveSelectionAs_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_save_selection_as_; QAction *const action_connect_; QAction *const action_quit_; QAction *const action_view_zoom_in_; diff --git a/pv/storesession.cpp b/pv/storesession.cpp index c7c2edb0..309fa1b3 100644 --- a/pv/storesession.cpp +++ b/pv/storesession.cpp @@ -71,10 +71,13 @@ const size_t StoreSession::BlockSize = 1024 * 1024; StoreSession::StoreSession(const std::string &file_name, const shared_ptr &output_format, - const map &options, const Session &session) : + const map &options, + const std::pair sample_range, + const Session &session) : file_name_(file_name), output_format_(output_format), options_(options), + sample_range_(sample_range), session_(session), interrupt_(false), units_stored_(0), @@ -141,6 +144,15 @@ bool StoreSession::start() const shared_ptr 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(); @@ -181,7 +193,6 @@ void StoreSession::store_proc(shared_ptr 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 @@ -191,26 +202,25 @@ void StoreSession::store_proc(shared_ptr segment) 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. - while ((sample_count >> progress_scale) > INT_MAX) + while ((sample_count_ >> progress_scale) > INT_MAX) progress_scale ++; - unit_count_ = sample_count >> progress_scale; + unit_count_ = sample_count_ >> progress_scale; const unsigned int samples_per_block = BlockSize / unit_size; - while (!interrupt_ && start_sample < sample_count) + while (!interrupt_ && sample_count_) { 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(); @@ -223,8 +233,9 @@ void StoreSession::store_proc(shared_ptr segment) 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 diff --git a/pv/storesession.hpp b/pv/storesession.hpp index 9f31d59a..6a081801 100644 --- a/pv/storesession.hpp +++ b/pv/storesession.hpp @@ -59,6 +59,7 @@ public: StoreSession(const std::string &file_name, const std::shared_ptr &output_format, const std::map &options, + const std::pair sample_range, const Session &session); ~StoreSession(); @@ -83,6 +84,7 @@ private: const std::string file_name_; const std::shared_ptr output_format_; const std::map options_; + const std::pair sample_range_; const Session &session_; std::shared_ptr output_; @@ -96,6 +98,8 @@ private: mutable std::mutex mutex_; QString error_; + + uint64_t start_sample_, sample_count_; }; } // pv