ValuesWidgetController.h 7.02 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.
 *
 ****************************************************************************/

10
#pragma once
11

12 13 14
#include "FactSystem.h"
#include "QmlObjectListModel.h"
#include "QGCApplication.h"
15 16 17

#include <QObject>

18 19 20 21 22 23 24 25 26 27 28 29 30
class ValuesWidgetController;

class InstrumentValue : public QObject
{
    Q_OBJECT

public:
    enum FontSize {
        DefaultFontSize=0,
        SmallFontSize,
        MediumFontSize,
        LargeFontSize
    };
31
    Q_ENUMS(FontSize)
32

33 34 35 36 37 38 39
    enum IconPosition {
        IconAbove = 0,
        IconLeft
    };
    Q_ENUMS(IconPosition)

    InstrumentValue(Vehicle* activeVehicle, FontSize fontSize, QmlObjectListModel* rowModel);
40

41 42 43 44 45 46 47 48 49 50
    Q_PROPERTY(QString      factGroupName       MEMBER  _factGroupName                              NOTIFY factGroupNameChanged)
    Q_PROPERTY(Fact*        fact                READ    fact                                        NOTIFY factChanged)
    Q_PROPERTY(QString      label               READ    label               WRITE setLabel          NOTIFY labelChanged)
    Q_PROPERTY(QString      icon                READ    icon                WRITE setIcon           NOTIFY iconChanged)             ///< If !isEmpty icon will be show instead of label
    Q_PROPERTY(IconPosition iconPosition        READ    iconPosition        WRITE setIconPosition   NOTIFY iconPositionChanged)
    Q_PROPERTY(QStringList  iconPositionNames   MEMBER _iconPositionNames                           CONSTANT)
    Q_PROPERTY(QStringList  iconNames           MEMBER _iconNames                                   CONSTANT)
    Q_PROPERTY(FontSize     fontSize            READ    fontSize            WRITE setFontSize       NOTIFY fontSizeChanged)
    Q_PROPERTY(QStringList  fontSizeNames       MEMBER _fontSizeNames                               CONSTANT)
    Q_PROPERTY(bool         showUnits           READ    showUnits           WRITE setShowUnits      NOTIFY showUnitsChanged)
51 52 53 54

    Q_INVOKABLE void setFact(QString factGroupName, QString factName, QString label);
    Q_INVOKABLE void clearFact(void);

55 56 57 58 59 60 61 62 63 64 65 66 67 68
    Fact*           fact                    (void) { return _fact; }
    FontSize        fontSize                (void) const { return _fontSize; }
    QString         label                   (void) const { return _label; }
    bool            showUnits               (void) const { return _showUnits; }
    QString         icon                    (void) const { return _icon; }
    IconPosition    iconPosition            (void) const { return _iconPosition; }
    void            setFontSize             (FontSize fontSize);
    void            setLabel                (const QString& label);
    void            setShowUnits            (bool showUnits);
    void            setIcon                 (const QString& icon);
    void            setIconPosition         (IconPosition iconPosition);
    void            activeVehicleChanged    (Vehicle* activeVehicle);
    void            saveToSettings          (QSettings& settings) const;
    void            readFromSettings        (const QSettings& settings);
69 70 71

signals:
    void factChanged            (Fact* fact);
72 73
    void factNameChanged        (const QString& factName);
    void factGroupNameChanged   (const QString& factGroup);
74
    void labelChanged           (QString label);
75
    void fontSizeChanged        (FontSize fontSize);
76
    void showUnitsChanged       (bool showUnits);
77 78
    void iconChanged            (const QString& icon);
    void iconPositionChanged    (IconPosition iconPosition);
79 80

private:
81
    void _setFontSize           (FontSize fontSize);
82 83 84 85

    Vehicle*            _activeVehicle =    nullptr;
    QmlObjectListModel* _rowModel =         nullptr;
    Fact*               _fact =             nullptr;
86
    QString             _factName;
87 88 89
    QString             _factGroupName;
    QString             _label;
    bool                _showUnits =        true;
90 91 92 93 94 95 96
    FontSize            _fontSize =         DefaultFontSize;
    QString             _icon;
    IconPosition        _iconPosition =     IconLeft;

    static const QStringList _iconPositionNames;
    static       QStringList _iconNames;
    static const QStringList _fontSizeNames;
97 98 99 100 101 102

    static const char*  _factGroupNameKey;
    static const char*  _factNameKey;
    static const char*  _labelKey;
    static const char*  _fontSizeKey;
    static const char*  _showUnitsKey;
103 104 105 106
    static const char*  _iconKey;
    static const char*  _iconPositionKey;

    static const QString _noIconName;
107 108
};

109 110 111
Q_DECLARE_METATYPE(InstrumentValue::FontSize)
Q_DECLARE_METATYPE(InstrumentValue::IconPosition)

112 113 114 115 116
class ValuesWidgetController : public QObject
{
    Q_OBJECT
    
public:
117
    ValuesWidgetController(bool forDefaultSettingsCreation = false);
118

119
    Q_PROPERTY(QmlObjectListModel* valuesModel READ valuesModel NOTIFY valuesModelChanged)
120

121 122 123 124 125 126 127 128
    Q_INVOKABLE InstrumentValue*    appendColumn        (int rowIndex);
    Q_INVOKABLE void                deleteLastColumn    (int rowIndex);
    Q_INVOKABLE QmlObjectListModel* appendRow           (bool addBlanksColumn = true);
    Q_INVOKABLE QmlObjectListModel* insertRow           (int atIndex, bool addBlanksColumn = true);
    Q_INVOKABLE void                deleteRow           (int rowIndex);
    Q_INVOKABLE int                 fontSizeForRow      (int rowIndex);
    Q_INVOKABLE void                setFontSizeForRow   (int rowIndex, int fontSize);
    Q_INVOKABLE void                resetToDefaults     (void);
129

130 131 132 133
    QmlObjectListModel* valuesModel(void) { return _valuesModel; }

    /// Turn on/off saving changes to QSettings
    void setPreventSaveSettings(bool preventSaveSettings);
134

135 136 137
    /// Allows the ownership of the _valuesModel to be re-parented to a different controller
    void setValuesModelParentController(ValuesWidgetController* newParentController);

138
signals:
139 140 141 142 143 144
    void valuesModelChanged(QmlObjectListModel* valuesModel);

private slots:
    void        _activeVehicleChanged(Vehicle* activeVehicle);
    Vehicle*    _currentActiveVehicle(void);
    void        _saveSettings  (void);
145 146

private:
147
    bool                _validRowIndex                      (int rowIndex);
148
    InstrumentValue*    _createNewInstrumentValueWorker     (Vehicle* activeVehicle, InstrumentValue::FontSize fontSize, QmlObjectListModel* rowModel);
149
    void                _loadSettings                       (void);
150 151
    void                _connectSignalsToController         (InstrumentValue* value, ValuesWidgetController* controller);

152 153 154 155 156

    MultiVehicleManager*    _multiVehicleMgr =      nullptr;
    QmlObjectListModel*     _valuesModel =          nullptr;
    QVariantList            _rgFontSizeByRow;
    bool                    _preventSaveSettings =  false;
157 158

    static const char* _groupKey;
159 160 161 162 163 164
    static const char* _rowsKey;
    static const char* _columnsKey;

    static const char* _deprecatedGroupKey;
    static const char* _deprecatedLargeValuesKey;
    static const char* _deprecatedSmallValuesKey;
165

166
};