comparison data/fileio/CSVFileWriter.cpp @ 1445:4437209348ef streaming-csv-writer

Introduce overload of writeInChunks taking a MultiSelection. Simplifies the progress reporting.
author Lucas Thompson <dev@lucas.im>
date Tue, 17 Apr 2018 10:03:50 +0100
parents cdb316c6a151
children a12fd0456f0c
comparison
equal deleted inserted replaced
1444:4507be815384 1445:4437209348ef
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>
33 32
34 CSVFileWriter::CSVFileWriter(QString path, 33 CSVFileWriter::CSVFileWriter(QString path,
35 Model *model, 34 Model *model,
36 QString delimiter, 35 QString delimiter,
37 DataExportOptions options) : 36 DataExportOptions options) :
84 return; 83 return;
85 } 84 }
86 85
87 QTextStream out(&file); 86 QTextStream out(&file);
88 87
89 bool completed = false; 88 bool completed = CSVStreamWriter::writeInChunks(
90 89 out,
91 const auto nFramesToWrite = std::accumulate( 90 *m_model,
92 selection->getSelections().begin(), 91 *selection,
93 selection->getSelections().end(), 92 m_reporter,
94 0, 93 m_delimiter,
95 [](sv_frame_t acc, const Selection& current) -> sv_frame_t { 94 m_options
96 return acc + (current.getEndFrame() - current.getStartFrame());
97 }
98 ); 95 );
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
119 for (const auto& bounds : selection->getSelections()) {
120 completed = CSVStreamWriter::writeInChunks(
121 out,
122 *m_model,
123 bounds,
124 m_reporter,
125 m_delimiter,
126 m_options,
127 16384,
128 createProgressCalculator
129 );
130 if (!completed) {
131 break;
132 }
133 }
134 96
135 file.close(); 97 file.close();
136 if (completed) { 98 if (completed) {
137 temp.moveToTarget(); 99 temp.moveToTarget();
138 } 100 }