swift
atcstationmodel.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 
4 #include "core/afv/model/atcstationmodel.h"
5 
6 #include <QtMath>
7 
8 using namespace swift::core::afv;
9 
10 namespace swift::core::afv::model
11 {
12  CSampleAtcStation::CSampleAtcStation(const QString &callsign, const TransceiverDto &transceiver)
13  : m_callsign(callsign), m_transceiver(transceiver)
14  {}
15 
16  double CSampleAtcStation::latitude() const { return m_transceiver.LatDeg; }
17 
18  double CSampleAtcStation::longitude() const { return m_transceiver.LonDeg; }
19 
20  quint32 CSampleAtcStation::frequency() const { return m_transceiver.frequencyHz; }
21 
23  {
24  return QString::number(m_transceiver.frequencyHz / 1000000.0, 'f', 3);
25  }
26 
28  {
29  const double sqrtAltM = qSqrt(m_transceiver.HeightMslM);
30  const double radioFactor = 4193.18014745372;
31  return radioFactor * sqrtAltM;
32  }
33 
34  CSampleAtcStationModel::CSampleAtcStationModel(QObject *parent) : QAbstractListModel(parent) {}
35 
37 
38  void CSampleAtcStationModel::updateAtcStations(const QVector<CSampleAtcStation> &atcStations)
39  {
40  // Add stations which didn't exist yet
41  for (const auto &station : atcStations)
42  {
43  if (!m_atcStations.contains(station)) { addStation(station); }
44  }
45 
46  // Remove all stations which are no longer there
47  for (int i = m_atcStations.size() - 1; i >= 0; i--)
48  {
49  CSampleAtcStation &station = m_atcStations[i];
50  if (!m_atcStations.contains(station)) { removeStationAtPosition(i); }
51  }
52  }
53 
54  void CSampleAtcStationModel::addStation(const CSampleAtcStation &atcStation)
55  {
56  beginInsertRows(QModelIndex(), rowCount(), rowCount());
57  m_atcStations << atcStation;
58  endInsertRows();
59  }
60 
61  void CSampleAtcStationModel::removeStationAtPosition(int i)
62  {
63  beginRemoveRows(QModelIndex(), i, i);
64  m_atcStations.removeAt(i);
65  endRemoveRows();
66  }
67 
68  int CSampleAtcStationModel::rowCount(const QModelIndex &parent) const
69  {
70  Q_UNUSED(parent)
71  return m_atcStations.count();
72  }
73 
74  QVariant CSampleAtcStationModel::data(const QModelIndex &index, int role) const
75  {
76  if (index.row() < 0 || index.row() >= m_atcStations.count()) return QVariant();
77 
78  const CSampleAtcStation &atcStation = m_atcStations[index.row()];
79  if (role == CallsignRole) return atcStation.callsign();
80  if (role == LatitudeRole) return atcStation.latitude();
81  if (role == LongitudeRole) return atcStation.longitude();
82  if (role == RadioDistanceRole) return atcStation.radioDistanceM();
83  if (role == FrequencyRole) return atcStation.formattedFrequency();
84  if (role == FrequencyKhzRole) return atcStation.frequency() / 1000;
85  return QVariant();
86  }
87 
89  {
91  roles[CallsignRole] = "callsign";
92  roles[LatitudeRole] = "latitude";
93  roles[LongitudeRole] = "longitude";
94  roles[RadioDistanceRole] = "radioDistanceM";
95  roles[FrequencyRole] = "frequencyAsString";
96  roles[FrequencyKhzRole] = "frequencyKhz";
97  return roles;
98  }
99 } // namespace swift::core::afv::model
const QString & callsign() const
Getter.
void updateAtcStations(const QVector< CSampleAtcStation > &atcStations)
Update the stations.
QHash< int, QByteArray > roleNames() const
copydoc QAbstractListModel::roleNames
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
copydoc QAbstractListModel::data
CSampleAtcStationModel(QObject *parent=nullptr)
Ctor.
int rowCount(const QModelIndex &parent=QModelIndex()) const
copydoc QAbstractListModel::rowCount
Transceiver DTO.
Definition: dto.h:117
double LonDeg
Properties.
Definition: dto.h:123
double LatDeg
Properties.
Definition: dto.h:122
double HeightMslM
Properties.
Definition: dto.h:124
quint32 frequencyHz
Properties.
Definition: dto.h:121