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)
{
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)),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Using old signal-slot syntax.

Switching to the new function pointer based connection syntax could provide compile-time checking and improve code clarity.

Suggested implementation:

        const auto ok = connect(m_config, &Config::valueChanged,
                                this, &SettingsOption::onValueChanged);

If m_config is not of type Config*, replace "Config" in the new connection call with the appropriate class name that declares the valueChanged signal.

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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the repeated meta-object handling and validity checks into helper functions to reduce nesting and improve readability without changing functionality.

Consider refactoring the repeated meta-object handling and validity checks into small helper functions. This will reduce nesting and condense the logic without changing functionality. For example:

static QVariant fetchValidProperty(QObject *config, const QString &key) {
    const auto value = config->property(key.toLocal8Bit());
    return value.isValid() ? value : QVariant{};
}

static bool resetConfigKey(QObject *config, const QString &key) {
    const int index = indexOfProperty(config, key);
    if (index < 0)
        return false;
    const auto mo = config->metaObject();
    QMetaProperty prop = mo->property(index);
    if (!prop.isResettable())
        return false;
    return prop.reset(config);
}

Then update your slot and reset methods:

void SettingsOption::onConfigValueChanged() {
    const auto value = fetchValidProperty(m_config, m_key);
    if (value.isValid()) {
        setValue(value, false);
        m_valueInitialized = true;
    }
}

void SettingsOption::resetValue() {
    if (!resetConfigKey(m_config, m_key)) {
        qCWarning(settingLog) << "Failed to reset the key" << m_key << "on Config object:" << m_config;
    }
}

This consolidates the logic into concise, reusable functions and reduces the complexity of your conditional branches.

{
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 @@ -7,10 +7,9 @@

#include <dtkdeclarative_global.h>

#include <QQmlParserStatus>

Check warning on line 10 in src/private/dsettingscontainer_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QQmlParserStatus> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QQmlComponent>

Check warning on line 11 in src/private/dsettingscontainer_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QQmlComponent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <private/qqmlobjectmodel_p.h>

Check warning on line 12 in src/private/dsettingscontainer_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <private/qqmlobjectmodel_p.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "dconfigwrapper_p.h"

DQUICK_BEGIN_NAMESPACE

Expand Down Expand Up @@ -40,7 +39,7 @@

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

static SettingsOption *qmlAttachedProperties(QObject *object);

Expand All @@ -50,8 +49,9 @@
void valueChanged(QVariant value);
void delegateChanged();

private Q_SLOTS:

Check warning on line 52 in src/private/dsettingscontainer_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void onConfigValueChanged();
void onValueChanged(const QString &key, const QVariant &value);

private:
void setValue(const QVariant &value, bool updateConfig);
Expand All @@ -61,7 +61,7 @@
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 @@
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 @@
{
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 @@
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 @@
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