swift
fsxsettingscomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "fsxsettingscomponent.h"
5 
6 #include <QPointer>
7 #include <QTimer>
8 
9 #include "simulatorfsxcommon.h"
10 #include "ui_fsxsettingscomponent.h"
11 
12 #include "config/buildconfig.h"
14 #include "gui/guiapplication.h"
16 
17 using namespace swift::core;
18 using namespace swift::core::context;
19 using namespace swift::gui;
20 using namespace swift::misc;
21 using namespace swift::misc::simulation;
22 using namespace swift::config;
23 
24 namespace swift::simplugin::fsxcommon
25 {
26  CFsxSettingsComponent::CFsxSettingsComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CFsxSettingsComponent)
27  {
28  ui->setupUi(this);
29  ui->cb_TraceSimConnectCalls->setChecked(false);
30 
31  connect(ui->cb_AddingAsSimulatedObject, &QCheckBox::released, this,
32  &CFsxSettingsComponent::onSimulatedObjectChanged);
33  connect(ui->cb_TraceSimConnectCalls, &QCheckBox::released, this,
34  &CFsxSettingsComponent::onSimConnectTraceChanged);
35  connect(ui->cb_EnableTerrainProbe, &QCheckBox::released, this,
36  &CFsxSettingsComponent::onEnableTerrainProbeChanged);
37  connect(ui->cb_SBOffsets, &QCheckBox::released, this, &CFsxSettingsComponent::onSBOffsetsChanged);
38  connect(ui->pb_Refresh, &QPushButton::released, this, &CFsxSettingsComponent::refresh);
39 
40  if (sGui && sGui->getIContextSimulator())
41  {
42  connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorStatusChanged, this,
43  &CFsxSettingsComponent::onSimulatorStatusChanged, Qt::QueuedConnection);
44  connect(sGui->getIContextSimulator(), &IContextSimulator::simulatorPluginChanged, this,
45  &CFsxSettingsComponent::onSimulatorPluginChanged, Qt::QueuedConnection);
46  }
47 
48  QPointer<CFsxSettingsComponent> myself(this);
49  QTimer::singleShot(2000, this, [=] {
50  if (!sGui || !myself || sGui->isShuttingDown()) { return; }
51  this->refresh();
52  });
53  }
54 
56 
58  {
59  const CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
60  return fsxOrP3D ? fsxOrP3D->getSimulatorInfo() : m_simulator;
61  }
62 
64  {
65  const CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
66  const bool localSim = fsxOrP3D;
67  if (fsxOrP3D)
68  {
69  ui->cb_TraceSimConnectCalls->setChecked(fsxOrP3D->isTraceSendId());
70  ui->cb_EnableTerrainProbe->setChecked(fsxOrP3D->isUsingFsxTerrainProbe());
71  ui->cb_SBOffsets->setChecked(fsxOrP3D->isUsingSbOffsetValues());
72  ui->cb_AddingAsSimulatedObject->setChecked(fsxOrP3D->isAddingAsSimulatedObjectEnabled());
73  }
74 
75  ui->lbl_NoLocalSimulator->setVisible(!localSim);
76  ui->cb_TraceSimConnectCalls->setEnabled(localSim);
77  ui->cb_EnableTerrainProbe->setEnabled(CBuildConfig::isRunningOnWindowsNtPlatform() &&
78  (CBuildConfig::buildWordSize() == 32) && localSim);
79  ui->cb_SBOffsets->setEnabled(localSim);
80  }
81 
82  void CFsxSettingsComponent::onSimConnectTraceChanged()
83  {
84  CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
85  if (!fsxOrP3D) { return; }
86  fsxOrP3D->setTractingSendId(ui->cb_TraceSimConnectCalls->isChecked());
87  }
88 
89  void CFsxSettingsComponent::onSimulatedObjectChanged()
90  {
91  CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
92  if (!fsxOrP3D) { return; }
93  fsxOrP3D->setAddingAsSimulatedObjectEnabled(ui->cb_AddingAsSimulatedObject->isChecked());
94  }
95 
96  void CFsxSettingsComponent::onEnableTerrainProbeChanged()
97  {
98  CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
99  if (!fsxOrP3D) { return; }
100  fsxOrP3D->setUsingFsxTerrainProbe(ui->cb_EnableTerrainProbe->isChecked());
101  }
102 
103  void CFsxSettingsComponent::onSBOffsetsChanged()
104  {
105  CSimulatorFsxCommon *fsxOrP3D = this->getFsxOrP3DSimulator();
106  if (!fsxOrP3D) { return; }
107  fsxOrP3D->setUsingSbOffsetValues(ui->cb_SBOffsets->isChecked());
108  }
109 
110  void CFsxSettingsComponent::onSimulatorStatusChanged(int status)
111  {
112  Q_UNUSED(status);
113  this->refresh();
114  }
115 
116  void CFsxSettingsComponent::onSimulatorPluginChanged(const CSimulatorPluginInfo &info)
117  {
118  Q_UNUSED(info);
119  this->refresh();
120  }
121 
122  CSimulatorFsxCommon *CFsxSettingsComponent::getFsxOrP3DSimulator() const
123  {
124  if (!sGui || sGui->isShuttingDown() || !sGui->getIContextSimulator()) { return nullptr; }
126  if (plugin.isEmulatedPlugin()) { return nullptr; } // cast would fail
127 
128  // ISimulator can only be obtained in local environment, not distributed UI
129  if (!sGui->hasSimulator()) { return nullptr; }
130 
131  ISimulator *simulator = sGui->getISimulator().data();
132  if (!simulator || simulator->isEmulatedDriver()) { return nullptr; }
133  if (!simulator->getSimulatorInfo().isFsxP3DFamily()) { return nullptr; }
134  if (simulator->getSimulatorInfo() != m_simulator) { return nullptr; }
135 
137  CSimulatorFsxCommon *fsx = static_cast<CSimulatorFsxCommon *>(simulator);
138  return fsx;
139  }
140 } // namespace swift::simplugin::fsxcommon
QPointer< ISimulator > getISimulator() const
The simulator plugin, if available.
bool hasSimulator() const
Simulator object available?
bool isShuttingDown() const
Is application shutting down?
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
Interface to a simulator.
Definition: simulator.h:59
bool isEmulatedDriver() const
Is this the emulated driver just pretending to be P3D, FSX, or XPlane.
Definition: simulator.cpp:292
virtual swift::misc::simulation::CSimulatorPluginInfo getSimulatorPluginInfo() const =0
Simulator info, currently loaded plugin.
Simple hardcoded info about the corresponding simulator.
Definition: simulatorinfo.h:41
bool isFsxP3DFamily() const
FSX family, i.e. FSX or P3D?
bool isEmulatedPlugin() const
Is this the emulated driver?
CSimulatorInfo getSimulatorInfo() const
Get the represented simulator.
swift::misc::simulation::CSimulatorInfo getSimulator() const
Represented simulator.
bool isUsingFsxTerrainProbe() const
FSX Terrain probe.
void setTractingSendId(bool trace)
Set tracing on/off.
bool isAddingAsSimulatedObjectEnabled() const
Allow adding as simulated object instead of non ATC.
bool isTraceSendId() const
Trace enable (can be auto enable also)
bool isUsingSbOffsetValues() const
Using the SB offsets?
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7
GUI related classes.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
Definition: threadutils.h:30