From 2e59750a3b2a2507e68a79c68ca88ec5936e7b8d 2017-09-28 09:12:37 From: Alexandre Leroux Date: 2017-09-28 09:12:37 Subject: [PATCH] Merge branch 'feature/CosinusProvider2' into develop --- diff --git a/plugins/mockplugin/include/CosinusProvider.h b/plugins/mockplugin/include/CosinusProvider.h index 1ef2c6e..2260032 100644 --- a/plugins/mockplugin/include/CosinusProvider.h +++ b/plugins/mockplugin/include/CosinusProvider.h @@ -27,8 +27,8 @@ public: private: - std::shared_ptr retrieveData(QUuid acqIdentifier, - const SqpRange &dataRangeRequested); + std::shared_ptr + retrieveData(QUuid acqIdentifier, const SqpRange &dataRangeRequested, const QVariantHash &data); QHash m_VariableToEnableProvider; }; diff --git a/plugins/mockplugin/include/MockDefs.h b/plugins/mockplugin/include/MockDefs.h new file mode 100644 index 0000000..02c23e6 --- /dev/null +++ b/plugins/mockplugin/include/MockDefs.h @@ -0,0 +1,23 @@ +#ifndef SCIQLOP_MOCKDEFS_H +#define SCIQLOP_MOCKDEFS_H + +#include "MockPluginGlobal.h" + +#include +#include + +// ////////////// // +// Mock constants // +// ////////////// // + +// Metadata for cosinus provider // + +/// Cosinus frequency (Hz) +extern SCIQLOP_MOCKPLUGIN_EXPORT const QString COSINUS_FREQUENCY_KEY; +extern SCIQLOP_MOCKPLUGIN_EXPORT const QVariant COSINUS_FREQUENCY_DEFAULT_VALUE; + +/// Cosinus type ("scalar" or "vector") +extern SCIQLOP_MOCKPLUGIN_EXPORT const QString COSINUS_TYPE_KEY; +extern SCIQLOP_MOCKPLUGIN_EXPORT const QVariant COSINUS_TYPE_DEFAULT_VALUE; + +#endif // SCIQLOP_MOCKDEFS_H diff --git a/plugins/mockplugin/src/CosinusProvider.cpp b/plugins/mockplugin/src/CosinusProvider.cpp index bab6302..ed23a60 100644 --- a/plugins/mockplugin/src/CosinusProvider.cpp +++ b/plugins/mockplugin/src/CosinusProvider.cpp @@ -1,7 +1,9 @@ #include "CosinusProvider.h" +#include "MockDefs.h" #include #include +#include #include @@ -11,6 +13,60 @@ Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") +namespace { + +/// Abstract cosinus type +struct ICosinusType { + virtual ~ICosinusType() = default; + /// @return the number of components generated for the type + virtual int componentCount() const = 0; + /// @return the data series created for the type + virtual std::shared_ptr createDataSeries(std::vector xAxisData, + std::vector valuesData, + Unit xAxisUnit, + Unit valuesUnit) const = 0; +}; + +struct ScalarCosinus : public ICosinusType { + int componentCount() const override { return 1; } + + std::shared_ptr createDataSeries(std::vector xAxisData, + std::vector valuesData, Unit xAxisUnit, + Unit valuesUnit) const override + { + return std::make_shared(std::move(xAxisData), std::move(valuesData), + xAxisUnit, valuesUnit); + } +}; +struct VectorCosinus : public ICosinusType { + int componentCount() const override { return 3; } + + std::shared_ptr createDataSeries(std::vector xAxisData, + std::vector valuesData, Unit xAxisUnit, + Unit valuesUnit) const override + { + return std::make_shared(std::move(xAxisData), std::move(valuesData), + xAxisUnit, valuesUnit); + } +}; + +/// Converts string to cosinus type +/// @return the cosinus type if the string could be converted, nullptr otherwise +std::unique_ptr cosinusType(const QString &type) noexcept +{ + if (type.compare(QStringLiteral("scalar"), Qt::CaseInsensitive) == 0) { + return std::make_unique(); + } + else if (type.compare(QStringLiteral("vector"), Qt::CaseInsensitive) == 0) { + return std::make_unique(); + } + else { + return nullptr; + } +} + +} // namespace + std::shared_ptr CosinusProvider::clone() const { // No copy is made in clone @@ -18,15 +74,36 @@ std::shared_ptr CosinusProvider::clone() const } std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, - const SqpRange &dataRangeRequested) + const SqpRange &dataRangeRequested, + const QVariantHash &data) { // TODO: Add Mutex auto dataIndex = 0; + // Retrieves cosinus type + auto typeVariant = data.value(COSINUS_TYPE_KEY, COSINUS_TYPE_DEFAULT_VALUE); + if (!typeVariant.canConvert()) { + qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid type"); + return nullptr; + } + + auto type = cosinusType(typeVariant.toString()); + if (!type) { + qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: unknown type"); + return nullptr; + } + + // Retrieves frequency + auto freqVariant = data.value(COSINUS_FREQUENCY_KEY, COSINUS_FREQUENCY_DEFAULT_VALUE); + if (!freqVariant.canConvert()) { + qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid frequency"); + return nullptr; + } + // Gets the timerange from the parameters - double freq = 100.0; - double start = std::ceil(dataRangeRequested.m_TStart * freq); // 100 htz - double end = std::floor(dataRangeRequested.m_TEnd * freq); // 100 htz + double freq = freqVariant.toDouble(); + double start = std::ceil(dataRangeRequested.m_TStart * freq); + double end = std::floor(dataRangeRequested.m_TEnd * freq); // We assure that timerange is valid if (end < start) { @@ -37,11 +114,14 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, // included) auto dataCount = end - start + 1; + // Number of components (depending on the cosinus type) + auto componentCount = type->componentCount(); + auto xAxisData = std::vector{}; xAxisData.resize(dataCount); auto valuesData = std::vector{}; - valuesData.resize(dataCount); + valuesData.resize(dataCount * componentCount); int progress = 0; auto progressEnd = dataCount; @@ -51,7 +131,13 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, const auto timeOnFreq = time / freq; xAxisData[dataIndex] = timeOnFreq; - valuesData[dataIndex] = std::cos(timeOnFreq); + + // Generates all components' values + // Example: for a vector, values will be : cos(x), cos(x)/2, cos(x)/3 + auto value = std::cos(timeOnFreq); + for (auto i = 0; i < componentCount; ++i) { + valuesData[componentCount * dataIndex + i] = value / (i + 1); + } // progression int currentProgress = (time - start) * 100.0 / progressEnd; @@ -76,8 +162,8 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, // We can close progression beacause all data has been retrieved emit dataProvidedProgress(acqIdentifier, 100); } - return std::make_shared(std::move(xAxisData), std::move(valuesData), - Unit{QStringLiteral("t"), true}, Unit{}); + return type->createDataSeries(std::move(xAxisData), std::move(valuesData), + Unit{QStringLiteral("t"), true}, Unit{}); } void CosinusProvider::requestDataLoading(QUuid acqIdentifier, @@ -92,7 +178,7 @@ void CosinusProvider::requestDataLoading(QUuid acqIdentifier, for (const auto &dateTime : qAsConst(times)) { if (m_VariableToEnableProvider[acqIdentifier]) { - auto scalarSeries = this->retrieveData(acqIdentifier, dateTime); + auto scalarSeries = this->retrieveData(acqIdentifier, dateTime, parameters.m_Data); qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::dataProvided"; emit dataProvided(acqIdentifier, scalarSeries, dateTime); } diff --git a/plugins/mockplugin/src/MockDefs.cpp b/plugins/mockplugin/src/MockDefs.cpp new file mode 100644 index 0000000..75d4574 --- /dev/null +++ b/plugins/mockplugin/src/MockDefs.cpp @@ -0,0 +1,7 @@ +#include "MockDefs.h" + +const QString COSINUS_FREQUENCY_KEY = QStringLiteral("cosinusFrequency"); +const QVariant COSINUS_FREQUENCY_DEFAULT_VALUE = 60.; + +const QString COSINUS_TYPE_KEY = QStringLiteral("cosinusType"); +const QVariant COSINUS_TYPE_DEFAULT_VALUE = QStringLiteral("scalar"); diff --git a/plugins/mockplugin/src/MockPlugin.cpp b/plugins/mockplugin/src/MockPlugin.cpp index f11ebbd..684ce94 100644 --- a/plugins/mockplugin/src/MockPlugin.cpp +++ b/plugins/mockplugin/src/MockPlugin.cpp @@ -1,5 +1,6 @@ #include "MockPlugin.h" #include "CosinusProvider.h" +#include "MockDefs.h" #include #include @@ -20,10 +21,11 @@ std::unique_ptr createDataProvider() noexcept return std::make_unique(); } -std::unique_ptr createProductItem(const QString &productName, +std::unique_ptr createProductItem(const QVariantHash &data, const QUuid &dataSourceUid) { - auto result = std::make_unique(DataSourceItemType::PRODUCT, productName); + auto result = std::make_unique(DataSourceItemType::PRODUCT, data); + auto productName = data.value(DataSourceItem::NAME_DATA_KEY).toString(); // Add action to load product from DataSourceController result->addAction(std::make_unique( @@ -43,8 +45,36 @@ std::unique_ptr createDataSourceItem(const QUuid &dataSourceUid) // Magnetic field products auto magneticFieldFolder = std::make_unique(DataSourceItemType::NODE, QStringLiteral("Magnetic field")); - magneticFieldFolder->appendChild(createProductItem(QStringLiteral("FGM"), dataSourceUid)); - magneticFieldFolder->appendChild(createProductItem(QStringLiteral("SC"), dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 10 Hz")}, + {COSINUS_TYPE_KEY, "scalar"}, + {COSINUS_FREQUENCY_KEY, 10.}}, + dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 60 Hz")}, + {COSINUS_TYPE_KEY, "scalar"}, + {COSINUS_FREQUENCY_KEY, 60.}}, + dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Scalar 100 Hz")}, + {COSINUS_TYPE_KEY, "scalar"}, + {COSINUS_FREQUENCY_KEY, 100.}}, + dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 10 Hz")}, + {COSINUS_TYPE_KEY, "vector"}, + {COSINUS_FREQUENCY_KEY, 10.}}, + dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 60 Hz")}, + {COSINUS_TYPE_KEY, "vector"}, + {COSINUS_FREQUENCY_KEY, 60.}}, + dataSourceUid)); + magneticFieldFolder->appendChild( + createProductItem({{DataSourceItem::NAME_DATA_KEY, QStringLiteral("Vector 100 Hz")}, + {COSINUS_TYPE_KEY, "vector"}, + {COSINUS_FREQUENCY_KEY, 100.}}, + dataSourceUid)); // Electric field products auto electricFieldFolder = std::make_unique(DataSourceItemType::NODE, diff --git a/plugins/mockplugin/tests/TestCosinusAcquisition.cpp b/plugins/mockplugin/tests/TestCosinusAcquisition.cpp index c178b92..472a671 100644 --- a/plugins/mockplugin/tests/TestCosinusAcquisition.cpp +++ b/plugins/mockplugin/tests/TestCosinusAcquisition.cpp @@ -1,4 +1,5 @@ #include "CosinusProvider.h" +#include "MockDefs.h" #include #include @@ -158,7 +159,8 @@ void TestCosinusAcquisition::testAcquisition() QFETCH(SqpRange, initialRange); sqpApp->timeController().onTimeToUpdate(initialRange); auto provider = std::make_shared(); - auto variable = sqpApp->variableController().createVariable("MMS", {}, provider); + auto variable = sqpApp->variableController().createVariable( + "MMS", {{COSINUS_TYPE_KEY, "scalar"}, {COSINUS_FREQUENCY_KEY, 100.}}, provider); QTest::qWait(OPERATION_DELAY); validateVariable(variable, initialRange);