From 875b311ede7a68c59c19188ad898611aa361d553 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 29 May 2016 12:37:03 +0200 Subject: [PATCH] refs #649, with #659 deferred caches available * adjustments to use deferred caches * removed simulator from CAircraftModelSetLoader`s signature as it was not used * only change current simulator when explicitly set (avoid unintended setting) * added function to obtain number of elements from model caches --- src/blackcore/db/databasereader.cpp | 11 +++- src/blackcore/db/databasereader.h | 5 +- src/blackcore/db/icaodatareader.cpp | 13 ++++- src/blackcore/db/icaodatareader.h | 3 +- src/blackcore/db/infodatareader.cpp | 9 ++- src/blackcore/db/infodatareader.h | 3 +- src/blackcore/db/modeldatareader.cpp | 8 ++- src/blackcore/db/modeldatareader.h | 3 +- src/blackcore/simulatorcommon.h | 2 +- .../components/dbownmodelsetcomponent.cpp | 32 +++++----- .../components/dbownmodelsetcomponent.h | 17 ++++-- .../components/dbownmodelsetdialog.cpp | 4 +- .../distributorpreferencescomponent.h | 2 +- .../components/modelmatchercomponent.h | 2 +- .../simulation/aircraftmodelsetloader.cpp | 4 +- .../simulation/aircraftmodelsetloader.h | 2 +- src/blackmisc/simulation/data/modelcaches.cpp | 58 +++++++++++-------- src/blackmisc/simulation/data/modelcaches.h | 9 +-- .../simulation/fscommon/fscommonutil.cpp | 2 +- 19 files changed, 123 insertions(+), 66 deletions(-) diff --git a/src/blackcore/db/databasereader.cpp b/src/blackcore/db/databasereader.cpp index 28f7a43f4..dd535059e 100644 --- a/src/blackcore/db/databasereader.cpp +++ b/src/blackcore/db/databasereader.cpp @@ -51,7 +51,6 @@ namespace BlackCore CEntityFlags::Entity allEntities = entities; CEntityFlags::Entity currentEntity = CEntityFlags::iterateDbEntities(allEntities); // CEntityFlags::InfoObjectEntity will be ignored const bool hasInfoObjects = this->hasInfoObjects(); - const bool changedUrl = this->hasChangedUrl(currentEntity); while (currentEntity) { const CDatabaseReaderConfig config(this->getConfigForEntity(currentEntity)); @@ -59,6 +58,7 @@ namespace BlackCore { if (hasInfoObjects) { + const bool changedUrl = this->hasChangedUrl(currentEntity); const QDateTime cacheTs(this->getCacheTimestamp(currentEntity)); const QDateTime latestEntityTs(this->getLatestEntityTimestamp(currentEntity)); const qint64 cacheTimestamp = cacheTs.isValid() ? cacheTs.toMSecsSinceEpoch() : -1; @@ -76,7 +76,8 @@ namespace BlackCore { if (changedUrl) { - CLogMessage(this).info("Data location changed, will override cache"); + CLogMessage(this).info("Data location changed, will override cache for %1") + << CEntityFlags::flagToString(currentEntity); } else { @@ -90,7 +91,11 @@ namespace BlackCore { // no info objects, server down this->syncronizeCaches(currentEntity); - CLogMessage(this).info("No info object for %1, using cache") << CEntityFlags::flagToString(currentEntity); + const int c = this->getCacheCount(currentEntity); + CLogMessage(this).info("No info object for %1, using cache with %2 objects") + << CEntityFlags::flagToString(currentEntity) + << c; + entities &= ~currentEntity; // do not load from web } } currentEntity = CEntityFlags::iterateDbEntities(allEntities); diff --git a/src/blackcore/db/databasereader.h b/src/blackcore/db/databasereader.h index 4ab098a4f..0d5d49287 100644 --- a/src/blackcore/db/databasereader.h +++ b/src/blackcore/db/databasereader.h @@ -149,7 +149,10 @@ namespace BlackCore virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) = 0; //! Cache`s timestamp for given entity - virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entities) = 0; + virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entities) const = 0; + + //! Cache`s number of entities + virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const = 0; //! Invalidate the caches for given entities virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) = 0; diff --git a/src/blackcore/db/icaodatareader.cpp b/src/blackcore/db/icaodatareader.cpp index a6cef2ffc..ae2c2d70f 100644 --- a/src/blackcore/db/icaodatareader.cpp +++ b/src/blackcore/db/icaodatareader.cpp @@ -399,7 +399,7 @@ namespace BlackCore if (entities.testFlag(CEntityFlags::CountryEntity)) {CDataCache::instance()->clearAllValues(this->m_countryCache.getKey()); } } - QDateTime CIcaoDataReader::getCacheTimestamp(CEntityFlags::Entity entity) + QDateTime CIcaoDataReader::getCacheTimestamp(CEntityFlags::Entity entity) const { switch (entity) { @@ -410,6 +410,17 @@ namespace BlackCore } } + int CIcaoDataReader::getCacheCount(CEntityFlags::Entity entity) const + { + switch (entity) + { + case CEntityFlags::AircraftIcaoEntity: return this->m_aircraftIcaoCache.getCopy().size(); + case CEntityFlags::AirlineIcaoEntity: return this->m_airlineIcaoCache.getCopy().size(); + case CEntityFlags::CountryEntity: return this->m_countryCache.getCopy().size(); + default: return 0; + } + } + bool CIcaoDataReader::hasChangedUrl(CEntityFlags::Entity entity) const { Q_UNUSED(entity); diff --git a/src/blackcore/db/icaodatareader.h b/src/blackcore/db/icaodatareader.h index 91befb1c7..21983f944 100644 --- a/src/blackcore/db/icaodatareader.h +++ b/src/blackcore/db/icaodatareader.h @@ -123,7 +123,8 @@ namespace BlackCore //! @{ virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; - virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override; + virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override; + virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override; virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override; //! @} diff --git a/src/blackcore/db/infodatareader.cpp b/src/blackcore/db/infodatareader.cpp index 3191ad869..3c10760b8 100644 --- a/src/blackcore/db/infodatareader.cpp +++ b/src/blackcore/db/infodatareader.cpp @@ -62,13 +62,20 @@ namespace BlackCore Q_UNUSED(entities); } - QDateTime CInfoDataReader::getCacheTimestamp(CEntityFlags::Entity entity) + QDateTime CInfoDataReader::getCacheTimestamp(CEntityFlags::Entity entity) const { // no caching used here Q_UNUSED(entity); return QDateTime(); } + int CInfoDataReader::getCacheCount(CEntityFlags::Entity entity) const + { + // no caching used here + Q_UNUSED(entity); + return 0; + } + bool CInfoDataReader::hasChangedUrl(CEntityFlags::Entity entity) const { // not implemented diff --git a/src/blackcore/db/infodatareader.h b/src/blackcore/db/infodatareader.h index 49e211461..95083d770 100644 --- a/src/blackcore/db/infodatareader.h +++ b/src/blackcore/db/infodatareader.h @@ -61,7 +61,8 @@ namespace BlackCore //! @{ virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; - virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override; + virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override; + virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override; virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override; //! @} diff --git a/src/blackcore/db/modeldatareader.cpp b/src/blackcore/db/modeldatareader.cpp index faabc55bd..10d876db4 100644 --- a/src/blackcore/db/modeldatareader.cpp +++ b/src/blackcore/db/modeldatareader.cpp @@ -437,12 +437,18 @@ namespace BlackCore Q_UNUSED(entities); } - QDateTime CModelDataReader::getCacheTimestamp(CEntityFlags::Entity entity) + QDateTime CModelDataReader::getCacheTimestamp(CEntityFlags::Entity entity) const { Q_UNUSED(entity); return QDateTime(); } + int CModelDataReader::getCacheCount(CEntityFlags::Entity entity) const + { + Q_UNUSED(entity); + return 0; + } + bool CModelDataReader::hasChangedUrl(CEntityFlags::Entity entity) const { Q_UNUSED(entity); diff --git a/src/blackcore/db/modeldatareader.h b/src/blackcore/db/modeldatareader.h index 92849cc68..12106a6f5 100644 --- a/src/blackcore/db/modeldatareader.h +++ b/src/blackcore/db/modeldatareader.h @@ -128,7 +128,8 @@ namespace BlackCore //! @{ virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override; - virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override; + virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override; + virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override; virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override; //! @} diff --git a/src/blackcore/simulatorcommon.h b/src/blackcore/simulatorcommon.h index 6816e0ef2..d34f9bc87 100644 --- a/src/blackcore/simulatorcommon.h +++ b/src/blackcore/simulatorcommon.h @@ -133,7 +133,7 @@ namespace BlackCore //! \todo unclear if this is valid for all simulators or for MS/P3D simulators only BlackCore::CAircraftMatcher m_modelMatcher; //!< Model matcher - BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { BlackMisc::Simulation::CSimulatorInfo(BlackMisc::Simulation::CSimulatorInfo::FSX), this }; //!< load model set from caches + BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { this }; //!< load model set from caches private: bool m_debugMessages = false; //!< Display debug messages diff --git a/src/blackgui/components/dbownmodelsetcomponent.cpp b/src/blackgui/components/dbownmodelsetcomponent.cpp index 42ad2e4d9..c664bf008 100644 --- a/src/blackgui/components/dbownmodelsetcomponent.cpp +++ b/src/blackgui/components/dbownmodelsetcomponent.cpp @@ -70,17 +70,15 @@ namespace BlackGui connect(ui->comp_SimulatorSelector, &CSimulatorSelector::changed, this, &CDbOwnModelSetComponent::ps_onSimulatorChanged); connect(&this->m_modelSetLoader, &CAircraftModelSetLoader::simulatorChanged, this, &CDbOwnModelSetComponent::ps_onSimulatorChanged); connect(ui->tvp_OwnModelSet, &CAircraftModelView::modelDataChanged, this, &CDbOwnModelSetComponent::ps_onRowCountChanged); - connect(ui->tvp_OwnModelSet, &CAircraftModelView::modelChanged, this, &CDbOwnModelSetComponent::ps_modelChanged); + connect(ui->tvp_OwnModelSet, &CAircraftModelView::modelChanged, this, &CDbOwnModelSetComponent::ps_viewModelChanged); connect(ui->tvp_OwnModelSet, &CAircraftModelView::jsonModelsForSimulatorLoaded, this, &CDbOwnModelSetComponent::ps_onJsonDataLoaded); - const CSimulatorInfo sim = this->m_modelSetLoader.getSimulator(); - if (sim.isSingleSimulator()) + const CSimulatorInfo simulator = this->m_modelSetLoader.getSimulator(); + if (simulator.isSingleSimulator()) { - // update display when all is set up - this->m_modelSetLoader.syncronizeCache(); // make sure data are loaded - QTimer::singleShot(500, [this, sim]() + QTimer::singleShot(500, [this]() { - this->ps_changeSimulator(sim); + this->updateViewToCurrentModels(); }); } } @@ -162,7 +160,7 @@ namespace BlackGui "Cannot add data for " + simulator.toQString(true) + " to " + this->getModelSetSimulator().toQString(true), true); } CAircraftModelList updateModels(this->getModelSet()); - int d = updateModels.replaceOrAddModelsWithString(models, Qt::CaseInsensitive); + const int d = updateModels.replaceOrAddModelsWithString(models, Qt::CaseInsensitive); if (d > 0) { this->ui->tvp_OwnModelSet->updateContainerMaybeAsync(updateModels); @@ -195,7 +193,8 @@ namespace BlackGui { // make sure both tabs display the same simulator Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "Missing mapping component"); - this->getMappingComponent()->setOwnModelsSimulator(this->getModelSetSimulator()); + const CSimulatorInfo sim(this->getModelSetSimulator()); + this->getMappingComponent()->setOwnModelsSimulator(sim); if (!this->m_modelSetDialog) { this->m_modelSetDialog.reset(new CDbOwnModelSetDialog(this)); @@ -214,7 +213,7 @@ namespace BlackGui } else { - static const CStatusMessage m = CStatusMessage(this).error("No model data for %1") << this->m_modelSetDialog->getSimulatorInfo().toQString(true); + static const CStatusMessage m = CStatusMessage(this).error("No model data for %1") << sim.toQString(true); this->getMappingComponent()->showOverlayMessage(m); } } @@ -239,8 +238,7 @@ namespace BlackGui if (this->getModelSetSimulator() == simulator) { return; } // avoid endless loops this->setModelSetSimulator(simulator); - const CAircraftModelList models(this->m_modelSetLoader.getAircraftModels()); - ui->tvp_OwnModelSet->updateContainerMaybeAsync(models); + this->updateViewToCurrentModels(); } void CDbOwnModelSetComponent::ps_onSimulatorChanged(const CSimulatorInfo &simulator) @@ -276,7 +274,7 @@ namespace BlackGui } } - void CDbOwnModelSetComponent::ps_preferencesChanged() + void CDbOwnModelSetComponent::ps_distributorPreferencesChanged() { const CDistributorListPreferences preferences = this->m_distributorPreferences.get(); const CSimulatorInfo simuulator = preferences.getLastUpdatedSimulator(); @@ -286,7 +284,7 @@ namespace BlackGui } } - void CDbOwnModelSetComponent::ps_modelChanged() + void CDbOwnModelSetComponent::ps_viewModelChanged() { ui->pb_SaveAsSetForSimulator->setEnabled(true); } @@ -298,6 +296,12 @@ namespace BlackGui this->ui->tvp_OwnModelSet->setSaveFileName(name); } + void CDbOwnModelSetComponent::updateViewToCurrentModels() + { + const CAircraftModelList models(this->m_modelSetLoader.getAircraftModels()); + ui->tvp_OwnModelSet->updateContainerMaybeAsync(models); + } + void CDbOwnModelSetComponent::setModelSetSimulator(const CSimulatorInfo &simulator) { if (this->m_modelSetLoader.getSimulator() == simulator) { return; } // avoid unnecessary signals diff --git a/src/blackgui/components/dbownmodelsetcomponent.h b/src/blackgui/components/dbownmodelsetcomponent.h index a6740d0b0..25f5b6bb7 100644 --- a/src/blackgui/components/dbownmodelsetcomponent.h +++ b/src/blackgui/components/dbownmodelsetcomponent.h @@ -123,22 +123,27 @@ namespace BlackGui void ps_onJsonDataLoaded(const BlackMisc::Simulation::CSimulatorInfo &simulator); //! Preferences changed - void ps_preferencesChanged(); + void ps_distributorPreferencesChanged(); - //! Model has been changed - void ps_modelChanged(); + //! Model (of view) has been changed + void ps_viewModelChanged(); private: //! Default file name void setSaveFileName(const BlackMisc::Simulation::CSimulatorInfo &sim); + //! Update view to current models + void updateViewToCurrentModels(); + //! Update distributor order void updateDistributorOrder(const BlackMisc::Simulation::CSimulatorInfo &simulator); - QScopedPointer ui; + QScopedPointer ui; QScopedPointer m_modelSetDialog; - BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { BlackMisc::Simulation::CSimulatorInfo(BlackMisc::Simulation::CSimulatorInfo::FSX), this }; - BlackMisc::CSetting m_distributorPreferences { this, &CDbOwnModelSetComponent::ps_preferencesChanged }; + BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { this }; + BlackMisc::CSetting m_distributorPreferences { this, &CDbOwnModelSetComponent::ps_distributorPreferencesChanged }; + + // -------------------------- custom menus ----------------------------------- //! The menu for loading and handling own models for mapping tasks //! \note This is specific for that very component diff --git a/src/blackgui/components/dbownmodelsetdialog.cpp b/src/blackgui/components/dbownmodelsetdialog.cpp index 5c7d70c72..aee32d578 100644 --- a/src/blackgui/components/dbownmodelsetdialog.cpp +++ b/src/blackgui/components/dbownmodelsetdialog.cpp @@ -63,7 +63,9 @@ namespace BlackGui int CDbOwnModelSetDialog::exec() { Q_ASSERT_X(this->getMappingComponent(), Q_FUNC_INFO, "missing mapping component"); - this->setSimulator(this->getMappingComponent()->getOwnModelsSimulator()); + const CSimulatorInfo sim(this->getMappingComponent()->getOwnModelsSimulator()); + Q_ASSERT_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator"); + this->setSimulator(sim); this->checkData(); return QDialog::exec(); } diff --git a/src/blackgui/components/distributorpreferencescomponent.h b/src/blackgui/components/distributorpreferencescomponent.h index 7bdfeab15..4b561d8b7 100644 --- a/src/blackgui/components/distributorpreferencescomponent.h +++ b/src/blackgui/components/distributorpreferencescomponent.h @@ -71,7 +71,7 @@ namespace BlackGui private: BlackGui::COverlayMessagesFrame *m_overlayMessageFrame = nullptr; QScopedPointer ui; - BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { BlackMisc::Simulation::CSimulatorInfo(BlackMisc::Simulation::CSimulatorInfo::FSX), this }; + BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { this }; BlackMisc::CSetting m_distributorPreferences { this, &CDistributorPreferencesComponent::ps_preferencesChanged }; void updateContainerMaybeAsync(const BlackMisc::Simulation::CDistributorList &models, bool sortByOrder = true); diff --git a/src/blackgui/components/modelmatchercomponent.h b/src/blackgui/components/modelmatchercomponent.h index 36b4932cc..634eab9cc 100644 --- a/src/blackgui/components/modelmatchercomponent.h +++ b/src/blackgui/components/modelmatchercomponent.h @@ -77,7 +77,7 @@ namespace BlackGui BlackMisc::Simulation::CAircraftModel defaultModel() const; QScopedPointer ui; - BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { BlackMisc::Simulation::CSimulatorInfo(BlackMisc::Simulation::CSimulatorInfo::FSX), this }; + BlackMisc::Simulation::CAircraftModelSetLoader m_modelSetLoader { this }; BlackCore::CAircraftMatcher m_matcher { BlackCore::CAircraftMatcher::All, this }; }; } // ns diff --git a/src/blackmisc/simulation/aircraftmodelsetloader.cpp b/src/blackmisc/simulation/aircraftmodelsetloader.cpp index 4974ec5ab..65724505f 100644 --- a/src/blackmisc/simulation/aircraftmodelsetloader.cpp +++ b/src/blackmisc/simulation/aircraftmodelsetloader.cpp @@ -18,11 +18,9 @@ namespace BlackMisc { namespace Simulation { - CAircraftModelSetLoader::CAircraftModelSetLoader(const CSimulatorInfo &simulator, QObject *parent) : + CAircraftModelSetLoader::CAircraftModelSetLoader(QObject *parent) : QObject(parent) { - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Only one simulator per loader"); - this->syncronizeCache(); connect(&this->m_caches, &CModelSetCaches::cacheChanged, this, &CAircraftModelSetLoader::cacheChanged); } diff --git a/src/blackmisc/simulation/aircraftmodelsetloader.h b/src/blackmisc/simulation/aircraftmodelsetloader.h index 77c4eb4a9..f07303dfb 100644 --- a/src/blackmisc/simulation/aircraftmodelsetloader.h +++ b/src/blackmisc/simulation/aircraftmodelsetloader.h @@ -46,7 +46,7 @@ namespace BlackMisc public: //! Constructor - CAircraftModelSetLoader(const CSimulatorInfo &simulator, QObject *parent = nullptr); + CAircraftModelSetLoader(QObject *parent = nullptr); //! Destructor virtual ~CAircraftModelSetLoader(); diff --git a/src/blackmisc/simulation/data/modelcaches.cpp b/src/blackmisc/simulation/data/modelcaches.cpp index 1299f6ebd..fb09617d4 100644 --- a/src/blackmisc/simulation/data/modelcaches.cpp +++ b/src/blackmisc/simulation/data/modelcaches.cpp @@ -8,6 +8,7 @@ */ #include "blackmisc/logmessage.h" +#include "blackmisc/verify.h" #include "blackmisc/simulation/data/modelcaches.h" #include @@ -28,6 +29,7 @@ namespace BlackMisc CAircraftModelList IMultiSimulatorModelCaches::getCurrentCachedModels() const { const CSimulatorInfo sim(this->getCurrentSimulator()); + BLACK_VERIFY_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator"); if (!sim.isSingleSimulator()) { return CAircraftModelList(); } return this->getCachedModels(sim); } @@ -35,6 +37,7 @@ namespace BlackMisc bool IMultiSimulatorModelCaches::syncronizeCurrentCache() { const CSimulatorInfo sim(this->getCurrentSimulator()); + BLACK_VERIFY_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator"); if (!sim.isSingleSimulator()) { return false; } this->syncronizeCache(sim); return true; @@ -43,7 +46,10 @@ namespace BlackMisc CModelCaches::CModelCaches(QObject *parent) : IMultiSimulatorModelCaches(parent) { this->m_currentSimulator.synchronize(); - this->syncronizeCache(this->m_currentSimulator.getCopy()); + const CSimulatorInfo sim(this->m_currentSimulator.getCopy()); + this->syncronizeCache(sim); + const QString simStr(sim.toQString(true)); + CLogMessage(this).info("Initialized model caches to %1") << simStr; } CAircraftModelList CModelCaches::getCachedModels(const CSimulatorInfo &simulator) const @@ -56,7 +62,7 @@ namespace BlackMisc case CSimulatorInfo::P3D: return this->m_modelCacheP3D.getCopy(); case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.getCopy(); default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CAircraftModelList(); } } @@ -64,8 +70,6 @@ namespace BlackMisc CStatusMessage CModelCaches::setCachedModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) { Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); - const CStatusMessage m = this->m_currentSimulator.set(simulator); - if (m.isFailure()) { return m; } switch (simulator.getSimulator()) { case CSimulatorInfo::FS9: return this->m_modelCacheFs9.set(models); @@ -73,7 +77,7 @@ namespace BlackMisc case CSimulatorInfo::P3D: return this->m_modelCacheP3D.set(models); case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.set(models); default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CStatusMessage(); } } @@ -81,6 +85,7 @@ namespace BlackMisc QDateTime IMultiSimulatorModelCaches::getCurrentCacheTimestamp() const { const CSimulatorInfo sim(this->getCurrentSimulator()); + BLACK_VERIFY_X(sim.isSingleSimulator(), Q_FUNC_INFO, "need single simulator"); if (!sim.isSingleSimulator()) { return QDateTime(); } return this->getCacheTimestamp(sim); } @@ -95,7 +100,7 @@ namespace BlackMisc case CSimulatorInfo::P3D: return this->m_modelCacheP3D.getTimestamp(); case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.getTimestamp(); default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return QDateTime(); } } @@ -103,28 +108,35 @@ namespace BlackMisc void CModelCaches::syncronizeCache(const CSimulatorInfo &simulator) { Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); - const CStatusMessage m = this->m_currentSimulator.set(simulator); - if (m.isFailure()) { CLogMessage::preformatted(m); } switch (simulator.getSimulator()) { - case CSimulatorInfo::FS9: return this->m_modelCacheFs9.synchronize(); break; - case CSimulatorInfo::FSX: return this->m_modelCacheFsx.synchronize(); break; - case CSimulatorInfo::P3D: return this->m_modelCacheP3D.synchronize(); break; - case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.synchronize(); break; + case CSimulatorInfo::FS9: this->m_modelCacheFs9.synchronize(); break; + case CSimulatorInfo::FSX: this->m_modelCacheFsx.synchronize(); break; + case CSimulatorInfo::P3D: this->m_modelCacheP3D.synchronize(); break; + case CSimulatorInfo::XPLANE: this->m_modelCacheXP.synchronize(); break; default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); + break; } } - void CModelCaches::setCurrentSimulator(const CSimulatorInfo &simulator) + CStatusMessage CModelCaches::setCurrentSimulator(const CSimulatorInfo &simulator) { + static const CStatusMessage sameSimMsg = CStatusMessage(this).info("Same simulator"); + const CSimulatorInfo s = this->m_currentSimulator.getCopy(); + if (s == simulator) { return sameSimMsg; } + const BlackMisc::CStatusMessage m = this->m_currentSimulator.set(simulator); this->syncronizeCache(simulator); + return m; } CModelSetCaches::CModelSetCaches(QObject *parent) : IMultiSimulatorModelCaches(parent) { this->m_currentSimulator.synchronize(); - this->syncronizeCache(this->m_currentSimulator.getCopy()); + const CSimulatorInfo sim(this->m_currentSimulator.getCopy()); + this->syncronizeCache(sim); + const QString simStr(sim.toQString(true)); + CLogMessage(this).info("Initialized model set caches to %1") << simStr; } CAircraftModelList CModelSetCaches::getCachedModels(const CSimulatorInfo &simulator) const @@ -137,7 +149,7 @@ namespace BlackMisc case CSimulatorInfo::P3D: return this->m_modelCacheP3D.getCopy(); case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.getCopy(); default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CAircraftModelList(); } } @@ -145,8 +157,6 @@ namespace BlackMisc CStatusMessage CModelSetCaches::setCachedModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) { Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); - const CStatusMessage m = this->m_currentSimulator.set(simulator); - if (m.isFailure()) { return m; } CAircraftModelList orderedModels(models); if (orderedModels.needsOrder()) { @@ -164,7 +174,7 @@ namespace BlackMisc case CSimulatorInfo::P3D: return this->m_modelCacheP3D.set(orderedModels); case CSimulatorInfo::XPLANE: return this->m_modelCacheXP.set(orderedModels); default: - Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); + Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CStatusMessage(); } } @@ -187,8 +197,6 @@ namespace BlackMisc void CModelSetCaches::syncronizeCache(const CSimulatorInfo &simulator) { Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); - const CStatusMessage m = this->m_currentSimulator.set(simulator); - if (m.isFailure()) { CLogMessage::preformatted(m); } switch (simulator.getSimulator()) { case CSimulatorInfo::FS9: this->m_modelCacheFs9.synchronize(); break; @@ -200,11 +208,15 @@ namespace BlackMisc } } - void CModelSetCaches::setCurrentSimulator(const CSimulatorInfo &simulator) + CStatusMessage CModelSetCaches::setCurrentSimulator(const CSimulatorInfo &simulator) { + static const CStatusMessage sameSimMsg = CStatusMessage(this).info("Same simulator"); + const CSimulatorInfo s = this->m_currentSimulator.getCopy(); + if (s == simulator) { return sameSimMsg; } + const BlackMisc::CStatusMessage m = this->m_currentSimulator.set(simulator); this->syncronizeCache(simulator); + return m; } - } // ns } // ns } // ns diff --git a/src/blackmisc/simulation/data/modelcaches.h b/src/blackmisc/simulation/data/modelcaches.h index 04d7cd949..d0cc3c922 100644 --- a/src/blackmisc/simulation/data/modelcaches.h +++ b/src/blackmisc/simulation/data/modelcaches.h @@ -187,7 +187,7 @@ namespace BlackMisc virtual BlackMisc::Simulation::CSimulatorInfo getCurrentSimulator() const = 0; //!Selected simulator - virtual void setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) = 0; + virtual BlackMisc::CStatusMessage setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) = 0; //! \copydoc IModelsPerSimulatorSetable::setModels virtual void setModels(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) override; @@ -207,6 +207,7 @@ namespace BlackMisc }; //! Bundle of caches for all simulators + //! \remark remembers its last simulator selection class CModelCaches : public IMultiSimulatorModelCaches { Q_OBJECT @@ -222,7 +223,7 @@ namespace BlackMisc virtual QDateTime getCacheTimestamp(const BlackMisc::Simulation::CSimulatorInfo &simulator) const override; virtual void syncronizeCache(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; virtual BlackMisc::Simulation::CSimulatorInfo getCurrentSimulator() const override { return this->m_currentSimulator.getCopy(); } - virtual void setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; + virtual BlackMisc::CStatusMessage setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; //! @} private: @@ -234,7 +235,7 @@ namespace BlackMisc }; //! Bundle of caches for model sets of all simulators - //! \remark Temp. workaround + //! \remark remembers its last simulator selection class CModelSetCaches : public IMultiSimulatorModelCaches { Q_OBJECT @@ -250,7 +251,7 @@ namespace BlackMisc virtual QDateTime getCacheTimestamp(const BlackMisc::Simulation::CSimulatorInfo &simulator) const override; virtual void syncronizeCache(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; virtual BlackMisc::Simulation::CSimulatorInfo getCurrentSimulator() const override { return this->m_currentSimulator.getCopy(); } - virtual void setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; + virtual BlackMisc::CStatusMessage setCurrentSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator) override; //! @} private: diff --git a/src/blackmisc/simulation/fscommon/fscommonutil.cpp b/src/blackmisc/simulation/fscommon/fscommonutil.cpp index b1b6b2dd0..1e965a0c7 100644 --- a/src/blackmisc/simulation/fscommon/fscommonutil.cpp +++ b/src/blackmisc/simulation/fscommon/fscommonutil.cpp @@ -146,7 +146,7 @@ namespace BlackMisc QString CFsCommonUtil::fs9AircraftDir() { - QString dir(fs9AircraftDirFromRegistry()); + const QString dir(fs9AircraftDirFromRegistry()); if (!dir.isEmpty()) { return dir; } //! \todo hardcoded sim parts should come from settings return "C:/Flight Simulator 9/Aircraft";