From 5fe9bfd0a5e20cef7393639712302825b803db29 Mon Sep 17 00:00:00 2001 From: Iceyer Date: Thu, 8 Feb 2018 00:02:50 +0800 Subject: [PATCH] feat(DSettings): support set default value Change-Id: I83684be7756e5038f55c9fb468195c474259f1e1 --- src/settings/dsettingsoption.cpp | 14 +++++++++++--- src/settings/dsettingsoption.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/settings/dsettingsoption.cpp b/src/settings/dsettingsoption.cpp index 2410c6f0..4edb493b 100644 --- a/src/settings/dsettingsoption.cpp +++ b/src/settings/dsettingsoption.cpp @@ -48,7 +48,6 @@ class DSettingsOptionPrivate DSettingsOption::DSettingsOption(QObject *parent) : QObject(parent), d_ptr(new DSettingsOptionPrivate(this)) { - } DSettingsOption::~DSettingsOption() @@ -95,7 +94,7 @@ QVariant DSettingsOption::defaultValue() const QVariant DSettingsOption::value() const { Q_D(const DSettingsOption); - return d->value; + return (d->value.isValid() || d->value.isNull()) ? d->defalutValue : d->value; } QVariant DSettingsOption::data(const QString &dataType) const @@ -135,6 +134,16 @@ void DSettingsOption::setValue(QVariant value) Q_EMIT valueChanged(value); } +//! +//! \brief DSettingsOption::setDefault will override default value of json +//! \param value +//! +void DSettingsOption::setDefault(QVariant value) +{ + Q_D(DSettingsOption); + d->defalutValue = value; +} + void DSettingsOption::setData(const QString &dataType, QVariant value) { Q_D(DSettingsOption); @@ -167,7 +176,6 @@ void DSettingsOptionPrivate::parseJson(const QString &prefixKey, const QJsonObje defalutValue = option.value("default").toVariant(); hidden = !option.contains("hide") ? false : option.value("hide").toBool(); viewType = option.value("type").toString(); - value = defalutValue; QStringList revserdKeys; revserdKeys << "key" << "name" << "reset" diff --git a/src/settings/dsettingsoption.h b/src/settings/dsettingsoption.h index 346923d3..c89a904d 100644 --- a/src/settings/dsettingsoption.h +++ b/src/settings/dsettingsoption.h @@ -56,6 +56,7 @@ class DSettingsOption : public QObject public Q_SLOTS: void setValue(QVariant value); + void setDefault(QVariant value); void setData(const QString &dataType, QVariant value); private: