3#include <QtCore/QApplicationStatic>
5#include <QtCore/QFileInfo>
6#include <QtCore/QJsonArray>
7#include <QtCore/QJsonParseError>
8#include <QtCore/QObject>
9#include <QtCore/QTranslator>
26QJsonObject
_translateRoot(QJsonObject& jsonObject,
const QString& translateContext,
const QStringList& translateKeys);
27QJsonObject
_translateObject(QJsonObject& jsonObject,
const QString& translateContext,
28 const QStringList& translateKeys);
29QJsonArray
_translateArray(QJsonArray& jsonArray,
const QString& translateContext,
const QStringList& translateKeys);
39 QString translateKeys;
41 if (!fileType.isEmpty()) {
46 translateKeys = QStringLiteral(
"label,enumStrings,friendlyName,description,category");
57 translateKeys = QStringLiteral(
"shortDesc,longDesc,enumStrings");
67 return translateKeys.split(
",");
71 const QStringList& translateKeys)
77 const QStringList& translateKeys)
79 for (
const QString& key : jsonObject.keys()) {
80 if (jsonObject[key].isString()) {
81 QString locString = jsonObject[key].toString();
82 if (!translateKeys.contains(key)) {
86 QString disambiguation;
87 QString disambiguationPrefix(
"#loc.disambiguation#");
89 if (locString.startsWith(disambiguationPrefix)) {
90 locString = locString.right(locString.length() - disambiguationPrefix.length());
91 const int commentEndIndex = locString.indexOf(
"#");
92 if (commentEndIndex != -1) {
93 disambiguation = locString.left(commentEndIndex);
94 locString = locString.right(locString.length() - disambiguation.length() - 1);
98 const QString xlatString =
99 translator()->translate(translateContext.toUtf8().constData(), locString.toUtf8().constData(),
100 disambiguation.toUtf8().constData());
101 if (!xlatString.isNull()) {
102 jsonObject[key] = xlatString;
104 }
else if (jsonObject[key].isArray()) {
105 QJsonArray childJsonArray = jsonObject[key].toArray();
106 jsonObject[key] =
_translateArray(childJsonArray, translateContext, translateKeys);
107 }
else if (jsonObject[key].isObject()) {
108 QJsonObject childJsonObject = jsonObject[key].toObject();
109 jsonObject[key] =
_translateObject(childJsonObject, translateContext, translateKeys);
117 const QStringList& translateKeys)
119 for (qsizetype i = 0; i < jsonArray.count(); i++) {
120 QJsonObject childJsonObject = jsonArray[i].toObject();
121 jsonArray[i] =
_translateObject(childJsonObject, translateContext, translateKeys);
135 if (!jsonValue.isArray()) {
136 errorString = QObject::tr(
"value for coordinate is not array");
140 const QJsonArray coordinateArray = jsonValue.toArray();
141 const int requiredCount = altitudeRequired ? 3 : 2;
142 if (coordinateArray.count() != requiredCount) {
143 errorString = QObject::tr(
"Coordinate array must contain %1 values").arg(requiredCount);
147 for (
const QJsonValue& coordinateValue : coordinateArray) {
148 if ((coordinateValue.type() != QJsonValue::Double) && (coordinateValue.type() != QJsonValue::Null)) {
150 QObject::tr(
"Coordinate array may only contain double values, found: %1").arg(coordinateValue.type());
156 coordinate = QGeoCoordinate(coordinateArray[1].toDouble(), coordinateArray[0].toDouble());
158 coordinate = QGeoCoordinate(
163 if (altitudeRequired) {
173 QJsonArray coordinateArray;
176 coordinateArray << coordinate.longitude() << coordinate.latitude();
178 coordinateArray << coordinate.latitude() << coordinate.longitude();
182 coordinateArray << coordinate.altitude();
185 jsonValue = QJsonValue(coordinateArray);
189 int minSupportedVersion,
int maxSupportedVersion,
int& version,
192 static const QList<JsonHelper::KeyValidateInfo> requiredKeys = {
202 if (fileTypeValue != expectedFileType) {
203 errorString = QObject::tr(
"Incorrect file type key expected:%1 actual:%2").arg(expectedFileType, fileTypeValue);
208 if (version < minSupportedVersion) {
209 errorString = QObject::tr(
"File version %1 is no longer supported").arg(version);
213 if (version > maxSupportedVersion) {
214 errorString = QObject::tr(
"File version %1 is newer than current supported version %2")
216 .arg(maxSupportedVersion);
224 int minSupportedVersion,
int maxSupportedVersion,
int& version,
227 static const QList<JsonHelper::KeyValidateInfo> requiredKeys = {
240 int minSupportedVersion,
int maxSupportedVersion,
int& version,
248 QJsonParseError jsonParseError;
250 if (jsonParseError.error != QJsonParseError::NoError) {
251 errorString = QObject::tr(
"Unable to parse json file: %1 error: %2 offset: %3")
252 .arg(jsonFilename, jsonParseError.errorString())
253 .arg(jsonParseError.offset);
257 if (!doc.isObject()) {
258 errorString = QObject::tr(
"Root of json file is not object: %1").arg(jsonFilename);
262 QJsonObject jsonObject = doc.object();
271 const QString context = QFileInfo(jsonFilename).fileName();
285 if (!jsonValue.isArray()) {
286 errorString = QObject::tr(
"value for coordinate array is not array");
290 const QJsonArray rgJsonPoints = jsonValue.toArray();
293 for (
const QJsonValue& point : rgJsonPoints) {
294 QGeoCoordinate coordinate;
298 rgVarPoints.append(QVariant::fromValue(coordinate));
305 QList<QGeoCoordinate>& rgPoints, QString&
errorString)
307 QVariantList rgVarPoints;
314 for (
const QVariant& point : rgVarPoints) {
315 rgPoints.append(point.value<QGeoCoordinate>());
323 QJsonArray rgJsonPoints;
324 for (
const QVariant& point : rgVarPoints) {
325 QJsonValue jsonPoint;
327 rgJsonPoints.append(jsonPoint);
330 jsonValue = rgJsonPoints;
334 QJsonValue& jsonValue)
336 QVariantList rgVarPoints;
337 for (
const QGeoCoordinate& coord : rgPoints) {
338 rgVarPoints.append(QVariant::fromValue(coord));
348 QList<QJsonValue::Type> typeList;
352 keyList.append(info.key);
361 keyList.append(info.key);
362 typeList.append(info.type);
371 for (
const QJsonValue& pointValue : polygonArray) {
372 QGeoCoordinate pointCoord;
385 for (qsizetype i = 0; i < list.
count(); i++) {
388 QJsonValue jsonValue;
390 polygonArray.append(jsonValue);
Q_APPLICATION_STATIC(ADSBVehicleManager, _adsbVehicleManager, SettingsManager::instance() ->adsbVehicleManagerSettings())
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static constexpr const char * qgcFileType
This is a QGeoCoordinate within a QObject such that it can be used on a QmlObjectListModel.
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
int count() const override final
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
bool validateExternalQGCJsonFile(const QJsonObject &jsonObject, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
returned error string if validation fails
void saveGeoCoordinateArray(const QVariantList &rgVarPoints, bool writeAltitude, QJsonValue &jsonValue)
Saves a list of QGeoCoordinates to a json array.
bool loadGeoCoordinateArray(const QJsonValue &jsonValue, bool altitudeRequired, QVariantList &rgVarPoints, QString &errorString)
returned error string if load failure
QJsonObject openInternalQGCJsonFile(const QString &jsonFilename, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
returned error string if validation fails
constexpr const char * _jsonGroundStationKey
QJsonObject _translateRoot(QJsonObject &jsonObject, const QString &translateContext, const QStringList &translateKeys)
constexpr const char * _jsonGroundStationValue
QJsonObject _translateObject(QJsonObject &jsonObject, const QString &translateContext, const QStringList &translateKeys)
constexpr const char * jsonVersionKey
QTranslator * translator()
bool loadPolygon(const QJsonArray &polygonArray, QmlObjectListModel &list, QObject *parent, QString &errorString)
Loads a polygon from an array.
constexpr const char * _translateKeysKey
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
void saveQGCJsonFileHeader(QJsonObject &jsonObject, const QString &fileType, int version)
Saves the standard file header the json object.
QJsonArray _translateArray(QJsonArray &jsonArray, const QString &translateContext, const QStringList &translateKeys)
constexpr const char * jsonFileTypeKey
void savePolygon(const QmlObjectListModel &list, QJsonArray &polygonArray)
Saves a polygon to a json array.
bool validateInternalQGCJsonFile(const QJsonObject &jsonObject, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
returned error string if validation fails
constexpr const char * _arrayIDKeysKey
void saveGeoCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue, bool geoJsonFormat=false)
QStringList _addDefaultLocKeys(QJsonObject &jsonObject)
bool loadGeoCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString, bool geoJsonFormat=false)
if true, use [lon, lat], [lat, lon] otherwise
bool validateKeyTypes(const QJsonObject &jsonObject, const QStringList &keys, const QList< QJsonValue::Type > &types, QString &errorString)
double possibleNaNJsonValue(const QJsonValue &value)
Returns NaN if the value is null, or the value converted to double otherwise.
bool validateRequiredKeys(const QJsonObject &jsonObject, const QStringList &keys, QString &errorString)
Validates that all listed keys are present in the object.
QByteArray readFile(const QString &filePath, QString *errorString, qint64 maxBytes)
QJsonDocument parseCompressedJson(const QByteArray &data, QJsonParseError *error)