comparison data/fileio/CSVFileWriter.cpp @ 1442:3c3eedc2fad8 streaming-csv-writer

Implement progress reporting for multi-selection. Needs refactoring.
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:50 +0100
parents 09c2ba31a711
children cdb316c6a151
comparison
equal deleted inserted replaced
1441:51493540a753 1442:3c3eedc2fad8
27 #include "base/Selection.h" 27 #include "base/Selection.h"
28 28
29 #include <QFile> 29 #include <QFile>
30 #include <QTextStream> 30 #include <QTextStream>
31 #include <exception> 31 #include <exception>
32 #include <numeric>
32 33
33 CSVFileWriter::CSVFileWriter(QString path, 34 CSVFileWriter::CSVFileWriter(QString path,
34 Model *model, 35 Model *model,
35 QString delimiter, 36 QString delimiter,
36 DataExportOptions options) : 37 DataExportOptions options) :
85 86
86 QTextStream out(&file); 87 QTextStream out(&file);
87 88
88 bool completed = false; 89 bool completed = false;
89 90
91 const auto nFramesToWrite = std::accumulate(
92 selection->getSelections().begin(),
93 selection->getSelections().end(),
94 0,
95 [](sv_frame_t acc, const Selection& current) -> sv_frame_t {
96 return acc + (current.getEndFrame() - current.getStartFrame());
97 }
98 );
99
100 sv_frame_t nFramesWritten = 0;
101 const auto createProgressCalculator = [
102 &nFramesWritten,
103 &nFramesToWrite
104 ](sv_frame_t nFramesToWriteForSelection) {
105 const auto nFramesWrittenAtSelectionStart = nFramesWritten;
106 nFramesWritten += nFramesToWriteForSelection;
107 return [
108 &nFramesWritten,
109 &nFramesToWrite,
110 nFramesWrittenAtSelectionStart
111 ] (sv_frame_t nFramesWrittenForSelection) {
112 const auto nFramesWrittenSoFar = (
113 nFramesWrittenAtSelectionStart + nFramesWrittenForSelection
114 );
115 return 100 * nFramesWrittenSoFar / nFramesToWrite;
116 };
117 };
118
90 for (const auto& bounds : selection->getSelections()) { 119 for (const auto& bounds : selection->getSelections()) {
91 completed = CSVStreamWriter::writeInChunks( 120 completed = CSV::writeToStreamInChunks(
92 out, 121 out,
93 *m_model, 122 *m_model,
94 bounds, 123 bounds,
95 m_reporter, 124 m_reporter,
96 m_delimiter, 125 m_delimiter,
97 m_options 126 m_options,
127 16384,
128 createProgressCalculator
98 ); 129 );
99 if (!completed) { 130 if (!completed) {
100 break; 131 break;
101 } 132 }
102 } 133 }