diff --git a/include/Data/DateTimeRangeHelper.h b/include/Data/DateTimeRangeHelper.h index 68780ba..afef041 100644 --- a/include/Data/DateTimeRangeHelper.h +++ b/include/Data/DateTimeRangeHelper.h @@ -45,16 +45,17 @@ namespace DateTimeRangeHelper { * @return trnaformation applied to range1 to get range2 or an object of type * InvalidDateTimeRangeTransformation if the transformation has NaN or forbiden values */ - inline std::variant + inline std::optional computeTransformation(const DateTimeRange& range1, const DateTimeRange& range2) { + std::optional transformation; double zoom = range2.delta()/range1.delta(); Seconds shift = range2.center() - (range1*zoom).center(); bool zoomValid = zoom!=0. && !std::isnan(zoom) && !std::isinf(zoom); bool shiftValid = !std::isnan(shift.value) && !std::isinf(shift.value); if(zoomValid && shiftValid) - return DateTimeRangeTransformation{zoom, shift}; - return InvalidDateTimeRangeTransformation{}; + transformation = DateTimeRangeTransformation{zoom, shift}; + return transformation; } } diff --git a/include/Variable/Variable.h b/include/Variable/Variable.h index b161229..baaa6e1 100644 --- a/include/Variable/Variable.h +++ b/include/Variable/Variable.h @@ -52,7 +52,7 @@ public: /// @return the real range, invalid range if the data series is null or empty /// @sa setDataSeries() /// @sa setRange() - DateTimeRange realRange() const noexcept; + std::optional realRange() const noexcept; /// @return the data of the variable, nullptr if there is no data std::shared_ptr dataSeries() const noexcept; diff --git a/include/Variable/VariableController2.h b/include/Variable/VariableController2.h index 2e5d848..4861c4e 100644 --- a/include/Variable/VariableController2.h +++ b/include/Variable/VariableController2.h @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -7,7 +8,7 @@ #include #include #include -#include +//#include #include #include #include "Data/DateTimeRange.h" diff --git a/src/Variable/Variable.cpp b/src/Variable/Variable.cpp index 3a735f4..7e4b4d3 100644 --- a/src/Variable/Variable.cpp +++ b/src/Variable/Variable.cpp @@ -1,11 +1,13 @@ +#include +#include +#include +#include + #include "Variable/Variable.h" #include #include -#include -#include -#include #include Q_LOGGING_CATEGORY(LOG_Variable, "Variable") @@ -33,6 +35,29 @@ DataSeriesType findDataSeriesType(const QVariantHash &metadata) } // namespace +#define VP_PROPERTY(property,getter,setter,type) \ + type getter() noexcept\ + {\ + QReadLocker lock{&m_Lock};\ + return property;\ + }\ + void setter(const type& getter) noexcept\ + {\ + QWriteLocker lock{&m_Lock};\ + property = getter;\ + }\ + type property;\ + +#define V_FW_GETTER_SETTER(getter,setter, type)\ + type Variable::getter() const noexcept \ + {\ + return impl->getter();\ + }\ + void Variable::setter(const type& getter) noexcept \ + {\ + impl->setter(getter);\ + }\ + struct Variable::VariablePrivate { explicit VariablePrivate(const QString &name, const QVariantHash &metadata) : m_Name{name}, @@ -89,20 +114,18 @@ struct Variable::VariablePrivate { m_DataSeries->unlock(); } else { - m_RealRange = INVALID_RANGE; + m_RealRange = std::nullopt; } } - QString m_Name; - - DateTimeRange m_Range; - DateTimeRange m_CacheRange; - QVariantHash m_Metadata; - std::shared_ptr m_DataSeries; - DateTimeRange m_RealRange; + VP_PROPERTY(m_Name, name, setName, QString) + VP_PROPERTY(m_Range, range, setRange, DateTimeRange) + VP_PROPERTY(m_CacheRange, cacheRange, setCacheRange, DateTimeRange) + VP_PROPERTY(m_Metadata, metadata, setMetadata, QVariantHash) + VP_PROPERTY(m_DataSeries, dataSeries, setDataSeries, std::shared_ptr) + VP_PROPERTY(m_RealRange, realRange, setRealRange, std::optional) unsigned int m_NbPoints; - DataSeriesType m_Type; - + VP_PROPERTY(m_Type, type, setType, DataSeriesType) QReadWriteLock m_Lock; }; @@ -123,64 +146,31 @@ std::shared_ptr Variable::clone() const return std::make_shared(*this); } -QString Variable::name() const noexcept -{ - impl->lockRead(); - auto name = impl->m_Name; - impl->unlock(); - return name; -} - -void Variable::setName(const QString &name) noexcept -{ - impl->lockWrite(); - impl->m_Name = name; - impl->unlock(); -} +V_FW_GETTER_SETTER(name,setName,QString) DateTimeRange Variable::range() const noexcept { - impl->lockRead(); - auto range = impl->m_Range; - impl->unlock(); - return range; + return impl->range(); } void Variable::setRange(const DateTimeRange &range, bool notify) noexcept { - impl->lockWrite(); - impl->m_Range = range; + impl->setRange(range); impl->updateRealRange(); - impl->unlock(); if(notify) emit this->updated(); } -DateTimeRange Variable::cacheRange() const noexcept -{ - impl->lockRead(); - auto cacheRange = impl->m_CacheRange; - impl->unlock(); - return cacheRange; -} - -void Variable::setCacheRange(const DateTimeRange &cacheRange) noexcept -{ - impl->lockWrite(); - if (cacheRange != impl->m_CacheRange) { - impl->m_CacheRange = cacheRange; - } - impl->unlock(); -} +V_FW_GETTER_SETTER(cacheRange, setCacheRange, DateTimeRange) unsigned int Variable::nbPoints() const noexcept { return impl->m_NbPoints; } -DateTimeRange Variable::realRange() const noexcept +std::optional Variable::realRange() const noexcept { - return impl->m_RealRange; + return impl->realRange(); } void Variable::mergeDataSeries(std::shared_ptr dataSeries) noexcept @@ -244,20 +234,12 @@ void Variable::mergeDataSeries(IDataSeries *dataSeries, bool notify) noexcept std::shared_ptr Variable::dataSeries() const noexcept { - impl->lockRead(); - auto dataSeries = impl->m_DataSeries; - impl->unlock(); - - return dataSeries; + return impl->dataSeries(); } DataSeriesType Variable::type() const noexcept { - impl->lockRead(); - auto type = impl->m_Type; - impl->unlock(); - - return type; + return impl->type(); } QVariantHash Variable::metadata() const noexcept diff --git a/src/Variable/VariableController2.cpp b/src/Variable/VariableController2.cpp index 730e2de..ab5a1ac 100644 --- a/src/Variable/VariableController2.cpp +++ b/src/Variable/VariableController2.cpp @@ -80,20 +80,25 @@ public: SCIQLOP_ERROR(VariableController2Private, "No provider found for given variable"); } + void asyncChangeRange(const std::shared_ptr& variable, const DateTimeRange& r) + { + + } + void changeRange(const std::shared_ptr& variable, DateTimeRange r) { if(p_contains(variable)) { if(!DateTimeRangeHelper::hasnan(r)) { - auto transformation = DateTimeRangeHelper::computeTransformation(variable->range(),r); auto group = _synchronizationGroups[variable->ID()]; - if(std::holds_alternative(transformation)) + if(auto transformation = DateTimeRangeHelper::computeTransformation(variable->range(),r); + transformation.has_value()) { for(auto varId:group->variables()) { auto var = _variables[varId]; - auto newRange = var->range().transform(std::get(transformation)); + auto newRange = var->range().transform(transformation.value()); _changeRange(var,newRange); } } @@ -178,6 +183,11 @@ void VariableController2::changeRange(const std::shared_ptr& variable, impl->changeRange(variable, r); } +void VariableController2::asyncChangeRange(const std::shared_ptr &variable, const DateTimeRange &r) +{ + impl->asyncChangeRange(variable, r); +} + const std::set > VariableController2::variables() { return impl->variables(); diff --git a/src/Variable/VariableModel.cpp b/src/Variable/VariableModel.cpp index bb96f8d..11054ca 100644 --- a/src/Variable/VariableModel.cpp +++ b/src/Variable/VariableModel.cpp @@ -196,16 +196,14 @@ QVariant VariableModel::data(const QModelIndex &index, int role) const case NAME_COLUMN: return variable->name(); case TSTART_COLUMN: { - auto range = variable->realRange(); - return range != INVALID_RANGE - ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT) - : QVariant{}; + if(auto range = variable->realRange(); range.has_value()) + return DateUtils::dateTime(range.value().m_TStart).toString(DATETIME_FORMAT); + return QVariant{}; } case TEND_COLUMN: { - auto range = variable->realRange(); - return range != INVALID_RANGE - ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT) - : QVariant{}; + if(auto range = variable->realRange(); range.has_value()) + return DateUtils::dateTime(range.value().m_TEnd).toString(DATETIME_FORMAT); + return QVariant{}; } case NBPOINTS_COLUMN: return variable->nbPoints(); diff --git a/tests/Data/TestDateTimeRange.cpp b/tests/Data/TestDateTimeRange.cpp index 0cc7f60..4eb434e 100644 --- a/tests/Data/TestDateTimeRange.cpp +++ b/tests/Data/TestDateTimeRange.cpp @@ -180,7 +180,7 @@ private slots: QFETCH(DateTimeRange,range1); QFETCH(DateTimeRange,range2); QFETCH(DateTimeRangeTransformation, transformation); - auto computed_tr = std::get(DateTimeRangeHelper::computeTransformation(range1,range2)); + auto computed_tr = DateTimeRangeHelper::computeTransformation(range1,range2).value(); QCOMPARE(computed_tr, transformation); QCOMPARE(range1.transform(transformation),range2); QCOMPARE(range1.transform(computed_tr),range2);