Fact.h 8.53 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

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

#ifndef Fact_H
#define Fact_H

#include "FactMetaData.h"

#include <QObject>
#include <QString>
#include <QVariant>
35
#include <QDebug>
Don Gagne's avatar
Don Gagne committed
36 37 38 39 40 41 42

/// @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
43
    Fact(QObject* parent = NULL);
44
    Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent = NULL);
Don Gagne's avatar
Don Gagne committed
45 46 47 48
    Fact(const Fact& other, QObject* parent = NULL);

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

49
    Q_PROPERTY(int          componentId             READ componentId                                        CONSTANT)
50 51
    Q_PROPERTY(QStringList  bitmaskStrings          READ bitmaskStrings                                     NOTIFY bitmaskStringsChanged)
    Q_PROPERTY(QVariantList bitmaskValues           READ bitmaskValues                                      NOTIFY bitmaskValuesChanged)
52
    Q_PROPERTY(int          decimalPlaces           READ decimalPlaces                                      CONSTANT)
53 54
    Q_PROPERTY(QVariant     defaultValue            READ cookedDefaultValue                                 CONSTANT)
    Q_PROPERTY(QString      defaultValueString      READ cookedDefaultValueString                           CONSTANT)
55 56
    Q_PROPERTY(bool         defaultValueAvailable   READ defaultValueAvailable                              CONSTANT)
    Q_PROPERTY(int          enumIndex               READ enumIndex              WRITE setEnumIndex          NOTIFY valueChanged)
57
    Q_PROPERTY(QStringList  enumStrings             READ enumStrings                                        NOTIFY enumStringsChanged)
58
    Q_PROPERTY(QString      enumStringValue         READ enumStringValue        WRITE setEnumStringValue    NOTIFY valueChanged)
59
    Q_PROPERTY(QVariantList enumValues              READ enumValues                                         NOTIFY enumValuesChanged)
60 61
    Q_PROPERTY(QString      group                   READ group                                              CONSTANT)
    Q_PROPERTY(QString      longDescription         READ longDescription                                    CONSTANT)
62 63
    Q_PROPERTY(QVariant     max                     READ cookedMax                                          CONSTANT)
    Q_PROPERTY(QString      maxString               READ cookedMaxString                                    CONSTANT)
64
    Q_PROPERTY(bool         maxIsDefaultForType     READ maxIsDefaultForType                                CONSTANT)
65 66
    Q_PROPERTY(QVariant     min                     READ cookedMin                                          CONSTANT)
    Q_PROPERTY(QString      minString               READ cookedMinString                                    CONSTANT)
67 68 69 70
    Q_PROPERTY(bool         minIsDefaultForType     READ minIsDefaultForType                                CONSTANT)
    Q_PROPERTY(QString      name                    READ name                                               CONSTANT)
    Q_PROPERTY(QString      shortDescription        READ shortDescription                                   CONSTANT)
    Q_PROPERTY(FactMetaData::ValueType_t type       READ type                                               CONSTANT)
71
    Q_PROPERTY(QString      units                   READ cookedUnits                                        CONSTANT)
Don Gagne's avatar
Don Gagne committed
72
    Q_PROPERTY(QVariant     value                   READ cookedValue            WRITE setCookedValue        NOTIFY valueChanged)
73
    Q_PROPERTY(bool         valueEqualsDefault      READ valueEqualsDefault                                 NOTIFY valueChanged)
74
    Q_PROPERTY(QVariant     valueString             READ cookedValueString                                  NOTIFY valueChanged)
75

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

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

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

118
    // C++ methods
119

120
    /// Sets and sends new value to vehicle even if value is the same
Don Gagne's avatar
Don Gagne committed
121
    void forceSetRawValue(const QVariant& value);
122
    
Don Gagne's avatar
Don Gagne committed
123
    /// Sets the meta data associated with the Fact.
124
    void setMetaData(FactMetaData* metaData);
Don Gagne's avatar
Don Gagne committed
125
    
Don Gagne's avatar
Don Gagne committed
126
    void _containerSetRawValue(const QVariant& value);
127
    
Don Gagne's avatar
Don Gagne committed
128 129 130
    /// 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
131
signals:
132 133
    void bitmaskStringsChanged(void);
    void bitmaskValuesChanged(void);
134 135 136
    void enumStringsChanged(void);
    void enumValuesChanged(void);

Don Gagne's avatar
Don Gagne committed
137 138 139
    /// 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.
140
    void valueChanged(QVariant value);
Don Gagne's avatar
Don Gagne committed
141
    
142 143 144
    /// Signalled when the param write ack comes back from the vehicle
    void vehicleUpdated(QVariant value);
    
Don Gagne's avatar
Don Gagne committed
145 146
    /// Signalled when property has been changed by a call to the property write accessor
    ///
147
    /// This signal is meant for use by Fact container implementations.
Don Gagne's avatar
Don Gagne committed
148
    void _containerRawValueChanged(const QVariant& value);
Don Gagne's avatar
Don Gagne committed
149
    
Don Gagne's avatar
Don Gagne committed
150
protected:
151 152
    QString _variantToString(const QVariant& variant) const;

153
    QString                     _name;
154
    int                         _componentId;
155
    QVariant                    _rawValue;
156 157
    FactMetaData::ValueType_t   _type;
    FactMetaData*               _metaData;
Don Gagne's avatar
Don Gagne committed
158 159
};

160
#endif