comparison data/fileio/CSVFileReader.cpp @ 1491:1dc64d3d323c import-audio-data

Support progress reporter for data file loads
author Chris Cannam
date Fri, 29 Jun 2018 13:44:53 +0100
parents 9ab8e5ec4731
children 6989d6215640
comparison
equal deleted inserted replaced
1490:9ab8e5ec4731 1491:1dc64d3d323c
16 #include "CSVFileReader.h" 16 #include "CSVFileReader.h"
17 17
18 #include "model/Model.h" 18 #include "model/Model.h"
19 #include "base/RealTime.h" 19 #include "base/RealTime.h"
20 #include "base/StringBits.h" 20 #include "base/StringBits.h"
21 #include "base/ProgressReporter.h"
21 #include "model/SparseOneDimensionalModel.h" 22 #include "model/SparseOneDimensionalModel.h"
22 #include "model/SparseTimeValueModel.h" 23 #include "model/SparseTimeValueModel.h"
23 #include "model/EditableDenseThreeDimensionalModel.h" 24 #include "model/EditableDenseThreeDimensionalModel.h"
24 #include "model/RegionModel.h" 25 #include "model/RegionModel.h"
25 #include "model/NoteModel.h" 26 #include "model/NoteModel.h"
38 #include <string> 39 #include <string>
39 40
40 using namespace std; 41 using namespace std;
41 42
42 CSVFileReader::CSVFileReader(QString path, CSVFormat format, 43 CSVFileReader::CSVFileReader(QString path, CSVFormat format,
43 sv_samplerate_t mainModelSampleRate) : 44 sv_samplerate_t mainModelSampleRate,
45 ProgressReporter *reporter) :
44 m_format(format), 46 m_format(format),
45 m_device(0), 47 m_device(0),
46 m_ownDevice(true), 48 m_ownDevice(true),
47 m_warnings(0), 49 m_warnings(0),
48 m_mainModelSampleRate(mainModelSampleRate) 50 m_mainModelSampleRate(mainModelSampleRate),
51 m_fileSize(0),
52 m_readCount(0),
53 m_progress(0),
54 m_reporter(reporter)
49 { 55 {
50 QFile *file = new QFile(path); 56 QFile *file = new QFile(path);
51 bool good = false; 57 bool good = false;
52 58
53 if (!file->exists()) { 59 if (!file->exists()) {
59 } 65 }
60 66
61 if (good) { 67 if (good) {
62 m_device = file; 68 m_device = file;
63 m_filename = QFileInfo(path).fileName(); 69 m_filename = QFileInfo(path).fileName();
70 m_fileSize = file->size();
71 if (m_reporter) m_reporter->setDefinite(true);
64 } else { 72 } else {
65 delete file; 73 delete file;
66 } 74 }
67 } 75 }
68 76
69 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format, 77 CSVFileReader::CSVFileReader(QIODevice *device, CSVFormat format,
70 sv_samplerate_t mainModelSampleRate) : 78 sv_samplerate_t mainModelSampleRate,
79 ProgressReporter *reporter) :
71 m_format(format), 80 m_format(format),
72 m_device(device), 81 m_device(device),
73 m_ownDevice(false), 82 m_ownDevice(false),
74 m_warnings(0), 83 m_warnings(0),
75 m_mainModelSampleRate(mainModelSampleRate) 84 m_mainModelSampleRate(mainModelSampleRate),
76 { 85 m_fileSize(0),
86 m_readCount(0),
87 m_progress(0),
88 m_reporter(reporter)
89 {
90 if (m_reporter) m_reporter->setDefinite(false);
77 } 91 }
78 92
79 CSVFileReader::~CSVFileReader() 93 CSVFileReader::~CSVFileReader()
80 { 94 {
81 SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl; 95 SVDEBUG << "CSVFileReader::~CSVFileReader: device is " << m_device << endl;
228 // splitting it. For CR and CR/LF line endings this will just 242 // splitting it. For CR and CR/LF line endings this will just
229 // read a line at a time, and that's obviously OK. 243 // read a line at a time, and that's obviously OK.
230 244
231 QString chunk = in.readLine(); 245 QString chunk = in.readLine();
232 QStringList lines = chunk.split('\r', QString::SkipEmptyParts); 246 QStringList lines = chunk.split('\r', QString::SkipEmptyParts);
247
248 m_readCount += chunk.size() + 1;
249
250 if (m_reporter) {
251 if (m_reporter->wasCancelled()) {
252 break;
253 }
254 int progress;
255 if (m_fileSize > 0) {
256 progress = int((double(m_readCount) / double(m_fileSize))
257 * 100.0);
258 } else {
259 progress = int(m_readCount / 10000);
260 }
261 if (progress != m_progress) {
262 m_reporter->setProgress(progress);
263 m_progress = progress;
264 }
265 }
233 266
234 for (int li = 0; li < lines.size(); ++li) { 267 for (int li = 0; li < lines.size(); ++li) {
235 268
236 QString line = lines[li]; 269 QString line = lines[li];
237 270