swift
simulatorxplaneconfigwindow.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <vector>
7 
8 #include <QComboBox>
9 #include <QDialogButtonBox>
10 
11 #include "ui_simulatorxplaneconfigwindow.h"
12 
14 #include "gui/guiapplication.h"
16 
17 using namespace swift::gui;
18 using namespace swift::core::context;
19 using namespace swift::misc;
20 using namespace swift::misc::simulation::settings;
21 using namespace swift::misc::simulation::xplane;
22 
23 namespace swift::simplugin::xplane
24 {
25  CSimulatorXPlaneConfigWindow::CSimulatorXPlaneConfigWindow(QWidget *parent)
27  {
28  ui->setupUi(this);
29  ui->comp_SettingsXSwiftBus->setDefaultP2PAddress(
30  m_xSwiftBusServerSettings.getDefault().getDBusServerAddressQt());
31  CGuiUtility::disableMinMaxCloseButtons(this);
32 
33  const CXSwiftBusSettings defaultSettings = TXSwiftBusSettings::defaultValue();
34  ui->sb_MaxAircraft->setMaximum(defaultSettings.getMaxPlanes() * 2);
35  ui->sb_FollowAircraftDistanceM->setMaximum(defaultSettings.getFollowAircraftDistanceM() * 5);
36  ui->ds_MaxDrawDistanceNM->setMaximum(qRound(defaultSettings.getMaxDrawDistanceNM() * 3));
37 
38  const CXSwiftBusSettings s = m_xSwiftBusServerSettings.getThreadLocal();
39  this->setUiValues(s);
40 
41  connect(ui->bb_OkCancel, &QDialogButtonBox::accepted, this, &CSimulatorXPlaneConfigWindow::onSettingsAccepted);
42  connect(ui->bb_OkCancel, &QDialogButtonBox::rejected, this, &CSimulatorXPlaneConfigWindow::close);
43  }
44 
46 
47  void CSimulatorXPlaneConfigWindow::onSettingsAccepted()
48  {
49  if (!sGui || sGui->isShuttingDown()) { return; }
50 
51  const CXSwiftBusSettings s = m_xSwiftBusServerSettings.getThreadLocal();
52  const CXSwiftBusSettings changed = this->getSettingsFromUI();
53  if (s != changed)
54  {
55  m_xSwiftBusServerSettings.set(changed);
56 
57  // this writes to a local XPlane directory
58  // if swift runs distributed it does nothing
59  // if XPlane is connected the settings will be written from config.cpp
61  {
62  CXSwiftBusConfigWriter xswiftbusConfigWriter;
63  xswiftbusConfigWriter.setDBusAddress(changed.getDBusServerAddressQt());
64  xswiftbusConfigWriter.setDebugMode(changed.isLogRenderPhases());
65  xswiftbusConfigWriter.setTcasEnabled(changed.isTcasEnabled());
66  xswiftbusConfigWriter.updateInAllXPlaneVersions();
67  }
68  }
69  this->close();
70  }
71 
72  CXSwiftBusSettings CSimulatorXPlaneConfigWindow::getSettingsFromUI() const
73  {
74  CXSwiftBusSettings s = m_xSwiftBusServerSettings.getThreadLocal();
75  s.setDBusServerAddressQt(ui->comp_SettingsXSwiftBus->getDBusAddress());
76  s.setMaxDrawDistanceNM(ui->ds_MaxDrawDistanceNM->value());
77  s.setMaxPlanes(ui->sb_MaxAircraft->value());
78  s.setFollowAircraftDistanceM(ui->sb_FollowAircraftDistanceM->value());
79  s.setDrawingLabels(ui->cb_DrawLabels->isChecked());
80  s.setLabelColor(ui->cs_LabelColor->getColor().packed());
81  s.setNightTextureModeQt(ui->cb_NightTextureMode->currentText());
82  s.setBundlingTaxiAndLandingLights(ui->cb_BundleTaxiLandingLights->isChecked());
83  s.setTcasEnabled(ui->cb_TcasEnabled->isChecked());
84  s.setTerrainProbeEnabled(ui->cb_TerrainProbeEnabled->isChecked());
85  s.setLogRenderPhases(ui->cb_LogRenderPhases->isChecked());
86 
87  // left, top, right, bottom, height
89  marginToInt(ui->le_MsgBoxMarginsLeft->text(), 20), marginToInt(ui->le_MsgBoxMarginsTop->text(), 20),
90  marginToInt(ui->le_MsgBoxMarginsRight->text(), 20), marginToInt(ui->le_MsgBoxMarginsBottom->text(), 20),
91  ui->sb_MessageBoxLines->value(), ui->sb_MessageBoxDuration->value(), ui->cs_ColorFreq->getColor().packed(),
92  ui->cs_ColorPriv->getColor().packed(), ui->cs_ColorServ->getColor().packed(),
93  ui->cs_ColorStat->getColor().packed(), ui->cs_ColorSup->getColor().packed());
94  return s;
95  }
96 
97  void CSimulatorXPlaneConfigWindow::setUiValues(const CXSwiftBusSettings &settings)
98  {
99  ui->comp_SettingsXSwiftBus->set(settings.getDBusServerAddressQt());
100  ui->sb_MaxAircraft->setValue(settings.getMaxPlanes());
101  ui->sb_FollowAircraftDistanceM->setValue(settings.getFollowAircraftDistanceM());
102  ui->ds_MaxDrawDistanceNM->setValue(settings.getMaxDrawDistanceNM());
103  ui->cb_DrawLabels->setChecked(settings.isDrawingLabels());
104  ui->cs_LabelColor->setColor(CRgbColor::fromPacked(settings.getLabelColor()));
105  ui->cb_BundleTaxiLandingLights->setChecked(settings.isBundlingTaxiAndLandingLights());
106  ui->cb_TcasEnabled->setChecked(settings.isTcasEnabled());
107  ui->cb_TerrainProbeEnabled->setChecked(settings.isTerrainProbeEnabled());
108  ui->cb_LogRenderPhases->setChecked(settings.isLogRenderPhases());
109 
110  const QString s = settings.getNightTextureModeQt().left(1);
111  if (!s.isEmpty())
112  {
113  for (int i = 0; i < ui->cb_NightTextureMode->count(); i++)
114  {
115  if (ui->cb_NightTextureMode->itemText(i).startsWith(s, Qt::CaseInsensitive))
116  {
117  ui->cb_NightTextureMode->setCurrentIndex(i);
118  break;
119  }
120  }
121  }
122 
123  const std::vector<int> values = settings.getMessageBoxValuesVector();
124  if (values.size() >= 6)
125  {
126  // left, top, right, bottom, height, duration
127  ui->le_MsgBoxMarginsLeft->setText(QString::number(values[0]));
128  ui->le_MsgBoxMarginsTop->setText(QString::number(values[1]));
129  ui->le_MsgBoxMarginsRight->setText(QString::number(values[2]));
130  ui->le_MsgBoxMarginsBottom->setText(QString::number(values[3]));
131  ui->sb_MessageBoxLines->setValue(values[4]);
132  ui->sb_MessageBoxDuration->setValue(values[5]);
133  }
134  if (values.size() >= 11)
135  {
136  // freq, priv, serv, stat, sup
137  ui->cs_ColorFreq->setColor(CRgbColor::fromPacked(values[6]));
138  ui->cs_ColorPriv->setColor(CRgbColor::fromPacked(values[7]));
139  ui->cs_ColorServ->setColor(CRgbColor::fromPacked(values[8]));
140  ui->cs_ColorStat->setColor(CRgbColor::fromPacked(values[9]));
141  ui->cs_ColorSup->setColor(CRgbColor::fromPacked(values[10]));
142  }
143  }
144 
145  void CSimulatorXPlaneConfigWindow::onSettingsChanged() { this->setUiValues(m_xSwiftBusServerSettings.get()); }
146 
147  int CSimulatorXPlaneConfigWindow::marginToInt(const QString &text, int defaultValue)
148  {
149  if (text.isEmpty()) { return defaultValue; }
150  bool ok;
151  const int v = text.toInt(&ok);
152  return ok ? v : defaultValue;
153  }
154 } // namespace swift::simplugin::xplane
bool isShuttingDown() const
Is application shutting down?
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
bool isSimulatorSimulating() const
Is available simulator simulating? Returns false if no simulator is available.
Base class for plugin config window.
CStatusMessage set(const T &value, qint64 timestamp=0)
Write a new value. Must be called from the thread in which the owner lives.
Definition: valuecache.h:411
const T & getThreadLocal() const
Read the current value.
Definition: valuecache.h:400
T get() const
Get a copy of the current value.
Definition: valuecache.h:408
auto getDefault() const
Gets the default value.
Definition: settingscache.h:97
void setDBusServerAddressQt(const QString &dBusAddress)
Set DBus server.
void setNightTextureModeQt(const QString &nightTexture)
The the night texture mode.
QString getNightTextureModeQt() const
The the night texture mode.
bool isDrawingLabels() const
Get whether the plugin draws type and callsign labels above aircraft.
void setBundlingTaxiAndLandingLights(bool bundle)
Bundle taxi and landing lights.
std::vector< int > getMessageBoxValuesVector() const
Left, top, right, bottom, lines, duration, color(freq, priv, serv, stat, sup)
int getLabelColor() const
Get the color to draw the callsign labels above aircraft.
bool setMaxPlanes(int planes)
Set the maximum number of aircraft.
double getMaxDrawDistanceNM() const
Set the maximum distance at which to draw aircraft (nautical miles).
void setTerrainProbeEnabled(bool enabled)
Terrain probe to query ground elevation enabled?
void setDrawingLabels(bool drawing)
Set whether the plugin draws type and callsign labels above aircraft.
bool isTerrainProbeEnabled() const
Terrain probe to query ground elevation enabled?
int getFollowAircraftDistanceM() const
Get follow aircraft distance.
bool isBundlingTaxiAndLandingLights() const
Bundle taxi and landing lights.
bool setMaxDrawDistanceNM(double nauticalMiles)
Set the maximum distance at which to draw aircraft (nautical miles).
int getMaxPlanes() const
Get the maximum number of aircraft.
void setMessageBoxValues(const std::string &positions)
Left, top, right, bottom, lines, duration, color(freq, priv, serv, stat, sup)
bool setFollowAircraftDistanceM(int meters)
Set follow aircraft distance.
void setLabelColor(int rgb)
Set the color to draw the callsign labels above aircraft.
void updateInAllXPlaneVersions()
Update xswiftbus.conf in all known X-Plane versions (XP9 - XP11 are supported)
void setDBusAddress(const QString &dBusAddress)
Set new DBus address.
A window that shows all the X-Plane plugin options.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
GUI related classes.
Free functions in swift::misc.