swift
transpondermodecomponent.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 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 #include <QTimer>
8 
9 #include "ui_transpondermodecomponent.h"
10 
12 #include "gui/guiapplication.h"
14 
15 using namespace swift::misc;
16 using namespace swift::misc::aviation;
17 using namespace swift::misc::simulation;
18 using namespace swift::core::context;
19 
20 namespace swift::gui::components
21 {
22  CTransponderModeComponent::CTransponderModeComponent(QWidget *parent)
23  : QFrame(parent), CIdentifiable(this), ui(new Ui::CTransponderModeComponent)
24  {
25  ui->setupUi(this);
26 
27  Q_ASSERT_X(sGui, Q_FUNC_INFO, "Need sApp");
28  Q_ASSERT_X(sGui->getIContextOwnAircraft(), Q_FUNC_INFO, "Need own aircraft");
29 
30  connect(ui->tb_TransponderMode, &QToolButton::released, this, &CTransponderModeComponent::onClicked,
31  Qt::QueuedConnection);
32  connect(sGui->getIContextOwnAircraft(), &IContextOwnAircraft::changedAircraftCockpit, this,
33  &CTransponderModeComponent::onChangedAircraftCockpit, Qt::QueuedConnection);
34 
35  this->onChangedAircraftCockpit(sGui->getIContextOwnAircraft()->getOwnAircraft(), CIdentifier::null());
36  this->init();
37 
38  QPointer<CTransponderModeComponent> myself(this);
39  QTimer::singleShot(10 * 1000, this, [=] {
40  if (!myself) { return; }
41  myself->onChangedAircraftCockpit(sGui->getIContextOwnAircraft()->getOwnAircraft(), CIdentifier::null());
42  });
43  }
44 
46 
47  void CTransponderModeComponent::init()
48  {
49  if (!sGui || sGui->isShuttingDown()) { return; }
51  {
52  this->setVisible(false);
53  return;
54  }
55 
56  this->setVisible(true);
57  ui->tb_TransponderMode->setText(m_transponder.getModeAsShortString());
58 
59  this->setProperty("xpdrmode", m_transponder.getTransponderMode());
60  this->setProperty("xpdrmodeshort", m_transponder.getModeAsShortString());
61  ui->tb_TransponderMode->setProperty("xpdrmode", m_transponder.getTransponderMode());
62  ui->tb_TransponderMode->setProperty("xpdrmodeshort", m_transponder.getModeAsShortString());
63 
64  this->setToolTip(m_transponder.toQString());
65  }
66 
67  void CTransponderModeComponent::onClicked()
68  {
69  if (!sGui || sGui->isShuttingDown()) { return; }
70  CTransponder xpdr = m_transponder;
71  xpdr.toggleTransponderMode();
73  }
74 
75  void CTransponderModeComponent::onChangedAircraftCockpit(const CSimulatedAircraft &aircraft,
76  const CIdentifier &originator)
77  {
78  if (this->identifier() == originator) { return; }
79  if (m_transponder == aircraft.getTransponder()) { return; }
80  m_transponder = aircraft.getTransponder();
81  this->init();
82  emit this->changed();
83  }
84 } // namespace swift::gui::components
const context::IContextOwnAircraft * getIContextOwnAircraft() const
Direct access to contexts if a CCoreFacade has been initialized.
bool isShuttingDown() const
Is application shutting down?
bool supportsContexts(bool ignoreShutdownTest=false) const
Supports contexts.
virtual bool setTransponderMode(swift::misc::aviation::CTransponder::TransponderMode mode)=0
Set XPDR mode.
virtual swift::misc::simulation::CSimulatedAircraft getOwnAircraft() const =0
Get own aircraft.
Base class with a member CIdentifier to be inherited by a class which has an identity in the environm...
Definition: identifiable.h:24
const CIdentifier & identifier() const
Get identifier.
Definition: identifiable.h:27
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition: identifier.h:29
void toggleTransponderMode()
Transponder mode toggled.
Definition: transponder.cpp:68
QString getModeAsShortString() const
Transponder mode as short string.
Definition: transponder.h:80
TransponderMode getTransponderMode() const
Transponder mode.
Definition: transponder.h:95
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Comprehensive information of an aircraft.
const aviation::CTransponder & getTransponder() const
Get transponder.
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