Move FGSwiftBus settings to own class

This commit is contained in:
Lars Toenning
2023-01-31 22:06:11 +01:00
parent c1dc5b0b06
commit 65b4251a3c
7 changed files with 133 additions and 15 deletions

View File

@@ -123,6 +123,7 @@ namespace BlackMisc
GlobalIndexCMatchingStatisticsEntry = 16100,
GlobalIndexCAircraftMatcherSetup = 16200,
GlobalIndexCXSwiftBusSettings = 16300,
GlobalIndexCFGSwiftBusSettings = 16400,
GlobalIndexSwiftPilotClient = 17000,
GlobalIndexSwiftCore = 17100,
GlobalIndexSwiftLauncher = 17200,

View File

@@ -28,6 +28,7 @@
#include "blackmisc/simulation/fscommon/aircraftcfgentrieslist.h"
#include "blackmisc/simulation/fscommon/vpilotmodelruleset.h"
#include "blackmisc/simulation/fsx/simconnectutilities.h"
#include "blackmisc/simulation/settings/fgswiftbussettings.h"
#include "blackmisc/simulation/settings/modelsettings.h"
#include "blackmisc/simulation/settings/simulatorsettings.h"
#include "blackmisc/simulation/settings/swiftpluginsettings.h"
@@ -74,6 +75,7 @@ namespace BlackMisc::Simulation
CVPilotModelRuleSet::registerMetadata();
CAircraftMatcherSetup::registerMetadata();
CXSwiftBusSettings::registerMetadata();
CFGSwiftBusSettings::registerMetadata();
qRegisterMetaType<CSimulatorSettings::CGSource>();
qRegisterMetaType<CAircraftMatcherSetup::MatchingAlgorithm>();

View File

@@ -0,0 +1,49 @@
/* Copyright (C) 2023
* 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 "blackmisc/simulation/settings/fgswiftbussettings.h"
BLACK_DEFINE_VALUEOBJECT_MIXINS(BlackMisc::Simulation::Settings, CFGSwiftBusSettings)
namespace BlackMisc::Simulation::Settings
{
QVariant CFGSwiftBusSettings::propertyByIndex(BlackMisc::CPropertyIndexRef index) const
{
if (index.isMyself()) { return QVariant::fromValue(*this); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDBusServerAddress:
return QVariant::fromValue(m_dBusServerAddress);
default: break;
}
return CValueObject::propertyByIndex(index);
}
void CFGSwiftBusSettings::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
{
if (index.isMyself()) { (*this) = variant.value<CFGSwiftBusSettings>(); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDBusServerAddress:
m_dBusServerAddress = variant.toString(); break;
default:
CValueObject::setPropertyByIndex(index, variant);
break;
}
}
QString CFGSwiftBusSettings::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
return "DBusServer: " + m_dBusServerAddress;
}
} // ns

View File

@@ -13,28 +13,71 @@
#define BLACKMISC_SIMULATION_SETTINGS_FGSWIFTBUSSETTINGS_H
#include <QString>
#include "blackmisc/settingscache.h"
#include "blackmisc/dbusserver.h"
#include "blackmisc/settingscache.h"
#include "blackmisc/valueobject.h"
BLACK_DECLARE_VALUEOBJECT_MIXINS(BlackMisc::Simulation::Settings, CFGSwiftBusSettings)
namespace BlackMisc::Simulation::Settings
{
//! FGSwiftBus settings
class BLACKMISC_EXPORT CFGSwiftBusSettings final :
public CValueObject<CFGSwiftBusSettings>
{
public:
//! Properties by index
enum ColumnIndex
{
IndexDBusServerAddress = CPropertyIndexRef::GlobalIndexCFGSwiftBusSettings,
};
//! Default constructor
CFGSwiftBusSettings() = default;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
QVariant propertyByIndex(BlackMisc::CPropertyIndexRef index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(BlackMisc::CPropertyIndexRef index, const QVariant &variant);
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! DBus server
QString getDBusServerAddress() const { return m_dBusServerAddress; }
//! Set DBus server
void setDBusServerAddress(const QString &dBusServer) { m_dBusServerAddress = dBusServer; }
private:
QString m_dBusServerAddress { "tcp:host=127.0.0.1,port=45003" }; //!< DBus server
BLACK_METACLASS(
CFGSwiftBusSettings,
BLACK_METAMEMBER(dBusServerAddress)
);
};
/*!
* Setting for FGSwiftBus.
*/
struct TFGSwiftBusServer : public BlackMisc::TSettingTrait<QString>
struct TFGSwiftBusServer : public BlackMisc::TSettingTrait<CFGSwiftBusSettings>
{
//! \copydoc BlackMisc::TSettingTrait::key
static const char *key() { return "fgswiftbus/server"; }
static const char *key() { return "fgswiftbus/settings"; }
//! \copydoc BlackCore::TSettingTrait::humanReadable
static const QString &humanReadable() { static const QString name("FGSwiftBus"); return name; }
//! \copydoc BlackMisc::TSettingTrait::defaultValue
static QString defaultValue() { return "tcp:host=127.0.0.1,port=45003"; }
static CFGSwiftBusSettings defaultValue() { return CFGSwiftBusSettings(); }
//! \copydoc BlackMisc::TSettingTrait::isValid
static bool isValid(const QString &dBusAddress, QString &) { return BlackMisc::CDBusServer::isSessionOrSystemAddress(dBusAddress) || BlackMisc::CDBusServer::isQtDBusAddress(dBusAddress); }
static bool isValid(const CFGSwiftBusSettings &settings, QString &) { return BlackMisc::CDBusServer::isSessionOrSystemAddress(settings.getDBusServerAddress()) || BlackMisc::CDBusServer::isQtDBusAddress(settings.getDBusServerAddress()); }
};
} // ns
Q_DECLARE_METATYPE(BlackMisc::Simulation::Settings::CFGSwiftBusSettings)
#endif // guard

View File

@@ -334,7 +334,7 @@ namespace BlackSimPlugin::Flightgear
bool CSimulatorFlightgear::connectTo()
{
if (isConnected()) { return true; }
QString dbusAddress = m_fgswiftbusServerSetting.getThreadLocal();
QString dbusAddress = m_fgswiftbusServerSetting.getThreadLocal().getDBusServerAddress();
if (CDBusServer::isSessionOrSystemAddress(dbusAddress))
{
@@ -1060,7 +1060,7 @@ namespace BlackSimPlugin::Flightgear
if (this->isShuttingDown()) { return; }
Q_ASSERT_X(!CThreadUtils::thisIsMainThread(), Q_FUNC_INFO, "Expect to run in background");
QString dbusAddress = m_fgSswiftBusServerSetting.getThreadLocal();
QString dbusAddress = m_fgSswiftBusServerSetting.getThreadLocal().getDBusServerAddress();
if (CDBusServer::isSessionOrSystemAddress(dbusAddress))
{
checkConnectionViaSessionBus();

View File

@@ -6,15 +6,17 @@
* or distributed except according to the terms contained in the LICENSE file.
*/
#include "ui_simulatorflightgearconfigwindow.h"
#include "simulatorflightgearconfigwindow.h"
#include "blackcore/application.h"
#include "ui_simulatorflightgearconfigwindow.h"
#include "blackgui/guiapplication.h"
#include <QComboBox>
#include <QDialogButtonBox>
using namespace BlackGui;
using namespace BlackMisc;
using namespace BlackMisc::Simulation::Settings;
//using namespace BlackMisc::Simulation::Flightgear;
namespace BlackSimPlugin::Flightgear
@@ -25,8 +27,8 @@ namespace BlackSimPlugin::Flightgear
{
ui->setupUi(this);
CGuiUtility::disableMinMaxCloseButtons(this);
ui->comp_SettingsFGSwiftBus->setDefaultP2PAddress(m_fgswiftbusServerSetting.getDefault());
ui->comp_SettingsFGSwiftBus->set(m_fgswiftbusServerSetting.getThreadLocal());
ui->comp_SettingsFGSwiftBus->setDefaultP2PAddress(m_fgswiftbusServerSetting.getDefault().getDBusServerAddress());
ui->comp_SettingsFGSwiftBus->set(m_fgswiftbusServerSetting.getThreadLocal().getDBusServerAddress());
connect(ui->bb_OkCancel, &QDialogButtonBox::accepted, this, &CSimulatorFlightgearConfigWindow::onSettingsAccepted);
connect(ui->bb_OkCancel, &QDialogButtonBox::rejected, this, &CSimulatorFlightgearConfigWindow::close);
@@ -35,13 +37,28 @@ namespace BlackSimPlugin::Flightgear
CSimulatorFlightgearConfigWindow::~CSimulatorFlightgearConfigWindow()
{ }
CFGSwiftBusSettings CSimulatorFlightgearConfigWindow::getSettingsFromUI() const
{
CFGSwiftBusSettings s = m_fgswiftbusServerSetting.getThreadLocal();
s.setDBusServerAddress(ui->comp_SettingsFGSwiftBus->getDBusAddress());
return s;
}
void CSimulatorFlightgearConfigWindow::onSettingsChanged()
{
ui->comp_SettingsFGSwiftBus->set(m_fgswiftbusServerSetting.getThreadLocal().getDBusServerAddress());
}
void CSimulatorFlightgearConfigWindow::onSettingsAccepted()
{
const QString currentAddress = m_fgswiftbusServerSetting.getThreadLocal();
const QString updatedAddress = ui->comp_SettingsFGSwiftBus->getDBusAddress();
if (currentAddress != ui->comp_SettingsFGSwiftBus->getDBusAddress())
if (!sGui || sGui->isShuttingDown()) { return; }
const CFGSwiftBusSettings s = m_fgswiftbusServerSetting.getThreadLocal();
CFGSwiftBusSettings changed = getSettingsFromUI();
if (s != changed)
{
m_fgswiftbusServerSetting.set(updatedAddress);
m_fgswiftbusServerSetting.set(changed);
}
close();
}

View File

@@ -38,8 +38,14 @@ namespace BlackSimPlugin::Flightgear
//! Settings have been accepted
void onSettingsAccepted();
//! Settings changed
void onSettingsChanged();
//! Get settings object with values from UI
BlackMisc::Simulation::Settings::CFGSwiftBusSettings getSettingsFromUI() const;
QScopedPointer<Ui::CSimulatorFlightgearConfigWindow> ui;
BlackMisc::CSetting<BlackMisc::Simulation::Settings::TFGSwiftBusServer> m_fgswiftbusServerSetting { this };
BlackMisc::CSetting<BlackMisc::Simulation::Settings::TFGSwiftBusServer> m_fgswiftbusServerSetting { this, &CSimulatorFlightgearConfigWindow::onSettingsChanged };
};
} // ns