Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qt6/src/qml/settings/SettingsDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DialogWindow {
id: control

property list<Settings.SettingsGroup> groups
property D.Config config
property QtObject config
property Settings.SettingsContainer container : Settings.SettingsContainer {
id: settingsContainer
config: control.config
Expand Down
10 changes: 6 additions & 4 deletions src/private/dconfigwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,11 @@ void DConfigWrapper::initializeProperties() const
// Must fallback to the initial value, in the sync mode, the DConfigWrapperMetaObject's
// properties is not initialize.
const auto value = impl->value(key, initialValue);
callInGuiThread(wrapper, [wrapper, key, value] {
if (value.isValid())
callInGuiThread(wrapper, [wrapper, key, value, currentValue] {
if (value.isValid() && value != currentValue) {
wrapper->mo->setValue(key.toLocal8Bit(), value);
Q_EMIT wrapper->valueChanged(key, value);
}
});
}
}
Expand All @@ -387,8 +389,8 @@ void DConfigWrapper::initializeProperties() const
wrapper->mo->setValue(propName, value);
});

QMetaObject::invokeMethod(wrapper, [wrapper, key] {
Q_EMIT wrapper->valueChanged(key);
QMetaObject::invokeMethod(wrapper, [wrapper, key, value] {
Q_EMIT wrapper->valueChanged(key, value);
});
}, Qt::DirectConnection);

Expand Down
2 changes: 1 addition & 1 deletion src/private/dconfigwrapper_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Q_SLOTS:
bool isDefaultValue(const QString &key) const;

Q_SIGNALS:
void valueChanged(const QString &key);
void valueChanged(const QString &key, const QVariant &value);
void initialized();

private:
Expand Down
66 changes: 50 additions & 16 deletions src/private/dsettingscontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
DCORE_USE_NAMESPACE;

DQUICK_BEGIN_NAMESPACE

#ifndef QT_DEBUG
Q_LOGGING_CATEGORY(settingLog, "dtk.dsg.settings" , QtInfoMsg);
#else
Q_LOGGING_CATEGORY(settingLog, "dtk.dsg.settings");
#endif

static constexpr char const *settingsOptionObjectName = "_d_settings_option";
static constexpr char const *settingsGroupObjectName = "_d_settings_group";

Expand Down Expand Up @@ -210,12 +217,12 @@ void SettingsContainer::onGroupVisibleChanged(bool visible)
}
}

DConfigWrapper *SettingsContainer::config() const
QObject *SettingsContainer::config() const
{
return m_config;
}

void SettingsContainer::setConfig(DConfigWrapper *config)
void SettingsContainer::setConfig(QObject *config)
{
if (m_config == config)
return;
Expand Down Expand Up @@ -245,9 +252,10 @@ QString SettingsOption::name() const

QVariant SettingsOption::value()
{
if (!m_valueInitialized) {
if (m_config->isValid()) {
m_value = m_config->value(m_key);
if (!m_valueInitialized && m_config) {
auto value = m_config->property(m_key.toLocal8Bit());
if (value.isValid()) {
m_value = value;
m_valueInitialized = true;
}
}
Expand All @@ -270,17 +278,16 @@ static int indexOfProperty(const QObject * obj, const QString &name)
return -1;
}

void SettingsOption::setConfig(DConfigWrapper *config)
void SettingsOption::setConfig(QObject *config)
Comment thread
zccrs marked this conversation as resolved.
{
m_config = config;
int propertyIndex = indexOfProperty(m_config, m_key);
if (propertyIndex < 0) {
connect(m_config, &DConfigWrapper::valueChanged, this, [this](const QString &key){
if (key == m_key) {
setValue(m_config->value(key), false);
m_valueInitialized = true;
}
});
const auto ok = connect(m_config, SIGNAL(valueChanged(QString, QVariant)),
this, SLOT(onValueChanged(QString, QVariant)));
if (!ok) {
qCWarning(settingLog) << "Failed to connect valueChanged signal from Config object:" << m_config;
}
} else {
// valueChanged is not emitted when the key of Config defined in qml
const auto mo = m_config->metaObject();
Expand All @@ -305,10 +312,21 @@ SettingsOption *SettingsOption::qmlAttachedProperties(QObject *object)

void SettingsOption::onConfigValueChanged()
{
setValue(m_config->value(m_key), false);
const auto value = m_config->property(m_key.toLocal8Bit());
if (!value.isValid())
return;
setValue(value, false);
m_valueInitialized = true;
}

void SettingsOption::onValueChanged(const QString &key, const QVariant &value)
{
if (key == m_key && value.isValid()) {
setValue(value, false);
m_valueInitialized = true;
}
}

void SettingsOption::setValue(QVariant value)
{
setValue(value, true);
Expand All @@ -321,14 +339,30 @@ void SettingsOption::setValue(const QVariant &value, bool updateConfig)

m_value = value;
if (updateConfig && m_config)
m_config->setValue(m_key, value);
m_config->setProperty(m_key.toLocal8Bit(), value);

Q_EMIT valueChanged(value);
}

void SettingsOption::resetValue()
{
m_config->resetValue(m_key);
const auto index = indexOfProperty(m_config, m_key);
if (index < 0) {
qCWarning(settingLog) << "The key" << m_key << "not found in the Config object:" << m_config;
return;
}

const auto mo = m_config->metaObject();
const auto p = mo->property(index);

if (!p.isResettable()) {
qCDebug(settingLog) << "The key" << m_key << "can't reset in the Config object:" << m_config;
return;
}

if (!p.reset(m_config)) {
qCWarning(settingLog) << "The key" << m_key << "reset failed in the Config object:" << m_config;
}
}

void SettingsOption::setKey(QString key)
Expand Down Expand Up @@ -404,7 +438,7 @@ void SettingsGroup::setBackground(QQmlComponent *background)
Q_EMIT backgroundChanged();
}

void SettingsGroup::setConfig(DConfigWrapper *config)
void SettingsGroup::setConfig(QObject *config)
{
for (auto childGroup : qAsConst(m_children)) {
childGroup->setConfig(config);
Expand Down
16 changes: 8 additions & 8 deletions src/private/dsettingscontainer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <QQmlParserStatus>
#include <QQmlComponent>
#include <private/qqmlobjectmodel_p.h>
#include "dconfigwrapper_p.h"

DQUICK_BEGIN_NAMESPACE

Expand Down Expand Up @@ -40,7 +39,7 @@ class SettingsOption : public QObject

QQmlComponent *delegate() const;
void setDelegate(QQmlComponent *delegate);
void setConfig(DConfigWrapper *config);
void setConfig(QObject *config);

static SettingsOption *qmlAttachedProperties(QObject *object);

Expand All @@ -52,6 +51,7 @@ class SettingsOption : public QObject

private Q_SLOTS:
void onConfigValueChanged();
void onValueChanged(const QString &key, const QVariant &value);

private:
void setValue(const QVariant &value, bool updateConfig);
Expand All @@ -61,7 +61,7 @@ private Q_SLOTS:
QVariant m_value;
bool m_valueInitialized = false;
QQmlComponent *m_delegate = nullptr;
DConfigWrapper *m_config = nullptr;
QObject *m_config = nullptr;
};

class SettingsGroup : public QObject
Expand Down Expand Up @@ -97,7 +97,7 @@ class SettingsGroup : public QObject
QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> children();
QQmlComponent *background() const;
void setBackground(QQmlComponent *background);
void setConfig(DConfigWrapper *config);
void setConfig(QObject *config);
SettingsGroup *parentGroup() const;
void setParentGroup(SettingsGroup *parentGroup);
int index() const;
Expand Down Expand Up @@ -190,7 +190,7 @@ class SettingsContainer : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(DConfigWrapper *config READ config WRITE setConfig NOTIFY configChanged)
Q_PROPERTY(QObject *config READ config WRITE setConfig NOTIFY configChanged)
Q_PROPERTY(QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> groups READ groups NOTIFY groupsChanged)
Q_PROPERTY(SettingsContentModel *contentModel READ contentModel NOTIFY contentModelChanged)
Q_PROPERTY(QQmlComponent *contentTitle READ contentTitle WRITE setContentTitle NOTIFY contentTitleChanged)
Expand All @@ -206,8 +206,8 @@ class SettingsContainer : public QObject, public QQmlParserStatus
explicit SettingsContainer(QObject *parent = nullptr);
virtual ~SettingsContainer() override;

DConfigWrapper *config() const;
void setConfig(DConfigWrapper *config);
QObject *config() const;
void setConfig(QObject *config);
QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> groups();
SettingsContentModel *contentModel() const;
SettingsNavigationModel *navigationModel() const;
Expand Down Expand Up @@ -248,7 +248,7 @@ private Q_SLOTS:
QQmlComponent *m_contentTitle = nullptr;
QQmlComponent *m_navigationTitle = nullptr;
QQmlComponent * m_contentBackground = nullptr;
DConfigWrapper *m_config = nullptr;
QObject *m_config = nullptr;
};

DQUICK_END_NAMESPACE
Expand Down
2 changes: 1 addition & 1 deletion src/qml/settings/SettingsDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DialogWindow {
id: control

property list<Settings.SettingsGroup> groups
property D.Config config
property QtObject config
property Settings.SettingsContainer container : Settings.SettingsContainer {
id: settingsContainer
config: control.config
Expand Down