-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWorker.cpp
More file actions
147 lines (130 loc) · 3.36 KB
/
Copy pathFileWorker.cpp
File metadata and controls
147 lines (130 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-FileCopyrightText: 2026 Błażej Szczygieł <mumei6102@gmail.com>
// SPDX-License-Identifier: BSD-3-Clause
#include "FileWorker.hpp"
FileWorker::FileWorker(QObject *parent)
: SpectrometerWorker(parent)
{
}
FileWorker::~FileWorker()
{
}
bool FileWorker::setParam(const QString &key, const QVariant &value)
{
if (!isRunning())
{
if (key == u"path"_sv)
{
m_path = value.toString();
return true;
}
else if (key == u"col"_sv)
{
m_col = value.toInt();
return true;
}
else if (key == u"min"_sv)
{
m_hintMinNm = value.toDouble();
return true;
}
else if (key == u"max"_sv)
{
m_hintMaxNm = value.toDouble();
return true;
}
else if (key == u"b64"_sv)
{
m_b64Name = value.toBool();
return true;
}
}
return false;
}
QString FileWorker::staticDataName() const
{
const QFileInfo fi(m_path);
if (m_b64Name)
{
return QString::fromUtf8(QByteArray::fromBase64(fi.baseName().toLatin1(), QByteArray::Base64UrlEncoding));
}
return fi.fileName();
}
void FileWorker::run()
{
auto selfDestroy = qScopeGuard([this] {
deleteLater();
});
if (m_path.isEmpty())
{
return;
}
QFile f(m_path);
if (f.size() > pow(2.0, 24.0))
{
emit errorMesssage(tr("File too large"));
return;
}
if (!f.open(QFile::ReadOnly | QFile::Text))
{
emit errorMesssage(tr("Unable to open the file"));
return;
}
Data data;
const QRegularExpression splitRx(uR"(\,|\ |\;)"_s);
while (!f.atEnd())
{
const auto line = QString::fromLatin1(f.readLine().trimmed());
if (line.startsWith(sDateKey))
{
m_date = QDateTime::fromString(line.mid(sDateKey.size() + 2), Qt::ISODate);
continue;
}
else if (line.startsWith(sDeviceKey))
{
m_deviceId = line.mid(sDeviceKey.size() + 2);
continue;
}
else if (line.startsWith(sExposureKey))
{
const auto exposureSplitted = line.mid(sExposureKey.size() + 2).split(u' ', Qt::SkipEmptyParts);
if (exposureSplitted.value(1) == u"ms"_sv)
{
m_exposure.time = exposureSplitted.value(0).toDouble();
emitNewExposureData();
}
continue;
}
const auto lineValues = line.split(splitRx, Qt::SkipEmptyParts);
if (lineValues.size() >= 2)
{
bool ok1 = false;
bool ok2 = false;
Entry entry {
lineValues[0].toDouble(&ok1),
lineValues.value(m_col).toDouble(&ok2),
};
if (ok1 && ok2)
{
data.emplace_back(entry);
}
}
}
if (data.size() < 2)
{
emit errorMesssage(tr("No data in the file"));
return;
}
if (!ranges::is_sorted(data, {}, &Entry::first))
{
emit errorMesssage(tr("Data is not sorted"));
return;
}
m_minNm = data.constFirst().first;
m_maxNm = data.constLast().first;
if (m_minNm <= 0 || m_maxNm <= m_minNm)
{
emit errorMesssage(tr("Wrong data"));
return;
}
processSpd(data);
}