Ref T292, using the central loader in UI component plus adjustments as of other changes

This commit is contained in:
Klaus Basan
2018-07-22 18:18:51 +02:00
parent 98e60b41ac
commit 5faace2bb2
2 changed files with 49 additions and 85 deletions

View File

@@ -50,26 +50,22 @@ namespace BlackGui
ui->tvp_OwnAircraftModels->setSimulatorForLoading(ui->comp_SimulatorSelector->getValue()); ui->tvp_OwnAircraftModels->setSimulatorForLoading(ui->comp_SimulatorSelector->getValue());
connect(ui->tvp_OwnAircraftModels, &CAircraftModelView::requestUpdate, this, &CDbOwnModelsComponent::requestOwnModelsUpdate); connect(ui->tvp_OwnAircraftModels, &CAircraftModelView::requestUpdate, this, &CDbOwnModelsComponent::requestOwnModelsUpdate);
connect(ui->comp_SimulatorSelector, &CSimulatorSelector::changed, this, &CDbOwnModelsComponent::onSimulatorSelectorChanged);
connect(&CMultiAircraftModelLoaderProvider::multiModelLoaderInstance(), &CMultiAircraftModelLoaderProvider::loadingFinished, this, &CDbOwnModelsComponent::onOwnModelsLoadingFinished, Qt::QueuedConnection);
// Last selection isPinned -> no sync needed // Last selection isPinned -> no sync needed
ui->comp_SimulatorSelector->setRememberSelectionAndSetToLastSelection(); ui->comp_SimulatorSelector->setRememberSelectionAndSetToLastSelection();
const CSimulatorInfo simulator = ui->comp_SimulatorSelector->getValue(); const CSimulatorInfo simulator = ui->comp_SimulatorSelector->getValue();
if (simulator.isSingleSimulator()) if (simulator.isSingleSimulator())
{ {
const bool success = this->initModelLoader(simulator); m_simulator = simulator;
if (success) const bool success = this->initModelLoader(simulator, IAircraftModelLoader::CacheOnly);
{ if (!success)
m_modelLoader->startLoading(IAircraftModelLoader::CacheOnly);
m_simulator = simulator;
}
else
{ {
CLogMessage(this).error("Init of model loader failed in component"); CLogMessage(this).error("Init of model loader failed in component");
} }
} }
connect(ui->comp_SimulatorSelector, &CSimulatorSelector::changed, this, &CDbOwnModelsComponent::onSimulatorSelectorChanged);
// menu // menu
ui->tvp_OwnAircraftModels->setCustomMenu(new CConsolidateWithDbDataMenu(ui->tvp_OwnAircraftModels, this, false)); ui->tvp_OwnAircraftModels->setCustomMenu(new CConsolidateWithDbDataMenu(ui->tvp_OwnAircraftModels, this, false));
} }
@@ -95,11 +91,6 @@ namespace BlackGui
return ui->tvp_OwnAircraftModels->derivedModel(); return ui->tvp_OwnAircraftModels->derivedModel();
} }
IAircraftModelLoader *CDbOwnModelsComponent::modelLoader() const
{
return m_modelLoader.get();
}
bool CDbOwnModelsComponent::requestModelsInBackground(const CSimulatorInfo &simulator, bool onlyIfNotEmpty) bool CDbOwnModelsComponent::requestModelsInBackground(const CSimulatorInfo &simulator, bool onlyIfNotEmpty)
{ {
this->setSimulator(simulator); this->setSimulator(simulator);
@@ -111,22 +102,19 @@ namespace BlackGui
CAircraftModel CDbOwnModelsComponent::getOwnModelForModelString(const QString &modelString) const CAircraftModel CDbOwnModelsComponent::getOwnModelForModelString(const QString &modelString) const
{ {
if (!m_modelLoader) { return CAircraftModel(); } return this->getOwnModels().findFirstByModelStringOrDefault(modelString);
return m_modelLoader->getAircraftModels().findFirstByModelStringOrDefault(modelString);
} }
CAircraftModelList CDbOwnModelsComponent::getOwnModels() const CAircraftModelList CDbOwnModelsComponent::getOwnModels() const
{ {
static const CAircraftModelList empty; return this->getOwnCachedModels(this->getOwnModelsSimulator());
if (!m_modelLoader) { return empty; }
return m_modelLoader->getAircraftModels();
} }
CAircraftModelList CDbOwnModelsComponent::getOwnCachedModels(const CSimulatorInfo &simulator) const CAircraftModelList CDbOwnModelsComponent::getOwnCachedModels(const CSimulatorInfo &simulator) const
{ {
static const CAircraftModelList empty; static const CAircraftModelList empty;
if (!m_modelLoader) { return empty; } if (!m_modelLoader) { return empty; }
return m_modelLoader->getCachedAircraftModels(simulator); return m_modelLoader->getCachedModels(simulator);
} }
CAircraftModelList CDbOwnModelsComponent::getOwnSelectedModels() const CAircraftModelList CDbOwnModelsComponent::getOwnSelectedModels() const
@@ -136,9 +124,7 @@ namespace BlackGui
const CSimulatorInfo CDbOwnModelsComponent::getOwnModelsSimulator() const const CSimulatorInfo CDbOwnModelsComponent::getOwnModelsSimulator() const
{ {
static const CSimulatorInfo noSim; return ui->comp_SimulatorSelector->getValue();
if (!m_modelLoader) { return noSim; }
return m_modelLoader->getSimulator();
} }
void CDbOwnModelsComponent::setSimulator(const CSimulatorInfo &simulator) void CDbOwnModelsComponent::setSimulator(const CSimulatorInfo &simulator)
@@ -163,19 +149,19 @@ namespace BlackGui
int CDbOwnModelsComponent::getOwnModelsCount() const int CDbOwnModelsComponent::getOwnModelsCount() const
{ {
if (!m_modelLoader) { return 0; } if (!m_modelLoader) { return 0; }
return m_modelLoader->getAircraftModelsCount(); return m_modelLoader->getCachedModelsCount(this->getOwnModelsSimulator());
} }
QString CDbOwnModelsComponent::getInfoString() const QString CDbOwnModelsComponent::getInfoString() const
{ {
if (!m_modelLoader) { return ""; } if (!m_modelLoader) { return ""; }
return m_modelLoader->getModelCacheInfoString(); return m_modelLoader->getInfoString();
} }
QString CDbOwnModelsComponent::getInfoStringFsFamily() const QString CDbOwnModelsComponent::getInfoStringFsFamily() const
{ {
if (!m_modelLoader) { return ""; } if (!m_modelLoader) { return ""; }
return m_modelLoader->getModelCacheInfoStringFsFamily(); return m_modelLoader->getInfoStringFsFamily();
} }
CStatusMessage CDbOwnModelsComponent::updateViewAndCache(const CAircraftModelList &models) CStatusMessage CDbOwnModelsComponent::updateViewAndCache(const CAircraftModelList &models)
@@ -190,58 +176,38 @@ namespace BlackGui
void CDbOwnModelsComponent::gracefulShutdown() void CDbOwnModelsComponent::gracefulShutdown()
{ {
if (m_modelLoader) { m_modelLoader->gracefulShutdown(); } // void
} }
void CDbOwnModelsComponent::setModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) void CDbOwnModelsComponent::setModelsForSimulator(const CAircraftModelList &models, const CSimulatorInfo &simulator)
{ {
this->modelLoader()->setCachedModels(models, simulator); if (!m_modelLoader) { return; }
m_modelLoader->setCachedModels(models, simulator);
ui->tvp_OwnAircraftModels->replaceOrAddModelsWithString(models); ui->tvp_OwnAircraftModels->replaceOrAddModelsWithString(models);
} }
void CDbOwnModelsComponent::updateModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) int CDbOwnModelsComponent::updateModelsForSimulator(const CAircraftModelList &models, const CSimulatorInfo &simulator)
{ {
this->modelLoader()->replaceOrAddCachedModels(models, simulator); if (!m_modelLoader) { return 0; }
const CAircraftModelList allModels(m_modelLoader->getAircraftModels()); const int c = m_modelLoader->updateModelsForSimulator(models, simulator);
const CAircraftModelList allModels(m_modelLoader->getCachedModels(simulator));
ui->tvp_OwnAircraftModels->updateContainerMaybeAsync(allModels); ui->tvp_OwnAircraftModels->updateContainerMaybeAsync(allModels);
return c;
} }
bool CDbOwnModelsComponent::initModelLoader(const CSimulatorInfo &simulator) bool CDbOwnModelsComponent::initModelLoader(const CSimulatorInfo &simulator, IAircraftModelLoader::LoadMode load)
{ {
// called when simulator is changed / init // called when simulator is changed / init
Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator"); Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Need single simulator");
// already loaded // already loaded
if (m_modelLoader && m_modelLoader->supportsSimulator(simulator))
{
this->setSaveFileName(simulator);
return true;
}
// mismatching loader
if (m_modelLoader)
{
m_modelLoader->gracefulShutdown();
m_loaderConnections.disconnectAll();
}
// create loader, also synchronizes the caches
m_modelLoader = IAircraftModelLoader::createModelLoader(simulator); // last selected simulator or explicit given
if (!m_modelLoader || !m_modelLoader->supportsSimulator(simulator)) if (!m_modelLoader || !m_modelLoader->supportsSimulator(simulator))
{ {
CLogMessage(this).error("Failed to init model loader %1") << simulator.toQString(); m_modelLoader = CMultiAircraftModelLoaderProvider::multiModelLoaderInstance().loaderInstance(simulator);
m_modelLoader.reset(); if (m_modelLoader) { m_modelLoader->startLoading(load); }
m_loaderConnections.disconnectAll();
return false;
}
else
{
const QMetaObject::Connection connection = connect(m_modelLoader.get(), &IAircraftModelLoader::loadingFinished, this, &CDbOwnModelsComponent::onOwnModelsLoadingFinished, Qt::QueuedConnection);
Q_ASSERT_X(connection, Q_FUNC_INFO, "Failed connect for model loader");
m_loaderConnections.append(connection);
this->setSaveFileName(simulator);
return true;
} }
this->setSaveFileName(simulator);
return m_modelLoader && m_modelLoader->supportsSimulator(simulator);
} }
void CDbOwnModelsComponent::setSaveFileName(const CSimulatorInfo &sim) void CDbOwnModelsComponent::setSaveFileName(const CSimulatorInfo &sim)
@@ -505,28 +471,26 @@ namespace BlackGui
void CDbOwnModelsComponent::requestOwnModelsUpdate() void CDbOwnModelsComponent::requestOwnModelsUpdate()
{ {
if (!m_modelLoader) { return; } if (!m_modelLoader) { return; }
ui->tvp_OwnAircraftModels->updateContainerMaybeAsync( ui->tvp_OwnAircraftModels->updateContainerMaybeAsync(this->getOwnModels());
m_modelLoader->getAircraftModels()
);
} }
void CDbOwnModelsComponent::loadInstalledModels(const CSimulatorInfo &simulator, IAircraftModelLoader::LoadMode mode, const QStringList &modelDirectories) void CDbOwnModelsComponent::loadInstalledModels(const CSimulatorInfo &simulator, IAircraftModelLoader::LoadMode mode, const QStringList &modelDirectories)
{ {
if (!this->initModelLoader(simulator)) if (!m_modelLoader) { return; }
{
CLogMessage(this).error("Cannot init model loader for %1") << simulator.toQString();
return;
}
if (m_modelLoader->isLoadingInProgress()) if (m_modelLoader->isLoadingInProgress())
{ {
CLogMessage(this).info("Loading for '%1' already in progress") << simulator.toQString(); CLogMessage(this).info("Loading for '%1' already in progress") << simulator.toQString();
return; return;
} }
if (!this->initModelLoader(simulator))
{
CLogMessage(this).error("Cannot init model loader for %1") << simulator.toQString();
return;
}
// Do not check for empty models die here, as depending on mode we could still load // Do not check for empty models die here, as depending on mode we could still load
// will be checked in model loader // will be checked in model loader
CLogMessage(this).info("Starting loading for '%1'") << simulator.toQString(); CLogMessage(this).info("Starting loading for '%1'") << simulator.toQString();
ui->tvp_OwnAircraftModels->showLoadIndicator(); ui->tvp_OwnAircraftModels->showLoadIndicator();
Q_ASSERT_X(sGui && sGui->getWebDataServices(), Q_FUNC_INFO, "missing web data services"); Q_ASSERT_X(sGui && sGui->getWebDataServices(), Q_FUNC_INFO, "missing web data services");
@@ -538,13 +502,13 @@ namespace BlackGui
Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Expect single simulator"); Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Expect single simulator");
if (statusMessages.isSuccess() && m_modelLoader) if (statusMessages.isSuccess() && m_modelLoader)
{ {
const CAircraftModelList models(m_modelLoader->getAircraftModels()); const CAircraftModelList models(m_modelLoader->getCachedModels(simulator));
const int modelsLoaded = models.size(); const int modelsLoaded = models.size();
ui->tvp_OwnAircraftModels->updateContainerMaybeAsync(models); ui->tvp_OwnAircraftModels->updateContainerMaybeAsync(models);
if (modelsLoaded < 1) if (modelsLoaded < 1)
{ {
// loading ok, but no data // loading ok, but no data
CLogMessage(this).warning("Loading completed, but no models"); CLogMessage(this).warning("Loading completed for simulator '%1', but no models") << simulator;
} }
emit this->successfullyLoadedModels(simulator); emit this->successfullyLoadedModels(simulator);
@@ -580,7 +544,8 @@ namespace BlackGui
void CDbOwnModelsComponent::clearSimulatorCache(const CSimulatorInfo &simulator) void CDbOwnModelsComponent::clearSimulatorCache(const CSimulatorInfo &simulator)
{ {
m_modelLoader->clearCache(simulator); if (!m_modelLoader) { return; }
m_modelLoader->clearCachedModels(simulator);
} }
} // ns } // ns
} // ns } // ns

View File

@@ -49,14 +49,14 @@ namespace BlackGui
public COverlayMessagesFrame, public COverlayMessagesFrame,
public BlackMisc::Simulation::IModelsSetable, public BlackMisc::Simulation::IModelsSetable,
public BlackMisc::Simulation::IModelsUpdatable, public BlackMisc::Simulation::IModelsUpdatable,
public BlackMisc::Simulation::IModelsPerSimulatorSetable, public BlackMisc::Simulation::IModelsForSimulatorSetable,
public BlackMisc::Simulation::IModelsPerSimulatorUpdatable public BlackMisc::Simulation::IModelsForSimulatorUpdatable
{ {
Q_OBJECT Q_OBJECT
Q_INTERFACES(BlackMisc::Simulation::IModelsSetable) Q_INTERFACES(BlackMisc::Simulation::IModelsSetable)
Q_INTERFACES(BlackMisc::Simulation::IModelsUpdatable) Q_INTERFACES(BlackMisc::Simulation::IModelsUpdatable)
Q_INTERFACES(BlackMisc::Simulation::IModelsPerSimulatorSetable) Q_INTERFACES(BlackMisc::Simulation::IModelsForSimulatorSetable)
Q_INTERFACES(BlackMisc::Simulation::IModelsPerSimulatorUpdatable) Q_INTERFACES(BlackMisc::Simulation::IModelsForSimulatorUpdatable)
public: public:
//! Constructor //! Constructor
@@ -105,7 +105,7 @@ namespace BlackGui
Models::CAircraftModelListModel *model() const; Models::CAircraftModelListModel *model() const;
//! Access to model loader //! Access to model loader
BlackMisc::Simulation::IAircraftModelLoader *modelLoader() const; BlackMisc::Simulation::IAircraftModelLoader *modelLoader() const { return m_modelLoader; }
//! Forced read for given simulator //! Forced read for given simulator
bool requestModelsInBackground(const BlackMisc::Simulation::CSimulatorInfo &simulator, bool onlyIfNotEmpty); bool requestModelsInBackground(const BlackMisc::Simulation::CSimulatorInfo &simulator, bool onlyIfNotEmpty);
@@ -115,10 +115,10 @@ namespace BlackGui
//! \name Implementations of the models interfaces //! \name Implementations of the models interfaces
//! @{ //! @{
virtual void setModels(const BlackMisc::Simulation::CAircraftModelList &models) override { this->setModels(models, this->getOwnModelsSimulator()); } virtual void setModels(const BlackMisc::Simulation::CAircraftModelList &models) override { this->setModelsForSimulator(models, this->getOwnModelsSimulator()); }
virtual void updateModels(const BlackMisc::Simulation::CAircraftModelList &models) override { this->updateModels(models, this->getOwnModelsSimulator()); } virtual void setModelsForSimulator(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) override;
virtual void setModels(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) override; virtual int updateModels(const BlackMisc::Simulation::CAircraftModelList &models) override { return this->updateModelsForSimulator(models, this->getOwnModelsSimulator()); }
virtual void updateModels(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) override; virtual int updateModelsForSimulator(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) override;
//! @} //! @}
signals: signals:
@@ -127,8 +127,7 @@ namespace BlackGui
private: private:
QScopedPointer<Ui::CDbOwnModelsComponent> ui; QScopedPointer<Ui::CDbOwnModelsComponent> ui;
std::unique_ptr<BlackMisc::Simulation::IAircraftModelLoader> m_modelLoader; //!< read own aircraft models, aka models on disk BlackMisc::Simulation::IAircraftModelLoader *m_modelLoader = nullptr; //!< read own aircraft models, aka models on disk
BlackMisc::CConnectionGuard m_loaderConnections;
BlackMisc::Simulation::CSimulatorInfo m_simulator; //!< currently init to simulator BlackMisc::Simulation::CSimulatorInfo m_simulator; //!< currently init to simulator
//! Request own models //! Request own models
@@ -153,7 +152,7 @@ namespace BlackGui
void onSimulatorSelectorChanged(); void onSimulatorSelectorChanged();
//! Init or change model loader //! Init or change model loader
bool initModelLoader(const BlackMisc::Simulation::CSimulatorInfo &simulator); bool initModelLoader(const BlackMisc::Simulation::CSimulatorInfo &simulator, BlackMisc::Simulation::IAircraftModelLoader::LoadMode load = BlackMisc::Simulation::IAircraftModelLoader::NotSet);
//! File name for saving as file //! File name for saving as file
void setSaveFileName(const BlackMisc::Simulation::CSimulatorInfo &sim); void setSaveFileName(const BlackMisc::Simulation::CSimulatorInfo &sim);