swift
atcbuttoncomponent.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 "atcbuttoncomponent.h"
5 
6 #include <math.h>
7 
8 #include <QGridLayout>
9 #include <QPushButton>
10 
11 #include "ui_atcbuttoncomponent.h"
12 
13 #include "gui/guiapplication.h"
14 #include "gui/guiutility.h"
16 
17 using namespace swift::misc;
18 using namespace swift::misc::aviation;
19 using namespace swift::misc::network;
20 using namespace swift::core;
21 using namespace swift::core::context;
22 
23 namespace swift::gui::components
24 {
25  CAtcButtonComponent::CAtcButtonComponent(QWidget *parent) : QFrame(parent), ui(new Ui::CAtcButtonComponent)
26  {
27  ui->setupUi(this);
28  if (sGui && sGui->getIContextNetwork())
29  {
30  connect(sGui->getIContextNetwork(), &IContextNetwork::changedAtcStationsOnlineDigest, this,
31  &CAtcButtonComponent::onChangedAtcStations);
32  connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this,
33  &CAtcButtonComponent::onConnectionStatusChanged);
34  }
35 
36  this->setVisible(false); // will be changed when ATC stations are reported
37  }
38 
40 
42  {
43  this->setVisible(false);
44  this->setMinimumHeight(0);
45 
46  if (!sGui || !sGui->getIContextNetwork()) { return; }
47  if (m_maxNumber < 1) { return; }
48  const int max = qMin(m_maxNumber, m_rows * m_cols);
50  if (stations.isEmpty()) { return; }
51 
52  CGuiUtility::deleteLayout(this->layout(), true);
53  QGridLayout *layout = new QGridLayout(this);
54 
55  layout->setObjectName("gl_CAtcButtonComponent");
56  layout->setSpacing(4);
57  layout->setContentsMargins(0, 0, 0, 0);
58 
59  int row = 0;
60  int col = 0;
61  int added = 0;
62  for (const CAtcStation &station : std::as_const(stations))
63  {
64  if (m_ignoreNonAtc)
65  {
66  // strict check, only "real ATC stations", no supervisors etc
67  if (!station.getCallsign().hasAtcSuffix()) { continue; }
68  }
69 
70  QPushButton *button = new QPushButton(this);
71  button->setText(station.getCallsignAsString());
72  if (m_withIcons) { button->setIcon(CIcon(station.toIcon()).toQIcon()); }
73  QObject::connect(button, &QPushButton::released, this, &CAtcButtonComponent::onButtonClicked);
74  const CVariant atcv = CVariant::fromValue(station);
75  layout->addWidget(button, row, col++);
76  button->show();
77  button->setProperty("atc", atcv.getQVariant());
78  added++;
79 
80  if (col >= m_cols)
81  {
82  if (row == m_rows) { break; }
83  row++;
84  col = 0;
85  }
86  }
87 
88  const double a = added;
89  const double c = m_cols;
90  const int rows = qRound(ceil(a / c)); // row can be too high
91  if (added > 0)
92  {
93  this->setVisible(true);
94  this->setMinimumHeight(rows * 25);
95  }
96  }
97 
98  void CAtcButtonComponent::setRowsColumns(int rows, int cols, bool setMaxElements)
99  {
100  m_rows = rows;
101  m_cols = cols;
102  if (setMaxElements) { m_maxNumber = rows * cols; }
103  }
104 
105  void CAtcButtonComponent::onChangedAtcStations()
106  {
107  if (!m_backgroundUpdates) { return; }
108  if (!this->isVisible()) { return; }
109  this->updateStations();
110  }
111 
112  void CAtcButtonComponent::onConnectionStatusChanged(const CConnectionStatus &from, const CConnectionStatus &to)
113  {
114  Q_UNUSED(from);
115  if (to.isDisconnected()) { this->setVisible(false); }
116  }
117 
118  void CAtcButtonComponent::onButtonClicked()
119  {
120  QPushButton *button = qobject_cast<QPushButton *>(QObject::sender());
121  if (!button) { return; }
122  const CVariant v(button->property("atc"));
123  if (!v.isValid() || !v.canConvert<CAtcStation>()) { return; }
124  const CAtcStation station = v.value<CAtcStation>();
125  emit this->requestAtcStation(station);
126  }
127 } // namespace swift::gui::components
const context::IContextNetwork * getIContextNetwork() const
Direct access to contexts if a CCoreFacade has been initialized.
virtual swift::misc::aviation::CAtcStationList getClosestAtcStationsOnline(int number) const =0
The ATC list with online ATC controllers.
static void deleteLayout(QLayout *layout, bool deleteWidgets)
Delete hierarchy of layouts.
Definition: guiutility.cpp:532
void requestAtcStation(const swift::misc::aviation::CAtcStation &station)
ATC station clicked.
void setRowsColumns(int rows, int cols, bool setMaxElements)
Rows/columns.
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition: icon.h:39
QIcon toQIcon() const
A QIcon.
Definition: icon.cpp:45
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
const QVariant & getQVariant() const
Return the internal QVariant.
Definition: variant.h:191
Value object encapsulating information about an ATC station.
Definition: atcstation.h:38
Value object for a list of ATC stations.
Value object encapsulating information about a connection status.
bool isDisconnected() const
Query status.
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
High level reusable GUI components.
Definition: aboutdialog.cpp:13
Free functions in swift::misc.