FlightGear next
CarriersLocationModel.cxx
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include <QPixmap>
6
9
11 : QAbstractListModel(parent)
12{
13 SGPropertyNode_ptr localRoot(new SGPropertyNode);
15
16// this code encodes some scenario structure, sorry
17 for (auto s : localRoot->getNode("sim/ai/scenarios")->getChildren("scenario")) {
18 const std::string scenarioId = s->getStringValue("id");
19 for (auto c : s->getChildren("carrier")) {
20 processCarrier(scenarioId, c);
21 }
22 }
23}
24
25void CarriersLocationModel::processCarrier(const std::string &scenario, SGPropertyNode_ptr carrierNode)
26{
27 const auto name = QString::fromStdString(carrierNode->getStringValue("name"));
28 const auto pennant = QString::fromStdString(carrierNode->getStringValue("pennant-number"));
29 const auto tacan = QString::fromStdString(carrierNode->getStringValue("TACAN-channel-ID"));
30 const auto desc = QString::fromStdString(carrierNode->getStringValue("description"));
31 SGGeod geod = SGGeod::fromDeg(carrierNode->getDoubleValue("longitude"),
32 carrierNode->getDoubleValue("latitude"));
33
34 QStringList parkings;
35 for (auto c : carrierNode->getChildren("parking-pos")) {
36 parkings.append(QString::fromStdString(c->getStringValue()));
37 }
38
39 mCarriers.push_back(Carrier{
40 QString::fromStdString(scenario),
41 pennant,
42 name,
43 desc,
44 geod,
45 tacan,
46 parkings
47 });
48}
49
50int CarriersLocationModel::rowCount(const QModelIndex &parent) const
51{
52 // For list models only the root node (an invalid parent) should return the list's size. For all
53 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
54 if (parent.isValid())
55 return 0;
56
57 return static_cast<int>(mCarriers.size());
58}
59
60QVariant CarriersLocationModel::data(const QModelIndex &index, int role) const
61{
62 if (!index.isValid())
63 return QVariant();
64
65 if (m_carrierPixmap.isNull()) {
66 auto iconProvider = QmlColoredImageProvider::instance();
67 QSize sz;
68 m_carrierPixmap = iconProvider->requestPixmap("aircraft-carrier?theme", &sz, {});
69 }
70
71 const auto& c = mCarriers.at(static_cast<size_t>(index.row()));
72 switch (role) {
73 case Qt::DisplayRole:
74 case NameRole: return c.mName;
75 // case GeodRole: return QVariant::fromValue(c.mInitialLocation);
76 case IdentRole: return c.mCallsign;
77 case DescriptionRole: return c.mDescription;
78 case TypeRole: return "Carrier";
79 case IconRole: return m_carrierPixmap;
80 default:
81 break;
82 }
83
84 return {};
85}
86
87QHash<int, QByteArray> CarriersLocationModel::roleNames() const
88{
89 QHash<int, QByteArray> result = QAbstractListModel::roleNames();
90
91 result[GeodRole] = "geod";
92 result[GuidRole] = "guid";
93 result[IdentRole] = "ident";
94 result[NameRole] = "name";
95 result[IconRole] = "icon";
96 result[TypeRole] = "type";
97 result[DescriptionRole] = "description";
98 result[NavFrequencyRole] = "frequency";
99 return result;
100}
101
102int CarriersLocationModel::indexOf(const QString name) const
103{
104 auto it = std::find_if(mCarriers.begin(), mCarriers.end(), [name]
105 (const Carrier& carrier)
106 { return name == carrier.mName || name == carrier.mCallsign; });
107 if (it == mCarriers.end())
108 return -1;
109
110 return static_cast<int>(std::distance(mCarriers.begin(), it));
111}
112
114{
115 const auto uIndex = static_cast<size_t>(index);
116 if ((index < 0) || (uIndex >= mCarriers.size())) {
117 return {};
118 }
119
120 const auto& c = mCarriers.at(uIndex);
121 return c.mInitialLocation;
122}
123
125{
126 const auto uIndex = static_cast<size_t>(index);
127 if ((index < 0) || (uIndex >= mCarriers.size())) {
128 return {};
129 }
130
131 const auto& c = mCarriers.at(uIndex);
132 return c.mCallsign;
133}
134
135QStringList CarriersLocationModel::parkingsForIndex(int index) const
136{
137 const auto uIndex = static_cast<size_t>(index);
138 if ((index < 0) || (uIndex >= mCarriers.size())) {
139 return {};
140 }
141
142 const auto& c = mCarriers.at(uIndex);
143 return c.mParkings;
144}
QString pennantForIndex(int index) const
SGGeod geodForIndex(int index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
CarriersLocationModel(QObject *parent=nullptr)
int indexOf(const QString name) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames() const override
QStringList parkingsForIndex(int index) const
static void registerScenarios(SGPropertyNode_ptr root={})
Static helper to register scenarios.
static QmlColoredImageProvider * instance()
const char * name