From 53de20817fd82ac03b812f34a622b015cb134259 Mon Sep 17 00:00:00 2001 From: JiDe Zhang Date: Thu, 20 Feb 2025 21:33:40 +0800 Subject: [PATCH] feat: dconfig2cpp generated codes supports qml On the DtkDeclarative's DConfigWrapper, it needs know whether the value is default, so always create the *IsDefaultValue functions. --- cmake/DtkTools/DtkDConfigMacros.cmake | 10 +--- src/log/dconfig_org_deepin_dtk_preference.hpp | 58 ++++++++++++++----- tools/dconfig2cpp/main.cpp | 48 ++++++++++----- 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/cmake/DtkTools/DtkDConfigMacros.cmake b/cmake/DtkTools/DtkDConfigMacros.cmake index a5f68957..f6a385cd 100644 --- a/cmake/DtkTools/DtkDConfigMacros.cmake +++ b/cmake/DtkTools/DtkDConfigMacros.cmake @@ -13,7 +13,7 @@ function(dtk_add_config_to_cpp OUTPUT_VAR JSON_FILE) cmake_parse_arguments( "arg" "" - "OUTPUT_FILE_NAME;CLASS_NAME;USE_QPROPERTY;GET_IS_DEFAULT_VALUE" + "OUTPUT_FILE_NAME;CLASS_NAME;USE_QPROPERTY" "" ${ARGN} ) @@ -45,16 +45,10 @@ function(dtk_add_config_to_cpp OUTPUT_VAR JSON_FILE) set(USE_QPROPERTY_ARG "") endif() - if (arg_GET_IS_DEFAULT_VALUE) - set(GET_IS_DEFAULT_VALUE_ARG "--get-is-default-value") - else() - set(GET_IS_DEFAULT_VALUE_ARG "") - endif() - # Add a custom command to run dconfig2cpp add_custom_command( OUTPUT ${OUTPUT_HEADER} - COMMAND ${DTK_DCONFIG2CPP} -o ${OUTPUT_HEADER} ${CLASS_NAME_ARG} ${USE_QPROPERTY_ARG} ${GET_IS_DEFAULT_VALUE_ARG} ${JSON_FILE} + COMMAND ${DTK_DCONFIG2CPP} -o ${OUTPUT_HEADER} ${CLASS_NAME_ARG} ${USE_QPROPERTY_ARG} ${JSON_FILE} DEPENDS ${JSON_FILE} ${DTK_XML2CPP} COMMENT "Generating ${OUTPUT_HEADER} from ${JSON_FILE}" VERBATIM diff --git a/src/log/dconfig_org_deepin_dtk_preference.hpp b/src/log/dconfig_org_deepin_dtk_preference.hpp index 811d5039..a300e9fd 100644 --- a/src/log/dconfig_org_deepin_dtk_preference.hpp +++ b/src/log/dconfig_org_deepin_dtk_preference.hpp @@ -1,7 +1,7 @@ /** * This file is generated by dconfig2cpp. - * Command line arguments: /home/zccrs/projects/dtkcore/build/unknown-Debug/tools/dconfig2cpp/dconfig2cpp --get-is-default-value /home/zccrs/projects/dtkcommon/configs/org.deepin.dtk.preference.json - * Generation time: 2025-02-20T19:16:13 + * Command line arguments: /home/zccrs/projects/dtkcore/build/unknown-Debug/tools/dconfig2cpp/dconfig2cpp /home/zccrs/projects/dtkcommon/configs/org.deepin.dtk.preference.json + * Generation time: 2025-02-21T13:49:21 * JSON file version: 1.0 * * WARNING: DO NOT MODIFY THIS FILE MANUALLY. @@ -28,6 +28,10 @@ class dconfig_org_deepin_dtk_preference : public QObject { Q_PROPERTY(qlonglong themeType READ themeType WRITE setThemeType NOTIFY themeTypeChanged RESET resetThemeType) Q_PROPERTY(qlonglong titlebarHeight READ titlebarHeight WRITE setTitlebarHeight NOTIFY titlebarHeightChanged RESET resetTitlebarHeight) Q_PROPERTY(bool underlineShortcut READ underlineShortcut WRITE setUnderlineShortcut NOTIFY underlineShortcutChanged RESET resetUnderlineShortcut) + Q_CLASSINFO("DConfigKeyList", "autoDisplayFeature;featureUpdated;keyboardsearchDisabled;rules;themeType;titlebarHeight;underlineShortcut") + Q_CLASSINFO("DConfigFileName", "org.deepin.dtk.preference") + Q_CLASSINFO("DConfigFileVersion", "1.0") + public: explicit dconfig_org_deepin_dtk_preference(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &appId, const QString &subpath, QObject *parent) : QObject(parent) { @@ -76,22 +80,50 @@ class dconfig_org_deepin_dtk_preference : public QObject { } } - DTK_CORE_NAMESPACE::DConfig *config() const { + Q_INVOKABLE DTK_CORE_NAMESPACE::DConfig *config() const { return m_config.loadRelaxed(); } - bool isInitializeSucceed() const { + Q_INVOKABLE bool isInitializeSucceed() const { return m_status.loadRelaxed() == static_cast(Status::Succeed); } - bool isInitializeFailed() const { + Q_INVOKABLE bool isInitializeFailed() const { return m_status.loadRelaxed() == static_cast(Status::Failed); } - bool isInitializing() const { + Q_INVOKABLE bool isInitializing() const { return m_status.loadRelaxed() == static_cast(Status::Invalid); } + Q_INVOKABLE QStringList keyList() const { + return { QStringLiteral(u"\u0061\u0075\u0074\u006f\u0044\u0069\u0073\u0070\u006c\u0061\u0079\u0046\u0065\u0061\u0074\u0075\u0072\u0065"), + QStringLiteral(u"\u0066\u0065\u0061\u0074\u0075\u0072\u0065\u0055\u0070\u0064\u0061\u0074\u0065\u0064"), + QStringLiteral(u"\u006b\u0065\u0079\u0062\u006f\u0061\u0072\u0064\u0073\u0065\u0061\u0072\u0063\u0068\u0044\u0069\u0073\u0061\u0062\u006c\u0065\u0064"), + QStringLiteral(u"\u0072\u0075\u006c\u0065\u0073"), + QStringLiteral(u"\u0074\u0068\u0065\u006d\u0065\u0054\u0079\u0070\u0065"), + QStringLiteral(u"\u0074\u0069\u0074\u006c\u0065\u0062\u0061\u0072\u0048\u0065\u0069\u0067\u0068\u0074"), + QStringLiteral(u"\u0075\u006e\u0064\u0065\u0072\u006c\u0069\u006e\u0065\u0053\u0068\u006f\u0072\u0074\u0063\u0075\u0074")}; + } + + Q_INVOKABLE bool isDefaultValue(const QString &key) const { + if (key == QStringLiteral(u"\u0061\u0075\u0074\u006f\u0044\u0069\u0073\u0070\u006c\u0061\u0079\u0046\u0065\u0061\u0074\u0075\u0072\u0065")) + return autoDisplayFeatureIsDefaultValue(); + if (key == QStringLiteral(u"\u0066\u0065\u0061\u0074\u0075\u0072\u0065\u0055\u0070\u0064\u0061\u0074\u0065\u0064")) + return featureUpdatedIsDefaultValue(); + if (key == QStringLiteral(u"\u006b\u0065\u0079\u0062\u006f\u0061\u0072\u0064\u0073\u0065\u0061\u0072\u0063\u0068\u0044\u0069\u0073\u0061\u0062\u006c\u0065\u0064")) + return keyboardsearchDisabledIsDefaultValue(); + if (key == QStringLiteral(u"\u0072\u0075\u006c\u0065\u0073")) + return rulesIsDefaultValue(); + if (key == QStringLiteral(u"\u0074\u0068\u0065\u006d\u0065\u0054\u0079\u0070\u0065")) + return themeTypeIsDefaultValue(); + if (key == QStringLiteral(u"\u0074\u0069\u0074\u006c\u0065\u0062\u0061\u0072\u0048\u0065\u0069\u0067\u0068\u0074")) + return titlebarHeightIsDefaultValue(); + if (key == QStringLiteral(u"\u0075\u006e\u0064\u0065\u0072\u006c\u0069\u006e\u0065\u0053\u0068\u006f\u0072\u0074\u0063\u0075\u0074")) + return underlineShortcutIsDefaultValue(); + return false; + } + bool autoDisplayFeature() const { return p_autoDisplayFeature; } @@ -116,7 +148,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool autoDisplayFeatureIsDefaultValue() const { + Q_INVOKABLE bool autoDisplayFeatureIsDefaultValue() const { return !testPropertySet(0); } bool featureUpdated() const { @@ -143,7 +175,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool featureUpdatedIsDefaultValue() const { + Q_INVOKABLE bool featureUpdatedIsDefaultValue() const { return !testPropertySet(1); } bool keyboardsearchDisabled() const { @@ -170,7 +202,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool keyboardsearchDisabledIsDefaultValue() const { + Q_INVOKABLE bool keyboardsearchDisabledIsDefaultValue() const { return !testPropertySet(2); } QString rules() const { @@ -197,7 +229,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool rulesIsDefaultValue() const { + Q_INVOKABLE bool rulesIsDefaultValue() const { return !testPropertySet(3); } qlonglong themeType() const { @@ -224,7 +256,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool themeTypeIsDefaultValue() const { + Q_INVOKABLE bool themeTypeIsDefaultValue() const { return !testPropertySet(4); } qlonglong titlebarHeight() const { @@ -251,7 +283,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool titlebarHeightIsDefaultValue() const { + Q_INVOKABLE bool titlebarHeightIsDefaultValue() const { return !testPropertySet(5); } bool underlineShortcut() const { @@ -278,7 +310,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { }); } } - bool underlineShortcutIsDefaultValue() const { + Q_INVOKABLE bool underlineShortcutIsDefaultValue() const { return !testPropertySet(6); } Q_SIGNALS: diff --git a/tools/dconfig2cpp/main.cpp b/tools/dconfig2cpp/main.cpp index 9fa4cd7f..30ee2583 100644 --- a/tools/dconfig2cpp/main.cpp +++ b/tools/dconfig2cpp/main.cpp @@ -87,10 +87,6 @@ int main(int argc, char *argv[]) { QLatin1String("Force request thread to create DConfig instance")); parser.addOption(forceRequestThread); - QCommandLineOption getIsDefaultValue(QStringList() << QLatin1String("get-is-default-value"), - QLatin1String("Generate *IsDefaultValue method")); - parser.addOption(getIsDefaultValue); - QCommandLineOption noComment(QStringList() << QLatin1String("no-comment"), QLatin1String("Do not generate comments in the generated code")); parser.addOption(noComment); @@ -189,15 +185,19 @@ int main(int argc, char *argv[]) { QJsonValue defaultValue; }; QList properties; + QStringList propertyNames; + QStringList propertyNameStrings; static QStringList usedKeywords = { className, "create", "createByName", "config", + "keyList", "isInitializeSucceed", "isInitializeFailed", "isInitializing", + "isDefaultValue", "m_config", "m_status", }; @@ -232,6 +232,7 @@ int main(int argc, char *argv[]) { capitalizedPropertyName[0] = capitalizedPropertyName[0].toUpper(); } + propertyNames << propertyName; properties.append(Property({ typeName, propertyName, @@ -239,6 +240,7 @@ int main(int argc, char *argv[]) { "QStringLiteral(u\"" + toUnicodeEscape(propertyName) + "\")", obj[QLatin1String("value")] })); + propertyNameStrings << properties.last().propertyNameString; const QString readFunction = usedKeywords.contains(propertyName) ? QLatin1String(" READ get") + capitalizedPropertyName : QLatin1String(" READ ") + propertyName; @@ -246,7 +248,11 @@ int main(int argc, char *argv[]) { << " WRITE set" << capitalizedPropertyName << " NOTIFY " << propertyName << "Changed" << " RESET reset" << capitalizedPropertyName << ")\n"; } - headerStream << "public:\n" + + headerStream << " Q_CLASSINFO(\"DConfigKeyList\", \"" << propertyNames.join(";") <<"\")\n" + << " Q_CLASSINFO(\"DConfigFileName\", \"" << QString(jsonFileName).replace("\n", "\\n").replace("\r", "\\r") <<"\")\n" + << " Q_CLASSINFO(\"DConfigFileVersion\", \"" << version <<"\")\n\n" + << "public:\n" << " explicit " << className << R"((QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &appId, const QString &subpath, QObject *parent) : QObject(parent) { @@ -313,24 +319,36 @@ int main(int argc, char *argv[]) { } } - DTK_CORE_NAMESPACE::DConfig *config() const { + Q_INVOKABLE DTK_CORE_NAMESPACE::DConfig *config() const { return m_config.loadRelaxed(); } - bool isInitializeSucceed() const { + Q_INVOKABLE bool isInitializeSucceed() const { return m_status.loadRelaxed() == static_cast(Status::Succeed); } - bool isInitializeFailed() const { + Q_INVOKABLE bool isInitializeFailed() const { return m_status.loadRelaxed() == static_cast(Status::Failed); } - bool isInitializing() const { + Q_INVOKABLE bool isInitializing() const { return m_status.loadRelaxed() == static_cast(Status::Invalid); } )"; + headerStream << " Q_INVOKABLE QStringList keyList() const {\n" + << " return { " << propertyNameStrings.join(",\n ") << "};\n" + << " }\n\n"; + + headerStream << " Q_INVOKABLE bool isDefaultValue(const QString &key) const {\n"; + for (int i = 0; i < properties.size(); ++i) { + headerStream << " if (key == " << properties.at(i).propertyNameString << ")\n" + << " return " << properties.at(i).propertyName << "IsDefaultValue();\n"; + } + headerStream << " return false;\n" + << " }\n\n"; + // Generate property getter and setter methods for (int i = 0; i < properties.size(); ++i) { const Property &property = properties[i]; @@ -367,11 +385,10 @@ int main(int argc, char *argv[]) { << " return QBindable<" << property.typeName << ">(this, " << property.propertyNameString << ");\n" << " }\n"; } - if (parser.isSet(getIsDefaultValue)) { - headerStream << " bool " << property.propertyName << "IsDefaultValue() const {\n" - << " return !testPropertySet(" << i << ");\n" - << " }\n"; - } + + headerStream << " Q_INVOKABLE bool " << property.propertyName << "IsDefaultValue() const {\n" + << " return !testPropertySet(" << i << ");\n" + << " }\n"; } // Generate signals for property changes @@ -418,8 +435,7 @@ int main(int argc, char *argv[]) { const Property &property = properties.at(i); headerStream << " if (key == " << property.propertyNameString << ") {\n"; - if (parser.isSet(getIsDefaultValue)) - headerStream << " markPropertySet(" << i << ", !m_config.loadRelaxed()->isDefaultValue(key));\n"; + headerStream << " markPropertySet(" << i << ", !m_config.loadRelaxed()->isDefaultValue(key));\n"; headerStream << " auto newValue = qvariant_cast<" << property.typeName << ">(value);\n" << " QMetaObject::invokeMethod(this, [this, newValue, key, value]() {\n"