diff --git a/src/blackmisc/simulation/aircraftmodelsetloader.cpp b/src/blackmisc/simulation/aircraftmodelsetloader.cpp new file mode 100644 index 000000000..f1b10bafe --- /dev/null +++ b/src/blackmisc/simulation/aircraftmodelsetloader.cpp @@ -0,0 +1,106 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "aircraftmodelsetloader.h" + +namespace BlackMisc +{ + namespace Simulation + { + CModelSetLoader::CModelSetLoader(const CSimulatorInfo &info, QObject *parent) : + QObject(parent), + m_simulatorInfo(info) + { + Q_ASSERT_X(info.isSingleSimulator(), Q_FUNC_INFO, "Only one simulator per loader"); + } + + CModelSetLoader::~CModelSetLoader() + { + this->gracefulShutdown(); + } + + CStatusMessage CModelSetLoader::setModelsInCache(const CAircraftModelList &models, const CSimulatorInfo &simulator) + { + const CSimulatorInfo sim = simulator.isSingleSimulator() ? simulator : this->m_simulatorInfo; + if (!sim.isSingleSimulator()) { return CStatusMessage(this, CStatusMessage::SeverityError, "Invalid simulator"); } + const CStatusMessage m(this->m_caches.setModels(models, sim)); + return m; + } + + CStatusMessage CModelSetLoader::replaceOrAddModelsInCache(const CAircraftModelList &models, const CSimulatorInfo &simulator) + { + if (models.isEmpty()) { return CStatusMessage(this, CStatusMessage::SeverityInfo, "No data"); } + const CSimulatorInfo sim = simulator.isSingleSimulator() ? simulator : this->m_simulatorInfo; + if (!sim.isSingleSimulator()) { return CStatusMessage(this, CStatusMessage::SeverityError, "Invalid simuataor"); } + CAircraftModelList allModels(this->m_caches.getModels(sim)); + int c = allModels.replaceOrAddModelsWithString(models, Qt::CaseInsensitive); + if (c > 0) + { + return this->setModelsInCache(models, sim); + } + else + { + return CStatusMessage(this, CStatusMessage::SeverityInfo, "No data changed"); + } + } + + void CModelSetLoader::changeSimulator(const CSimulatorInfo &simulator) + { + Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "Only one simulator per loader"); + this->m_caches.syncronize(simulator); + this->m_simulatorInfo = simulator; + emit simulatorChanged(simulator); + } + + CAircraftModelList CModelSetLoader::getAircraftModels() const + { + return this->m_caches.getModels(this->m_simulatorInfo); + } + + QDateTime CModelSetLoader::getCacheTimestamp() const + { + return this->m_caches.getCacheTimestamp(this->m_simulatorInfo); + } + + void CModelSetLoader::syncronizeCache() + { + return this->m_caches.syncronize(this->m_simulatorInfo); + } + + bool CModelSetLoader::hasCachedData() const + { + return !this->m_caches.getModels(this->m_simulatorInfo).isEmpty(); + } + + CStatusMessage CModelSetLoader::clearCache() + { + return this->setModelsInCache(CAircraftModelList()); + } + + const CSimulatorInfo &CModelSetLoader::getSimulator() const + { + return m_simulatorInfo; + } + + QString CModelSetLoader::getSimulatorAsString() const + { + return m_simulatorInfo.toQString(); + } + + bool CModelSetLoader::supportsSimulator(const CSimulatorInfo &info) + { + return getSimulator().matchesAny(info); + } + + void CModelSetLoader::gracefulShutdown() + { + // void + } + } // ns +} // ns diff --git a/src/blackmisc/simulation/aircraftmodelsetloader.h b/src/blackmisc/simulation/aircraftmodelsetloader.h new file mode 100644 index 000000000..a57f00cab --- /dev/null +++ b/src/blackmisc/simulation/aircraftmodelsetloader.h @@ -0,0 +1,94 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_SIMULATION_AIRCRAFTMODELSETLOADER_H +#define BLACKMISC_SIMULATION_AIRCRAFTMODESETLLOADER_H + +#include "blackmisc/blackmiscexport.h" +#include "blackmisc/simulation/aircraftmodellist.h" +#include "blackmisc/simulation/simulatorinfo.h" +#include "blackmisc/simulation/data/modelcaches.h" +#include "blackmisc/pixmap.h" +#include + +namespace BlackMisc +{ + namespace Simulation + { + /*! + * Handling of current set for simulator + */ + class BLACKMISC_EXPORT CModelSetLoader : public QObject + { + Q_OBJECT + + public: + //! Constructor + CModelSetLoader(const CSimulatorInfo &info, QObject *parent = nullptr); + + //! Destructor + virtual ~CModelSetLoader(); + + //! The loaded models + //! \threadsafe + BlackMisc::Simulation::CAircraftModelList getAircraftModels() const; + + //! Count of loaded models + int getAircraftModelsCount() const { return getAircraftModels().size(); } + + //! Which simulator is supported by that very loader + const BlackMisc::Simulation::CSimulatorInfo &getSimulator() const; + + //! Supported simulators as string + QString getSimulatorAsString() const; + + //! Is the given simulator supported? + bool supportsSimulator(const BlackMisc::Simulation::CSimulatorInfo &info); + + //! Shutdown + void gracefulShutdown(); + + signals: + //! Simulator has been changed + void simulatorChanged(const BlackMisc::Simulation::CSimulatorInfo &simulator); + + public slots: + //! Set cache from outside, this should only be used in special cases. + //! But it allows to modify data elsewhere and update the cache with manipulated data. + BlackMisc::CStatusMessage setModelsInCache(const CAircraftModelList &models, const CSimulatorInfo &simulator = CSimulatorInfo()); + + //! Set cache from outside, this should only be used in special cases. + //! But it allows to modify data elsewhere and update the cache with manipulated data. + BlackMisc::CStatusMessage replaceOrAddModelsInCache(const CAircraftModelList &models, const CSimulatorInfo &simulator = CSimulatorInfo()); + + //! Change the simulator + void changeSimulator(const BlackMisc::Simulation::CSimulatorInfo &simulator); + + protected: + //! Cache timestamp + QDateTime getCacheTimestamp() const; + + //! Make sure cache is syncronized + void syncronizeCache(); + + //! Any cached data? + bool hasCachedData() const; + + //! Clear cache + BlackMisc::CStatusMessage clearCache(); + + BlackMisc::Simulation::CSimulatorInfo m_simulatorInfo; //!< Corresponding simulator + BlackMisc::Simulation::Data::CModelSetCaches m_caches { this }; //!< caches + }; + } // namespace +} // namespace + +#endif // guard