diff --git a/src/blackmisc/simulation/simulatorinfo.cpp b/src/blackmisc/simulation/simulatorinfo.cpp index c130aafb5..d6e5efd3b 100644 --- a/src/blackmisc/simulation/simulatorinfo.cpp +++ b/src/blackmisc/simulation/simulatorinfo.cpp @@ -8,8 +8,11 @@ */ #include "simulatorinfo.h" +#include "blackmisc/project.h" +#include "blackmisc/simulation/fscommon/fscommonutil.h" using namespace BlackMisc; +using namespace BlackMisc::Simulation::FsCommon; namespace BlackMisc { @@ -18,11 +21,18 @@ namespace BlackMisc CSimulatorInfo::CSimulatorInfo() { } - CSimulatorInfo::CSimulatorInfo(Simulator s) : m_simulator(static_cast(s)) + CSimulatorInfo::CSimulatorInfo(const QString &identifierString) : m_simulator(identifierToFlag(identifierString)) { } - CSimulatorInfo::CSimulatorInfo(bool fsx, bool fs9, bool xp) : - m_simulator(boolToFlag(fsx, fs9, xp)) + CSimulatorInfo::CSimulatorInfo(Simulator simulator) : m_simulator(static_cast(simulator)) + { } + + CSimulatorInfo::CSimulatorInfo(bool fsx, bool fs9, bool xp, bool p3d) : + m_simulator(boolToFlag(fsx, fs9, xp, p3d)) + { } + + CSimulatorInfo::CSimulatorInfo(int flagsAsInt) : + m_simulator(flagsAsInt) { } bool CSimulatorInfo::isUnspecified() const @@ -30,6 +40,51 @@ namespace BlackMisc return m_simulator < 1; } + bool CSimulatorInfo::fsx() const + { + return getSimulator().testFlag(FSX); + } + + bool CSimulatorInfo::fs9() const + { + return getSimulator().testFlag(FS9); + } + + bool CSimulatorInfo::xplane() const + { + return getSimulator().testFlag(XPLANE); + } + + bool CSimulatorInfo::p3d() const + { + return getSimulator().testFlag(P3D); + } + + bool CSimulatorInfo::isAnySimulator() const + { + return fsx() || fs9() || xplane() || p3d(); + } + + bool CSimulatorInfo::isNoSimulator() const + { + return m_simulator == 0; + } + + bool CSimulatorInfo::isAllSimulators() const + { + return fsx() && fs9() && xplane() && p3d(); + } + + bool CSimulatorInfo::matchesAll(const CSimulatorInfo &otherInfo) const + { + return (this->m_simulator & otherInfo.m_simulator) == otherInfo.m_simulator; + } + + bool CSimulatorInfo::matchesAny(const CSimulatorInfo &otherInfo) const + { + return (this->m_simulator & otherInfo.m_simulator) > 0; + } + QString CSimulatorInfo::convertToQString(bool i18n) const { Q_UNUSED(i18n); @@ -37,15 +92,23 @@ namespace BlackMisc QString str; if (s.testFlag(FSX)) { str.append("FSX "); } if (s.testFlag(FS9)) { str.append("FS9 "); } - if (s.testFlag(XP)) { str.append("XPlane "); } + if (s.testFlag(XPLANE)) { str.append("XPlane "); } + if (s.testFlag(P3D)) { str.append("P3D "); } return str.trimmed(); } - CSimulatorInfo::Simulator CSimulatorInfo::boolToFlag(bool fsx, bool fs9, bool xp) + void CSimulatorInfo::add(const CSimulatorInfo &other) + { + if (other.isUnspecified()) { return; } + this->setSimulator(this->getSimulator() | other.getSimulator()); + } + + CSimulatorInfo::Simulator CSimulatorInfo::boolToFlag(bool fsx, bool fs9, bool xp, bool p3d) { Simulator s = fsx ? FSX : None; if (fs9) { s |= FS9; } - if (xp) { s |= XP; } + if (xp) { s |= XPLANE; } + if (p3d) { s |= P3D; } return s; } @@ -55,7 +118,7 @@ namespace BlackMisc if (i.isEmpty()) { return None; } Simulator s = None; - if (i.contains("fsx")) + if (i.contains("fsx") || i.contains(" sx")) { s |= FSX; } @@ -63,11 +126,45 @@ namespace BlackMisc { s |= FS9; } - if (i.contains("xplane") || i.contains("xp")) + if (i.contains("xplane") || i.contains("xp") || i.contains("x plane")) { - s |= XP; + s |= XPLANE; + } + if (i.contains("3d") || i.contains("prepare")) + { + s |= P3D; } return s; } + + const CSimulatorInfo &CSimulatorInfo::allSimulators() + { + static const CSimulatorInfo s(All); + return s; + } + + const CSimulatorInfo CSimulatorInfo::getLocallyInstalledSimulators() + { + //! \todo add XP, ... + CSimulatorInfo sim; + bool fs9 = + CProject::isRunningOnWindowsNtPlatform() && + !CFsCommonUtil::fs9AircraftDir().isEmpty() && + !CFsCommonUtil::fs9Dir().isEmpty(); + bool fsx = + CProject::isRunningOnWindowsNtPlatform() && + !CFsCommonUtil::fsxSimObjectsDir().isEmpty() && + !CFsCommonUtil::fsxDir().isEmpty(); + bool p3d = + CProject::isRunningOnWindowsNtPlatform() && + !CFsCommonUtil::p3dDir().isEmpty() && + !CFsCommonUtil::p3dSimObjectsDir().isEmpty(); + bool xp = true; //! \todo XP resolution + + sim.setSimulator( + CSimulatorInfo::boolToFlag(fsx, fs9, xp, p3d) + ); + return sim; + } } // ns } // ns diff --git a/src/blackmisc/simulation/simulatorinfo.h b/src/blackmisc/simulation/simulatorinfo.h index 2ba995a56..828eb6287 100644 --- a/src/blackmisc/simulation/simulatorinfo.h +++ b/src/blackmisc/simulation/simulatorinfo.h @@ -36,40 +36,89 @@ namespace BlackMisc //! Simulator enum SimulatorFlags { - None = 0, - FSX = 1 << 0, - FS9 = 1 << 1, - XP = 1 << 2 + None = 0, + FSX = 1 << 0, + FS9 = 1 << 1, + XPLANE = 1 << 2, + P3D = 1 << 3, + FSX_P3D = FSX | P3D, + AllMS = FSX | FS9 | P3D, + All = FSX | FS9 | XPLANE | P3D }; Q_DECLARE_FLAGS(Simulator, SimulatorFlags) //! Default constructor CSimulatorInfo(); + //! Constructor + CSimulatorInfo(const QString &identifierString); + //! Constructor CSimulatorInfo(Simulator s); //! Constructor - CSimulatorInfo(bool fsx, bool fs9, bool xp); + CSimulatorInfo(int flagsAsInt); + + //! Constructor + CSimulatorInfo(bool fsx, bool fs9, bool xp, bool p3d); //! Unspecified simulator bool isUnspecified() const; + //! FSX? + bool fsx() const; + + //! FS9? + bool fs9() const; + + //! XPlane + bool xplane() const; + + //! P3D? + bool p3d() const; + + //! Any simulator? + bool isAnySimulator() const; + + //! No simulator? + bool isNoSimulator() const; + + //! Is all simulators? + bool isAllSimulators() const; + + //! Matches all simulators + bool matchesAll(const CSimulatorInfo &otherInfo) const; + + //! Matches any simulator + bool matchesAny(const CSimulatorInfo &otherInfo) const; + //! Simulator Simulator getSimulator() const { return static_cast(m_simulator); } //! Simulator void setSimulator(Simulator s) { m_simulator = static_cast(s); } + //! All simulators + void setAllSimulators() { setSimulator(All); } + //! \copydoc CValueObject::convertToQString QString convertToQString(bool i18n = false) const; + //! Add simulator + void add(const CSimulatorInfo &other); + //! Bool flags to enum - static Simulator boolToFlag(bool fsx, bool fs9, bool xp); + static Simulator boolToFlag(bool fsx, bool fs9, bool xp, bool p3d); //! Identifer, as provided by plugin static Simulator identifierToFlag(const QString &identifier); + //! All simulators + static const CSimulatorInfo &allSimulators(); + + //! Locally installed simulators + static const CSimulatorInfo getLocallyInstalledSimulators(); + private: BLACK_ENABLE_TUPLE_CONVERSION(CSimulatorInfo) int m_simulator = static_cast(None); @@ -81,6 +130,7 @@ BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Simulation::CSimulatorInfo, ( attr(o.m_simulator) )) Q_DECLARE_METATYPE(BlackMisc::Simulation::CSimulatorInfo) +Q_DECLARE_METATYPE(BlackMisc::Simulation::CSimulatorInfo::SimulatorFlags) Q_DECLARE_OPERATORS_FOR_FLAGS(BlackMisc::Simulation::CSimulatorInfo::Simulator) #endif // guard