| 1 | #include "CarriersLocationModel.hxx" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | |
| 5 | #include <QPixmap> |
| 6 | |
| 7 | #include "AIModel/AIManager.hxx" |
| 8 | #include "QmlColoredImageProvider.hxx" |
| 9 | |
| 10 | CarriersLocationModel::CarriersLocationModel(QObject *parent) |
| 11 | : QAbstractListModel(parent) |
| 12 | { |
| 13 | SGPropertyNode_ptr localRoot(new SGPropertyNode); |
| 14 | FGAIManager::registerScenarios(localRoot); |
| 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 | |
| 25 | void 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 | |
| 50 | int 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 | |
| 60 | QVariant 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 | |
| 87 | QHash<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 | |
| 102 | int 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 | |
| 113 | SGGeod CarriersLocationModel::geodForIndex(int index) const |
| 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 | |
| 124 | QString CarriersLocationModel::pennantForIndex(int index) const |
| 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 | |
| 135 | QStringList 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 | } |