# HG changeset patch # User Lucas Thompson # Date 1523955830 -3600 # Node ID 51493540a753a61f137aa12cd3889cc5d248561b # Parent 04caefd353913488c31e4442fdff2c7ea96d99e7 Fix progress reporting and allow for changing how progress is calculating (TODO multi-selection) diff -r 04caefd35391 -r 51493540a753 data/fileio/CSVStreamWriter.h --- a/data/fileio/CSVStreamWriter.h Tue Apr 17 10:03:50 2018 +0100 +++ b/data/fileio/CSVStreamWriter.h Tue Apr 17 10:03:50 2018 +0100 @@ -24,23 +24,42 @@ #include #include +namespace +{ + const auto initProgressCalculator = [](sv_frame_t nFramesToWrite) { + return [nFramesToWrite](sv_frame_t nFramesWritten) { + return 100 * nFramesWritten / nFramesToWrite; + }; + }; +} // namespace + namespace CSVStreamWriter { -template -bool writeInChunks(OutStream& oss, - const Model& model, - const Selection& extents, - ProgressReporter* reporter = nullptr, - QString delimiter = ",", - DataExportOptions options = DataExportDefaults, - const sv_frame_t blockSize = 16384) +template < + class OutStream, + class ProgressCalculatorInit = decltype(initProgressCalculator) +> +bool +writeInChunks(OutStream& oss, + const Model& model, + const Selection& extents, + ProgressReporter* reporter = nullptr, + QString delimiter = ",", + DataExportOptions options = DataExportDefaults, + const sv_frame_t blockSize = 16384, + const ProgressCalculatorInit& initCalc = initProgressCalculator) { if (blockSize <= 0) return false; - sv_frame_t readPtr = extents.isEmpty() ? + const auto startFrame = extents.isEmpty() ? model.getStartFrame() : extents.getStartFrame(); - sv_frame_t endFrame = extents.isEmpty() ? + const auto endFrame = extents.isEmpty() ? model.getEndFrame() : extents.getEndFrame(); + const auto hasValidExtents = startFrame >= 0 && endFrame > startFrame; + if (!hasValidExtents) return false; + const auto calculateProgress = initCalc(endFrame - startFrame); + + auto readPtr = startFrame; int previousPercentagePoint = 0; const auto wasCancelled = [&reporter]() { @@ -59,8 +78,8 @@ start, end ) << (end < endFrame ? "\n" : ""); - - const auto currentPercentage = 100 * end / endFrame; + const auto nFramesWritten = end - startFrame; + const auto currentPercentage = calculateProgress(nFramesWritten); const bool hasIncreased = currentPercentage > previousPercentagePoint; if (hasIncreased) { @@ -73,12 +92,13 @@ } template -bool writeInChunks(OutStream& oss, - const Model& model, - ProgressReporter* reporter = nullptr, - QString delimiter = ",", - DataExportOptions options = DataExportDefaults, - const sv_frame_t blockSize = 16384) +bool +writeInChunks(OutStream& oss, + const Model& model, + ProgressReporter* reporter = nullptr, + QString delimiter = ",", + DataExportOptions options = DataExportDefaults, + const sv_frame_t blockSize = 16384) { const Selection empty; return CSV::writeInChunks(