swift
settingsmatchingcomponent.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 
5 
6 #include <QPointer>
7 
8 #include "ui_settingsmatchingcomponent.h"
9 
11 #include "gui/guiapplication.h"
12 #include "misc/logmessage.h"
14 
15 using namespace swift::misc;
16 using namespace swift::misc::simulation;
17 using namespace swift::core::context;
18 
19 namespace swift::gui::components
20 {
21  CSettingsMatchingComponent::CSettingsMatchingComponent(QWidget *parent)
22  : QFrame(parent), ui(new Ui::CSettingsMatchingComponent)
23  {
24  ui->setupUi(this);
25  connect(ui->pb_Save, &QPushButton::released, this, &CSettingsMatchingComponent::onSavePressed);
26  connect(ui->pb_Reload, &QPushButton::released, this, &CSettingsMatchingComponent::onReloadPressed);
27  connect(ui->pb_MatchingAgain, &QPushButton::released, this,
28  &CSettingsMatchingComponent::onMatchingsAgainPressed);
29 
30  // also used in mapping tool, must also work without contexts
31  IContextSimulator *simContext = simulatorContext();
32  if (simContext)
33  {
34  connect(simContext, &IContextSimulator::matchingSetupChanged, this,
35  &CSettingsMatchingComponent::onSetupChanged, Qt::QueuedConnection);
36  this->deferredReload(5000);
37  }
38  else { this->showButtons(false); }
39  }
40 
42 
43  CAircraftMatcherSetup CSettingsMatchingComponent::getMatchingSetup() const { return ui->form_Matching->value(); }
44 
46  {
47  ui->form_Matching->setValue(setup);
48  }
49 
50  void CSettingsMatchingComponent::showButtons(bool show) { ui->fr_Buttons->setVisible(show); }
51 
52  void CSettingsMatchingComponent::onSavePressed() const
53  {
54  IContextSimulator *simContext = simulatorContext();
55  if (!simContext) { return; }
56  const CAircraftMatcherSetup setup = ui->form_Matching->value();
57  simContext->setMatchingSetup(setup);
58  }
59 
60  void CSettingsMatchingComponent::onReloadPressed() { this->deferredReload(0); }
61 
62  void CSettingsMatchingComponent::onMatchingsAgainPressed()
63  {
64  if (!sGui || !sGui->getISimulator() || !sGui->getISimulator()->isConnected()) { return; }
65  const int reMatchedNo = sGui->getIContextSimulator()->doMatchingsAgain();
66  CLogMessage(this).info(u"Triggered re-apping of %1 aircraft") << reMatchedNo;
67  }
68 
69  void CSettingsMatchingComponent::onSetupChanged()
70  {
71  const IContextSimulator *simContext = simulatorContext();
72  if (!simContext) { return; }
73  this->deferredReload(0);
74  }
75 
76  void CSettingsMatchingComponent::deferredReload(int deferMs)
77  {
78  IContextSimulator *simContext = simulatorContext();
79  if (!simContext) { return; }
80  if (deferMs < 1)
81  {
82  const CAircraftMatcherSetup setup = simContext->getMatchingSetup();
83  ui->form_Matching->setValue(setup);
84  }
85  else
86  {
87  QPointer<CSettingsMatchingComponent> myself(this);
88  QTimer::singleShot(deferMs, this, [=] {
89  if (!myself) { return; }
90  this->deferredReload(0);
91  });
92  }
93  }
94 
95  IContextSimulator *CSettingsMatchingComponent::simulatorContext()
96  {
97  if (!sGui || sGui->isShuttingDown() || !sGui->getIContextSimulator()) { return nullptr; }
98  return sGui->getIContextSimulator();
99  }
100 } // namespace swift::gui::components
QPointer< ISimulator > getISimulator() const
The simulator plugin, if available.
bool isShuttingDown() const
Is application shutting down?
const context::IContextSimulator * getIContextSimulator() const
Direct access to contexts if a CCoreFacade has been initialized.
virtual int doMatchingsAgain()=0
Repeat all matchings.
virtual swift::misc::simulation::CAircraftMatcherSetup getMatchingSetup() const =0
Get matching setup.
virtual void setMatchingSetup(const swift::misc::simulation::CAircraftMatcherSetup &setup)=0
Set matching setup.
void setMatchingSetup(const swift::misc::simulation::CAircraftMatcherSetup &setup)
Set setup.
swift::misc::simulation::CAircraftMatcherSetup getMatchingSetup() const
Get setup.
Class for emitting a log message.
Definition: logmessage.h:27
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
High level reusable GUI components.
Definition: aboutdialog.cpp:13
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