12#include <QtCore/QJsonArray>
13#include <QtQml/QQmlEngine>
24MavlinkActionManager::MavlinkActionManager(
Fact *actionFileNameFact, QObject *parent)
28 setActionFileNameFact(actionFileNameFact);
31MavlinkActionManager::~MavlinkActionManager()
36void MavlinkActionManager::setActionFileNameFact(
Fact *actionFileNameFact)
38 _actionFileNameFact = actionFileNameFact;
40 (void) connect(_actionFileNameFact, &
Fact::rawValueChanged,
this, &MavlinkActionManager::_loadActionsFile);
45void MavlinkActionManager::_loadActionsFile()
48 const QString actionFileName = _actionFileNameFact->rawValue().toString();
49 if (actionFileName.isEmpty()) {
55 const QDir saveDir = QDir(savePath);
56 const QString fullPath = saveDir.absoluteFilePath(actionFileName);
59 const QFileInfo fileInfo = QFileInfo(fullPath);
60 if (!fileInfo.exists()) {
64 constexpr const char *kQgcFileType =
"MavlinkActions";
65 constexpr const char *kActionListKey =
"actions";
73 qgcApp()->showAppMessage(tr(
"Failed to load custom actions file: `%1` error: `%2`").arg(fullPath,
errorString));
77 const QList<JsonHelper::KeyValidateInfo> keyInfoList = {
78 { kActionListKey, QJsonValue::Array,
true },
81 qgcApp()->showAppMessage(tr(
"Custom actions file - incorrect format: %1").arg(
errorString));
85 const QJsonArray actionList = jsonObject[kActionListKey].toArray();
86 for (
const auto &actionJson: actionList) {
87 if (!actionJson.isObject()) {
88 qgcApp()->showAppMessage(tr(
"Custom actions file - incorrect format: JsonValue not an object"));
93 const QList<JsonHelper::KeyValidateInfo> actionKeyInfoList = {
94 {
"label", QJsonValue::String,
true },
95 {
"description", QJsonValue::String,
true },
96 {
"mavCmd", QJsonValue::Double,
true },
98 {
"compId", QJsonValue::Double,
false },
99 {
"param1", QJsonValue::Double,
false },
100 {
"param2", QJsonValue::Double,
false },
101 {
"param3", QJsonValue::Double,
false },
102 {
"param4", QJsonValue::Double,
false },
103 {
"param5", QJsonValue::Double,
false },
104 {
"param6", QJsonValue::Double,
false },
105 {
"param7", QJsonValue::Double,
false },
108 const auto actionObj = actionJson.toObject();
110 qgcApp()->showAppMessage(tr(
"Custom actions file - incorrect format: %1").arg(
errorString));
115 const auto label = actionObj[
"label"].toString();
116 const auto description = actionObj[
"description"].toString();
117 const auto mavCmd = (MAV_CMD)actionObj[
"mavCmd"].toInt();
118 const auto compId = (MAV_COMPONENT)actionObj[
"compId"].toInt(MAV_COMP_ID_AUTOPILOT1);
119 const auto param1 = actionObj[
"param1"].toDouble(0.0);
120 const auto param2 = actionObj[
"param2"].toDouble(0.0);
121 const auto param3 = actionObj[
"param3"].toDouble(0.0);
122 const auto param4 = actionObj[
"param4"].toDouble(0.0);
123 const auto param5 = actionObj[
"param5"].toDouble(0.0);
124 const auto param6 = actionObj[
"param6"].toDouble(0.0);
125 const auto param7 = actionObj[
"param7"].toDouble(0.0);
127 MavlinkAction *
const action =
new MavlinkAction(label, description, mavCmd, compId, param1, param2, param3, param4, param5, param6, param7,
this);
128 QQmlEngine::setObjectOwnership(action, QQmlEngine::CppOwnership);
129 (void) _actions->
append(action);
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QString mavlinkActionsSavePath()
A Fact is used to hold a single value within the system.
void rawValueChanged(const QVariant &value)
void actionFileNameFactChanged()
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
QJsonObject openInternalQGCJsonFile(const QString &jsonFilename, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
returned error string if validation fails
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)