Fact.h 12.6 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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>
23 24 25
#include <QAbstractListModel>

class FactValueSliderListModel;
Don Gagne's avatar
Don Gagne committed
26 27 28 29 30 31 32

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

37 38
    /// Creates a Fact using the name and type from metaData. Also calls QGCCorePlugin::adjustSettingsMetaData allowing
    /// custom builds to override the metadata.
Gus Grubba's avatar
Gus Grubba committed
39
    Fact(const QString& settingsGroup, FactMetaData* metaData, QObject* parent = nullptr);
40

Don Gagne's avatar
Don Gagne committed
41 42
    const Fact& operator=(const Fact& other);

43
    Q_PROPERTY(int          componentId             READ componentId                                        CONSTANT)
44 45
    Q_PROPERTY(QStringList  bitmaskStrings          READ bitmaskStrings                                     NOTIFY bitmaskStringsChanged)
    Q_PROPERTY(QVariantList bitmaskValues           READ bitmaskValues                                      NOTIFY bitmaskValuesChanged)
46
    Q_PROPERTY(int          decimalPlaces           READ decimalPlaces                                      CONSTANT)
47 48
    Q_PROPERTY(QVariant     defaultValue            READ cookedDefaultValue                                 CONSTANT)
    Q_PROPERTY(QString      defaultValueString      READ cookedDefaultValueString                           CONSTANT)
49 50
    Q_PROPERTY(bool         defaultValueAvailable   READ defaultValueAvailable                              CONSTANT)
    Q_PROPERTY(int          enumIndex               READ enumIndex              WRITE setEnumIndex          NOTIFY valueChanged)
51
    Q_PROPERTY(QStringList  enumStrings             READ enumStrings                                        NOTIFY enumsChanged)
52
    Q_PROPERTY(QString      enumStringValue         READ enumStringValue        WRITE setEnumStringValue    NOTIFY valueChanged)
53
    Q_PROPERTY(QVariantList enumValues              READ enumValues                                         NOTIFY enumsChanged)
Gus Grubba's avatar
Gus Grubba committed
54
    Q_PROPERTY(QString      category                READ category                                           CONSTANT)
55 56
    Q_PROPERTY(QString      group                   READ group                                              CONSTANT)
    Q_PROPERTY(QString      longDescription         READ longDescription                                    CONSTANT)
57 58
    Q_PROPERTY(QVariant     max                     READ cookedMax                                          CONSTANT)
    Q_PROPERTY(QString      maxString               READ cookedMaxString                                    CONSTANT)
59
    Q_PROPERTY(bool         maxIsDefaultForType     READ maxIsDefaultForType                                CONSTANT)
60 61
    Q_PROPERTY(QVariant     min                     READ cookedMin                                          CONSTANT)
    Q_PROPERTY(QString      minString               READ cookedMinString                                    CONSTANT)
62 63
    Q_PROPERTY(bool         minIsDefaultForType     READ minIsDefaultForType                                CONSTANT)
    Q_PROPERTY(QString      name                    READ name                                               CONSTANT)
64 65
    Q_PROPERTY(bool         vehicleRebootRequired   READ vehicleRebootRequired                              CONSTANT)
    Q_PROPERTY(bool         qgcRebootRequired       READ qgcRebootRequired                                  CONSTANT)
66
    Q_PROPERTY(QString      shortDescription        READ shortDescription                                   CONSTANT)
67
    Q_PROPERTY(QString      units                   READ cookedUnits                                        CONSTANT)
Don Gagne's avatar
Don Gagne committed
68
    Q_PROPERTY(QVariant     value                   READ cookedValue            WRITE setCookedValue        NOTIFY valueChanged)
69
    Q_PROPERTY(QVariant     rawValue                READ rawValue               WRITE setRawValue           NOTIFY rawValueChanged)
70
    Q_PROPERTY(bool         valueEqualsDefault      READ valueEqualsDefault                                 NOTIFY valueChanged)
71 72
    Q_PROPERTY(QString      valueString             READ cookedValueString                                  NOTIFY valueChanged)
    Q_PROPERTY(QString      enumOrValueString       READ enumOrValueString                                  NOTIFY valueChanged)
73
    Q_PROPERTY(double       increment               READ cookedIncrement                                    CONSTANT)
74 75
    Q_PROPERTY(bool         typeIsString            READ typeIsString                                       CONSTANT)
    Q_PROPERTY(bool         typeIsBool              READ typeIsBool                                         CONSTANT)
76
    Q_PROPERTY(bool         hasControl              READ hasControl                                         CONSTANT)
77
    Q_PROPERTY(bool         readOnly                READ readOnly                                           CONSTANT)
78
    Q_PROPERTY(bool         writeOnly               READ writeOnly                                          CONSTANT)
79
    Q_PROPERTY(bool         volatileValue           READ volatileValue                                      CONSTANT)
80

Gus Grubba's avatar
Gus Grubba committed
81 82 83
    /// @brief Convert and validate value
    /// @param cookedValue: Value to convert and validate
    /// @param convertOnly true: validate type conversion only, false: validate against meta data as well
84
    Q_INVOKABLE QString validate(const QString& cookedValue, bool convertOnly);
85 86
    /// Convert and clamp value
    Q_INVOKABLE QVariant clamp(const QString& cookedValue);
87

Don Gagne's avatar
Don Gagne committed
88
    QVariant        cookedValue             (void) const;   /// Value after translation
89
    QVariant        rawValue                (void) const { return _rawValue; }  /// value prior to translation, careful
90 91
    int             componentId             (void) const;
    int             decimalPlaces           (void) const;
92 93
    QVariant        rawDefaultValue         (void) const;
    QVariant        cookedDefaultValue      (void) const;
94
    bool            defaultValueAvailable   (void) const;
95
    QString         cookedDefaultValueString(void) const;
Don Gagne's avatar
Don Gagne committed
96 97
    QStringList     bitmaskStrings          (void) const;
    QVariantList    bitmaskValues           (void) const;
98
    int             enumIndex               (void);         // This is not const, since an unknown value can modify the enum lists
99
    QStringList     enumStrings             (void) const;
100
    QString         enumStringValue         (void);         // This is not const, since an unknown value can modify the enum lists
101
    QVariantList    enumValues              (void) const;
102
    QString         category                (void) const;
103 104
    QString         group                   (void) const;
    QString         longDescription         (void) const;
105 106 107
    QVariant        rawMax                  (void) const;
    QVariant        cookedMax               (void) const;
    QString         cookedMaxString         (void) const;
108
    bool            maxIsDefaultForType     (void) const;
109 110 111
    QVariant        rawMin                  (void) const;
    QVariant        cookedMin               (void) const;
    QString         cookedMinString         (void) const;
112 113 114 115
    bool            minIsDefaultForType     (void) const;
    QString         name                    (void) const;
    QString         shortDescription        (void) const;
    FactMetaData::ValueType_t type          (void) const;
116 117 118 119
    QString         cookedUnits             (void) const;
    QString         rawUnits                (void) const;
    QString         rawValueString          (void) const;
    QString         cookedValueString       (void) const;
120
    bool            valueEqualsDefault      (void) const;
121 122
    bool            vehicleRebootRequired   (void) const;
    bool            qgcRebootRequired       (void) const;
123
    QString         enumOrValueString       (void);         // This is not const, since an unknown value can modify the enum lists
124
    double          rawIncrement            (void) const;
125
    double          cookedIncrement         (void) const;
126 127
    bool            typeIsString            (void) const { return type() == FactMetaData::valueTypeString; }
    bool            typeIsBool              (void) const { return type() == FactMetaData::valueTypeBool; }
128
    bool            hasControl              (void) const;
129
    bool            readOnly                (void) const;
130 131
    bool            writeOnly               (void) const;
    bool            volatileValue           (void) const;
132

133 134 135 136
    // Internal hack to allow changes to fact which do not signal reboot. Currently used by font point size
    // code in ScreenTools.qml to set initial sizing at first boot.
    Q_INVOKABLE void _setIgnoreQGCRebootRequired(bool ignore);

137 138
    Q_INVOKABLE FactValueSliderListModel* valueSliderModel(void);

139 140 141
    /// Returns the values as a string with full 18 digit precision if float/double.
    QString rawValueStringFullPrecision(void) const;

Gus Grubba's avatar
Gus Grubba committed
142
    void setRawValue        (const QVariant& value);
Don Gagne's avatar
Don Gagne committed
143
    void setCookedValue     (const QVariant& value);
144 145
    void setEnumIndex       (int index);
    void setEnumStringValue (const QString& value);
Gus Grubba's avatar
Gus Grubba committed
146
    int  valueIndex         (const QString& value);
147

Don Gagne's avatar
Don Gagne committed
148 149 150 151 152 153 154 155 156
    // 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);

157
    // C++ methods
158

159
    /// Sets and sends new value to vehicle even if value is the same
Don Gagne's avatar
Don Gagne committed
160
    void forceSetRawValue(const QVariant& value);
161
    
Don Gagne's avatar
Don Gagne committed
162
    /// Sets the meta data associated with the Fact.
163 164 165
    ///     @param metaData FactMetaData for Fact
    ///     @param setDefaultFromMetaData true: set the fact value to the default specified in the meta data
    void setMetaData(FactMetaData* metaData, bool setDefaultFromMetaData = false);
Don Gagne's avatar
Don Gagne committed
166
    
167
    FactMetaData* metaData() { return _metaData; }
Gus Grubba's avatar
Gus Grubba committed
168 169

    //-- Value coming from Vehicle. This does NOT send a _containerRawValueChanged signal.
Don Gagne's avatar
Don Gagne committed
170
    void _containerSetRawValue(const QVariant& value);
171
    
Don Gagne's avatar
Don Gagne committed
172 173
    /// 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
174

175 176 177
    /// Generally this is done during parsing. But if you know what you are doing, you can.
    void setEnumInfo(const QStringList& strings, const QVariantList& values);

Don Gagne's avatar
Don Gagne committed
178
signals:
179 180
    void bitmaskStringsChanged(void);
    void bitmaskValuesChanged(void);
181
    void enumsChanged(void);
Don Gagne's avatar
Don Gagne committed
182
    void sendValueChangedSignalsChanged(bool sendValueChangedSignals);
183

Don Gagne's avatar
Don Gagne committed
184 185 186
    /// 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.
187
    void valueChanged(QVariant value);
188
    void rawValueChanged(QVariant value);
Don Gagne's avatar
Don Gagne committed
189
    
190 191 192
    /// Signalled when the param write ack comes back from the vehicle
    void vehicleUpdated(QVariant value);
    
Don Gagne's avatar
Don Gagne committed
193 194
    /// Signalled when property has been changed by a call to the property write accessor
    ///
Gus Grubba's avatar
Gus Grubba committed
195
    /// This signal is meant for use by Fact container implementations. Used to send changed values to vehicle.
Don Gagne's avatar
Don Gagne committed
196
    void _containerRawValueChanged(const QVariant& value);
197 198 199 200 201 202

private slots:
    void _checkForRebootMessaging(void);

private:
    void _init(void);
Don Gagne's avatar
Don Gagne committed
203
    
Don Gagne's avatar
Don Gagne committed
204
protected:
205
    QString _variantToString(const QVariant& variant, int decimalPlaces) const;
Don Gagne's avatar
Don Gagne committed
206
    void _sendValueChangedSignal(QVariant value);
207

208
    QString                     _name;
209
    int                         _componentId;
210
    QVariant                    _rawValue;
211 212
    FactMetaData::ValueType_t   _type;
    FactMetaData*               _metaData;
Don Gagne's avatar
Don Gagne committed
213 214
    bool                        _sendValueChangedSignals;
    bool                        _deferredValueChangeSignal;
215
    FactValueSliderListModel*   _valueSliderModel;
216
    bool                        _ignoreQGCRebootRequired;
Don Gagne's avatar
Don Gagne committed
217 218
};

219
#endif