Fact.h 5.36 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    Q_PROPERTY(int      componentId             READ componentId                            CONSTANT)
    Q_PROPERTY(QString  group                   READ group                                  CONSTANT)
    Q_PROPERTY(QString  name                    READ name                                   CONSTANT)
    Q_PROPERTY(QVariant value                   READ value                  WRITE setValue  NOTIFY valueChanged USER true)
    Q_PROPERTY(QVariant valueString             READ valueString                            NOTIFY valueChanged)
    Q_PROPERTY(QString  units                   READ units                                  CONSTANT)
    Q_PROPERTY(QVariant defaultValue            READ defaultValue                           CONSTANT)
    Q_PROPERTY(bool     defaultValueAvailable   READ defaultValueAvailable                  CONSTANT)
    Q_PROPERTY(bool     valueEqualsDefault      READ valueEqualsDefault                     NOTIFY valueChanged)
    Q_PROPERTY(FactMetaData::ValueType_t type   READ type                                   CONSTANT)
    Q_PROPERTY(QString  shortDescription        READ shortDescription                       CONSTANT)
    Q_PROPERTY(QString  longDescription         READ longDescription                        CONSTANT)
    Q_PROPERTY(QVariant min                     READ min                                    CONSTANT)
    Q_PROPERTY(bool     minIsDefaultForType     READ minIsDefaultForType                    CONSTANT)
    Q_PROPERTY(QVariant max                     READ max                                    CONSTANT)
    Q_PROPERTY(bool     maxIsDefaultForType     READ maxIsDefaultForType                    CONSTANT)
    Q_PROPERTY(int      decimalPlaces           READ decimalPlaces                          CONSTANT)
Don Gagne's avatar
Don Gagne committed
66
    
Don Gagne's avatar
Don Gagne committed
67 68 69 70
    /// Convert and validate value
    ///     @param convertOnly true: validate type conversion only, false: validate against meta data as well
    Q_INVOKABLE QString validate(const QString& value, bool convertOnly);
    
Don Gagne's avatar
Don Gagne committed
71 72
    // Property system methods
    
73
    QString name(void) const;
74
    int componentId(void) const;
75 76
    QVariant value(void) const;
    QString valueString(void) const;
77
    void setValue(const QVariant& value);
78 79 80 81 82 83 84 85 86 87 88 89
    QVariant defaultValue(void) const;
	bool defaultValueAvailable(void) const;
    bool valueEqualsDefault(void) const;
    FactMetaData::ValueType_t type(void) const;
    QString shortDescription(void) const;
    QString longDescription(void) const;
    QString units(void) const;
    QVariant min(void) const;
    bool minIsDefaultForType(void) const;
    QVariant max(void) const;
    bool maxIsDefaultForType(void) const;
    QString group(void) const;
90
    int decimalPlaces(void) const;
Don Gagne's avatar
Don Gagne committed
91
    
92 93 94
    /// Sets and sends new value to vehicle even if value is the same
    void forceSetValue(const QVariant& value);
    
Don Gagne's avatar
Don Gagne committed
95
    /// Sets the meta data associated with the Fact.
96
    void setMetaData(FactMetaData* metaData);
Don Gagne's avatar
Don Gagne committed
97
    
98 99
    void _containerSetValue(const QVariant& value);
    
Don Gagne's avatar
Don Gagne committed
100 101 102
    /// 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
103 104 105 106
signals:
    /// 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.
107
    void valueChanged(QVariant value);
Don Gagne's avatar
Don Gagne committed
108
    
109 110 111
    /// Signalled when the param write ack comes back from the vehicle
    void vehicleUpdated(QVariant value);
    
Don Gagne's avatar
Don Gagne committed
112 113
    /// Signalled when property has been changed by a call to the property write accessor
    ///
114
    /// This signal is meant for use by Fact container implementations.
Don Gagne's avatar
Don Gagne committed
115
    void _containerValueChanged(const QVariant& value);
Don Gagne's avatar
Don Gagne committed
116 117
    
private:
118
    QString                     _name;
119
    int                         _componentId;
120 121 122
    QVariant                    _value;
    FactMetaData::ValueType_t   _type;
    FactMetaData*               _metaData;
Don Gagne's avatar
Don Gagne committed
123 124
};

125
#endif