VariableController.cpp
207 lines
| 6.8 KiB
| text/x-c
|
CppLexer
r235 | #include <Variable/Variable.h> | |||
r225 | #include <Variable/VariableCacheController.h> | |||
Alexandre Leroux
|
r111 | #include <Variable/VariableController.h> | ||
Alexandre Leroux
|
r113 | #include <Variable/VariableModel.h> | ||
Alexandre Leroux
|
r111 | |||
Alexandre Leroux
|
r166 | #include <Data/DataProviderParameters.h> | ||
#include <Data/IDataProvider.h> | ||||
#include <Data/IDataSeries.h> | ||||
r193 | #include <Time/TimeController.h> | |||
Alexandre Leroux
|
r166 | |||
#include <QDateTime> | ||||
Alexandre Leroux
|
r111 | #include <QMutex> | ||
#include <QThread> | ||||
r304 | #include <QtCore/QItemSelectionModel> | |||
Alexandre Leroux
|
r111 | |||
r225 | #include <unordered_map> | |||
Alexandre Leroux
|
r111 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
Alexandre Leroux
|
r166 | namespace { | ||
/// @todo Generates default dataseries, according to the provider passed in parameter. This method | ||||
/// will be deleted when the timerange is recovered from SciQlop | ||||
Alexandre Leroux
|
r310 | std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, | ||
r193 | const SqpDateTime &dateTime) noexcept | |||
Alexandre Leroux
|
r166 | { | ||
r193 | auto parameters = DataProviderParameters{dateTime}; | |||
Alexandre Leroux
|
r166 | |||
return provider.retrieveData(parameters); | ||||
} | ||||
} // namespace | ||||
Alexandre Leroux
|
r111 | struct VariableController::VariableControllerPrivate { | ||
Alexandre Leroux
|
r159 | explicit VariableControllerPrivate(VariableController *parent) | ||
r225 | : m_WorkingMutex{}, | |||
m_VariableModel{new VariableModel{parent}}, | ||||
r304 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, | |||
r225 | m_VariableCacheController{std::make_unique<VariableCacheController>()} | |||
Alexandre Leroux
|
r111 | { | ||
} | ||||
QMutex m_WorkingMutex; | ||||
Alexandre Leroux
|
r159 | /// Variable model. The VariableController has the ownership | ||
VariableModel *m_VariableModel; | ||||
r304 | QItemSelectionModel *m_VariableSelectionModel; | |||
r193 | ||||
r225 | ||||
r195 | TimeController *m_TimeController{nullptr}; | |||
r225 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |||
r235 | ||||
std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | ||||
m_VariableToProviderMap; | ||||
Alexandre Leroux
|
r111 | }; | ||
VariableController::VariableController(QObject *parent) | ||||
Alexandre Leroux
|
r159 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
Alexandre Leroux
|
r111 | { | ||
r134 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
Alexandre Leroux
|
r111 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r134 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |||
<< QThread::currentThread(); | ||||
Alexandre Leroux
|
r111 | this->waitForFinish(); | ||
} | ||||
Alexandre Leroux
|
r166 | VariableModel *VariableController::variableModel() noexcept | ||
Alexandre Leroux
|
r113 | { | ||
Alexandre Leroux
|
r166 | return impl->m_VariableModel; | ||
Alexandre Leroux
|
r113 | } | ||
r304 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |||
{ | ||||
return impl->m_VariableSelectionModel; | ||||
} | ||||
r193 | void VariableController::setTimeController(TimeController *timeController) noexcept | |||
{ | ||||
impl->m_TimeController = timeController; | ||||
} | ||||
Alexandre Leroux
|
r329 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | ||||
return; | ||||
} | ||||
Alexandre Leroux
|
r335 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | ||
// make some treatments before the deletion | ||||
emit variableAboutToBeDeleted(variable); | ||||
Alexandre Leroux
|
r330 | |||
// Deletes provider | ||||
auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); | ||||
qCDebug(LOG_VariableController()) | ||||
<< tr("Number of providers deleted for variable %1: %2") | ||||
.arg(variable->name(), QString::number(nbProvidersDeleted)); | ||||
Alexandre Leroux
|
r329 | |||
Alexandre Leroux
|
r331 | // Clears cache | ||
impl->m_VariableCacheController->clear(variable); | ||||
Alexandre Leroux
|
r332 | // Deletes from model | ||
impl->m_VariableModel->deleteVariable(variable); | ||||
} | ||||
Alexandre Leroux
|
r329 | |||
void VariableController::deleteVariables( | ||||
const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
{ | ||||
for (auto variable : qAsConst(variables)) { | ||||
deleteVariable(variable); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r166 | void VariableController::createVariable(const QString &name, | ||
std::shared_ptr<IDataProvider> provider) noexcept | ||||
Alexandre Leroux
|
r152 | { | ||
r193 | ||||
if (!impl->m_TimeController) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to create variable: The time controller is null"); | ||||
return; | ||||
} | ||||
Alexandre Leroux
|
r166 | /// @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 | ||||
r225 | auto dateTime = impl->m_TimeController->dateTime(); | |||
r319 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |||
r225 | ||||
r235 | // store the provider | |||
impl->m_VariableToProviderMap[newVariable] = provider; | ||||
r318 | ||||
Alexandre Leroux
|
r361 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( | ||
auto dataSeriesAcquired, auto dateTimeToPutInCache) | ||||
{ | ||||
if (auto variable = varW.lock()) { | ||||
impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); | ||||
variable->setDataSeries(dataSeriesAcquired); | ||||
} | ||||
}; | ||||
r318 | ||||
connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | ||||
r319 | this->onRequestDataLoading(newVariable, dateTime); | |||
Alexandre Leroux
|
r166 | } | ||
Alexandre Leroux
|
r152 | } | ||
r304 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) | |||
{ | ||||
auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); | ||||
for (const auto &selectedRow : qAsConst(selectedRows)) { | ||||
if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { | ||||
selectedVariable->setDateTime(dateTime); | ||||
r305 | this->onRequestDataLoading(selectedVariable, dateTime); | |||
r304 | } | |||
} | ||||
} | ||||
r235 | ||||
r298 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |||
const SqpDateTime &dateTime) | ||||
r235 | { | |||
// 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); | ||||
r298 | if (!dateTimeListNotInCache.empty()) { | |||
// Ask the provider for each data on the dateTimeListNotInCache | ||||
impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | ||||
std::move(dateTimeListNotInCache)); | ||||
} | ||||
else { | ||||
emit variable->updated(); | ||||
} | ||||
r235 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r111 | 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}; | ||||
} | ||||