Fact.h 9.97 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15 16 17 18 19 20 21

/// @file
///     @author Don Gagne <don@thegagnes.com>

#ifndef Fact_H
#define Fact_H

#include "FactMetaData.h"

#include <QObject>
#include <QString>
#include <QVariant>
22
#include <QDebug>
Don Gagne's avatar
Don Gagne committed
23 24 25 26 27 28 29

/// @brief A Fact is used to hold a single value within the system.
class Fact : public QObject
{
    Q_OBJECT
    
public:
Don Gagne's avatar
Don Gagne committed
30
    Fact(QObject* parent = NULL);
31
    Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent = NULL);
Don Gagne's avatar
Don Gagne committed
32 33 34 35
    Fact(const Fact& other, QObject* parent = NULL);

    const Fact& operator=(const Fact& other);

36
    Q_PROPERTY(int          componentId             READ componentId                                        CONSTANT)
37 38
    Q_PROPERTY(QStringList  bitmaskStrings          READ bitmaskStrings                                     NOTIFY bitmaskStringsChanged)
    Q_PROPERTY(QVariantList bitmaskValues           READ bitmaskValues                                      NOTIFY bitmaskValuesChanged)
39
    Q_PROPERTY(int          decimalPlaces           READ decimalPlaces                                      CONSTANT)
40 41
    Q_PROPERTY(QVariant     defaultValue            READ cookedDefaultValue                                 CONSTANT)
    Q_PROPERTY(QString      defaultValueString      READ cookedDefaultValueString                           CONSTANT)
42 43
    Q_PROPERTY(bool         defaultValueAvailable   READ defaultValueAvailable                              CONSTANT)
    Q_PROPERTY(int          enumIndex               READ enumIndex              WRITE setEnumIndex          NOTIFY valueChanged)
44
    Q_PROPERTY(QStringList  enumStrings             READ enumStrings                                        NOTIFY enumStringsChanged)
45
    Q_PROPERTY(QString      enumStringValue         READ enumStringValue        WRITE setEnumStringValue    NOTIFY valueChanged)
46
    Q_PROPERTY(QVariantList enumValues              READ enumValues                                         NOTIFY enumValuesChanged)
47 48
    Q_PROPERTY(QString      group                   READ group                                              CONSTANT)
    Q_PROPERTY(QString      longDescription         READ longDescription                                    CONSTANT)
49 50
    Q_PROPERTY(QVariant     max                     READ cookedMax                                          CONSTANT)
    Q_PROPERTY(QString      maxString               READ cookedMaxString                                    CONSTANT)
51
    Q_PROPERTY(bool         maxIsDefaultForType     READ maxIsDefaultForType                                CONSTANT)
52 53
    Q_PROPERTY(QVariant     min                     READ cookedMin                                          CONSTANT)
    Q_PROPERTY(QString      minString               READ cookedMinString                                    CONSTANT)
54 55
    Q_PROPERTY(bool         minIsDefaultForType     READ minIsDefaultForType                                CONSTANT)
    Q_PROPERTY(QString      name                    READ name                                               CONSTANT)
56
    Q_PROPERTY(bool         rebootRequired          READ rebootRequired                                     CONSTANT)
57
    Q_PROPERTY(QString      shortDescription        READ shortDescription                                   CONSTANT)
58
    Q_PROPERTY(QString      units                   READ cookedUnits                                        CONSTANT)
Don Gagne's avatar
Don Gagne committed
59
    Q_PROPERTY(QVariant     value                   READ cookedValue            WRITE setCookedValue        NOTIFY valueChanged)
60
    Q_PROPERTY(QVariant     rawValue                READ rawValue               WRITE setRawValue           NOTIFY rawValueChanged)
61
    Q_PROPERTY(bool         valueEqualsDefault      READ valueEqualsDefault                                 NOTIFY valueChanged)
62 63
    Q_PROPERTY(QString      valueString             READ cookedValueString                                  NOTIFY valueChanged)
    Q_PROPERTY(QString      enumOrValueString       READ enumOrValueString                                  NOTIFY valueChanged)
Don Gagne's avatar
Don Gagne committed
64
    Q_PROPERTY(double       increment               READ increment                                          CONSTANT)
65 66
    Q_PROPERTY(bool         typeIsString            READ typeIsString                                       CONSTANT)
    Q_PROPERTY(bool         typeIsBool              READ typeIsBool                                         CONSTANT)
67

Don Gagne's avatar
Don Gagne committed
68 69
    /// Convert and validate value
    ///     @param convertOnly true: validate type conversion only, false: validate against meta data as well
70
    Q_INVOKABLE QString validate(const QString& cookedValue, bool convertOnly);
71

Don Gagne's avatar
Don Gagne committed
72
    QVariant        cookedValue             (void) const;   /// Value after translation
73
    QVariant        rawValue                (void) const { return _rawValue; }  /// value prior to translation, careful
74 75
    int             componentId             (void) const;
    int             decimalPlaces           (void) const;
76 77
    QVariant        rawDefaultValue         (void) const;
    QVariant        cookedDefaultValue      (void) const;
78
    bool            defaultValueAvailable   (void) const;
79
    QString         cookedDefaultValueString(void) const;
Don Gagne's avatar
Don Gagne committed
80 81
    QStringList     bitmaskStrings          (void) const;
    QVariantList    bitmaskValues           (void) const;
82
    int             enumIndex               (void);         // This is not const, since an unknown value can modify the enum lists
83
    QStringList     enumStrings             (void) const;
84
    QString         enumStringValue         (void);         // This is not const, since an unknown value can modify the enum lists
85 86 87
    QVariantList    enumValues              (void) const;
    QString         group                   (void) const;
    QString         longDescription         (void) const;
88 89 90
    QVariant        rawMax                  (void) const;
    QVariant        cookedMax               (void) const;
    QString         cookedMaxString         (void) const;
91
    bool            maxIsDefaultForType     (void) const;
92 93 94
    QVariant        rawMin                  (void) const;
    QVariant        cookedMin               (void) const;
    QString         cookedMinString         (void) const;
95 96 97 98
    bool            minIsDefaultForType     (void) const;
    QString         name                    (void) const;
    QString         shortDescription        (void) const;
    FactMetaData::ValueType_t type          (void) const;
99 100 101 102
    QString         cookedUnits             (void) const;
    QString         rawUnits                (void) const;
    QString         rawValueString          (void) const;
    QString         cookedValueString       (void) const;
103
    bool            valueEqualsDefault      (void) const;
104
    bool            rebootRequired          (void) const;
105
    QString         enumOrValueString       (void);         // This is not const, since an unknown value can modify the enum lists
Don Gagne's avatar
Don Gagne committed
106
    double          increment               (void) const;
107 108
    bool            typeIsString            (void) const { return type() == FactMetaData::valueTypeString; }
    bool            typeIsBool              (void) const { return type() == FactMetaData::valueTypeBool; }
109

110 111 112
    /// Returns the values as a string with full 18 digit precision if float/double.
    QString rawValueStringFullPrecision(void) const;

113
    void setRawValue        (const QVariant& value);
Don Gagne's avatar
Don Gagne committed
114
    void setCookedValue     (const QVariant& value);
115 116
    void setEnumIndex       (int index);
    void setEnumStringValue (const QString& value);
117

Don Gagne's avatar
Don Gagne committed
118 119 120 121 122 123 124 125 126
    // The following methods allow you to defer sending of the valueChanged signals in order to implement
    // rate limited signalling for ui performance. Used by FactGroup for example.

    void setSendValueChangedSignals (bool sendValueChangedSignals);
    bool sendValueChangedSignals (void) const { return _sendValueChangedSignals; }
    bool deferredValueChangeSignal(void) const { return _deferredValueChangeSignal; }
    void clearDeferredValueChangeSignal(void) { _deferredValueChangeSignal = false; }
    void sendDeferredValueChangedSignal(void);

127
    // C++ methods
128

129
    /// Sets and sends new value to vehicle even if value is the same
Don Gagne's avatar
Don Gagne committed
130
    void forceSetRawValue(const QVariant& value);
131
    
Don Gagne's avatar
Don Gagne committed
132
    /// Sets the meta data associated with the Fact.
133
    void setMetaData(FactMetaData* metaData);
Don Gagne's avatar
Don Gagne committed
134
    
Don Gagne's avatar
Don Gagne committed
135
    void _containerSetRawValue(const QVariant& value);
136
    
Don Gagne's avatar
Don Gagne committed
137 138
    /// Generally you should not change the name of a fact. But if you know what you are doing, you can.
    void _setName(const QString& name) { _name = name; }
Don Gagne's avatar
Don Gagne committed
139

Don Gagne's avatar
Don Gagne committed
140
    
Don Gagne's avatar
Don Gagne committed
141
signals:
142 143
    void bitmaskStringsChanged(void);
    void bitmaskValuesChanged(void);
144 145
    void enumStringsChanged(void);
    void enumValuesChanged(void);
Don Gagne's avatar
Don Gagne committed
146
    void sendValueChangedSignalsChanged(bool sendValueChangedSignals);
147

Don Gagne's avatar
Don Gagne committed
148 149 150
    /// QObject Property System signal for value property changes
    ///
    /// This signal is only meant for use by the QT property system. It should not be connected to by client code.
151
    void valueChanged(QVariant value);
152
    void rawValueChanged(QVariant value);
Don Gagne's avatar
Don Gagne committed
153
    
154 155 156
    /// Signalled when the param write ack comes back from the vehicle
    void vehicleUpdated(QVariant value);
    
Don Gagne's avatar
Don Gagne committed
157 158
    /// Signalled when property has been changed by a call to the property write accessor
    ///
159
    /// This signal is meant for use by Fact container implementations.
Don Gagne's avatar
Don Gagne committed
160
    void _containerRawValueChanged(const QVariant& value);
Don Gagne's avatar
Don Gagne committed
161
    
Don Gagne's avatar
Don Gagne committed
162
protected:
163
    QString _variantToString(const QVariant& variant, int decimalPlaces) const;
Don Gagne's avatar
Don Gagne committed
164
    void _sendValueChangedSignal(QVariant value);
165

166
    QString                     _name;
167
    int                         _componentId;
168
    QVariant                    _rawValue;
169 170
    FactMetaData::ValueType_t   _type;
    FactMetaData*               _metaData;
Don Gagne's avatar
Don Gagne committed
171 172
    bool                        _sendValueChangedSignals;
    bool                        _deferredValueChangeSignal;
Don Gagne's avatar
Don Gagne committed
173 174
};

175
#endif