From 9a7b9cf5359edf2c0ea674167b3e8eb59a9aa3ae 2017-08-16 12:54:32 From: leroux Date: 2017-08-16 12:54:32 Subject: [PATCH] Merge pull request #222 from SCIQLOP-Initialisation develop Develop --- diff --git a/core/include/Data/AcquisitionDataPacket.h b/core/include/Data/AcquisitionDataPacket.h new file mode 100644 index 0000000..3e02ba6 --- /dev/null +++ b/core/include/Data/AcquisitionDataPacket.h @@ -0,0 +1,25 @@ +#ifndef SCIQLOP_ACQUISITIONDATAPACKET_H +#define SCIQLOP_ACQUISITIONDATAPACKET_H + +#include + +#include +#include +#include +#include + +#include + +/** + * @brief The AcquisitionDataPacket struct holds the information of an acquisition request result + * part. + */ +struct AcquisitionDataPacket { + std::shared_ptr m_DateSeries; + SqpRange m_Range; +}; + +SCIQLOP_REGISTER_META_TYPE(ACQUISITIONDATAPACKET_REGISTRY, AcquisitionDataPacket) +SCIQLOP_REGISTER_META_TYPE(ACQUISITIONDATAPACKETVECTOR_REGISTRY, QVector) + +#endif // SCIQLOP_ACQUISITIONREQUEST_H diff --git a/core/include/Data/AcquisitionRequest.h b/core/include/Data/AcquisitionRequest.h new file mode 100644 index 0000000..f1cc89d --- /dev/null +++ b/core/include/Data/AcquisitionRequest.h @@ -0,0 +1,38 @@ +#ifndef SCIQLOP_ACQUISITIONREQUEST_H +#define SCIQLOP_ACQUISITIONREQUEST_H + +#include + +#include + +#include +#include +#include +#include +#include + +#include + +/** + * @brief The AcquisitionRequest struct holds the information of an acquisition request + */ +struct AcquisitionRequest { + AcquisitionRequest() + { + + m_AcqIdentifier = QUuid::createUuid(); + m_Size = 0; + } + + QUuid m_AcqIdentifier; + QUuid m_vIdentifier; + DataProviderParameters m_DataProviderParameters; + SqpRange m_RangeRequested; + SqpRange m_CacheRangeRequested; + int m_Size; + std::shared_ptr m_Provider; +}; + +SCIQLOP_REGISTER_META_TYPE(ACQUISITIONREQUEST_REGISTRY, AcquisitionRequest) + +#endif // SCIQLOP_ACQUISITIONREQUEST_H diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index f6cbde5..3b95662 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -43,6 +43,16 @@ public: /// @sa IDataSeries::valuesUnit() Unit valuesUnit() const override { return m_ValuesUnit; } + + SqpRange range() const override + { + if (!m_XAxisData->cdata().isEmpty()) { + return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; + } + + return SqpRange{}; + } + void clear() { m_XAxisData->clear(); diff --git a/core/include/Data/IDataProvider.h b/core/include/Data/IDataProvider.h index a819162..805348d 100644 --- a/core/include/Data/IDataProvider.h +++ b/core/include/Data/IDataProvider.h @@ -34,35 +34,37 @@ public: virtual ~IDataProvider() noexcept = default; /** - * @brief requestDataLoading provide datas for the data identified by identifier and parameters + * @brief requestDataLoading provide datas for the data identified by acqIdentifier and + * parameters */ - virtual void requestDataLoading(QUuid identifier, const DataProviderParameters ¶meters) = 0; + virtual void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) + = 0; /** - * @brief requestDataAborting stop data loading of the data identified by identifier + * @brief requestDataAborting stop data loading of the data identified by acqIdentifier */ - virtual void requestDataAborting(QUuid identifier) = 0; + virtual void requestDataAborting(QUuid acqIdentifier) = 0; signals: /** * @brief dataProvided send dataSeries under dateTime and that corresponds of the data - * identified by identifier + * identified by acqIdentifier */ - void dataProvided(QUuid identifier, std::shared_ptr dateSerie, - const SqpRange &dateTime); + void dataProvided(QUuid acqIdentifier, std::shared_ptr dateSeriesAcquired, + const SqpRange &dataRangeAcquired); /** * @brief dataProvided send dataSeries under dateTime and that corresponds of the data * identified by identifier */ - void dataProvidedProgress(QUuid identifier, double progress); + void dataProvidedProgress(QUuid acqIdentifier, double progress); /** - * @brief requestConstructed send a request for the data identified by identifier + * @brief requestConstructed send a request for the data identified by acqIdentifier * @callback is the methode call by the reply of the request when it is finished. */ - void requestConstructed(const QNetworkRequest &request, QUuid identifier, + void requestConstructed(const QNetworkRequest &request, QUuid acqIdentifier, std::function callback); }; diff --git a/core/include/Data/IDataSeries.h b/core/include/Data/IDataSeries.h index 2710e1b..b66ae68 100644 --- a/core/include/Data/IDataSeries.h +++ b/core/include/Data/IDataSeries.h @@ -2,6 +2,7 @@ #define SCIQLOP_IDATASERIES_H #include +#include #include @@ -55,8 +56,10 @@ public: virtual Unit valuesUnit() const = 0; virtual void merge(IDataSeries *dataSeries) = 0; + virtual std::shared_ptr subData(const SqpRange &range) = 0; virtual std::unique_ptr clone() const = 0; + virtual SqpRange range() const = 0; virtual void lockRead() = 0; virtual void lockWrite() = 0; diff --git a/core/include/Data/ScalarSeries.h b/core/include/Data/ScalarSeries.h index ce301f4..9183e3d 100644 --- a/core/include/Data/ScalarSeries.h +++ b/core/include/Data/ScalarSeries.h @@ -19,7 +19,9 @@ public: explicit ScalarSeries(QVector xAxisData, QVector valuesData, const Unit &xAxisUnit, const Unit &valuesUnit); - std::unique_ptr clone() const; + std::unique_ptr clone() const override; + + std::shared_ptr subData(const SqpRange &range) override; }; #endif // SCIQLOP_SCALARSERIES_H diff --git a/core/include/Settings/SqpSettingsDefs.h b/core/include/Settings/SqpSettingsDefs.h index 5d110fe..e9f6423 100644 --- a/core/include/Settings/SqpSettingsDefs.h +++ b/core/include/Settings/SqpSettingsDefs.h @@ -9,6 +9,10 @@ // General settings // // //////////////// // + +struct SCIQLOP_CORE_EXPORT SqpSettings { + static double toleranceValue(const QString &key, double defaultValue) noexcept; +}; extern SCIQLOP_CORE_EXPORT const QString GENERAL_TOLERANCE_AT_INIT_KEY; extern SCIQLOP_CORE_EXPORT const double GENERAL_TOLERANCE_AT_INIT_DEFAULT_VALUE; diff --git a/core/include/Variable/Variable.h b/core/include/Variable/Variable.h index dcea562..e67ba68 100644 --- a/core/include/Variable/Variable.h +++ b/core/include/Variable/Variable.h @@ -28,20 +28,27 @@ public: const QVariantHash &metadata = {}); QString name() const noexcept; - SqpRange dateTime() const noexcept; - void setDateTime(const SqpRange &dateTime) noexcept; + SqpRange range() const noexcept; + void setRange(const SqpRange &range) noexcept; + SqpRange cacheRange() const noexcept; + void setCacheRange(const SqpRange &cacheRange) noexcept; /// @return the data of the variable, nullptr if there is no data - IDataSeries *dataSeries() const noexcept; + std::shared_ptr dataSeries() const noexcept; QVariantHash metadata() const noexcept; - bool contains(const SqpRange &dateTime) const noexcept; - bool intersect(const SqpRange &dateTime) const noexcept; - bool isInside(const SqpRange &dateTime) const noexcept; + bool contains(const SqpRange &range) const noexcept; + bool intersect(const SqpRange &range) const noexcept; + bool isInside(const SqpRange &range) const noexcept; -public slots: + bool cacheContains(const SqpRange &range) const noexcept; + bool cacheIntersect(const SqpRange &range) const noexcept; + bool cacheIsInside(const SqpRange &range) const noexcept; + + QVector provideNotInCacheRangeList(const SqpRange &range) const noexcept; void setDataSeries(std::shared_ptr dataSeries) noexcept; + void mergeDataSeries(std::shared_ptr dataSeries) noexcept; signals: void updated(); @@ -53,5 +60,6 @@ private: // Required for using shared_ptr in signals/slots SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr) +SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_VECTOR_REGISTRY, QVector >) #endif // SCIQLOP_VARIABLE_H diff --git a/core/include/Variable/VariableAcquisitionWorker.h b/core/include/Variable/VariableAcquisitionWorker.h new file mode 100644 index 0000000..d2adf83 --- /dev/null +++ b/core/include/Variable/VariableAcquisitionWorker.h @@ -0,0 +1,61 @@ +#ifndef SCIQLOP_VARIABLEACQUISITIONWORKER_H +#define SCIQLOP_VARIABLEACQUISITIONWORKER_H + +#include "CoreGlobal.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +Q_DECLARE_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker) + +class Variable; +class IDataProvider; + +/// This class aims to handle all acquisition request +class SCIQLOP_CORE_EXPORT VariableAcquisitionWorker : public QObject { + Q_OBJECT +public: + explicit VariableAcquisitionWorker(QObject *parent = 0); + virtual ~VariableAcquisitionWorker(); + + void pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequested, + SqpRange cacheRangeRequested, DataProviderParameters parameters, + std::shared_ptr provider); + + void abortProgressRequested(QUuid vIdentifier); + + void initialize(); + void finalize(); +signals: + void dataProvided(QUuid vIdentifier, const SqpRange &rangeRequested, + const SqpRange &cacheRangeRequested, + QVector dataAcquired); + + void variableRequestInProgress(QUuid vIdentifier, double progress); + +public slots: + void onVariableDataAcquired(QUuid acqIdentifier, std::shared_ptr dataSeries, + SqpRange dataRangeAcquired); + void onVariableRetrieveDataInProgress(QUuid acqIdentifier, double progress); + +private slots: + void onExecuteRequest(QUuid acqIdentifier); + +private: + void waitForFinish(); + + class VariableAcquisitionWorkerPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_VARIABLEACQUISITIONWORKER_H diff --git a/core/include/Variable/VariableCacheStrategy.h b/core/include/Variable/VariableCacheStrategy.h new file mode 100644 index 0000000..50f417e --- /dev/null +++ b/core/include/Variable/VariableCacheStrategy.h @@ -0,0 +1,40 @@ +#ifndef SCIQLOP_VARIABLECACHESTRATEGY_H +#define SCIQLOP_VARIABLECACHESTRATEGY_H + +#include "CoreGlobal.h" + +#include +#include + +#include + +#include + +#include +#include + + +Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheStrategy) + +class Variable; + +/** + * Possible types of zoom operation + */ +enum class CacheStrategy { FixedTolerance, TwoThreashold }; + +/// This class aims to hande the cache strategy. +class SCIQLOP_CORE_EXPORT VariableCacheStrategy : public QObject { + Q_OBJECT +public: + explicit VariableCacheStrategy(QObject *parent = 0); + + std::pair computeCacheRange(const SqpRange &vRange, + const SqpRange &rangeRequested); + +private: + class VariableCacheStrategyPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_VARIABLECACHESTRATEGY_H diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index d37a9f7..cf14962 100644 --- a/core/include/Variable/VariableController.h +++ b/core/include/Variable/VariableController.h @@ -3,6 +3,7 @@ #include "CoreGlobal.h" +#include #include #include @@ -18,6 +19,13 @@ class VariableModel; Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController) + +/** + * Possible types of zoom operation + */ +enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown }; + + /** * @brief The VariableController class aims to handle the variables in SciQlop. */ @@ -57,6 +65,7 @@ public: */ void abortProgress(std::shared_ptr variable); + static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange); signals: /// Signal emitted when a variable is about to be deleted from the controller void variableAboutToBeDeleted(std::shared_ptr variable); @@ -65,8 +74,9 @@ signals: void rangeChanged(std::shared_ptr variable, const SqpRange &range); public slots: - /// Request the data loading of the variable whithin dateTime - void onRequestDataLoading(std::shared_ptr variable, const SqpRange &dateTime); + /// Request the data loading of the variable whithin range + void onRequestDataLoading(QVector > variables, const SqpRange &range, + const SqpRange &oldRange, bool synchronise); /** * Creates a new variable and adds it to the model * @param name the name of the new variable @@ -80,10 +90,20 @@ public slots: void onDateTimeOnSelection(const SqpRange &dateTime); + void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested, + const SqpRange &cacheRangeRequested, + QVector dataAcquired); + void onVariableRetrieveDataInProgress(QUuid identifier, double progress); + /// Cancel the current request for the variable void onAbortProgressRequested(std::shared_ptr variable); + /// synchronization group methods + void onAddSynchronizationGroupId(QUuid synchronizationGroupId); + void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId); + void onAddSynchronized(std::shared_ptr variable, QUuid synchronizationGroupId); + void initialize(); void finalize(); diff --git a/core/include/Variable/VariableSynchronizationGroup.h b/core/include/Variable/VariableSynchronizationGroup.h new file mode 100644 index 0000000..ffb5ce8 --- /dev/null +++ b/core/include/Variable/VariableSynchronizationGroup.h @@ -0,0 +1,38 @@ +#ifndef SCIQLOP_VARIABLESYNCHRONIZATIONGROUP_H +#define SCIQLOP_VARIABLESYNCHRONIZATIONGROUP_H + +#include "CoreGlobal.h" + +#include +#include +#include + +#include + +#include + +#include + +#include + +Q_DECLARE_LOGGING_CATEGORY(LOG_VariableSynchronizationGroup) + +class Variable; + +/// This class aims to hande the cache strategy. +class SCIQLOP_CORE_EXPORT VariableSynchronizationGroup : public QObject { + Q_OBJECT +public: + explicit VariableSynchronizationGroup(QObject *parent = 0); + + void addVariableId(QUuid vIdentifier); + void removeVariableId(QUuid vIdentifier); + + const std::set &getIds() const noexcept; + +private: + class VariableSynchronizationGroupPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_VARIABLESYNCHRONIZATIONGROUP_H diff --git a/core/src/Data/ScalarSeries.cpp b/core/src/Data/ScalarSeries.cpp index 8080c67..9732921 100644 --- a/core/src/Data/ScalarSeries.cpp +++ b/core/src/Data/ScalarSeries.cpp @@ -11,3 +11,31 @@ std::unique_ptr ScalarSeries::clone() const { return std::make_unique(*this); } + +std::shared_ptr ScalarSeries::subData(const SqpRange &range) +{ + auto subXAxisData = QVector(); + auto subValuesData = QVector(); + this->lockRead(); + { + const auto ¤tXData = this->xAxisData()->cdata(); + const auto ¤tValuesData = this->valuesData()->cdata(); + + auto xDataBegin = currentXData.cbegin(); + auto xDataEnd = currentXData.cend(); + + auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, range.m_TStart); + auto upperIt = std::upper_bound(xDataBegin, xDataEnd, range.m_TEnd); + auto distance = std::distance(xDataBegin, lowerIt); + + auto valuesDataIt = currentValuesData.cbegin() + distance; + for (auto xAxisDataIt = lowerIt; xAxisDataIt != upperIt; ++xAxisDataIt, ++valuesDataIt) { + subXAxisData.append(*xAxisDataIt); + subValuesData.append(*valuesDataIt); + } + } + this->unlock(); + + return std::make_shared(subXAxisData, subValuesData, this->xAxisUnit(), + this->valuesUnit()); +} diff --git a/core/src/Network/NetworkController.cpp b/core/src/Network/NetworkController.cpp index ddf4654..1649cc5 100644 --- a/core/src/Network/NetworkController.cpp +++ b/core/src/Network/NetworkController.cpp @@ -13,15 +13,16 @@ Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") struct NetworkController::NetworkControllerPrivate { explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {} + + void lockRead() { m_Lock.lockForRead(); } + void lockWrite() { m_Lock.lockForWrite(); } + void unlock() { m_Lock.unlock(); } + QMutex m_WorkingMutex; QReadWriteLock m_Lock; std::unordered_map m_NetworkReplyToVariableId; std::unique_ptr m_AccessManager{nullptr}; - - void lockRead() { m_Lock.lockForRead(); } - void lockWrite() { m_Lock.lockForWrite(); } - void unlock() { m_Lock.unlock(); } }; NetworkController::NetworkController(QObject *parent) diff --git a/core/src/Settings/SqpSettingsDefs.cpp b/core/src/Settings/SqpSettingsDefs.cpp index f0f6b44..69a36e2 100644 --- a/core/src/Settings/SqpSettingsDefs.cpp +++ b/core/src/Settings/SqpSettingsDefs.cpp @@ -1,5 +1,16 @@ #include "Settings/SqpSettingsDefs.h" +#include + + +/// Gets a tolerance value from application settings. If the setting can't be found, the default +/// value passed in parameter is returned +double SqpSettings::toleranceValue(const QString &key, double defaultValue) noexcept +{ + return QSettings{}.value(key, defaultValue).toDouble(); +} + + const QString GENERAL_TOLERANCE_AT_INIT_KEY = QStringLiteral("toleranceInit"); const double GENERAL_TOLERANCE_AT_INIT_DEFAULT_VALUE = 0.2; diff --git a/core/src/Variable/Variable.cpp b/core/src/Variable/Variable.cpp index c64269d..04fac1c 100644 --- a/core/src/Variable/Variable.cpp +++ b/core/src/Variable/Variable.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -11,15 +12,22 @@ Q_LOGGING_CATEGORY(LOG_Variable, "Variable") struct Variable::VariablePrivate { explicit VariablePrivate(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata) - : m_Name{name}, m_DateTime{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr} + : m_Name{name}, m_Range{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr} { } + void lockRead() { m_Lock.lockForRead(); } + void lockWrite() { m_Lock.lockForWrite(); } + void unlock() { m_Lock.unlock(); } + QString m_Name; - SqpRange m_DateTime; // The dateTime available in the view and loaded. not the cache. + SqpRange m_Range; + SqpRange m_CacheRange; QVariantHash m_Metadata; - std::unique_ptr m_DataSeries; + std::shared_ptr m_DataSeries; + + QReadWriteLock m_Lock; }; Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata) @@ -29,58 +37,175 @@ Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariant QString Variable::name() const noexcept { - return impl->m_Name; + impl->lockRead(); + auto name = impl->m_Name; + impl->unlock(); + return name; +} + +SqpRange Variable::range() const noexcept +{ + impl->lockRead(); + auto range = impl->m_Range; + impl->unlock(); + return range; } -SqpRange Variable::dateTime() const noexcept +void Variable::setRange(const SqpRange &range) noexcept { - return impl->m_DateTime; + impl->lockWrite(); + impl->m_Range = range; + impl->unlock(); } -void Variable::setDateTime(const SqpRange &dateTime) noexcept +SqpRange Variable::cacheRange() const noexcept { - impl->m_DateTime = dateTime; + impl->lockRead(); + auto cacheRange = impl->m_CacheRange; + impl->unlock(); + return cacheRange; +} + +void Variable::setCacheRange(const SqpRange &cacheRange) noexcept +{ + impl->lockWrite(); + impl->m_CacheRange = cacheRange; + impl->unlock(); } void Variable::setDataSeries(std::shared_ptr dataSeries) noexcept { - qCDebug(LOG_Variable()) << "Variable::setDataSeries" << QThread::currentThread()->objectName(); + qCDebug(LOG_Variable()) << "TORM Variable::setDataSeries" + << QThread::currentThread()->objectName(); if (!dataSeries) { /// @todo ALX : log return; } + impl->lockWrite(); + impl->m_DataSeries = dataSeries->clone(); + impl->unlock(); +} +void Variable::mergeDataSeries(std::shared_ptr dataSeries) noexcept +{ + qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries" + << QThread::currentThread()->objectName(); + if (!dataSeries) { + /// @todo ALX : log + return; + } + + // Add or merge the data // Inits the data series of the variable + impl->lockWrite(); if (!impl->m_DataSeries) { impl->m_DataSeries = dataSeries->clone(); } else { impl->m_DataSeries->merge(dataSeries.get()); - // emit updated(); } + impl->unlock(); + + // sub the data + auto subData = this->dataSeries()->subData(this->cacheRange()); + qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries sub" << subData->range(); + this->setDataSeries(subData); + qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries set" << this->dataSeries()->range(); } -IDataSeries *Variable::dataSeries() const noexcept +std::shared_ptr Variable::dataSeries() const noexcept { - return impl->m_DataSeries.get(); + impl->lockRead(); + auto dataSeries = impl->m_DataSeries; + impl->unlock(); + + return dataSeries; } QVariantHash Variable::metadata() const noexcept { - return impl->m_Metadata; + impl->lockRead(); + auto metadata = impl->m_Metadata; + impl->unlock(); + return metadata; } -bool Variable::contains(const SqpRange &dateTime) const noexcept +bool Variable::contains(const SqpRange &range) const noexcept { - return impl->m_DateTime.contains(dateTime); + impl->lockRead(); + auto res = impl->m_Range.contains(range); + impl->unlock(); + return res; } -bool Variable::intersect(const SqpRange &dateTime) const noexcept +bool Variable::intersect(const SqpRange &range) const noexcept { - return impl->m_DateTime.intersect(dateTime); + + impl->lockRead(); + auto res = impl->m_Range.intersect(range); + impl->unlock(); + return res; +} + +bool Variable::isInside(const SqpRange &range) const noexcept +{ + impl->lockRead(); + auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd}); + impl->unlock(); + return res; +} + +bool Variable::cacheContains(const SqpRange &range) const noexcept +{ + impl->lockRead(); + auto res = impl->m_CacheRange.contains(range); + impl->unlock(); + return res; +} + +bool Variable::cacheIntersect(const SqpRange &range) const noexcept +{ + impl->lockRead(); + auto res = impl->m_CacheRange.intersect(range); + impl->unlock(); + return res; +} + +bool Variable::cacheIsInside(const SqpRange &range) const noexcept +{ + impl->lockRead(); + auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd}); + impl->unlock(); + return res; } -bool Variable::isInside(const SqpRange &dateTime) const noexcept + +QVector Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept { - return dateTime.contains(SqpRange{impl->m_DateTime.m_TStart, impl->m_DateTime.m_TEnd}); + auto notInCache = QVector{}; + + if (!this->cacheContains(range)) { + if (range.m_TEnd <= impl->m_CacheRange.m_TStart + || range.m_TStart >= impl->m_CacheRange.m_TEnd) { + notInCache << range; + } + else if (range.m_TStart < impl->m_CacheRange.m_TStart + && range.m_TEnd <= impl->m_CacheRange.m_TEnd) { + notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}; + } + else if (range.m_TStart < impl->m_CacheRange.m_TStart + && range.m_TEnd > impl->m_CacheRange.m_TEnd) { + notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart} + << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; + } + else if (range.m_TStart < impl->m_CacheRange.m_TEnd) { + notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; + } + else { + qCCritical(LOG_Variable()) << tr("Detection of unknown case.") + << QThread::currentThread(); + } + } + + return notInCache; } diff --git a/core/src/Variable/VariableAcquisitionWorker.cpp b/core/src/Variable/VariableAcquisitionWorker.cpp new file mode 100644 index 0000000..e84ab16 --- /dev/null +++ b/core/src/Variable/VariableAcquisitionWorker.cpp @@ -0,0 +1,225 @@ +#include "Variable/VariableAcquisitionWorker.h" + +#include "Variable/Variable.h" + +#include +#include + +#include +#include + +#include +#include +#include + +Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker") + +struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { + + explicit VariableAcquisitionWorkerPrivate() : m_Lock{QReadWriteLock::Recursive} {} + + void lockRead() { m_Lock.lockForRead(); } + void lockWrite() { m_Lock.lockForWrite(); } + void unlock() { m_Lock.unlock(); } + + void removeVariableRequest(QUuid vIdentifier); + + QMutex m_WorkingMutex; + QReadWriteLock m_Lock; + + std::map > m_AcqIdentifierToAcqDataPacketVectorMap; + std::map m_AcqIdentifierToAcqRequestMap; + std::map > m_VIdentifierToCurrrentAcqIdNextIdPairMap; +}; + + +VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent) + : QObject{parent}, impl{spimpl::make_unique_impl()} +{ +} + +VariableAcquisitionWorker::~VariableAcquisitionWorker() +{ + qCInfo(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker destruction") + << QThread::currentThread(); + this->waitForFinish(); +} + + +void VariableAcquisitionWorker::pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequested, + SqpRange cacheRangeRequested, + DataProviderParameters parameters, + std::shared_ptr provider) +{ + qCInfo(LOG_VariableAcquisitionWorker()) + << tr("TORM VariableAcquisitionWorker::pushVariableRequest ") << cacheRangeRequested; + + // Request creation + auto acqRequest = AcquisitionRequest{}; + acqRequest.m_vIdentifier = vIdentifier; + acqRequest.m_DataProviderParameters = parameters; + acqRequest.m_RangeRequested = rangeRequested; + acqRequest.m_CacheRangeRequested = cacheRangeRequested; + acqRequest.m_Size = parameters.m_Times.size(); + acqRequest.m_Provider = provider; + + // Register request + impl->lockWrite(); + impl->m_AcqIdentifierToAcqRequestMap.insert( + std::make_pair(acqRequest.m_AcqIdentifier, acqRequest)); + + auto it = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); + if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + // A current request already exists, we can replace the next one + it->second.second = acqRequest.m_AcqIdentifier; + impl->unlock(); + } + else { + // First request for the variable, it must be stored and executed + impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.insert( + std::make_pair(vIdentifier, std::make_pair(acqRequest.m_AcqIdentifier, QUuid()))); + impl->unlock(); + + QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection, + Q_ARG(QUuid, acqRequest.m_AcqIdentifier)); + } +} + +void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier) +{ + // TODO +} + +void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier, + double progress) +{ + // TODO +} + +void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier, + std::shared_ptr dataSeries, + SqpRange dataRangeAcquired) +{ + qCDebug(LOG_VariableAcquisitionWorker()) << tr("onVariableDataAcquired on range ") + << acqIdentifier << dataRangeAcquired; + impl->lockWrite(); + auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); + if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) { + // Store the result + auto dataPacket = AcquisitionDataPacket{}; + dataPacket.m_Range = dataRangeAcquired; + dataPacket.m_DateSeries = dataSeries; + + auto aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier); + if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) { + // A current request result already exists, we can update it + aIdToADPVit->second.push_back(dataPacket); + } + else { + // First request result for the variable, it must be stored + impl->m_AcqIdentifierToAcqDataPacketVectorMap.insert( + std::make_pair(acqIdentifier, QVector() << dataPacket)); + } + + + // Decrement the counter of the request + auto &acqRequest = aIdToARit->second; + acqRequest.m_Size = acqRequest.m_Size - 1; + + // if the counter is 0, we can return data then run the next request if it exists and + // removed the finished request + if (acqRequest.m_Size == 0) { + // Return the data + aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier); + if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) { + emit dataProvided(acqRequest.m_vIdentifier, acqRequest.m_RangeRequested, + acqRequest.m_CacheRangeRequested, aIdToADPVit->second); + } + + // Execute the next one + auto it + = impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(acqRequest.m_vIdentifier); + + if (it != impl->m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + if (it->second.second.isNull()) { + // There is no next request, we can remove the varibale request + impl->removeVariableRequest(acqRequest.m_vIdentifier); + } + else { + auto acqIdentifierToRemove = it->second.first; + // Move the next request to the current request + it->second.first = it->second.second; + it->second.second = QUuid(); + // Remove AcquisitionRequest and results; + impl->m_AcqIdentifierToAcqRequestMap.erase(acqIdentifierToRemove); + impl->m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqIdentifierToRemove); + // Execute the current request + QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection, + Q_ARG(QUuid, it->second.first)); + } + } + else { + qCCritical(LOG_VariableAcquisitionWorker()) + << tr("Impossible to execute the acquisition on an unfound variable "); + } + } + } + else { + qCCritical(LOG_VariableAcquisitionWorker()) + << tr("Impossible to retrieve AcquisitionRequest for the incoming data"); + } + impl->unlock(); +} + +void VariableAcquisitionWorker::onExecuteRequest(QUuid acqIdentifier) +{ + qCInfo(LOG_VariableAcquisitionWorker()) << tr("onExecuteRequest") << QThread::currentThread(); + impl->lockRead(); + auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); + if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) { + auto request = it->second; + impl->unlock(); + request.m_Provider->requestDataLoading(acqIdentifier, request.m_DataProviderParameters); + } + else { + impl->unlock(); + // TODO log no acqIdentifier recognized + } +} + +void VariableAcquisitionWorker::initialize() +{ + qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init") + << QThread::currentThread(); + impl->m_WorkingMutex.lock(); + qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init END"); +} + +void VariableAcquisitionWorker::finalize() +{ + impl->m_WorkingMutex.unlock(); +} + +void VariableAcquisitionWorker::waitForFinish() +{ + QMutexLocker locker{&impl->m_WorkingMutex}; +} + +void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariableRequest( + QUuid vIdentifier) +{ + lockWrite(); + auto it = m_VIdentifierToCurrrentAcqIdNextIdPairMap.find(vIdentifier); + + if (it != m_VIdentifierToCurrrentAcqIdNextIdPairMap.cend()) { + // A current request already exists, we can replace the next one + + m_AcqIdentifierToAcqRequestMap.erase(it->second.first); + m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.first); + + m_AcqIdentifierToAcqRequestMap.erase(it->second.second); + m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second.second); + } + m_VIdentifierToCurrrentAcqIdNextIdPairMap.erase(vIdentifier); + unlock(); +} diff --git a/core/src/Variable/VariableCacheStrategy.cpp b/core/src/Variable/VariableCacheStrategy.cpp new file mode 100644 index 0000000..8ab1a85 --- /dev/null +++ b/core/src/Variable/VariableCacheStrategy.cpp @@ -0,0 +1,52 @@ +#include "Variable/VariableCacheStrategy.h" + +#include "Settings/SqpSettingsDefs.h" + +#include "Variable/Variable.h" +#include "Variable/VariableController.h" + +Q_LOGGING_CATEGORY(LOG_VariableCacheStrategy, "VariableCacheStrategy") + +struct VariableCacheStrategy::VariableCacheStrategyPrivate { + VariableCacheStrategyPrivate() : m_CacheStrategy{CacheStrategy::FixedTolerance} {} + + CacheStrategy m_CacheStrategy; +}; + + +VariableCacheStrategy::VariableCacheStrategy(QObject *parent) + : QObject{parent}, impl{spimpl::make_unique_impl()} +{ +} + +std::pair +VariableCacheStrategy::computeCacheRange(const SqpRange &vRange, const SqpRange &rangeRequested) +{ + + auto varRanges = std::pair{}; + + auto toleranceFactor = SqpSettings::toleranceValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, + GENERAL_TOLERANCE_AT_UPDATE_DEFAULT_VALUE); + auto tolerance = toleranceFactor * (rangeRequested.m_TEnd - rangeRequested.m_TStart); + + switch (impl->m_CacheStrategy) { + case CacheStrategy::FixedTolerance: { + varRanges.first = rangeRequested; + varRanges.second + = SqpRange{rangeRequested.m_TStart - tolerance, rangeRequested.m_TEnd + tolerance}; + break; + } + + case CacheStrategy::TwoThreashold: { + // TODO Implement + break; + } + default: + qCCritical(LOG_VariableCacheStrategy()) + << tr("Impossible to use compute the cache range with an unknow cache strategy"); + // No action + break; + } + + return varRanges; +} diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 6296525..b2703f8 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -1,7 +1,10 @@ #include +#include #include +#include #include #include +#include #include #include @@ -13,19 +16,97 @@ #include #include +#include #include Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") +namespace { + +SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &grapheRange, + const SqpRange &oldGraphRange) +{ + auto zoomType = VariableController::getZoomType(grapheRange, oldGraphRange); + + auto varRangeRequested = varRange; + switch (zoomType) { + case AcquisitionZoomType::ZoomIn: { + auto deltaLeft = grapheRange.m_TStart - oldGraphRange.m_TStart; + auto deltaRight = oldGraphRange.m_TEnd - grapheRange.m_TEnd; + varRangeRequested.m_TStart += deltaLeft; + varRangeRequested.m_TEnd -= deltaRight; + break; + } + + case AcquisitionZoomType::ZoomOut: { + auto deltaLeft = oldGraphRange.m_TStart - grapheRange.m_TStart; + auto deltaRight = grapheRange.m_TEnd - oldGraphRange.m_TEnd; + varRangeRequested.m_TStart -= deltaLeft; + varRangeRequested.m_TEnd += deltaRight; + break; + } + case AcquisitionZoomType::PanRight: { + auto deltaRight = grapheRange.m_TEnd - oldGraphRange.m_TEnd; + varRangeRequested.m_TStart += deltaRight; + varRangeRequested.m_TEnd += deltaRight; + break; + } + case AcquisitionZoomType::PanLeft: { + auto deltaLeft = oldGraphRange.m_TStart - grapheRange.m_TStart; + varRangeRequested.m_TStart -= deltaLeft; + varRangeRequested.m_TEnd -= deltaLeft; + break; + } + case AcquisitionZoomType::Unknown: { + qCCritical(LOG_VariableController()) + << VariableController::tr("Impossible to synchronize: zoom type unknown"); + break; + } + default: + qCCritical(LOG_VariableController()) << VariableController::tr( + "Impossible to synchronize: zoom type not take into account"); + // No action + break; + } + + return varRangeRequested; +} +} + struct VariableController::VariableControllerPrivate { explicit VariableControllerPrivate(VariableController *parent) : m_WorkingMutex{}, m_VariableModel{new VariableModel{parent}}, m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, - m_VariableCacheController{std::make_unique()} + m_VariableCacheController{std::make_unique()}, + m_VariableCacheStrategy{std::make_unique()}, + m_VariableAcquisitionWorker{std::make_unique()} + { + + m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread); + m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread"); + } + + + virtual ~VariableControllerPrivate() { + qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction"); + m_VariableAcquisitionWorkerThread.quit(); + m_VariableAcquisitionWorkerThread.wait(); } + + void processRequest(std::shared_ptr var, const SqpRange &rangeRequested); + + QVector provideNotInCacheDateTimeList(std::shared_ptr variable, + const SqpRange &dateTime); + + std::shared_ptr findVariable(QUuid vIdentifier); + std::shared_ptr + retrieveDataSeries(const QVector acqDataPacketVector); + + void registerProvider(std::shared_ptr provider); + QMutex m_WorkingMutex; /// Variable model. The VariableController has the ownership VariableModel *m_VariableModel; @@ -34,12 +115,20 @@ struct VariableController::VariableControllerPrivate { TimeController *m_TimeController{nullptr}; std::unique_ptr m_VariableCacheController; + std::unique_ptr m_VariableCacheStrategy; + std::unique_ptr m_VariableAcquisitionWorker; + QThread m_VariableAcquisitionWorkerThread; std::unordered_map, std::shared_ptr > m_VariableToProviderMap; std::unordered_map, QUuid> m_VariableToIdentifierMap; + std::map > + m_GroupIdToVariableSynchronizationGroupMap; + std::map m_VariableIdGroupIdMap; + std::set > m_ProviderSet; }; + VariableController::VariableController(QObject *parent) : QObject{parent}, impl{spimpl::make_unique_impl(this)} { @@ -48,6 +137,20 @@ VariableController::VariableController(QObject *parent) connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, &VariableController::onAbortProgressRequested); + + connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this, + &VariableController::onDataProvided); + connect(impl->m_VariableAcquisitionWorker.get(), + &VariableAcquisitionWorker::variableRequestInProgress, this, + &VariableController::onVariableRetrieveDataInProgress); + + connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started, + impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize); + connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished, + impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize); + + + impl->m_VariableAcquisitionWorkerThread.start(); } VariableController::~VariableController() @@ -121,45 +224,34 @@ void VariableController::createVariable(const QString &name, const QVariantHash return; } - auto dateTime = impl->m_TimeController->dateTime(); + auto range = impl->m_TimeController->dateTime(); - if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { + if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) { auto identifier = QUuid::createUuid(); // store the provider + impl->registerProvider(provider); + + // Associate the provider impl->m_VariableToProviderMap[newVariable] = provider; impl->m_VariableToIdentifierMap[newVariable] = identifier; - auto addDateTimeAcquired = [ this, varW = std::weak_ptr{newVariable} ]( - QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) - { - if (auto variable = varW.lock()) { - auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable); - if (varIdentifier == identifier) { - impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); - variable->setDataSeries(dataSeriesAcquired); - emit variable->updated(); - } - } - }; - connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); - connect(provider.get(), &IDataProvider::dataProvidedProgress, this, - &VariableController::onVariableRetrieveDataInProgress); - this->onRequestDataLoading(newVariable, dateTime); + impl->processRequest(newVariable, range); } } void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) { + // TODO check synchronisation qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" << QThread::currentThread()->objectName(); auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); for (const auto &selectedRow : qAsConst(selectedRows)) { if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { - selectedVariable->setDateTime(dateTime); - this->onRequestDataLoading(selectedVariable, dateTime); + selectedVariable->setRange(dateTime); + impl->processRequest(selectedVariable, dateTime); // notify that rescale operation has to be done emit rangeChanged(selectedVariable, dateTime); @@ -167,14 +259,38 @@ void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) } } -void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) +void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested, + const SqpRange &cacheRangeRequested, + QVector dataAcquired) { - auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; + auto var = impl->findVariable(vIdentifier); + if (var != nullptr) { + var->setRange(rangeRequested); + var->setCacheRange(cacheRangeRequested); + qCDebug(LOG_VariableController()) << tr("1: onDataProvided") << rangeRequested; + qCDebug(LOG_VariableController()) << tr("2: onDataProvided") << cacheRangeRequested; + + auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired); + qCDebug(LOG_VariableController()) << tr("3: onDataProvided") + << retrievedDataSeries->range(); + var->mergeDataSeries(retrievedDataSeries); + qCDebug(LOG_VariableController()) << tr("4: onDataProvided"); + emit var->updated(); + } + else { + qCCritical(LOG_VariableController()) << tr("Impossible to provide data to a null variable"); + } +} - auto end = impl->m_VariableToIdentifierMap.cend(); - auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply); - if (it != end) { - impl->m_VariableModel->setDataProgress(it->first, progress); +void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) +{ + auto var = impl->findVariable(identifier); + if (var != nullptr) { + impl->m_VariableModel->setDataProgress(var, progress); + } + else { + qCCritical(LOG_VariableController()) + << tr("Impossible to notify progression of a null variable"); } } @@ -194,33 +310,111 @@ void VariableController::onAbortProgressRequested(std::shared_ptr vari } } +void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId) +{ + qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId" + << QThread::currentThread()->objectName() + << synchronizationGroupId; + auto vSynchroGroup = std::make_shared(); + impl->m_GroupIdToVariableSynchronizationGroupMap.insert( + std::make_pair(synchronizationGroupId, vSynchroGroup)); +} -void VariableController::onRequestDataLoading(std::shared_ptr variable, - const SqpRange &dateTime) +void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId) { - qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" - << QThread::currentThread()->objectName(); - // we want to load data of the variable for the dateTime. - // First we check if the cache contains some of them. - // For the other, we ask the provider to give them. - if (variable) { + impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId); +} - auto dateTimeListNotInCache - = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); +void VariableController::onAddSynchronized(std::shared_ptr variable, + QUuid synchronizationGroupId) - if (!dateTimeListNotInCache.empty()) { - // Ask the provider for each data on the dateTimeListNotInCache - auto identifier = impl->m_VariableToIdentifierMap.at(variable); - impl->m_VariableToProviderMap.at(variable)->requestDataLoading( - identifier, - DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); +{ + qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized" + << synchronizationGroupId; + auto vToVIdit = impl->m_VariableToIdentifierMap.find(variable); + if (vToVIdit != impl->m_VariableToIdentifierMap.cend()) { + auto itSynchroGroup + = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId); + if (itSynchroGroup != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) { + impl->m_VariableIdGroupIdMap.insert( + std::make_pair(vToVIdit->second, synchronizationGroupId)); + itSynchroGroup->second->addVariableId(vToVIdit->second); } else { - emit variable->updated(); + qCCritical(LOG_VariableController()) + << tr("Impossible to synchronize a variable with an unknown sycnhronization group") + << variable->name(); } } else { - qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); + qCCritical(LOG_VariableController()) + << tr("Impossible to synchronize a variable with no identifier") << variable->name(); + } +} + + +void VariableController::onRequestDataLoading(QVector > variables, + const SqpRange &range, const SqpRange &oldRange, + bool synchronise) +{ + // NOTE: oldRange isn't really necessary since oldRange == variable->range(). + + qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading" + << QThread::currentThread()->objectName(); + // we want to load data of the variable for the dateTime. + // First we check if the cache contains some of them. + // For the other, we ask the provider to give them. + + foreach (auto var, variables) { + qCDebug(LOG_VariableController()) << "processRequest for" << var->name(); + impl->processRequest(var, range); + } + + if (synchronise) { + // Get the group ids + qCDebug(LOG_VariableController()) + << "VariableController::onRequestDataLoading for synchro var ENABLE"; + auto groupIds = std::set(); + foreach (auto var, variables) { + auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var); + if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) { + auto vId = varToVarIdIt->second; + auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId); + if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) { + auto gId = varIdToGroupIdIt->second; + if (groupIds.find(gId) == groupIds.cend()) { + qCDebug(LOG_VariableController()) << "Synchro detect group " << gId; + groupIds.insert(gId); + } + } + } + } + + // We assume here all group ids exist + foreach (auto gId, groupIds) { + auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId); + auto vSyncIds = vSynchronizationGroup->getIds(); + qCDebug(LOG_VariableController()) << "Var in synchro group "; + for (auto vId : vSyncIds) { + auto var = impl->findVariable(vId); + + // Don't process already processed var + if (!variables.contains(var)) { + if (var != nullptr) { + qCDebug(LOG_VariableController()) << "processRequest synchro for" + << var->name(); + auto vSyncRangeRequested + = computeSynchroRangeRequested(var->range(), range, oldRange); + impl->processRequest(var, vSyncRangeRequested); + } + else { + qCCritical(LOG_VariableController()) + + << tr("Impossible to synchronize a null variable"); + } + } + } + } } } @@ -241,3 +435,109 @@ void VariableController::waitForFinish() { QMutexLocker locker{&impl->m_WorkingMutex}; } + +AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange) +{ + // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd + auto zoomType = AcquisitionZoomType::Unknown; + if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) { + zoomType = AcquisitionZoomType::ZoomOut; + } + else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) { + zoomType = AcquisitionZoomType::PanRight; + } + else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) { + zoomType = AcquisitionZoomType::PanLeft; + } + else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) { + zoomType = AcquisitionZoomType::ZoomIn; + } + else { + qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected"; + } + return zoomType; +} + +void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr var, + const SqpRange &rangeRequested) +{ + + auto varRangesRequested + = m_VariableCacheStrategy->computeCacheRange(var->range(), rangeRequested); + auto notInCacheRangeList = var->provideNotInCacheRangeList(varRangesRequested.second); + + if (!notInCacheRangeList.empty()) { + auto identifier = m_VariableToIdentifierMap.at(var); + auto varProvider = m_VariableToProviderMap.at(var); + if (varProvider != nullptr) { + m_VariableAcquisitionWorker->pushVariableRequest( + identifier, varRangesRequested.first, varRangesRequested.second, + DataProviderParameters{std::move(notInCacheRangeList), var->metadata()}, + varProvider); + } + else { + qCCritical(LOG_VariableController()) + << "Impossible to provide data with a null provider"; + } + } + else { + var->setRange(rangeRequested); + var->setCacheRange(varRangesRequested.second); + var->setDataSeries(var->dataSeries()->subData(varRangesRequested.second)); + emit var->updated(); + } +} + +std::shared_ptr +VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier) +{ + std::shared_ptr var; + auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; }; + + auto end = m_VariableToIdentifierMap.cend(); + auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply); + if (it != end) { + var = it->first; + } + else { + qCCritical(LOG_VariableController()) + << tr("Impossible to find the variable with the identifier: ") << vIdentifier; + } + + return var; +} + +std::shared_ptr VariableController::VariableControllerPrivate::retrieveDataSeries( + const QVector acqDataPacketVector) +{ + qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size") + << acqDataPacketVector.size(); + std::shared_ptr dataSeries; + if (!acqDataPacketVector.isEmpty()) { + dataSeries = acqDataPacketVector[0].m_DateSeries; + for (int i = 1; i < acqDataPacketVector.size(); ++i) { + dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get()); + } + } + qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END") + << acqDataPacketVector.size(); + return dataSeries; +} + +void VariableController::VariableControllerPrivate::registerProvider( + std::shared_ptr provider) +{ + if (m_ProviderSet.find(provider) == m_ProviderSet.end()) { + qCDebug(LOG_VariableController()) << tr("Registering of a new provider") + << provider->objectName(); + m_ProviderSet.insert(provider); + connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(), + &VariableAcquisitionWorker::onVariableDataAcquired); + connect(provider.get(), &IDataProvider::dataProvidedProgress, + m_VariableAcquisitionWorker.get(), + &VariableAcquisitionWorker::onVariableRetrieveDataInProgress); + } + else { + qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists "); + } +} diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index 971f48a..e5f6f37 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -160,9 +160,9 @@ QVariant VariableModel::data(const QModelIndex &index, int role) const case NAME_COLUMN: return variable->name(); case TSTART_COLUMN: - return dateTimeVariant(variable->dateTime().m_TStart); + return dateTimeVariant(variable->range().m_TStart); case TEND_COLUMN: - return dateTimeVariant(variable->dateTime().m_TEnd); + return dateTimeVariant(variable->range().m_TEnd); default: // No action break; diff --git a/core/src/Variable/VariableSynchronizationGroup.cpp b/core/src/Variable/VariableSynchronizationGroup.cpp new file mode 100644 index 0000000..a0454f9 --- /dev/null +++ b/core/src/Variable/VariableSynchronizationGroup.cpp @@ -0,0 +1,32 @@ +#include "Variable/VariableSynchronizationGroup.h" + +#include "Variable/Variable.h" + + +Q_LOGGING_CATEGORY(LOG_VariableSynchronizationGroup, "VariableSynchronizationGroup") + +struct VariableSynchronizationGroup::VariableSynchronizationGroupPrivate { + + std::set m_VariableIdSet; +}; + + +VariableSynchronizationGroup::VariableSynchronizationGroup(QObject *parent) + : QObject{parent}, impl{spimpl::make_unique_impl()} +{ +} + +void VariableSynchronizationGroup::addVariableId(QUuid vIdentifier) +{ + impl->m_VariableIdSet.insert(vIdentifier); +} + +void VariableSynchronizationGroup::removeVariableId(QUuid vIdentifier) +{ + impl->m_VariableIdSet.erase(vIdentifier); +} + +const std::set &VariableSynchronizationGroup::getIds() const noexcept +{ + return impl->m_VariableIdSet; +} diff --git a/core/tests/Data/TestOneDimArrayData.cpp b/core/tests/Data/TestOneDimArrayData.cpp index d946465..a7f0720 100644 --- a/core/tests/Data/TestOneDimArrayData.cpp +++ b/core/tests/Data/TestOneDimArrayData.cpp @@ -64,8 +64,8 @@ void TestOneDimArrayData::testDataByComponentIndex_data() // Test cases QTest::newRow("validIndex") << QVector{1., 2., 3., 4., 5.} << 0 << QVector{1., 2., 3., 4., 5.}; - QTest::newRow("invalidIndex1") - << QVector{1., 2., 3., 4., 5.} << -1 << QVector{}; + QTest::newRow("invalidIndex1") << QVector{1., 2., 3., 4., 5.} << -1 + << QVector{}; QTest::newRow("invalidIndex2") << QVector{1., 2., 3., 4., 5.} << 1 << QVector{}; } diff --git a/core/tests/Data/TestTwoDimArrayData.cpp b/core/tests/Data/TestTwoDimArrayData.cpp index 39aef29..a8de502 100644 --- a/core/tests/Data/TestTwoDimArrayData.cpp +++ b/core/tests/Data/TestTwoDimArrayData.cpp @@ -75,8 +75,8 @@ void TestTwoDimArrayData::testCtor_data() QTest::newRow("malformedInput (components of the array data haven't the same size") << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8.}, {11., 12.}} << true << DataContainer{{}, {}, {}}; - QTest::newRow("invalidInput (less than tow components") - << DataContainer{{1., 2., 3., 4., 5.}} << false << DataContainer{{}, {}, {}}; + QTest::newRow("invalidInput (less than tow components") << DataContainer{{1., 2., 3., 4., 5.}} + << false << DataContainer{{}, {}, {}}; } void TestTwoDimArrayData::testCtor() diff --git a/gui/include/Visualization/VisualizationGraphHelper.h b/gui/include/Visualization/VisualizationGraphHelper.h index efdb6f2..c0d4b3d 100644 --- a/gui/include/Visualization/VisualizationGraphHelper.h +++ b/gui/include/Visualization/VisualizationGraphHelper.h @@ -31,8 +31,8 @@ struct VisualizationGraphHelper { static QVector create(std::shared_ptr variable, QCustomPlot &plot) noexcept; - static void updateData(QVector plotableVect, IDataSeries *dataSeries, - const SqpRange &dateTime); + static void updateData(QVector plotableVect, + std::shared_ptr dataSeries, const SqpRange &dateTime); }; #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H diff --git a/gui/include/Visualization/VisualizationGraphWidget.h b/gui/include/Visualization/VisualizationGraphWidget.h index 6ddcc72..a39d5fd 100644 --- a/gui/include/Visualization/VisualizationGraphWidget.h +++ b/gui/include/Visualization/VisualizationGraphWidget.h @@ -16,11 +16,6 @@ class QCPRange; class SqpRange; class Variable; -/** - * Possible types of zoom operation - */ -enum class VisualizationGraphWidgetZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown }; - namespace Ui { class VisualizationGraphWidget; } // namespace Ui @@ -32,7 +27,8 @@ public: explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0); virtual ~VisualizationGraphWidget(); - void enableSynchronize(bool enable); + /// If acquisition isn't enable, requestDataLoading signal cannot be emit + void enableAcquisition(bool enable); void addVariable(std::shared_ptr variable); void addVariableUsingGraph(std::shared_ptr variable); @@ -51,9 +47,12 @@ public: signals: - void requestDataLoading(std::shared_ptr variable, const SqpRange &dateTime); - void synchronize(const SqpRange &dateTime, const SqpRange &oldDateTime, - VisualizationGraphWidgetZoomType zoomType); + void synchronize(const SqpRange &range, const SqpRange &oldRange); + void requestDataLoading(QVector > variable, const SqpRange &range, + const SqpRange &oldRange, bool synchronise); + + + void variableAdded(std::shared_ptr var); private: diff --git a/gui/include/Visualization/VisualizationZoneWidget.h b/gui/include/Visualization/VisualizationZoneWidget.h index da13188..1f5125f 100644 --- a/gui/include/Visualization/VisualizationZoneWidget.h +++ b/gui/include/Visualization/VisualizationZoneWidget.h @@ -6,6 +6,10 @@ #include #include +#include + +#include + Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationZoneWidget) namespace Ui { @@ -38,8 +42,15 @@ public: bool contains(const Variable &variable) const override; QString name() const override; + +private slots: + void onVariableAdded(std::shared_ptr variable); + private: Ui::VisualizationZoneWidget *ui; + + class VisualizationZoneWidgetPrivate; + spimpl::unique_impl_ptr impl; }; #endif // SCIQLOP_VISUALIZATIONZONEWIDGET_H diff --git a/gui/src/SqpApplication.cpp b/gui/src/SqpApplication.cpp index 1bc25b7..a4b99df 100644 --- a/gui/src/SqpApplication.cpp +++ b/gui/src/SqpApplication.cpp @@ -60,7 +60,7 @@ public: virtual ~SqpApplicationPrivate() { - qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction"); + qCDebug(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction"); m_DataSourceControllerThread.quit(); m_DataSourceControllerThread.wait(); diff --git a/gui/src/Visualization/VisualizationGraphHelper.cpp b/gui/src/Visualization/VisualizationGraphHelper.cpp index 0e80e3c..a67534c 100644 --- a/gui/src/Visualization/VisualizationGraphHelper.cpp +++ b/gui/src/Visualization/VisualizationGraphHelper.cpp @@ -35,21 +35,21 @@ QSharedPointer axisTicker(bool isTimeAxis) } } -void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, +void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr scalarSeries, const SqpRange &dateTime) { qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" << QThread::currentThread()->objectName(); if (auto qcpGraph = dynamic_cast(component)) { - scalarSeries.lockRead(); + scalarSeries->lockRead(); { - const auto &xData = scalarSeries.xAxisData()->cdata(); - const auto &valuesData = scalarSeries.valuesData()->cdata(); + const auto &xData = scalarSeries->xAxisData()->cdata(); + const auto &valuesData = scalarSeries->valuesData()->cdata(); auto xDataBegin = xData.cbegin(); auto xDataEnd = xData.cend(); - qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" + qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points in cache" << xData.count(); auto sqpDataContainer = QSharedPointer::create(); @@ -65,10 +65,10 @@ void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSerie sqpDataContainer->appendGraphData(QCPGraphData(*xAxisDataIt, *valuesDataIt)); } - qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" + qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed" << sqpDataContainer->size(); } - scalarSeries.unlock(); + scalarSeries->unlock(); // Display all data @@ -79,14 +79,14 @@ void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSerie } } -QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, - const SqpRange &dateTime) +QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr scalarSeries, + QCustomPlot &plot, const SqpRange &dateTime) { auto component = plot.addGraph(); if (component) { // // Graph data - component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), + component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(), true); updateScalarData(component, scalarSeries, dateTime); @@ -102,8 +102,8 @@ QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QC // ticker (depending on the type of unit) axis->setTicker(axisTicker(unit.m_TimeUnit)); }; - setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); - setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); + setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); + setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); // Display all data component->rescaleAxes(); @@ -127,8 +127,8 @@ QVector VisualizationGraphHelper::create(std::shared_ptr if (variable) { // Gets the data series of the variable to call the creation of the right components // according to its type - if (auto scalarSeries = dynamic_cast(variable->dataSeries())) { - result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); + if (auto scalarSeries = std::dynamic_pointer_cast(variable->dataSeries())) { + result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range())); } else { qCDebug(LOG_VisualizationGraphHelper()) @@ -144,11 +144,12 @@ QVector VisualizationGraphHelper::create(std::shared_ptr } void VisualizationGraphHelper::updateData(QVector plotableVect, - IDataSeries *dataSeries, const SqpRange &dateTime) + std::shared_ptr dataSeries, + const SqpRange &dateTime) { - if (auto scalarSeries = dynamic_cast(dataSeries)) { + if (auto scalarSeries = std::dynamic_pointer_cast(dataSeries)) { if (plotableVect.size() == 1) { - updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); + updateScalarData(plotableVect.at(0), scalarSeries, dateTime); } else { qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index 57c2cec..1ed0384 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -23,28 +23,18 @@ const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; /// Key pressed to enable zoom on vertical axis const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; -/// Gets a tolerance value from application settings. If the setting can't be found, the default -/// value passed in parameter is returned -double toleranceValue(const QString &key, double defaultValue) noexcept -{ - return QSettings{}.value(key, defaultValue).toDouble(); -} - } // namespace struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { explicit VisualizationGraphWidgetPrivate() - : m_DoSynchronize{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} + : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} { } - // Return the operation when range changed - VisualizationGraphWidgetZoomType getZoomType(const QCPRange &t1, const QCPRange &t2); - // 1 variable -> n qcpplot std::multimap, QCPAbstractPlottable *> m_VariableToPlotMultiMap; - bool m_DoSynchronize; + bool m_DoAcquisition; bool m_IsCalibration; QCPItemTracer *m_TextTracer; /// Delegate used to attach rendering features to the plot @@ -98,50 +88,37 @@ VisualizationGraphWidget::~VisualizationGraphWidget() delete ui; } -void VisualizationGraphWidget::enableSynchronize(bool enable) +void VisualizationGraphWidget::enableAcquisition(bool enable) { - impl->m_DoSynchronize = enable; + impl->m_DoAcquisition = enable; } void VisualizationGraphWidget::addVariable(std::shared_ptr variable) { + auto calibrationState = impl->m_IsCalibration; + impl->m_IsCalibration = true; // Uses delegate to create the qcpplot components according to the variable auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); + impl->m_IsCalibration = calibrationState; for (auto createdPlottable : qAsConst(createdPlottables)) { impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); } connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); + + emit variableAdded(variable); } void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr variable) { - - // when adding a variable, we need to set its time range to the current graph range - auto grapheRange = ui->widget->xAxis->range(); - auto dateTime = SqpRange{grapheRange.lower, grapheRange.upper}; - variable->setDateTime(dateTime); - - auto variableDateTimeWithTolerance = dateTime; - - // add tolerance for each side - auto toleranceFactor - = toleranceValue(GENERAL_TOLERANCE_AT_INIT_KEY, GENERAL_TOLERANCE_AT_INIT_DEFAULT_VALUE); - auto tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); - variableDateTimeWithTolerance.m_TStart -= tolerance; - variableDateTimeWithTolerance.m_TEnd += tolerance; - // Uses delegate to create the qcpplot components according to the variable - auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); - - for (auto createdPlottable : qAsConst(createdPlottables)) { - impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); - } + this->addVariable(variable); - connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); + // Request range for the variable + auto graphRange = ui->widget->xAxis->range(); - // CHangement detected, we need to ask controller to request data loading - emit requestDataLoading(variable, variableDateTimeWithTolerance); + emit requestDataLoading(QVector >() << variable, + SqpRange{graphRange.lower, graphRange.upper}, variable->range(), false); } void VisualizationGraphWidget::removeVariable(std::shared_ptr variable) noexcept @@ -239,103 +216,31 @@ void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) { - qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") - << QThread::currentThread()->objectName(); + qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") + << QThread::currentThread()->objectName() << "DoAcqui" + << impl->m_DoAcquisition; - auto dateTimeRange = SqpRange{t1.lower, t1.upper}; + auto graphRange = SqpRange{t1.lower, t1.upper}; + auto oldGraphRange = SqpRange{t2.lower, t2.upper}; - auto zoomType = impl->getZoomType(t1, t2); - for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); - it != impl->m_VariableToPlotMultiMap.cend(); ++it) { + if (impl->m_DoAcquisition) { + QVector > variableUnderGraphVector; - auto variable = it->first; - auto currentDateTime = dateTimeRange; - - auto toleranceFactor = toleranceValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, - GENERAL_TOLERANCE_AT_UPDATE_DEFAULT_VALUE); - auto tolerance = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); - auto variableDateTimeWithTolerance = currentDateTime; - variableDateTimeWithTolerance.m_TStart -= tolerance; - variableDateTimeWithTolerance.m_TEnd += tolerance; - - qCDebug(LOG_VisualizationGraphWidget()) << "r" << currentDateTime; - qCDebug(LOG_VisualizationGraphWidget()) << "t" << variableDateTimeWithTolerance; - qCDebug(LOG_VisualizationGraphWidget()) << "v" << variable->dateTime(); - // If new range with tol is upper than variable datetime parameters. we need to request new - // data - if (!variable->contains(variableDateTimeWithTolerance)) { - - auto variableDateTimeWithTolerance = currentDateTime; - if (!variable->isInside(currentDateTime)) { - auto variableDateTime = variable->dateTime(); - if (variable->contains(variableDateTimeWithTolerance)) { - qCDebug(LOG_VisualizationGraphWidget()) - << tr("TORM: Detection zoom in that need request:"); - // add tolerance for each side - tolerance - = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); - variableDateTimeWithTolerance.m_TStart -= tolerance; - variableDateTimeWithTolerance.m_TEnd += tolerance; - } - else if (variableDateTime.m_TStart < currentDateTime.m_TStart) { - qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to right:"); - - auto diffEndToKeepDelta = currentDateTime.m_TEnd - variableDateTime.m_TEnd; - currentDateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; - // Tolerance have to be added to the right - // add tolerance for right (end) side - tolerance - = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); - variableDateTimeWithTolerance.m_TEnd += tolerance; - } - else if (variableDateTime.m_TEnd > currentDateTime.m_TEnd) { - qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to left: "); - auto diffStartToKeepDelta - = variableDateTime.m_TStart - currentDateTime.m_TStart; - currentDateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; - // Tolerance have to be added to the left - // add tolerance for left (start) side - tolerance - = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); - variableDateTimeWithTolerance.m_TStart -= tolerance; - } - else { - qCCritical(LOG_VisualizationGraphWidget()) - << tr("Detection anormal zoom detection: "); - } - } - else { - qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Detection zoom out: "); - // add tolerance for each side - tolerance = toleranceFactor * (currentDateTime.m_TEnd - currentDateTime.m_TStart); - variableDateTimeWithTolerance.m_TStart -= tolerance; - variableDateTimeWithTolerance.m_TEnd += tolerance; - zoomType = VisualizationGraphWidgetZoomType::ZoomOut; - } - if (!variable->contains(dateTimeRange)) { - qCDebug(LOG_VisualizationGraphWidget()) - << "TORM: Modif on variable datetime detected" << currentDateTime; - variable->setDateTime(currentDateTime); - } - - qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: Request data detection: "); - // CHangement detected, we need to ask controller to request data loading - emit requestDataLoading(variable, variableDateTimeWithTolerance); + for (auto it = impl->m_VariableToPlotMultiMap.begin(), + end = impl->m_VariableToPlotMultiMap.end(); + it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { + variableUnderGraphVector.push_back(it->first); } - else { - qCInfo(LOG_VisualizationGraphWidget()) - << tr("TORM: Detection zoom in that doesn't need request: "); - zoomType = VisualizationGraphWidgetZoomType::ZoomIn; + emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange, + !impl->m_IsCalibration); + + if (!impl->m_IsCalibration) { + qCDebug(LOG_VisualizationGraphWidget()) + << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") + << QThread::currentThread()->objectName() << graphRange << oldGraphRange; + emit synchronize(graphRange, oldGraphRange); } } - - if (impl->m_DoSynchronize && !impl->m_IsCalibration) { - auto oldDateTime = SqpRange{t2.lower, t2.upper}; - qCDebug(LOG_VisualizationGraphWidget()) - << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") - << QThread::currentThread()->objectName(); - emit synchronize(dateTimeRange, oldDateTime, zoomType); - } } void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept @@ -390,38 +295,13 @@ void VisualizationGraphWidget::onDataCacheVariableUpdated() it != impl->m_VariableToPlotMultiMap.cend(); ++it) { auto variable = it->first; qCDebug(LOG_VisualizationGraphWidget()) - << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" - << variable->dateTime(); + << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); qCDebug(LOG_VisualizationGraphWidget()) << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; - if (dateTime.contains(variable->dateTime()) || dateTime.intersect(variable->dateTime())) { + if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { VisualizationGraphHelper::updateData(QVector{} << it->second, - variable->dataSeries(), variable->dateTime()); + variable->dataSeries(), variable->range()); } } } - -VisualizationGraphWidgetZoomType -VisualizationGraphWidget::VisualizationGraphWidgetPrivate::getZoomType(const QCPRange &t1, - const QCPRange &t2) -{ - // t1.lower <= t2.lower && t2.upper <= t1.upper - auto zoomType = VisualizationGraphWidgetZoomType::Unknown; - if (t1.lower <= t2.lower && t2.upper <= t1.upper) { - zoomType = VisualizationGraphWidgetZoomType::ZoomOut; - } - else if (t1.lower > t2.lower && t1.upper > t2.upper) { - zoomType = VisualizationGraphWidgetZoomType::PanRight; - } - else if (t1.lower < t2.lower && t1.upper < t2.upper) { - zoomType = VisualizationGraphWidgetZoomType::PanLeft; - } - else if (t1.lower > t2.lower && t2.upper > t1.upper) { - zoomType = VisualizationGraphWidgetZoomType::ZoomIn; - } - else { - qCCritical(LOG_VisualizationGraphWidget()) << "getZoomType: Unknown type detected"; - } - return zoomType; -} diff --git a/gui/src/Visualization/VisualizationZoneWidget.cpp b/gui/src/Visualization/VisualizationZoneWidget.cpp index 05651a3..592c9c1 100644 --- a/gui/src/Visualization/VisualizationZoneWidget.cpp +++ b/gui/src/Visualization/VisualizationZoneWidget.cpp @@ -1,12 +1,14 @@ #include "Visualization/VisualizationZoneWidget.h" -#include "Data/SqpRange.h" #include "Visualization/IVisualizationWidgetVisitor.h" #include "Visualization/VisualizationGraphWidget.h" #include "ui_VisualizationZoneWidget.h" +#include +#include +#include #include Q_LOGGING_CATEGORY(LOG_VisualizationZoneWidget, "VisualizationZoneWidget") @@ -32,8 +34,16 @@ QString defaultGraphName(const QLayout &layout) } // namespace +struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate { + + explicit VisualizationZoneWidgetPrivate() : m_SynchronisationGroupId{QUuid::createUuid()} {} + QUuid m_SynchronisationGroupId; +}; + VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *parent) - : QWidget{parent}, ui{new Ui::VisualizationZoneWidget} + : QWidget{parent}, + ui{new Ui::VisualizationZoneWidget}, + impl{spimpl::make_unique_impl()} { ui->setupUi(this); @@ -43,6 +53,10 @@ VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *p setAttribute(Qt::WA_DeleteOnClose); connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationZoneWidget::close); ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); + + // Synchronisation id + QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronizationGroupId", + Qt::QueuedConnection, Q_ARG(QUuid, impl->m_SynchronisationGroupId)); } VisualizationZoneWidget::~VisualizationZoneWidget() @@ -65,14 +79,12 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptrsetSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding); graphWidget->setMinimumHeight(GRAPH_MINIMUM_HEIGHT); - this->addGraph(graphWidget); - - graphWidget->addVariable(variable); // Lambda to synchronize zone widget - auto synchronizeZoneWidget = [this, graphWidget](const SqpRange &dateTime, - const SqpRange &oldDateTime, - VisualizationGraphWidgetZoomType zoomType) { + auto synchronizeZoneWidget = [this, graphWidget](const SqpRange &grapheRange, + const SqpRange &oldGraphRange) { + + auto zoomType = VariableController::getZoomType(grapheRange, oldGraphRange); auto frameLayout = ui->visualizationZoneFrame->layout(); for (auto i = 0; i < frameLayout->count(); ++i) { auto graphChild @@ -81,9 +93,9 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptrgraphRange(); switch (zoomType) { - case VisualizationGraphWidgetZoomType::ZoomIn: { - auto deltaLeft = dateTime.m_TStart - oldDateTime.m_TStart; - auto deltaRight = oldDateTime.m_TEnd - dateTime.m_TEnd; + case AcquisitionZoomType::ZoomIn: { + auto deltaLeft = grapheRange.m_TStart - oldGraphRange.m_TStart; + auto deltaRight = oldGraphRange.m_TEnd - grapheRange.m_TEnd; graphChildRange.m_TStart += deltaLeft; graphChildRange.m_TEnd -= deltaRight; qCCritical(LOG_VisualizationZoneWidget()) << tr("TORM: ZoomIn"); @@ -92,42 +104,42 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptrenableSynchronize(false); + graphChild->enableAcquisition(false); qCCritical(LOG_VisualizationZoneWidget()) << tr("TORM: Range before: ") << graphChild->graphRange(); qCCritical(LOG_VisualizationZoneWidget()) << tr("TORM: Range after : ") @@ -146,13 +158,19 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptrsetGraphRange(graphChildRange); - graphChild->enableSynchronize(true); + graphChild->enableAcquisition(true); } } }; // connection for synchronization connect(graphWidget, &VisualizationGraphWidget::synchronize, synchronizeZoneWidget); + connect(graphWidget, &VisualizationGraphWidget::variableAdded, this, + &VisualizationZoneWidget::onVariableAdded); + + this->addGraph(graphWidget); + + graphWidget->addVariable(variable); return graphWidget; } @@ -198,3 +216,10 @@ QString VisualizationZoneWidget::name() const { return ui->zoneNameLabel->text(); } + +void VisualizationZoneWidget::onVariableAdded(std::shared_ptr variable) +{ + QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronized", + Qt::QueuedConnection, Q_ARG(std::shared_ptr, variable), + Q_ARG(QUuid, impl->m_SynchronisationGroupId)); +} diff --git a/gui/ui/Visualization/VisualizationTabWidget.ui b/gui/ui/Visualization/VisualizationTabWidget.ui index c39c764..1e886a8 100644 --- a/gui/ui/Visualization/VisualizationTabWidget.ui +++ b/gui/ui/Visualization/VisualizationTabWidget.ui @@ -28,9 +28,6 @@ - - background-color: transparent; - QFrame::NoFrame diff --git a/plugins/amda/include/AmdaProvider.h b/plugins/amda/include/AmdaProvider.h index 1f0438d..292d9b8 100644 --- a/plugins/amda/include/AmdaProvider.h +++ b/plugins/amda/include/AmdaProvider.h @@ -19,9 +19,9 @@ class SCIQLOP_AMDA_EXPORT AmdaProvider : public IDataProvider { public: explicit AmdaProvider(); - void requestDataLoading(QUuid token, const DataProviderParameters ¶meters) override; + void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) override; - void requestDataAborting(QUuid identifier) override; + void requestDataAborting(QUuid acqIdentifier) override; private: void retrieveData(QUuid token, const SqpRange &dateTime, const QVariantHash &data); diff --git a/plugins/amda/src/AmdaProvider.cpp b/plugins/amda/src/AmdaProvider.cpp index 48d35ba..9f90d82 100644 --- a/plugins/amda/src/AmdaProvider.cpp +++ b/plugins/amda/src/AmdaProvider.cpp @@ -55,21 +55,22 @@ AmdaProvider::AmdaProvider() } } -void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) +void AmdaProvider::requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) { // NOTE: Try to use multithread if possible const auto times = parameters.m_Times; const auto data = parameters.m_Data; for (const auto &dateTime : qAsConst(times)) { - retrieveData(token, dateTime, data); + this->retrieveData(acqIdentifier, dateTime, data); + QThread::msleep(200); } } -void AmdaProvider::requestDataAborting(QUuid identifier) +void AmdaProvider::requestDataAborting(QUuid acqIdentifier) { if (auto app = sqpApp) { auto &networkController = app->networkController(); - networkController.onReplyCanceled(identifier); + networkController.onReplyCanceled(acqIdentifier); } } @@ -81,7 +82,7 @@ void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVa qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); return; } - qCInfo(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData") << dateTime; + qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData") << dateTime; // /////////// // // Creates URL // @@ -96,8 +97,7 @@ void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVa // LAMBDA auto httpDownloadFinished - = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { - Q_UNUSED(dataId); + = [this, dateTime, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { // Don't do anything if the reply was abort if (reply->error() != QNetworkReply::OperationCanceledError) { @@ -111,7 +111,7 @@ void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVa // Parse results file if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { - emit dataProvided(token, dataSeries, dateTime); + emit dataProvided(dataId, dataSeries, dateTime); } else { /// @todo ALX : debug diff --git a/plugins/mockplugin/include/CosinusProvider.h b/plugins/mockplugin/include/CosinusProvider.h index edcd531..05db4d1 100644 --- a/plugins/mockplugin/include/CosinusProvider.h +++ b/plugins/mockplugin/include/CosinusProvider.h @@ -17,15 +17,16 @@ Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider) class SCIQLOP_MOCKPLUGIN_EXPORT CosinusProvider : public IDataProvider { public: /// @sa IDataProvider::requestDataLoading(). The current impl isn't thread safe. - void requestDataLoading(QUuid token, const DataProviderParameters ¶meters) override; + void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) override; /// @sa IDataProvider::requestDataAborting(). The current impl isn't thread safe. - void requestDataAborting(QUuid identifier) override; + void requestDataAborting(QUuid acqIdentifier) override; private: - std::shared_ptr retrieveData(QUuid token, const SqpRange &dateTime); + std::shared_ptr retrieveData(QUuid acqIdentifier, + const SqpRange &dataRangeRequested); QHash m_VariableToEnableProvider; }; diff --git a/plugins/mockplugin/src/CosinusProvider.cpp b/plugins/mockplugin/src/CosinusProvider.cpp index d00c3e5..ab3b4c0 100644 --- a/plugins/mockplugin/src/CosinusProvider.cpp +++ b/plugins/mockplugin/src/CosinusProvider.cpp @@ -11,15 +11,16 @@ Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") -std::shared_ptr CosinusProvider::retrieveData(QUuid token, const SqpRange &dateTime) +std::shared_ptr CosinusProvider::retrieveData(QUuid acqIdentifier, + const SqpRange &dataRangeRequested) { // TODO: Add Mutex auto dataIndex = 0; // Gets the timerange from the parameters double freq = 100.0; - double start = std::ceil(dateTime.m_TStart * freq); // 100 htz - double end = std::floor(dateTime.m_TEnd * freq); // 100 htz + double start = std::ceil(dataRangeRequested.m_TStart * freq); // 100 htz + double end = std::floor(dataRangeRequested.m_TEnd * freq); // 100 htz // We assure that timerange is valid if (end < start) { @@ -38,7 +39,7 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid token, const Sq int progress = 0; auto progressEnd = dataCount; for (auto time = start; time < end; ++time, ++dataIndex) { - auto it = m_VariableToEnableProvider.find(token); + auto it = m_VariableToEnableProvider.find(acqIdentifier); if (it != m_VariableToEnableProvider.end() && it.value()) { const auto timeOnFreq = time / freq; @@ -50,7 +51,7 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid token, const Sq if (currentProgress != progress) { progress = currentProgress; - emit dataProvidedProgress(token, progress); + emit dataProvidedProgress(acqIdentifier, progress); } } else { @@ -61,35 +62,37 @@ std::shared_ptr CosinusProvider::retrieveData(QUuid token, const Sq } } } - emit dataProvidedProgress(token, 0.0); + emit dataProvidedProgress(acqIdentifier, 0.0); return std::make_shared(std::move(xAxisData), std::move(valuesData), Unit{QStringLiteral("t"), true}, Unit{}); } -void CosinusProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) +void CosinusProvider::requestDataLoading(QUuid acqIdentifier, + const DataProviderParameters ¶meters) { // TODO: Add Mutex - m_VariableToEnableProvider[token] = true; + m_VariableToEnableProvider[acqIdentifier] = true; qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataLoading" << QThread::currentThread()->objectName(); // NOTE: Try to use multithread if possible const auto times = parameters.m_Times; for (const auto &dateTime : qAsConst(times)) { - if (m_VariableToEnableProvider[token]) { - auto scalarSeries = this->retrieveData(token, dateTime); - emit dataProvided(token, scalarSeries, dateTime); + if (m_VariableToEnableProvider[acqIdentifier]) { + auto scalarSeries = this->retrieveData(acqIdentifier, dateTime); + qCCritical(LOG_CosinusProvider()) << "CosinusProvider::dataProvided"; + emit dataProvided(acqIdentifier, scalarSeries, dateTime); } } } -void CosinusProvider::requestDataAborting(QUuid identifier) +void CosinusProvider::requestDataAborting(QUuid acqIdentifier) { // TODO: Add Mutex - qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataAborting" << identifier + qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataAborting" << acqIdentifier << QThread::currentThread()->objectName(); - auto it = m_VariableToEnableProvider.find(identifier); + auto it = m_VariableToEnableProvider.find(acqIdentifier); if (it != m_VariableToEnableProvider.end()) { it.value() = false; }