From 220eb033d99a760838f6467f3d464a3c4ab7a067 Mon Sep 17 00:00:00 2001 From: Laurent Lalanne Date: Fri, 19 Feb 2016 20:08:11 +0100 Subject: [PATCH] LP-230 UAVObject generator : Add enum count. Thanks @filnet --- .../plugins/uavobjects/uavobject.h.template | 10 +++++++++- .../generators/gcs/uavobjectgeneratorgcs.cpp | 11 ++++++++++- ground/uavobjgenerator/uavobjectparser.cpp | 6 ++++-- ground/uavobjgenerator/uavobjectparser.h | 18 ++++++++++-------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/ground/gcs/src/plugins/uavobjects/uavobject.h.template b/ground/gcs/src/plugins/uavobjects/uavobject.h.template index e931e3eab..6ec22eb39 100644 --- a/ground/gcs/src/plugins/uavobjects/uavobject.h.template +++ b/ground/gcs/src/plugins/uavobjects/uavobject.h.template @@ -2,7 +2,8 @@ ****************************************************************************** * * @file $(NAMELC).h - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @see The GNU Public License (GPL) Version 3 * @addtogroup GCSPlugins GCS Plugins * @{ @@ -37,6 +38,13 @@ class UAVObjectManager; +class $(NAME)Constants : public QObject { + Q_OBJECT +public: + enum Enum { $(ENUMS_COUNT) }; + Q_ENUMS(Enum) // TODO switch to Q_ENUM once on Qt 5.5 +}; + $(ENUMS) class UAVOBJECTS_EXPORT $(NAME): public UAVDataObject diff --git a/ground/uavobjgenerator/generators/gcs/uavobjectgeneratorgcs.cpp b/ground/uavobjgenerator/generators/gcs/uavobjectgeneratorgcs.cpp index 6b566af28..b8002c0e9 100644 --- a/ground/uavobjgenerator/generators/gcs/uavobjectgeneratorgcs.cpp +++ b/ground/uavobjgenerator/generators/gcs/uavobjectgeneratorgcs.cpp @@ -2,7 +2,8 @@ ****************************************************************************** * * @file uavobjectgeneratorgcs.cpp - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief produce gcs code for uavobjects * * @see The GNU Public License (GPL) Version 3 @@ -54,6 +55,7 @@ struct Context { ObjectInfo *object; // enums QString enums; + QString enumsCount; QString registerImpl; // interface QString fields; @@ -222,6 +224,7 @@ QString generate(Context &ctxt, FieldContext &fieldCtxt, const QString &fragment str.replace(":fieldLimitValues", fieldCtxt.field->limitValues); str.replace(":elementCount", QString::number(fieldCtxt.field->numElements)); + str.replace(":enumCount", QString::number(fieldCtxt.field->numOptions)); return str; } @@ -362,6 +365,8 @@ void generateEnum(Context &ctxt, FieldContext &fieldCtxt) " Q_ENUMS(Enum) // TODO switch to Q_ENUM once on Qt 5.5\n" "};\n\n").arg(enumStringList); + ctxt.enumsCount += generate(ctxt, fieldCtxt, ":PropNameCount = :enumCount, "); + ctxt.registerImpl += generate(ctxt, fieldCtxt, " qmlRegisterType<:ClassName_:PropName>(\"%1.:ClassName\", 1, 0, \":PropName\");\n").arg("UAVTalk"); } @@ -622,6 +627,9 @@ bool UAVObjectGeneratorGCS::process_object(ObjectInfo *object) ctxt.registerImpl += ::generate(ctxt, " qmlRegisterType<:ClassName>(\"%1.:ClassName\", 1, 0, \":ClassName\");\n").arg("UAVTalk"); + ctxt.registerImpl += ::generate(ctxt, + " qmlRegisterType<:ClassNameConstants>(\"%1.:ClassName\", 1, 0, \":ClassNameConstants\");\n").arg("UAVTalk"); + for (int n = 0; n < object->fields.length(); ++n) { FieldInfo *field = object->fields[n]; @@ -664,6 +672,7 @@ bool UAVObjectGeneratorGCS::process_object(ObjectInfo *object) } outInclude.replace("$(ENUMS)", ctxt.enums); + outInclude.replace("$(ENUMS_COUNT)", ctxt.enumsCount); outInclude.replace("$(DATAFIELDS)", ctxt.fields); outInclude.replace("$(DATAFIELDINFO)", ctxt.fieldsInfo); outInclude.replace("$(PROPERTIES)", ctxt.properties); diff --git a/ground/uavobjgenerator/uavobjectparser.cpp b/ground/uavobjgenerator/uavobjectparser.cpp index 50a70bef5..33466eacf 100644 --- a/ground/uavobjgenerator/uavobjectparser.cpp +++ b/ground/uavobjgenerator/uavobjectparser.cpp @@ -2,7 +2,8 @@ ****************************************************************************** * * @file uavobjectparser.cpp - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief Parses XML files and extracts object information. * * @see The GNU Public License (GPL) Version 3 @@ -545,7 +546,8 @@ QString UAVObjectParser::processObjectFields(QDomNode & childNode, ObjectInfo *i } } } - if (field->options.length() == 0) { + field->numOptions = field->options.size(); + if (field->numOptions == 0) { return QString("Object:field:options attribute/element is missing"); } } diff --git a/ground/uavobjgenerator/uavobjectparser.h b/ground/uavobjgenerator/uavobjectparser.h index 368b2ebde..ee9d9460c 100644 --- a/ground/uavobjgenerator/uavobjectparser.h +++ b/ground/uavobjgenerator/uavobjectparser.h @@ -2,7 +2,8 @@ ****************************************************************************** * * @file uavobjectparser.h - * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016. + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief Parses XML files and extracts object information. * * @see The GNU Public License (GPL) Version 3 @@ -46,15 +47,16 @@ typedef enum { } FieldType; typedef struct { - QString name; - QString description; - QString units; - FieldType type; - int numElements; - int numBytes; + QString name; + QString description; + QString units; + FieldType type; + int numElements; + int numOptions; + int numBytes; QStringList elementNames; QStringList options; // for enums only - bool defaultElementNames; + bool defaultElementNames; QStringList defaultValues; QString limitValues; } FieldInfo;