VariableController.cpp
209 lines
| 6.7 KiB
| text/x-c
|
CppLexer
r219 | #include <Variable/Variable.h> | |||
r209 | #include <Variable/VariableCacheController.h> | |||
|
r106 | #include <Variable/VariableController.h> | ||
|
r108 | #include <Variable/VariableModel.h> | ||
|
r106 | |||
|
r154 | #include <Data/DataProviderParameters.h> | ||
#include <Data/IDataProvider.h> | ||||
#include <Data/IDataSeries.h> | ||||
r179 | #include <Time/TimeController.h> | |||
|
r154 | |||
#include <QDateTime> | ||||
|
r106 | #include <QMutex> | ||
#include <QThread> | ||||
r281 | #include <QtCore/QItemSelectionModel> | |||
|
r106 | |||
r209 | #include <unordered_map> | |||
|
r106 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
|
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 | ||||
|
r287 | std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, | ||
r179 | const SqpDateTime &dateTime) noexcept | |||
|
r154 | { | ||
r179 | auto parameters = DataProviderParameters{dateTime}; | |||
|
r154 | |||
return provider.retrieveData(parameters); | ||||
} | ||||
} // namespace | ||||
|
r106 | struct VariableController::VariableControllerPrivate { | ||
|
r148 | explicit VariableControllerPrivate(VariableController *parent) | ||
r209 | : m_WorkingMutex{}, | |||
m_VariableModel{new VariableModel{parent}}, | ||||
r281 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, | |||
r209 | m_VariableCacheController{std::make_unique<VariableCacheController>()} | |||
|
r106 | { | ||
} | ||||
QMutex m_WorkingMutex; | ||||
|
r148 | /// Variable model. The VariableController has the ownership | ||
VariableModel *m_VariableModel; | ||||
r281 | QItemSelectionModel *m_VariableSelectionModel; | |||
r179 | ||||
r209 | ||||
r181 | TimeController *m_TimeController{nullptr}; | |||
r209 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |||
r219 | ||||
std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | ||||
m_VariableToProviderMap; | ||||
|
r106 | }; | ||
VariableController::VariableController(QObject *parent) | ||||
|
r148 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
|
r106 | { | ||
r126 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
|
r106 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r126 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |||
<< QThread::currentThread(); | ||||
|
r106 | this->waitForFinish(); | ||
} | ||||
|
r154 | VariableModel *VariableController::variableModel() noexcept | ||
|
r108 | { | ||
|
r154 | return impl->m_VariableModel; | ||
|
r108 | } | ||
r281 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |||
{ | ||||
return impl->m_VariableSelectionModel; | ||||
} | ||||
r179 | void VariableController::setTimeController(TimeController *timeController) noexcept | |||
{ | ||||
impl->m_TimeController = timeController; | ||||
} | ||||
|
r303 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | ||||
return; | ||||
} | ||||
|
r309 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | ||
// make some treatments before the deletion | ||||
emit variableAboutToBeDeleted(variable); | ||||
|
r304 | |||
// 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)); | ||||
|
r303 | |||
|
r305 | // Clears cache | ||
impl->m_VariableCacheController->clear(variable); | ||||
|
r306 | // Deletes from model | ||
impl->m_VariableModel->deleteVariable(variable); | ||||
} | ||||
|
r303 | |||
void VariableController::deleteVariables( | ||||
const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
{ | ||||
for (auto variable : qAsConst(variables)) { | ||||
deleteVariable(variable); | ||||
} | ||||
} | ||||
|
r154 | void VariableController::createVariable(const QString &name, | ||
std::shared_ptr<IDataProvider> provider) noexcept | ||||
|
r143 | { | ||
r179 | ||||
if (!impl->m_TimeController) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to create variable: The time controller is null"); | ||||
return; | ||||
} | ||||
|
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(); | |||
r294 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |||
r209 | ||||
r219 | // store the provider | |||
impl->m_VariableToProviderMap[newVariable] = provider; | ||||
r293 | ||||
auto addDateTimeAcquired | ||||
= [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { | ||||
impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); | ||||
newVariable->setDataSeries(dataSeriesAcquired); | ||||
}; | ||||
connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | ||||
r294 | this->onRequestDataLoading(newVariable, dateTime); | |||
r209 | ||||
// notify the creation | ||||
|
r154 | emit variableCreated(newVariable); | ||
} | ||||
|
r143 | } | ||
r281 | 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); | ||||
r282 | this->onRequestDataLoading(selectedVariable, dateTime); | |||
r281 | } | |||
} | ||||
} | ||||
r219 | ||||
r276 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |||
const SqpDateTime &dateTime) | ||||
r219 | { | |||
// 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); | ||||
r276 | 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(); | ||||
} | ||||
r219 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | ||||
} | ||||
} | ||||
|
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}; | ||||
} | ||||