diff --git a/core/include/Variable/Variable.h b/core/include/Variable/Variable.h index 88c5682..1941339 100644 --- a/core/include/Variable/Variable.h +++ b/core/include/Variable/Variable.h @@ -4,6 +4,7 @@ #include "CoreGlobal.h" #include +#include #include #include @@ -54,6 +55,9 @@ public: /// @return the data of the variable, nullptr if there is no data std::shared_ptr dataSeries() const noexcept; + /// @return the type of data that the variable holds + DataSeriesType type() const noexcept; + QVariantHash metadata() const noexcept; bool contains(const SqpRange &range) const noexcept; diff --git a/core/src/Variable/Variable.cpp b/core/src/Variable/Variable.cpp index 8d68fa0..b9869d0 100644 --- a/core/src/Variable/Variable.cpp +++ b/core/src/Variable/Variable.cpp @@ -9,6 +9,29 @@ Q_LOGGING_CATEGORY(LOG_Variable, "Variable") +namespace { + +/** + * Searches in metadata for a value that can be converted to DataSeriesType + * @param metadata the metadata where to search + * @return the value converted to a DataSeriesType if it was found, UNKNOWN type otherwise + * @sa DataSeriesType + */ +DataSeriesType findDataSeriesType(const QVariantHash &metadata) +{ + auto dataSeriesType = DataSeriesType::UNKNOWN; + + // Go through the metadata and stop at the first value that could be converted to DataSeriesType + for (auto it = metadata.cbegin(), end = metadata.cend(); + it != end && dataSeriesType == DataSeriesType::UNKNOWN; ++it) { + dataSeriesType = DataSeriesTypeUtils::fromString(it.value().toString()); + } + + return dataSeriesType; +} + +} // namespace + struct Variable::VariablePrivate { explicit VariablePrivate(const QString &name, const QVariantHash &metadata) : m_Name{name}, @@ -17,7 +40,8 @@ struct Variable::VariablePrivate { m_Metadata{metadata}, m_DataSeries{nullptr}, m_RealRange{INVALID_RANGE}, - m_NbPoints{0} + m_NbPoints{0}, + m_Type{findDataSeriesType(m_Metadata)} { } @@ -28,7 +52,8 @@ struct Variable::VariablePrivate { m_Metadata{other.m_Metadata}, m_DataSeries{other.m_DataSeries != nullptr ? other.m_DataSeries->clone() : nullptr}, m_RealRange{other.m_RealRange}, - m_NbPoints{other.m_NbPoints} + m_NbPoints{other.m_NbPoints}, + m_Type{findDataSeriesType(m_Metadata)} { } @@ -75,6 +100,7 @@ struct Variable::VariablePrivate { std::shared_ptr m_DataSeries; SqpRange m_RealRange; int m_NbPoints; + DataSeriesType m_Type; QReadWriteLock m_Lock; }; @@ -183,6 +209,15 @@ std::shared_ptr Variable::dataSeries() const noexcept return dataSeries; } +DataSeriesType Variable::type() const noexcept +{ + impl->lockRead(); + auto type = impl->m_Type; + impl->unlock(); + + return type; +} + QVariantHash Variable::metadata() const noexcept { impl->lockRead();