Skip to content
Snippets Groups Projects
SettingsFact.cc 2.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • /****************************************************************************
     *
     *   (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
    
    #include "SettingsFact.h"
    
    #include "QGCCorePlugin.h"
    #include "QGCApplication.h"
    
    Don Gagne's avatar
    Don Gagne committed
    
    #include <QSettings>
    
    SettingsFact::SettingsFact(QObject* parent)
        : Fact(parent)
    {    
    
    Don Gagne's avatar
    Don Gagne committed
        QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
    
    Don Gagne's avatar
    Don Gagne committed
    }
    
    
    SettingsFact::SettingsFact(QString settingsGroup, FactMetaData* metaData, QObject* parent)
        : Fact          (0, metaData->name(), metaData->type(), parent)
        , _settingsGroup(settingsGroup)
        , _visible      (true)
    
    Don Gagne's avatar
    Don Gagne committed
    {
        QSettings settings;
    
    
        if (!_settingsGroup.isEmpty()) {
            settings.beginGroup(_settingsGroup);
    
    Don Gagne's avatar
    Don Gagne committed
        }
    
    
        // Allow core plugin a chance to override the default value
    
        _visible = qgcApp()->toolbox()->corePlugin()->adjustSettingMetaData(settingsGroup, *metaData);
    
        setMetaData(metaData);
    
        QVariant rawDefaultValue = metaData->rawDefaultValue();
        if (_visible) {
            QVariant typedValue;
            QString errorString;
            metaData->convertAndValidateRaw(settings.value(_name, rawDefaultValue), true /* conertOnly */, typedValue, errorString);
            _rawValue = typedValue;
        } else {
            // Setting is not visible, force to default value always
            settings.setValue(_name, rawDefaultValue);
            _rawValue = rawDefaultValue;
        }
    
    Don Gagne's avatar
    Don Gagne committed
    
    
        connect(this, &Fact::rawValueChanged, this, &SettingsFact::_rawValueChanged);
    
    Don Gagne's avatar
    Don Gagne committed
    }
    
    SettingsFact::SettingsFact(const SettingsFact& other, QObject* parent)
        : Fact(other, parent)
    {
        *this = other;
    }
    
    const SettingsFact& SettingsFact::operator=(const SettingsFact& other)
    {
        Fact::operator=(other);
        
    
        _settingsGroup = other._settingsGroup;
    
    Don Gagne's avatar
    Don Gagne committed
    
        return *this;
    }
    
    
    void SettingsFact::_rawValueChanged(QVariant value)
    
    Don Gagne's avatar
    Don Gagne committed
    {
        QSettings settings;
    
    
        if (!_settingsGroup.isEmpty()) {
            settings.beginGroup(_settingsGroup);
    
    Don Gagne's avatar
    Don Gagne committed
        }
    
        settings.setValue(_name, value);
    }