From 81c832457efa8251ec53905a17c7bce193afd90a Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 25 Jul 2019 16:08:36 +0200 Subject: [PATCH] Ref T709, based on the Qt free base class, added swift compliant settings class --- src/blackmisc/propertyindex.h | 1 + .../simulation/registermetadatasimulation.cpp | 1 + .../simulation/settings/xswiftbussettings.cpp | 79 +++++++++++++++ .../simulation/settings/xswiftbussettings.h | 95 ++++++++++++++++++- src/blackmisc/simulation/simulation.h | 1 + 5 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 src/blackmisc/simulation/settings/xswiftbussettings.cpp diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index aca7d7aef..f7b7e7f6d 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -173,6 +173,7 @@ namespace BlackMisc GlobalIndexCInterpolatioRenderingSetup = 16000, GlobalIndexCMatchingStatisticsEntry = 16100, GlobalIndexCAircraftMatcherSetup = 16200, + GlobalIndexCXSwiftBusSettings = 16300, GlobalIndexSwiftPilotClient = 17000, GlobalIndexSwiftCore = 17100, GlobalIndexSwiftLauncher = 17200, diff --git a/src/blackmisc/simulation/registermetadatasimulation.cpp b/src/blackmisc/simulation/registermetadatasimulation.cpp index 684524e00..744eb64a4 100644 --- a/src/blackmisc/simulation/registermetadatasimulation.cpp +++ b/src/blackmisc/simulation/registermetadatasimulation.cpp @@ -49,6 +49,7 @@ namespace BlackMisc CVPilotModelRule::registerMetadata(); CVPilotModelRuleSet::registerMetadata(); CAircraftMatcherSetup::registerMetadata(); + CXSwiftBusSettings::registerMetadata(); qRegisterMetaType(); qRegisterMetaType(); diff --git a/src/blackmisc/simulation/settings/xswiftbussettings.cpp b/src/blackmisc/simulation/settings/xswiftbussettings.cpp new file mode 100644 index 000000000..a84aa348a --- /dev/null +++ b/src/blackmisc/simulation/settings/xswiftbussettings.cpp @@ -0,0 +1,79 @@ +/* Copyright (C) 2019 + * 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. 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 "xswiftbussettings.h" + +namespace BlackMisc +{ + namespace Simulation + { + namespace Settings + { + CXSwiftBusSettings::CXSwiftBusSettings() { } + + CXSwiftBusSettings::CXSwiftBusSettings(const QString &json) + { + this->parseXSwiftBusStringQt(json); + } + + CVariant CXSwiftBusSettings::propertyByIndex(const CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexMaxPlanes: return CVariant::fromValue(m_maxPlanes); + case IndexDBusServerAddress: return CVariant::fromValue(QString::fromStdString(m_dBusServerAddress)); + case IndexDrawingLabels: return CVariant::fromValue(m_drawingLabels); + case IndexMaxDrawingDistance: return CVariant::fromValue(m_maxDrawDistanceNM); + default: break; + } + return CValueObject::propertyByIndex(index); + } + + void CXSwiftBusSettings::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexMaxPlanes: m_maxPlanes = variant.toInt(); break; + case IndexDBusServerAddress: m_dBusServerAddress = variant.toStdString(); break; + case IndexDrawingLabels: m_drawingLabels = variant.toBool(); break; + case IndexMaxDrawingDistance: m_maxDrawDistanceNM = variant.toDouble(); break; + default: + CValueObject::setPropertyByIndex(index, variant); + break; + } + } + + QString CXSwiftBusSettings::convertToQString(bool i18n) const + { + Q_UNUSED(i18n); + return ""; + } + + CStatusMessageList CXSwiftBusSettings::validate() const + { + CStatusMessageList msgs; + const QString dbus = QString::fromStdString(m_dBusServerAddress); + if (!CDBusServer::isSessionOrSystemAddress(dbus) && !CDBusServer::isQtDBusAddress(dbus)) + { + msgs.addValidationMessage(QStringLiteral("Invalid DBus address '%1'").arg(dbus), CStatusMessage::SeverityError); + } + return msgs; + } + + const CXSwiftBusSettings &CXSwiftBusSettings::defaultValue() + { + static const CXSwiftBusSettings s; + return s; + } + } // ns + } // ns +} // ns diff --git a/src/blackmisc/simulation/settings/xswiftbussettings.h b/src/blackmisc/simulation/settings/xswiftbussettings.h index 0959c6347..f823f84f9 100644 --- a/src/blackmisc/simulation/settings/xswiftbussettings.h +++ b/src/blackmisc/simulation/settings/xswiftbussettings.h @@ -11,9 +11,14 @@ #ifndef BLACKMISC_SIMULATION_SETTINGS_XSWIFTBUSSETTINGS_H #define BLACKMISC_SIMULATION_SETTINGS_XSWIFTBUSSETTINGS_H -#include +#include "xswiftbussettingsqtfree.h" +#include "blackmisc/statusmessagelist.h" #include "blackmisc/settingscache.h" #include "blackmisc/dbusserver.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/blackmiscexport.h" + +#include namespace BlackMisc { @@ -21,10 +26,71 @@ namespace BlackMisc { namespace Settings { + //! XSwiftBus settings + class BLACKMISC_EXPORT CXSwiftBusSettings : + public CValueObject, + public CXSwiftBusSettingsQtFree, + public ITimestampBased + { + public: + //! Properties by index + enum ColumnIndex + { + IndexDBusServerAddress = CPropertyIndex::GlobalIndexCXSwiftBusSettings, + IndexMaxPlanes, + IndexDrawingLabels, + IndexMaxDrawingDistance + }; + + //! Default constructor + CXSwiftBusSettings(); + + //! From JSON constructor + CXSwiftBusSettings(const QString &json); + + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant); + + //! \copydoc BlackMisc::Mixin::String::toQString + QString convertToQString(bool i18n = false) const; + + //! \copydoc CXSwiftBusSettingsQtFree::getDBusServerAddress + QString getDBusServerAddressQt() const { return QString::fromStdString(this->getDBusServerAddress()); } + + //! \copydoc CXSwiftBusSettingsQtFree::setDBusServerAddress + void setDBusServerAddressQt(const QString &dBusAddress) { this->setDBusServerAddress(dBusAddress.toStdString()); } + + //! \copydoc CXSwiftBusSettingsQtFree::toXSwiftBusJsonString + QString toXSwiftBusJsonStringQt() const { return QString::fromStdString(this->toXSwiftBusJsonString()); } + + //! \copydoc CXSwiftBusSettingsQtFree::parseXSwiftBusString + void parseXSwiftBusStringQt(const QString &json) { this->parseXSwiftBusString(json.toStdString()); } + + //! Valid settings? + CStatusMessageList validate() const; + + //! Default value for settings + static const CXSwiftBusSettings &defaultValue(); + + private: + BLACK_METACLASS( + CXSwiftBusSettings, + BLACK_METAMEMBER(dBusServerAddress), + BLACK_METAMEMBER(maxPlanes), + BLACK_METAMEMBER(drawingLabels), + BLACK_METAMEMBER(maxDrawDistanceNM), + BLACK_METAMEMBER(timestampMSecsSinceEpoch, 0, DisabledForComparison | DisabledForHashing) + ); + }; + /*! * Setting for XSwiftBus. + * @deprecated will be changed to JSON Settings */ - struct TXSwiftBusServer : public BlackMisc::TSettingTrait + struct TXSwiftBusServer : public TSettingTrait { //! \copydoc BlackMisc::TSettingTrait::key static const char *key() { return "xswiftbus/server"; } @@ -38,8 +104,33 @@ namespace BlackMisc //! \copydoc BlackMisc::TSettingTrait::isValid static bool isValid(const QString &dBusAddress, QString &) { return BlackMisc::CDBusServer::isSessionOrSystemAddress(dBusAddress) || BlackMisc::CDBusServer::isQtDBusAddress(dBusAddress); } }; + + /*! + * Setting for XSwiftBus. + */ + struct TXSwiftBusSettings : public TSettingTrait + { + //! \copydoc BlackMisc::TSettingTrait::key + static const char *key() { return "xswiftbus/settings"; } + + //! \copydoc BlackMisc::TSettingTrait::humanReadable + static const QString &humanReadable() { static const QString name("XSwiftBus"); return name; } + + //! \copydoc BlackMisc::TSettingTrait::defaultValue + static CXSwiftBusSettings defaultValue() { return CXSwiftBusSettings::defaultValue(); } + + //! \copydoc BlackMisc::TSettingTrait::isValid + static bool isValid(const CXSwiftBusSettings &settings, QString &reason) + { + const CStatusMessageList msgs = settings.validate(); + reason = msgs.toQString(true); + return msgs.isSuccess(); + } + }; } // ns } // ns } // ns +Q_DECLARE_METATYPE(BlackMisc::Simulation::Settings::CXSwiftBusSettings) + #endif // guard diff --git a/src/blackmisc/simulation/simulation.h b/src/blackmisc/simulation/simulation.h index 3eecd206b..fd29afab0 100644 --- a/src/blackmisc/simulation/simulation.h +++ b/src/blackmisc/simulation/simulation.h @@ -36,5 +36,6 @@ #include "blackmisc/simulation/settings/modelsettings.h" #include "blackmisc/simulation/settings/simulatorsettings.h" #include "blackmisc/simulation/settings/swiftpluginsettings.h" +#include "blackmisc/simulation/settings/xswiftbussettings.h" #endif // guard