From 7fcd08eb22c453014093e7dc5e2c903b2f1b08f3 2017-11-16 10:22:49 From: Alexandre Leroux Date: 2017-11-16 10:22:49 Subject: [PATCH] Spectrograms implementation (3) Reads properties --- diff --git a/plugins/amda/include/AmdaResultParserHelper.h b/plugins/amda/include/AmdaResultParserHelper.h index 606cd00..14ec217 100644 --- a/plugins/amda/include/AmdaResultParserHelper.h +++ b/plugins/amda/include/AmdaResultParserHelper.h @@ -73,6 +73,9 @@ public: std::shared_ptr createSeries() override; void readPropertyLine(const QString &line) override; void readResultLine(const QString &line) override; + +private: + Properties m_Properties{}; }; /** diff --git a/plugins/amda/src/AmdaResultParserHelper.cpp b/plugins/amda/src/AmdaResultParserHelper.cpp index 446e98e..00000cb 100644 --- a/plugins/amda/src/AmdaResultParserHelper.cpp +++ b/plugins/amda/src/AmdaResultParserHelper.cpp @@ -154,6 +154,54 @@ bool tryReadProperty(Properties &properties, const QString &key, const QString & } /** + * Reads a line from the AMDA file and tries to extract a double from it + * @sa tryReadProperty() + */ +bool tryReadDouble(Properties &properties, const QString &key, const QString &line, + const QRegularExpression ®ex) +{ + return tryReadProperty(properties, key, line, regex, [](const auto &match) { + bool ok; + + // If the value can't be converted to double, it is set to NaN + auto doubleValue = match.captured(1).toDouble(&ok); + if (!ok) { + doubleValue = std::numeric_limits::quiet_NaN(); + } + + return QVariant::fromValue(doubleValue); + }); +} + +/** + * Reads a line from the AMDA file and tries to extract a vector of doubles from it + * @param sep the separator of double values in the line + * @sa tryReadProperty() + */ +bool tryReadDoubles(Properties &properties, const QString &key, const QString &line, + const QRegularExpression ®ex, const QString &sep = QStringLiteral(",")) +{ + return tryReadProperty(properties, key, line, regex, [sep](const auto &match) { + std::vector doubleValues{}; + + // If the value can't be converted to double, it is set to NaN + auto values = match.captured(1).split(sep); + for (auto value : values) { + bool ok; + + auto doubleValue = value.toDouble(&ok); + if (!ok) { + doubleValue = std::numeric_limits::quiet_NaN(); + } + + doubleValues.push_back(doubleValue); + } + + return QVariant::fromValue(doubleValues); + }); +} + +/** * Reads a line from the AMDA file and tries to extract a unit from it * @sa tryReadProperty() */ @@ -207,7 +255,17 @@ std::vector ScalarParserHelper::valuesIndexes() const bool SpectrogramParserHelper::checkProperties() { - /// @todo ALX + // Generates y-axis data from bands extracted (take the middle of the intervals) + auto minBands = m_Properties.value(MIN_BANDS_PROPERTY).value >(); + auto maxBands = m_Properties.value(MAX_BANDS_PROPERTY).value >(); + + if (minBands.size() != maxBands.size()) { + qCWarning(LOG_AmdaResultParserHelper()) << QObject::tr( + "Can't generate y-axis data from bands extracted: bands intervals are invalid"); + return false; + } + + return true; } std::shared_ptr SpectrogramParserHelper::createSeries() @@ -217,7 +275,52 @@ std::shared_ptr SpectrogramParserHelper::createSeries() void SpectrogramParserHelper::readPropertyLine(const QString &line) { - /// @todo ALX + // Set of functions to test on the line to generate a property. If a function is valid (i.e. a + // property has been generated for the line), the line is treated as processed and the other + // functions are not called + std::vector > functions{ + // values unit + [&] { + return tryReadUnit(m_Properties, VALUES_UNIT_PROPERTY, line, + SPECTROGRAM_VALUES_UNIT_REGEX); + }, + // y-axis unit + [&] { + return tryReadUnit(m_Properties, Y_AXIS_UNIT_PROPERTY, line, + SPECTROGRAM_Y_AXIS_UNIT_REGEX); + }, + // min sampling + [&] { + return tryReadDouble(m_Properties, MIN_SAMPLING_PROPERTY, line, + SPECTROGRAM_MIN_SAMPLING_REGEX); + }, + // max sampling + [&] { + return tryReadDouble(m_Properties, MAX_SAMPLING_PROPERTY, line, + SPECTROGRAM_MAX_SAMPLING_REGEX); + }, + // fill value + [&] { + return tryReadDouble(m_Properties, FILL_VALUE_PROPERTY, line, + SPECTROGRAM_FILL_VALUE_REGEX); + }, + // min bounds of each band + [&] { + return tryReadDoubles(m_Properties, MIN_BANDS_PROPERTY, line, + SPECTROGRAM_MIN_BANDS_REGEX); + }, + // max bounds of each band + [&] { + return tryReadDoubles(m_Properties, MAX_BANDS_PROPERTY, line, + SPECTROGRAM_MAX_BANDS_REGEX); + }}; + + for (auto function : functions) { + // Stops at the first function that is valid + if (function()) { + return; + } + } } void SpectrogramParserHelper::readResultLine(const QString &line)