VariableController.cpp
238 lines
| 8.3 KiB
| text/x-c
|
CppLexer
r235 | #include <Variable/Variable.h> | |||
r225 | #include <Variable/VariableCacheController.h> | |||
|
r111 | #include <Variable/VariableController.h> | ||
|
r113 | #include <Variable/VariableModel.h> | ||
|
r111 | |||
|
r166 | #include <Data/DataProviderParameters.h> | ||
#include <Data/IDataProvider.h> | ||||
#include <Data/IDataSeries.h> | ||||
r193 | #include <Time/TimeController.h> | |||
|
r166 | |||
#include <QDateTime> | ||||
|
r111 | #include <QMutex> | ||
#include <QThread> | ||||
|
r376 | #include <QUuid> | ||
r304 | #include <QtCore/QItemSelectionModel> | |||
|
r111 | |||
r225 | #include <unordered_map> | |||
|
r111 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
struct VariableController::VariableControllerPrivate { | ||||
|
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>()} | |||
|
r111 | { | ||
} | ||||
QMutex m_WorkingMutex; | ||||
|
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; | ||||
|
r420 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; | ||
|
r111 | }; | ||
VariableController::VariableController(QObject *parent) | ||||
|
r159 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
|
r111 | { | ||
r365 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
r422 | ||||
connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, | ||||
&VariableController::onAbortProgressRequested); | ||||
|
r111 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r365 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |||
<< QThread::currentThread(); | ||||
|
r111 | this->waitForFinish(); | ||
} | ||||
|
r166 | VariableModel *VariableController::variableModel() noexcept | ||
|
r113 | { | ||
|
r166 | return impl->m_VariableModel; | ||
|
r113 | } | ||
r304 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |||
{ | ||||
return impl->m_VariableSelectionModel; | ||||
} | ||||
r193 | void VariableController::setTimeController(TimeController *timeController) noexcept | |||
{ | ||||
impl->m_TimeController = timeController; | ||||
} | ||||
|
r329 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | ||||
return; | ||||
} | ||||
|
r335 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | ||
// make some treatments before the deletion | ||||
emit variableAboutToBeDeleted(variable); | ||||
|
r330 | |||
|
r420 | // Deletes identifier | ||
impl->m_VariableToIdentifierMap.erase(variable); | ||||
|
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)); | ||||
|
r329 | |||
|
r331 | // Clears cache | ||
impl->m_VariableCacheController->clear(variable); | ||||
|
r332 | // Deletes from model | ||
impl->m_VariableModel->deleteVariable(variable); | ||||
} | ||||
|
r329 | |||
void VariableController::deleteVariables( | ||||
const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
{ | ||||
for (auto variable : qAsConst(variables)) { | ||||
deleteVariable(variable); | ||||
} | ||||
} | ||||
r422 | void VariableController::abortProgress(std::shared_ptr<Variable> variable) | |||
{ | ||||
} | ||||
|
r410 | void VariableController::createVariable(const QString &name, const QVariantHash &metadata, | ||
|
r166 | std::shared_ptr<IDataProvider> provider) noexcept | ||
|
r152 | { | ||
r193 | ||||
if (!impl->m_TimeController) { | ||||
qCCritical(LOG_VariableController()) | ||||
<< tr("Impossible to create variable: The time controller is null"); | ||||
return; | ||||
} | ||||
r225 | auto dateTime = impl->m_TimeController->dateTime(); | |||
|
r410 | |||
if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { | ||||
r389 | auto identifier = QUuid::createUuid(); | |||
r225 | ||||
r235 | // store the provider | |||
impl->m_VariableToProviderMap[newVariable] = provider; | ||||
|
r420 | impl->m_VariableToIdentifierMap[newVariable] = identifier; | ||
r318 | ||||
|
r361 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( | ||
r389 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) | |||
|
r361 | { | ||
if (auto variable = varW.lock()) { | ||||
|
r420 | auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable); | ||
r389 | if (varIdentifier == identifier) { | |||
|
r376 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); | ||
variable->setDataSeries(dataSeriesAcquired); | ||||
} | ||||
|
r361 | } | ||
}; | ||||
r318 | ||||
connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | ||||
r319 | this->onRequestDataLoading(newVariable, dateTime); | |||
|
r166 | } | ||
|
r152 | } | ||
r304 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) | |||
{ | ||||
r365 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" | |||
<< QThread::currentThread()->objectName(); | ||||
r304 | 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 | } | |||
} | ||||
} | ||||
r401 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) | |||
{ | ||||
auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; | ||||
|
r420 | auto end = impl->m_VariableToIdentifierMap.cend(); | ||
auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply); | ||||
r401 | if (it != end) { | |||
impl->m_VariableModel->setDataProgress(it->first, progress); | ||||
} | ||||
} | ||||
r422 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) | |||
{ | ||||
qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested" | ||||
<< QThread::currentThread()->objectName(); | ||||
auto it = impl->m_VariableToIdentifier.find(variable); | ||||
if (it != impl->m_VariableToIdentifier.cend()) { | ||||
impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second); | ||||
} | ||||
else { | ||||
qCWarning(LOG_VariableController()) | ||||
<< tr("Aborting progression of inexistant variable detected !!!") | ||||
<< QThread::currentThread()->objectName(); | ||||
} | ||||
} | ||||
r235 | ||||
r298 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |||
const SqpDateTime &dateTime) | ||||
r235 | { | |||
r365 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" | |||
<< QThread::currentThread()->objectName(); | ||||
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 | ||||
|
r420 | auto identifier = impl->m_VariableToIdentifierMap.at(variable); | ||
r298 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | |||
|
r411 | identifier, | ||
DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); | ||||
r298 | } | |||
else { | ||||
emit variable->updated(); | ||||
} | ||||
r235 | } | |||
else { | ||||
qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | ||||
} | ||||
} | ||||
|
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}; | ||||
} | ||||