mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-07 19:35:32 +08:00
refs #619, enhance local model data with DB data (if possible)
* use new slot syntax on menu actions * allow reload from disk and reload from cache * added utility functions for model/modellist
This commit is contained in:
@@ -266,6 +266,11 @@ namespace BlackMisc
|
||||
return this->m_aircraftIcao.hasDesignator();
|
||||
}
|
||||
|
||||
bool CAircraftModel::hasKnownAircraftDesignator() const
|
||||
{
|
||||
return this->m_aircraftIcao.hasKnownDesignator();
|
||||
}
|
||||
|
||||
bool CAircraftModel::hasAirlineDesignator() const
|
||||
{
|
||||
return this->m_livery.hasValidAirlineDesignator();
|
||||
@@ -291,9 +296,9 @@ namespace BlackMisc
|
||||
this->setModelMode(CAircraftModel::modelModeFromString(mode));
|
||||
}
|
||||
|
||||
void CAircraftModel::updateMissingParts(const CAircraftModel &otherModel)
|
||||
void CAircraftModel::updateMissingParts(const CAircraftModel &otherModel, bool dbModelPriority)
|
||||
{
|
||||
if (!this->hasValidDbKey() && otherModel.hasValidDbKey())
|
||||
if (dbModelPriority && !this->hasValidDbKey() && otherModel.hasValidDbKey())
|
||||
{
|
||||
// we have no DB data, but the other one has
|
||||
// so we change roles. We take the DB object as base, and update our parts
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace BlackMisc
|
||||
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
|
||||
void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index);
|
||||
|
||||
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex()
|
||||
//! Compare by index
|
||||
int comparePropertyByIndex(const CAircraftModel &compareValue, const CPropertyIndex &index) const;
|
||||
|
||||
//! Can be initialized from FSD
|
||||
@@ -153,6 +153,9 @@ namespace BlackMisc
|
||||
//! Has aircraft designator?
|
||||
bool hasAircraftDesignator() const;
|
||||
|
||||
//! Has known aircraft designator?
|
||||
bool hasKnownAircraftDesignator() const;
|
||||
|
||||
//! Airline designator?
|
||||
bool hasAirlineDesignator() const;
|
||||
|
||||
@@ -217,7 +220,7 @@ namespace BlackMisc
|
||||
void setFileName(const QString &fileName) { m_fileName = fileName; }
|
||||
|
||||
//! Update missing parts from another model
|
||||
void updateMissingParts(const CAircraftModel &otherModel);
|
||||
void updateMissingParts(const CAircraftModel &otherModel, bool dbModelPriority = true);
|
||||
|
||||
//! Queried model string?
|
||||
bool hasQueriedModelString() const;
|
||||
|
||||
@@ -102,6 +102,40 @@ namespace BlackMisc
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftModelList CAircraftModelList::withAircraftDesignator() const
|
||||
{
|
||||
return this->findBy([ = ](const CAircraftModel & model)
|
||||
{
|
||||
return model.hasAircraftDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftModelList CAircraftModelList::withAircraftDesignator(const QStringList &designators) const
|
||||
{
|
||||
if (designators.isEmpty()) { return CAircraftModelList(); }
|
||||
return this->findBy([ = ](const CAircraftModel & model)
|
||||
{
|
||||
return designators.contains(model.getAircraftIcaoCodeDesignator());
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftModelList CAircraftModelList::withKnownAircraftDesignator() const
|
||||
{
|
||||
return this->findBy([ = ](const CAircraftModel & model)
|
||||
{
|
||||
return model.hasKnownAircraftDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftModelList CAircraftModelList::byDistributor(const CDistributorList &distributors) const
|
||||
{
|
||||
if (distributors.isEmpty()) { return CAircraftModelList(); }
|
||||
return this->findBy([ = ](const CAircraftModel & model)
|
||||
{
|
||||
return model.matchesAnyDistributor(distributors);
|
||||
});
|
||||
}
|
||||
|
||||
void CAircraftModelList::setSimulatorInfo(const CSimulatorInfo &info)
|
||||
{
|
||||
for (CAircraftModel &model : (*this))
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace BlackMisc
|
||||
bool containsModelStringOrId(const BlackMisc::Simulation::CAircraftModel &model, Qt::CaseSensitivity sensitivity = Qt::CaseInsensitive) const;
|
||||
|
||||
//! Find by model string
|
||||
//! \remark normally CAircraftModelList::findFirstByModelString would be used
|
||||
CAircraftModelList findByModelString(const QString &modelString, Qt::CaseSensitivity sensitivity = Qt::CaseInsensitive) const;
|
||||
|
||||
//! Find first by model string
|
||||
@@ -67,6 +68,18 @@ namespace BlackMisc
|
||||
//! With file name
|
||||
CAircraftModelList findWithFileName() const;
|
||||
|
||||
//! Models with aircraft ICAO code set
|
||||
CAircraftModelList withAircraftDesignator() const;
|
||||
|
||||
//! Models with aircraft ICAO code from list
|
||||
CAircraftModelList withAircraftDesignator(const QStringList &designators) const;
|
||||
|
||||
//! Models with a known aircraft ICAO code set
|
||||
CAircraftModelList withKnownAircraftDesignator() const;
|
||||
|
||||
//! All models from given distributors
|
||||
CAircraftModelList byDistributor(const CDistributorList &distributors) const;
|
||||
|
||||
//! Set simulator for all elements
|
||||
void setSimulatorInfo(const BlackMisc::Simulation::CSimulatorInfo &info);
|
||||
|
||||
|
||||
@@ -38,6 +38,23 @@ namespace BlackMisc
|
||||
return dir.exists();
|
||||
}
|
||||
|
||||
bool IAircraftModelLoader::mergeWithDbData(CAircraftModelList &modelsFromSimulator, const CAircraftModelList &dbModels)
|
||||
{
|
||||
if (dbModels.isEmpty() || modelsFromSimulator.isEmpty()) { return false; }
|
||||
for (CAircraftModel &simModel : modelsFromSimulator)
|
||||
{
|
||||
if (simModel.hasValidDbKey()) { continue; } // already done
|
||||
CAircraftModel dbModel(dbModels.findFirstByModelString(simModel.getModelString()));
|
||||
if (!dbModel.hasValidDbKey())
|
||||
{
|
||||
continue; // not found
|
||||
}
|
||||
dbModel.updateMissingParts(simModel, false);
|
||||
simModel = dbModel;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void IAircraftModelLoader::ps_loadFinished(bool success)
|
||||
{
|
||||
Q_UNUSED(success);
|
||||
@@ -53,7 +70,7 @@ namespace BlackMisc
|
||||
return true;
|
||||
}
|
||||
|
||||
void IAircraftModelLoader::startLoading(LoadMode mode)
|
||||
void IAircraftModelLoader::startLoading(LoadMode mode, const CAircraftModelList &dbModels)
|
||||
{
|
||||
if (this->m_loadingInProgress) { return; }
|
||||
this->m_loadingInProgress = true;
|
||||
@@ -65,6 +82,7 @@ namespace BlackMisc
|
||||
}
|
||||
else if (useCachedData && mode.testFlag(CacheUntilNewer))
|
||||
{
|
||||
//! \todo currently too slow, does not make sense with that overhead
|
||||
if (!this->areModelFilesUpdated())
|
||||
{
|
||||
emit loadingFinished(true, this->m_simulatorInfo);
|
||||
@@ -77,7 +95,7 @@ namespace BlackMisc
|
||||
emit loadingFinished(false, this->m_simulatorInfo);
|
||||
return;
|
||||
}
|
||||
this->startLoadingFromDisk(mode);
|
||||
this->startLoadingFromDisk(mode, dbModels);
|
||||
}
|
||||
|
||||
const CSimulatorInfo &IAircraftModelLoader::supportedSimulators() const
|
||||
|
||||
@@ -36,22 +36,24 @@ namespace BlackMisc
|
||||
//! Parser mode
|
||||
enum LoadModeFlag
|
||||
{
|
||||
NotSet = 0,
|
||||
LoadDirectly = 1 << 0, //!< load syncronously (blocking), normally for testing
|
||||
LoadInBackground = 1 << 1, //!< load in background, asnycronously
|
||||
CacheUntilNewer = 1 << 2, //!< use cache until newer data re available
|
||||
CacheFirst = 1 << 3, //!< always use cache (if it has data)
|
||||
CacheSkipped = 1 << 4, //!< ignore cache
|
||||
CacheOnly = 1 << 5, //!< only read cache, never load from disk
|
||||
Default = LoadInBackground | CacheFirst //!< default mode
|
||||
NotSet = 0,
|
||||
LoadDirectly = 1 << 0, //!< load syncronously (blocking), normally for testing
|
||||
LoadInBackground = 1 << 1, //!< load in background, asnycronously
|
||||
CacheUntilNewer = 1 << 2, //!< use cache until newer data re available
|
||||
CacheFirst = 1 << 3, //!< always use cache (if it has data)
|
||||
CacheSkipped = 1 << 4, //!< ignore cache
|
||||
CacheOnly = 1 << 5, //!< only read cache, never load from disk
|
||||
InBackgroundWithCache = LoadInBackground | CacheFirst, //!< Background, cached
|
||||
InBackgroundNoCache = LoadInBackground | CacheSkipped //!< Background, not cached
|
||||
};
|
||||
Q_DECLARE_FLAGS(LoadMode, LoadModeFlag)
|
||||
|
||||
//! Destructor
|
||||
virtual ~IAircraftModelLoader();
|
||||
|
||||
//! Start the loading process from disk
|
||||
void startLoading(LoadMode mode = Default);
|
||||
//! Start the loading process from disk.
|
||||
//! Optional DB models can be passed and used for data consolidation.
|
||||
void startLoading(LoadMode mode = InBackgroundWithCache, const CAircraftModelList &dbModels = {});
|
||||
|
||||
//! Change the directory
|
||||
bool changeRootDirectory(const QString &directory);
|
||||
@@ -65,6 +67,9 @@ namespace BlackMisc
|
||||
//! The loaded models
|
||||
virtual const BlackMisc::Simulation::CAircraftModelList &getAircraftModels() const = 0;
|
||||
|
||||
//! Count of loaded models
|
||||
const int getAircraftModelsCount() const { return getAircraftModels().size(); }
|
||||
|
||||
//! Cache timestamp
|
||||
virtual QDateTime getCacheTimestamp() const = 0;
|
||||
|
||||
@@ -107,7 +112,10 @@ namespace BlackMisc
|
||||
bool existsDir(const QString &directory) const;
|
||||
|
||||
//! Start the loading process from disk
|
||||
virtual void startLoadingFromDisk(LoadMode mode) = 0;
|
||||
virtual void startLoadingFromDisk(LoadMode mode, const BlackMisc::Simulation::CAircraftModelList &dbModels) = 0;
|
||||
|
||||
//! Merge with DB data if possible
|
||||
virtual bool mergeWithDbData(BlackMisc::Simulation::CAircraftModelList &modelsFromSimulator, const BlackMisc::Simulation::CAircraftModelList &dbModels);
|
||||
|
||||
BlackMisc::Simulation::CSimulatorInfo m_simulatorInfo; //!< Corresponding simulator
|
||||
std::atomic<bool> m_cancelLoading { false }; //!< flag
|
||||
|
||||
@@ -19,10 +19,8 @@ namespace BlackMisc
|
||||
{
|
||||
namespace Simulation
|
||||
{
|
||||
|
||||
namespace FsCommon
|
||||
{
|
||||
|
||||
bool CAircraftCfgEntriesList::containsModelWithTitle(const QString &title, Qt::CaseSensitivity caseSensitivity)
|
||||
{
|
||||
if (title.isEmpty()) { return false; }
|
||||
@@ -61,9 +59,9 @@ namespace BlackMisc
|
||||
CAircraftModelList CAircraftCfgEntriesList::toAircraftModelList() const
|
||||
{
|
||||
CAircraftModelList ml;
|
||||
for (auto it = this->begin() ; it != this->end(); ++it)
|
||||
for (const CAircraftCfgEntries &entries : (*this))
|
||||
{
|
||||
ml.push_back(it->toAircraftModel());
|
||||
ml.push_back(entries.toAircraftModel());
|
||||
}
|
||||
return ml;
|
||||
}
|
||||
@@ -71,9 +69,9 @@ namespace BlackMisc
|
||||
CAircraftModelList CAircraftCfgEntriesList::toAircraftModelList(const CSimulatorInfo &simInfo) const
|
||||
{
|
||||
CAircraftModelList ml;
|
||||
for (auto it = this->begin() ; it != this->end(); ++it)
|
||||
for (const CAircraftCfgEntries &entries : (*this))
|
||||
{
|
||||
CAircraftModel m(it->toAircraftModel());
|
||||
CAircraftModel m(entries.toAircraftModel());
|
||||
m.setSimulatorInfo(simInfo);
|
||||
ml.push_back(m);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "blackmisc/predicates.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
|
||||
#include <tuple>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Simulation;
|
||||
using namespace BlackMisc::Simulation::FsCommon;
|
||||
@@ -24,6 +26,9 @@ namespace BlackMisc
|
||||
{
|
||||
namespace FsCommon
|
||||
{
|
||||
// response for async. loading
|
||||
using LoaderResponse = std::tuple<CAircraftCfgEntriesList, CAircraftModelList, bool>;
|
||||
|
||||
CAircraftCfgParser::CAircraftCfgParser(const CSimulatorInfo &simInfo, const QString &rootDirectory, const QStringList &excludeDirs) :
|
||||
IAircraftModelLoader(simInfo, rootDirectory, excludeDirs)
|
||||
{ }
|
||||
@@ -89,7 +94,7 @@ namespace BlackMisc
|
||||
return empty;
|
||||
}
|
||||
|
||||
void CAircraftCfgParser::startLoadingFromDisk(LoadMode mode)
|
||||
void CAircraftCfgParser::startLoadingFromDisk(LoadMode mode, const CAircraftModelList &dbModels)
|
||||
{
|
||||
if (mode.testFlag(LoadInBackground))
|
||||
{
|
||||
@@ -97,19 +102,36 @@ namespace BlackMisc
|
||||
const QString rootDirectory(m_rootDirectory); // copy
|
||||
const QStringList excludedDirectories(m_excludedDirectories); // copy
|
||||
m_parserWorker = BlackMisc::CWorker::fromTask(this, "CAircraftCfgParser::changeDirectory",
|
||||
[this, rootDirectory, excludedDirectories]()
|
||||
[this, rootDirectory, excludedDirectories, dbModels]()
|
||||
{
|
||||
bool ok;
|
||||
auto aircraftCfgEntriesList = this->performParsing(rootDirectory, excludedDirectories, &ok);
|
||||
return std::make_pair(aircraftCfgEntriesList, ok);
|
||||
});
|
||||
m_parserWorker->thenWithResult<std::pair<CAircraftCfgEntriesList, bool>>(this, [this](const auto &pair)
|
||||
{
|
||||
if (pair.second)
|
||||
bool ok = false;
|
||||
const auto aircraftCfgEntriesList = this->performParsing(rootDirectory, excludedDirectories, &ok);
|
||||
CAircraftModelList models;
|
||||
if (ok)
|
||||
{
|
||||
this->m_parsedCfgEntriesList = pair.first;
|
||||
this->setModelsInCache(pair.first.toAircraftModelList());
|
||||
emit loadingFinished(true, this->m_simulatorInfo);
|
||||
models = (aircraftCfgEntriesList.toAircraftModelList(this->supportedSimulators()));
|
||||
this->mergeWithDbData(models, dbModels);
|
||||
}
|
||||
return std::make_tuple(aircraftCfgEntriesList, models, ok);
|
||||
});
|
||||
m_parserWorker->thenWithResult<LoaderResponse>(this, [this](const LoaderResponse & tuple)
|
||||
{
|
||||
const bool ok = std::get<2>(tuple);
|
||||
if (ok)
|
||||
{
|
||||
this->m_parsedCfgEntriesList = std::get<0>(tuple);
|
||||
const CAircraftModelList models(std::get<1>(tuple));
|
||||
const bool hasData = !models.isEmpty();
|
||||
if (hasData)
|
||||
{
|
||||
this->setModelsInCache(models); // not thread safe
|
||||
}
|
||||
// currently I treat no data as error
|
||||
emit this->loadingFinished(hasData, this->m_simulatorInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
emit this->loadingFinished(false, this->m_simulatorInfo);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -117,8 +139,15 @@ namespace BlackMisc
|
||||
{
|
||||
bool ok;
|
||||
this->m_parsedCfgEntriesList = performParsing(m_rootDirectory, m_excludedDirectories, &ok);
|
||||
this->setModelsInCache(this->m_parsedCfgEntriesList.toAircraftModelList());
|
||||
emit loadingFinished(ok, this->m_simulatorInfo);
|
||||
CAircraftModelList models(this->m_parsedCfgEntriesList.toAircraftModelList(this->supportedSimulators()));
|
||||
this->mergeWithDbData(models, dbModels);
|
||||
const bool hasData = !models.isEmpty();
|
||||
if (hasData)
|
||||
{
|
||||
this->setModelsInCache(models); // not thread safe
|
||||
}
|
||||
// currently I treat no data as error
|
||||
emit this->loadingFinished(hasData, this->m_simulatorInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +279,7 @@ namespace BlackMisc
|
||||
if (m_cancelLoading) { return CAircraftCfgEntriesList(); }
|
||||
if (file.isDir())
|
||||
{
|
||||
QString nextDir = file.absoluteFilePath();
|
||||
const QString nextDir = file.absoluteFilePath();
|
||||
if (currentDir.startsWith(nextDir, Qt::CaseInsensitive)) { continue; } // do not go up
|
||||
if (dir == currentDir) { continue; } // do not recursively call same directory
|
||||
|
||||
@@ -414,7 +443,7 @@ namespace BlackMisc
|
||||
}
|
||||
else if (static_cast<QMetaType::Type>(qv.type()) == QMetaType::QStringList)
|
||||
{
|
||||
QStringList l = qv.toStringList();
|
||||
const QStringList l = qv.toStringList();
|
||||
return l.join(",").trimmed();
|
||||
}
|
||||
else if (static_cast<QMetaType::Type>(qv.type()) == QMetaType::QString)
|
||||
@@ -453,3 +482,5 @@ namespace BlackMisc
|
||||
} // namespace
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::Simulation::FsCommon::LoaderResponse)
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace BlackMisc
|
||||
|
||||
//! \name Interface functions
|
||||
//! @{
|
||||
virtual void startLoadingFromDisk(LoadMode mode) override;
|
||||
virtual void startLoadingFromDisk(LoadMode mode, const BlackMisc::Simulation::CAircraftModelList &dbModels) override;
|
||||
//! @}
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace BlackMisc
|
||||
return {};
|
||||
}
|
||||
|
||||
void CAircraftModelLoaderXPlane::startLoadingFromDisk(LoadMode mode)
|
||||
void CAircraftModelLoaderXPlane::startLoadingFromDisk(LoadMode mode, const CAircraftModelList &dbModels)
|
||||
{
|
||||
m_installedModels.clear();
|
||||
if (m_rootDirectory.isEmpty())
|
||||
@@ -77,9 +77,10 @@ namespace BlackMisc
|
||||
auto rootDirectory = m_rootDirectory;
|
||||
auto excludedDirectories = m_excludedDirectories;
|
||||
m_parserWorker = BlackMisc::CWorker::fromTask(this, "CAircraftModelLoaderXPlane::performParsing",
|
||||
[this, rootDirectory, excludedDirectories]()
|
||||
[this, rootDirectory, excludedDirectories, dbModels]()
|
||||
{
|
||||
auto models = performParsing(rootDirectory, excludedDirectories);
|
||||
mergeWithDbData(models, dbModels);
|
||||
return models;
|
||||
});
|
||||
m_parserWorker->thenWithResult<CAircraftModelList>(this, [this](const auto & models)
|
||||
@@ -90,6 +91,7 @@ namespace BlackMisc
|
||||
else if (mode.testFlag(LoadDirectly))
|
||||
{
|
||||
m_installedModels = performParsing(m_rootDirectory, m_excludedDirectories);
|
||||
mergeWithDbData(m_installedModels, dbModels);
|
||||
emit loadingFinished(true, this->m_simulatorInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace BlackMisc
|
||||
protected:
|
||||
//! \name Interface functions
|
||||
//! @{
|
||||
virtual void startLoadingFromDisk(LoadMode mode) override;
|
||||
virtual void startLoadingFromDisk(LoadMode mode, const BlackMisc::Simulation::CAircraftModelList &dbModels) override;
|
||||
//! @}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user