Debian Codegraph @

src/GUI/DefaultAircraftLocator.cxx

raw symbols SCIP
1 #include "DefaultAircraftLocator.hxx"
2
3 #include <simgear/props/props_io.hxx>
4 #include <simgear/debug/logstream.hxx>
5 #include <simgear/structure/exception.hxx>
6
7 #include <Main/globals.hxx>
8 #include <Main/locale.hxx>
9
10 using namespace std::string_literals;
11
12 static SGPropertyNode_ptr loadXMLDefaults()
13 {
14 SGPropertyNode_ptr root(new SGPropertyNode);
15 const SGPath defaultsXML = globals->get_fg_root() / "defaults.xml";
16 if (!defaultsXML.exists()) {
17 SG_LOG(SG_GUI, SG_POPUP, "Failed to find required data file (defaults.xml)");
18 return {};
19 }
20
21 try {
22 readProperties(defaultsXML, root);
23 } catch (sg_exception& e) {
24 SG_LOG(SG_GUI, SG_POPUP, "Failed to read required data file (defaults.xml)");
25 return {};
26 }
27
28 if (!root->hasChild("sim")) {
29 SG_LOG(SG_GUI, SG_POPUP, "Failed to find /sim node in defaults.xml, broken");
30 return {};
31 }
32
33 return root;
34 }
35
36
37 namespace flightgear
38 {
39
40 std::string defaultAirportICAO()
41 {
42 SGPropertyNode_ptr root = loadXMLDefaults();
43 if (!root) {
44 return {};
45 }
46
47 std::string airportCode = root->getStringValue("/sim/presets/airport-id");
48 return airportCode;
49 }
50
51 string_list defaultSplashScreenPaths()
52 {
53 string_list result;
54 SGPath tpath = globals->get_fg_root() / "Textures";
55 auto paths = simgear::Dir(tpath).children(simgear::Dir::TYPE_FILE);
56 paths.erase(std::remove_if(paths.begin(), paths.end(), [](const SGPath& p) {
57 const auto f = p.file();
58 if (f.find("Splash") != 0) return true;
59 const auto ext = p.extension();
60 return ext != "png" && ext != "jpg";
61 }), paths.end());
62
63 for (auto c : paths) {
64 result.push_back(c.utf8Str());
65 }
66
67 return result;
68 }
69
70 DefaultAircraftLocator::DefaultAircraftLocator()
71 {
72 SGPropertyNode_ptr root = loadXMLDefaults();
73 if (root) {
74 _aircraftId = root->getStringValue("/sim/aircraft");
75 } else {
76 SG_LOG(SG_GUI, SG_WARN, "failed to load default aircraft identifier");
77 _aircraftId = "ufo"; // last ditch fallback
78 }
79
80 _aircraftId += "-set.xml";
81 const SGPath rootAircraft = globals->get_fg_root() / "Aircraft";
82 visitDir(rootAircraft, 0);
83 }
84
85 SGPath DefaultAircraftLocator::foundPath() const
86 {
87 return _foundPath;
88 }
89
90 AircraftDirVistorBase::VisitResult
91 DefaultAircraftLocator::visit(const SGPath& p)
92 {
93 if (p.file() == _aircraftId) {
94 _foundPath = p;
95 return VISIT_DONE;
96 }
97
98 return VISIT_CONTINUE;
99 }
100
101 WeatherScenariosModel::WeatherScenariosModel(QObject *pr) :
102 QAbstractListModel(pr)
103 {
104 SGPropertyNode_ptr root = loadXMLDefaults();
105 if (root) {
106 SGPropertyNode_ptr scenarios = root->getNode("environment/weather-scenarios");
107 Q_ASSERT(scenarios);
108 int nChildren = scenarios->nChildren();
109 for (int i = 0; i < nChildren; i++) {
110 SGPropertyNode_ptr scenario = scenarios->getChild(i);
111 if (scenario->getNameString() != "scenario") {
112 continue;
113 }
114
115 // omit the 'live data' option, we have a distinct UI for that, we'll
116 // pass --real-wxr option on launch
117 if (scenario->getStringValue("local-weather/tile-type") == "live"s) {
118 continue;
119 }
120
121 WeatherScenario ws;
122 const std::string wsId = scenario->getStringValue("id");
123 if (!wsId.empty()) {
124 // translated
125 auto locale = globals->get_locale();
126 ws.name = QString::fromStdString(locale->getLocalizedString(wsId + "-name", "weather-scenarios"));
127 ws.description = QString::fromStdString(locale->getLocalizedString(wsId + "-desc", "weather-scenarios"));
128 } else {
129 ws.name = QString::fromStdString(scenario->getStringValue("name"));
130 ws.description = QString::fromStdString(scenario->getStringValue("description")).simplified();
131 }
132
133 ws.metar = QString::fromStdString(scenario->getStringValue("metar"));
134 if (scenario->hasChild("local-weather")) {
135 ws.localWeatherTileManagement = QString::fromStdString(scenario->getStringValue("local-weather/tile-management"));
136 ws.localWeatherTileType = QString::fromStdString(scenario->getStringValue("local-weather/tile-type"));
137 }
138 m_scenarios.push_back(ws);
139 }
140 }
141 }
142
143 int WeatherScenariosModel::rowCount(const QModelIndex&) const
144 {
145 return static_cast<int>(m_scenarios.size());
146 }
147
148 QVariant WeatherScenariosModel::data(const QModelIndex &index, int role) const
149 {
150 const int row = index.row();
151 if ((row < 0) || (static_cast<std::vector<WeatherScenariosModel::WeatherScenario>::size_type>(row) >= m_scenarios.size())) {
152 return QVariant();
153 }
154
155 const WeatherScenario& scenario(m_scenarios.at(row));
156 if ((role == Qt::DisplayRole) || (role == NameRole)) {
157 return scenario.name;
158 } else if (role == DescriptionRole) {
159 return scenario.description;
160 } else if (role == MetarRole) {
161 return scenario.metar;
162 }
163
164 return QVariant();
165 }
166
167 QHash<int, QByteArray> WeatherScenariosModel::roleNames() const
168 {
169 QHash<int, QByteArray> result;
170 result[NameRole] = "name";
171 result[DescriptionRole] = "description";
172 result[MetarRole] = "metar";
173 return result;
174 }
175
176 QString WeatherScenariosModel::metarForItem(quint32 index) const
177 {
178 if (index >= m_scenarios.size()) {
179 return {};
180 }
181
182 return m_scenarios.at(index).metar;
183 }
184
185 QString WeatherScenariosModel::descriptionForItem(quint32 index) const
186 {
187 if (index >= m_scenarios.size()) {
188 return {};
189 }
190
191 return m_scenarios.at(index).description;
192 }
193
194 QStringList WeatherScenariosModel::localWeatherData(quint32 index) const
195 {
196 if (index >= m_scenarios.size()) {
197 return {};
198 }
199
200 const auto& s = m_scenarios.at(index);
201 if (s.localWeatherTileManagement.isEmpty() || s.localWeatherTileType.isEmpty()) {
202 return {};
203 }
204
205 return QStringList() << s.localWeatherTileManagement << s.localWeatherTileType;
206 }
207
208 QString WeatherScenariosModel::nameForItem(quint32 index) const
209 {
210 if (index >= m_scenarios.size()) {
211 return {};
212 }
213
214 return m_scenarios.at(index).name;
215 }
216
217 } // of namespace flightgear