VariableController.cpp
123 lines
| 4.0 KiB
| text/x-c
|
CppLexer
r209 | #include <Variable/VariableCacheController.h> | |||
Alexandre Leroux
|
r106 | #include <Variable/VariableController.h> | ||
Alexandre Leroux
|
r108 | #include <Variable/VariableModel.h> | ||
Alexandre Leroux
|
r106 | |||
Alexandre Leroux
|
r154 | #include <Data/DataProviderParameters.h> | ||
#include <Data/IDataProvider.h> | ||||
#include <Data/IDataSeries.h> | ||||
r179 | #include <Time/TimeController.h> | |||
Alexandre Leroux
|
r154 | |||
#include <QDateTime> | ||||
Alexandre Leroux
|
r106 | #include <QMutex> | ||
#include <QThread> | ||||
r209 | #include <unordered_map> | |||
Alexandre Leroux
|
r106 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
Alexandre Leroux
|
r154 | namespace { | ||
/// @todo Generates default dataseries, according to the provider passed in parameter. This method | ||||
/// will be deleted when the timerange is recovered from SciQlop | ||||
r179 | std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, | |||
const SqpDateTime &dateTime) noexcept | ||||
Alexandre Leroux
|
r154 | { | ||
r179 | auto parameters = DataProviderParameters{dateTime}; | |||
Alexandre Leroux
|
r154 | |||
return provider.retrieveData(parameters); | ||||
} | ||||
} // namespace | ||||
Alexandre Leroux
|
r106 | struct VariableController::VariableControllerPrivate { | ||
Alexandre Leroux
|
r148 | explicit VariableControllerPrivate(VariableController *parent) | ||
r209 | : m_WorkingMutex{}, | |||
m_VariableModel{new VariableModel{parent}}, | ||||
m_VariableCacheController{std::make_unique<VariableCacheController>()} | ||||
Alexandre Leroux
|
r106 | { | ||
} | ||||
QMutex m_WorkingMutex; | ||||
Alexandre Leroux
|
r148 | /// Variable model. The VariableController has the ownership | ||
VariableModel *m_VariableModel; | ||||
r179 | ||||
r209 | ||||
r181 | TimeController *m_TimeController{nullptr}; | |||
r209 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |||
Alexandre Leroux
|
r106 | }; | ||
VariableController::VariableController(QObject *parent) | ||||
Alexandre Leroux
|
r148 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
Alexandre Leroux
|
r106 | { | ||
r126 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
Alexandre Leroux
|
r106 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r126 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |||
<< QThread::currentThread(); | ||||
Alexandre Leroux
|
r106 | this->waitForFinish(); | ||
} | ||||
Alexandre Leroux
|
r154 | VariableModel *VariableController::variableModel() noexcept | ||
Alexandre Leroux
|
r108 | { | ||
Alexandre Leroux
|
r154 | return impl->m_VariableModel; | ||
Alexandre Leroux
|
r108 | } | ||
r179 | void VariableController::setTimeController(TimeController *timeController) noexcept | |||
{ | ||||
impl->m_TimeController = timeController; | ||||
} | ||||
Alexandre Leroux
|
r154 | void VariableController::createVariable(const QString &name, | ||
std::shared_ptr<IDataProvider> provider) noexcept | ||||
Alexandre Leroux
|
r143 | { | ||
r179 | // TORM | |||
// auto dateTime = SqpDateTime{ | ||||
// // Remarks : we don't use toSecsSinceEpoch() here (method is for Qt 5.8 or above) | ||||
// static_cast<double>(QDateTime{QDate{2017, 01, 01}, QTime{12, 00}}.toMSecsSinceEpoch() | ||||
// / 1000.), | ||||
// static_cast<double>(QDateTime{QDate{2017, 01, 01}, QTime{12, 01}}.toMSecsSinceEpoch()) | ||||
// / 1000.}; | ||||
if (!impl->m_TimeController) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to create variable: The time controller is null"); | ||||
return; | ||||
} | ||||
Alexandre Leroux
|
r154 | /// @todo : for the moment : | ||
/// - the provider is only used to retrieve data from the variable for its initialization, but | ||||
/// it will be retained later | ||||
/// - default data are generated for the variable, without taking into account the timerange set | ||||
/// in sciqlop | ||||
r209 | auto dateTime = impl->m_TimeController->dateTime(); | |||
r179 | if (auto newVariable = impl->m_VariableModel->createVariable( | |||
r212 | name, dateTime, generateDefaultDataSeries(*provider, dateTime))) { | |||
r209 | ||||
// store in cache | ||||
impl->m_VariableCacheController->addDateTime(newVariable, dateTime); | ||||
// notify the creation | ||||
Alexandre Leroux
|
r154 | emit variableCreated(newVariable); | ||
} | ||||
Alexandre Leroux
|
r143 | } | ||
Alexandre Leroux
|
r106 | void VariableController::initialize() | ||
{ | ||||
qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); | ||||
impl->m_WorkingMutex.lock(); | ||||
qCDebug(LOG_VariableController()) << tr("VariableController init END"); | ||||
} | ||||
void VariableController::finalize() | ||||
{ | ||||
impl->m_WorkingMutex.unlock(); | ||||
} | ||||
void VariableController::waitForFinish() | ||||
{ | ||||
QMutexLocker locker{&impl->m_WorkingMutex}; | ||||
} | ||||