From 8e3f1375816829f528d4b380f4d823ef9377903b Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Tue, 23 Dec 2014 15:59:30 -0800 Subject: [PATCH] More work on Fact meta data --- src/AutoPilotPlugins/PX4/PX4ParameterFacts.cc | 13 ++++- src/AutoPilotPlugins/PX4/PX4ParameterFacts.h | 4 ++ src/FactSystem/Fact.cc | 8 ++-- src/FactSystem/Fact.h | 7 ++- src/FactSystem/FactLoader.cc | 47 +++++++++++++++++-- src/FactSystem/FactLoader.h | 5 ++ src/FactSystem/FactMetaData.cc | 7 +++ src/FactSystem/FactMetaData.h | 6 ++- 8 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/AutoPilotPlugins/PX4/PX4ParameterFacts.cc b/src/AutoPilotPlugins/PX4/PX4ParameterFacts.cc index b3f20f474..a7fdf87ac 100644 --- a/src/AutoPilotPlugins/PX4/PX4ParameterFacts.cc +++ b/src/AutoPilotPlugins/PX4/PX4ParameterFacts.cc @@ -263,4 +263,15 @@ void PX4ParameterFacts::clearStaticData(void) } _mapParameterName2FactMetaData.clear(); _parameterMetaDataLoaded = false; -} \ No newline at end of file +} + +/// Override from FactLoad which connects the meta data to the fact +void PX4ParameterFacts::_addMetaDataToFact(Fact* fact) +{ + if (_mapParameterName2FactMetaData.contains(fact->name())) { + fact->setMetaData(_mapParameterName2FactMetaData[fact->name()]); + } else { + // Use generic meta data + FactLoader::_addMetaDataToFact(fact); + } +} diff --git a/src/AutoPilotPlugins/PX4/PX4ParameterFacts.h b/src/AutoPilotPlugins/PX4/PX4ParameterFacts.h index 01771a264..e39ba6eac 100644 --- a/src/AutoPilotPlugins/PX4/PX4ParameterFacts.h +++ b/src/AutoPilotPlugins/PX4/PX4ParameterFacts.h @@ -52,6 +52,10 @@ public: static void clearStaticData(void); private: + // Overrides from FactLoader + virtual void _addMetaDataToFact(Fact* fact); + + // Class methods static FactMetaData* _parseParameter(QXmlStreamReader& xml, const QString& group); static void _initMetaData(FactMetaData* metaData); static QVariant _stringToTypedVariant(const QString& string, FactMetaData::ValueType_t type, bool failOk = false); diff --git a/src/FactSystem/Fact.cc b/src/FactSystem/Fact.cc index e6bb1b475..11cd7b201 100644 --- a/src/FactSystem/Fact.cc +++ b/src/FactSystem/Fact.cc @@ -28,10 +28,12 @@ #include -Fact::Fact(QObject* parent) : - QObject(parent) +Fact::Fact(QString name, QObject* parent) : + QObject(parent), + _name(name), + _metaData(NULL) { - + _value = ""; } void Fact::setValue(const QVariant& value) diff --git a/src/FactSystem/Fact.h b/src/FactSystem/Fact.h index 23ee2d682..ebfedfc57 100644 --- a/src/FactSystem/Fact.h +++ b/src/FactSystem/Fact.h @@ -42,6 +42,7 @@ class Fact : public QObject { Q_OBJECT + Q_PROPERTY(QString name READ name CONSTANT) Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged USER true) Q_PROPERTY(QVariant defaultValue READ defaultValue CONSTANT) Q_PROPERTY(FactMetaData::ValueType_t type READ type CONSTANT) @@ -54,10 +55,13 @@ class Fact : public QObject Q_ENUMS(FactMetaData::ValueType_t) public: - Fact(QObject* parent = NULL); + Fact(QString name = "", QObject* parent = NULL); // Property system methods + /// Read accessor or name property + QString name(void) const { return _name; } + /// Read accessor for value property QVariant value(void) const { return _value; } @@ -102,6 +106,7 @@ signals: void _containerValueChanged(QVariant& value); private: + QString _name; ///< Fact name QVariant _value; ///< Fact value FactMetaData* _metaData; ///< FactMetaData object for Fact }; diff --git a/src/FactSystem/FactLoader.cc b/src/FactSystem/FactLoader.cc index c1ad52872..5504b8826 100644 --- a/src/FactSystem/FactLoader.cc +++ b/src/FactSystem/FactLoader.cc @@ -83,7 +83,9 @@ void FactLoader::_parameterChanged(int uas, int component, QString parameterName } if (!_mapParameterName2Variant.contains(parameterName)) { - Fact* fact = new Fact(this); + qCDebug(FactLoaderLog) << "Adding new fact" << parameterName; + + Fact* fact = new Fact(parameterName, this); _mapParameterName2Variant[parameterName] = QVariant::fromValue(fact); _mapFact2ParameterName[fact] = parameterName; @@ -91,7 +93,7 @@ void FactLoader::_parameterChanged(int uas, int component, QString parameterName // We need to know when the fact changes from QML so that we can send the new value to the parameter manager connect(fact, &Fact::_containerValueChanged, this, &FactLoader::_valueUpdated); - qCDebug(FactLoaderLog) << "Adding new fact" << parameterName; + _addMetaDataToFact(fact); } Q_ASSERT(_mapParameterName2Variant.contains(parameterName)); @@ -105,7 +107,7 @@ void FactLoader::_parameterChanged(int uas, int component, QString parameterName /// Connected to Fact::valueUpdated /// -/// Sets the new value into the Parameter Manager. Paramter is persisted after send. +/// Sets the new value into the Parameter Manager. Parameter is persisted after send. void FactLoader::_valueUpdated(QVariant value) { Fact* fact = qobject_cast(sender()); @@ -157,3 +159,42 @@ void FactLoader::_paramMgrParameterListUpToDate(void) emit factsReady(); } } + +void FactLoader::_addMetaDataToFact(Fact* fact) +{ + // Create generic meta data based on value variant type + + FactMetaData::ValueType_t factType = FactMetaData::valueTypeInt32; // init to in32 to silence compiler warning + + switch ((QMetaType::Type)fact->value().type()) { + case QMetaType::Int: + factType = FactMetaData::valueTypeInt32; + break; + + case QMetaType::UInt: + factType = FactMetaData::valueTypeUint32; + break; + + case QMetaType::Double: + factType = FactMetaData::valueTypeDouble; + + case QMetaType::Short: + factType = FactMetaData::valueTypeInt16; + break; + + case QMetaType::UShort: + factType = FactMetaData::valueTypeUint16; + break; + + case QMetaType::Float: + factType = FactMetaData::valueTypeFloat; + break; + + default: + qCWarning(FactLoaderLog) << "Invalid variant type" << fact->value().type(); + break; + } + + FactMetaData* metaData = new FactMetaData(this); + metaData->initFromTypeOnly(factType); +} diff --git a/src/FactSystem/FactLoader.h b/src/FactSystem/FactLoader.h index ed9503df0..699a1fbe0 100644 --- a/src/FactSystem/FactLoader.h +++ b/src/FactSystem/FactLoader.h @@ -66,6 +66,11 @@ signals: /// Signalled when the full set of facts are ready void factsReady(void); +protected: + /// Base implementation adds generic meta data based on variant type. Derived class can override to provide + /// more details meta data. + virtual void _addMetaDataToFact(Fact* fact); + private slots: void _parameterChanged(int uas, int component, QString parameterName, QVariant value); void _valueUpdated(QVariant value); diff --git a/src/FactSystem/FactMetaData.cc b/src/FactSystem/FactMetaData.cc index 769bb4900..1b4de8954 100644 --- a/src/FactSystem/FactMetaData.cc +++ b/src/FactSystem/FactMetaData.cc @@ -31,5 +31,12 @@ FactMetaData::FactMetaData(QObject* parent) : QObject(parent) { + initFromTypeOnly(valueTypeInt32); +} +void FactMetaData::initFromTypeOnly(ValueType_t initType) +{ + type = initType; + + // FIXME: NYI } diff --git a/src/FactSystem/FactMetaData.h b/src/FactSystem/FactMetaData.h index 9370b4316..47924be5e 100644 --- a/src/FactSystem/FactMetaData.h +++ b/src/FactSystem/FactMetaData.h @@ -54,8 +54,12 @@ public: valueTypeDouble } ValueType_t; - QVariant defaultValue; + /// Initialize the meta data given only the type. + void initFromTypeOnly(ValueType_t initType); + + // FIXME: This needs to switch over to Q_PROPERTY mechanism ValueType_t type; + QVariant defaultValue; QString shortDescription; QString longDescription; QString units; -- 2.22.0