VariableController.cpp
177 lines
| 5.8 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> | ||||
r304 | #include <QtCore/QItemSelectionModel> | |||
|
r111 | |||
r225 | #include <unordered_map> | |||
|
r111 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | ||
|
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 | ||||
r193 | std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, | |||
const SqpDateTime &dateTime) noexcept | ||||
|
r166 | { | ||
r193 | auto parameters = DataProviderParameters{dateTime}; | |||
|
r166 | |||
return provider.retrieveData(parameters); | ||||
} | ||||
} // namespace | ||||
|
r111 | 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; | ||||
|
r111 | }; | ||
VariableController::VariableController(QObject *parent) | ||||
|
r159 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | ||
|
r111 | { | ||
r134 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |||
<< QThread::currentThread(); | ||||
|
r111 | } | ||
VariableController::~VariableController() | ||||
{ | ||||
r134 | 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; | ||||
} | ||||
|
r166 | void VariableController::createVariable(const QString &name, | ||
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; | ||||
} | ||||
|
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(); | |||
r193 | if (auto newVariable = impl->m_VariableModel->createVariable( | |||
r228 | name, dateTime, generateDefaultDataSeries(*provider, dateTime))) { | |||
r225 | ||||
r235 | // store the provider | |||
impl->m_VariableToProviderMap[newVariable] = provider; | ||||
qRegisterMetaType<std::shared_ptr<IDataSeries> >(); | ||||
qRegisterMetaType<SqpDateTime>(); | ||||
connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(), | ||||
&Variable::onAddDataSeries); | ||||
r225 | // store in cache | |||
impl->m_VariableCacheController->addDateTime(newVariable, dateTime); | ||||
// notify the creation | ||||
|
r166 | emit variableCreated(newVariable); | ||
} | ||||
|
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)); | ||||
// store in cache | ||||
impl->m_VariableCacheController->addDateTime(variable, dateTime); | ||||
} | ||||
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}; | ||||
} | ||||