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/Variable/VariableAcquisitionWorker.h b/core/include/Variable/VariableAcquisitionWorker.h index 1cdb82e..d2adf83 100644 --- a/core/include/Variable/VariableAcquisitionWorker.h +++ b/core/include/Variable/VariableAcquisitionWorker.h @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include @@ -24,11 +26,34 @@ class SCIQLOP_CORE_EXPORT VariableAcquisitionWorker : public QObject { Q_OBJECT public: explicit VariableAcquisitionWorker(QObject *parent = 0); + virtual ~VariableAcquisitionWorker(); - void pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequest, SqpRange cacheRangeRequested, - DataProviderParameters parameters, IDataProvider *provider); + 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; }; diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index d37a9f7..632ae7b 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,19 @@ 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 initialize(); void finalize(); 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/Variable/VariableAcquisitionWorker.cpp b/core/src/Variable/VariableAcquisitionWorker.cpp index 8e7ead9..ae8a0e9 100644 --- a/core/src/Variable/VariableAcquisitionWorker.cpp +++ b/core/src/Variable/VariableAcquisitionWorker.cpp @@ -1,15 +1,35 @@ #include "Variable/VariableAcquisitionWorker.h" #include "Variable/Variable.h" + +#include +#include + #include +#include +#include +#include #include + Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker") struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { - std::unordered_map, QVector > - m_VariableToSqpDateTimeListMap; + 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; }; @@ -18,9 +38,188 @@ VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent) { } -void VariableAcquisitionWorker::pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequest, +VariableAcquisitionWorker::~VariableAcquisitionWorker() +{ + qCInfo(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker destruction") + << QThread::currentThread(); + this->waitForFinish(); +} + + +void VariableAcquisitionWorker::pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequested, SqpRange cacheRangeRequested, DataProviderParameters parameters, - IDataProvider *provider) + std::shared_ptr provider) +{ + qCDebug(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) +{ + qCDebug(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/VariableController.cpp b/core/src/Variable/VariableController.cpp index e27b918..4797879 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,22 +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; @@ -37,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)} { @@ -51,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() @@ -124,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); @@ -170,14 +259,39 @@ 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; }; + qCCritical(LOG_VariableController()) << tr("onDataProvided") << dataAcquired.isEmpty(); + + auto var = impl->findVariable(vIdentifier); + if (var != nullptr) { + var->setRange(rangeRequested); + var->setCacheRange(cacheRangeRequested); + qCCritical(LOG_VariableController()) << tr("1: onDataProvided") << rangeRequested; + qCCritical(LOG_VariableController()) << tr("2: onDataProvided") << cacheRangeRequested; + + auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired); + qCCritical(LOG_VariableController()) << tr("3: onDataProvided") + << retrievedDataSeries->range(); + var->mergeDataSeries(retrievedDataSeries); + 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"); } } @@ -197,34 +311,75 @@ void VariableController::onAbortProgressRequested(std::shared_ptr vari } } +void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId) +{ + auto vSynchroGroup = std::make_shared(); + impl->m_GroupIdToVariableSynchronizationGroupMap.insert( + std::make_pair(synchronizationGroupId, vSynchroGroup)); +} + +void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId) +{ + impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId); +} + -void VariableController::onRequestDataLoading(std::shared_ptr variable, - const SqpRange &dateTime) +void VariableController::onRequestDataLoading(QVector > variables, + const SqpRange &range, const SqpRange &oldRange, + bool synchronise) { + // NOTE: oldRange isn't really necessary since oldRange == variable->range(). + 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) { - auto dateTimeListNotInCache - = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); + foreach (auto var, variables) { + qCInfo(LOG_VariableController()) << "processRequest for" << var->name(); + impl->processRequest(var, range); + } - 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()}); + if (synchronise) { + // Get the group ids + qCInfo(LOG_VariableController()) + << "VariableController::onRequestDataLoading for synchro var ENABLE"; + auto groupIds = std::set(); + foreach (auto var, variables) { + auto vToVIdit = impl->m_VariableToIdentifierMap.find(var); + if (vToVIdit != impl->m_VariableToIdentifierMap.cend()) { + auto vId = vToVIdit->second; + + auto vIdToGIdit = impl->m_VariableIdGroupIdMap.find(vId); + if (vIdToGIdit != impl->m_VariableIdGroupIdMap.cend()) { + auto gId = vToVIdit->second; + if (groupIds.find(gId) == groupIds.cend()) { + groupIds.insert(gId); + } + } + } } - else { - emit variable->updated(); + + // We assume here all group ids exist + foreach (auto gId, groupIds) { + auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId); + auto vSyncIds = vSynchronizationGroup->getIds(); + for (auto vId : vSyncIds) { + auto var = impl->findVariable(vId); + if (var != nullptr) { + qCInfo(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"); + } + } } } - else { - qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); - } } @@ -245,32 +400,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; +} -QVector VariableController::VariableControllerPrivate::provideNotInCacheDateTimeList( - std::shared_ptr variable, const SqpRange &dateTime) +void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr var, + const SqpRange &rangeRequested) { - auto notInCache = QVector{}; - if (!variable->contains(dateTime)) { - auto vDateTime = variable->dateTime(); - if (dateTime.m_TEnd <= vDateTime.m_TStart || dateTime.m_TStart >= vDateTime.m_TEnd) { - notInCache << dateTime; - } - else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd <= vDateTime.m_TEnd) { - notInCache << SqpRange{dateTime.m_TStart, vDateTime.m_TStart}; - } - else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd > vDateTime.m_TEnd) { - notInCache << SqpRange{dateTime.m_TStart, vDateTime.m_TStart} - << SqpRange{vDateTime.m_TEnd, dateTime.m_TStart}; - } - else if (dateTime.m_TStart < vDateTime.m_TEnd) { - notInCache << SqpRange{vDateTime.m_TEnd, dateTime.m_TStart}; + auto varRangesRequested + = m_VariableCacheStrategy->computeCacheRange(var->range(), rangeRequested); + auto notInCacheRangeList = var->provideNotInCacheRangeList(varRangesRequested.second); + + if (!notInCacheRangeList.empty()) { + // Display part of data which are already there + // Ask the provider for each data on the dateTimeListNotInCache + 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()) << tr("Detection of unknown case.") - << QThread::currentThread(); + 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 notInCache; + return var; +} + +std::shared_ptr VariableController::VariableControllerPrivate::retrieveDataSeries( + const QVector acqDataPacketVector) +{ + qCInfo(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()); + } + } + + return dataSeries; +} + +void VariableController::VariableControllerPrivate::registerProvider( + std::shared_ptr provider) +{ + if (m_ProviderSet.find(provider) == m_ProviderSet.end()) { + qCInfo(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 { + qCInfo(LOG_VariableController()) << tr("Cannot register provider, it already exists "); + } } diff --git a/gui/include/Visualization/VisualizationGraphWidget.h b/gui/include/Visualization/VisualizationGraphWidget.h index 6ddcc72..b5d02f0 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,9 @@ 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); private: diff --git a/gui/include/Visualization/VisualizationZoneWidget.h b/gui/include/Visualization/VisualizationZoneWidget.h index da13188..a8968d1 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 { @@ -40,6 +44,9 @@ public: 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/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index 57c2cec..a795419 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,9 +88,9 @@ 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) @@ -116,32 +106,33 @@ void VisualizationGraphWidget::addVariable(std::shared_ptr variable) } void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr variable) { + // TODO + // // 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}); + // } - // 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}); - } + // connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); - connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); - - // CHangement detected, we need to ask controller to request data loading - emit requestDataLoading(variable, variableDateTimeWithTolerance); + // // CHangement detected, we need to ask controller to request data loading + // emit requestDataLoading(variable, variableDateTimeWithTolerance); } void VisualizationGraphWidget::removeVariable(std::shared_ptr variable) noexcept @@ -239,103 +230,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(); + 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 +309,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..7e4f26a 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() @@ -70,9 +84,10 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptraddVariable(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 +96,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 +107,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,7 +161,7 @@ VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptrsetGraphRange(graphChildRange); - graphChild->enableSynchronize(true); + graphChild->enableAcquisition(true); } } }; 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 46aa047..b75ff5c 100644 --- a/plugins/amda/src/AmdaProvider.cpp +++ b/plugins/amda/src/AmdaProvider.cpp @@ -55,21 +55,21 @@ 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); + retrieveData(acqIdentifier, dateTime, data); } } -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 +81,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 // 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..ec77d32 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,36 @@ 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); + 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; }