From ec207330d5bd61799a47092bf555a523ab000f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Thu, 5 Mar 2020 10:02:46 +0100 Subject: KconfigXT: Add a value attribute to Enum field choices Summary: Allow to write choices such as : ``` ``` Test Plan: ctest Reviewers: ervin, bport, crossi, #frameworks Reviewed By: ervin Subscribers: ngraham, davidre, kde-frameworks-devel Tags: #frameworks Differential Revision: https://phabricator.kde.org/D27463 --- src/kconfig_compiler/KConfigCommonStructs.h | 6 ++++++ src/kconfig_compiler/KConfigSourceGenerator.cpp | 19 ++++++++++++++++--- src/kconfig_compiler/KConfigXmlParser.cpp | 7 ++++++- src/kconfig_compiler/README.dox | 7 +++++-- src/kconfig_compiler/kcfg.xsd | 1 + 5 files changed, 34 insertions(+), 6 deletions(-) (limited to 'src/kconfig_compiler') diff --git a/src/kconfig_compiler/KConfigCommonStructs.h b/src/kconfig_compiler/KConfigCommonStructs.h index 06c8b80f..fef45a9f 100644 --- a/src/kconfig_compiler/KConfigCommonStructs.h +++ b/src/kconfig_compiler/KConfigCommonStructs.h @@ -55,6 +55,12 @@ public: QString label; QString toolTip; QString whatsThis; + QString val; + + QString value() const + { + return !val.isEmpty() ? val : name; + } }; class Choices { diff --git a/src/kconfig_compiler/KConfigSourceGenerator.cpp b/src/kconfig_compiler/KConfigSourceGenerator.cpp index 33e0ed69..3e8a8156 100644 --- a/src/kconfig_compiler/KConfigSourceGenerator.cpp +++ b/src/kconfig_compiler/KConfigSourceGenerator.cpp @@ -313,13 +313,14 @@ void KConfigSourceGenerator::createEnums(const CfgEntry *entry) void KConfigSourceGenerator::createNormalEntry(const CfgEntry *entry, const QString &key) { + const QString itemVarStr = itemPath(entry, cfg()); const QString innerItemVarStr = innerItemVar(entry, cfg()); if (!entry->signalList.isEmpty()) { stream() << " " << innerItemVarStr << " = " << newInnerItem(entry, key, entry->defaultValue, cfg()) << '\n'; } - stream() << " " << itemPath(entry, cfg()) << " = " + stream() << " " << itemVarStr << " = " << newItem(entry, key, entry->defaultValue, cfg()) << '\n'; if (!entry->min.isEmpty()) { @@ -335,10 +336,16 @@ void KConfigSourceGenerator::createNormalEntry(const CfgEntry *entry, const QStr } if (cfg().allNotifiers || cfg().notifiers.contains(entry->name)) { - stream() << " " << itemPath(entry, cfg()) << "->setWriteFlags(KConfigBase::Notify);\n"; + stream() << " " << itemVarStr << "->setWriteFlags(KConfigBase::Notify);\n"; } - stream() << " addItem( " << itemPath(entry, cfg()); + for (const CfgEntry::Choice &choice : qAsConst(entry->choices.choices)) { + if (!choice.val.isEmpty()) { + stream() << " " << itemVarStr << "->setValueForChoice(QStringLiteral( \"" << choice.name << "\" ), QStringLiteral( \"" << choice.val << "\" ));\n"; + } + } + + stream() << " addItem( " << itemVarStr; QString quotedName = entry->name; addQuotes(quotedName); if (quotedName != key) { @@ -374,6 +381,12 @@ void KConfigSourceGenerator::createIndexedEntry(const CfgEntry *entry, const QSt stream() << " " << innerItemVarStr << "->setMaxValue(" << entry->max << ");\n"; } + for (const CfgEntry::Choice &choice : qAsConst(entry->choices.choices)) { + if (!choice.val.isEmpty()) { + stream() << " " << itemVarStr << "->setValueForChoice(QStringLiteral( \"" << choice.name << "\" ), QStringLiteral( \"" << choice.val << "\" ));\n"; + } + } + if (cfg().setUserTexts) { stream() << userTextsFunctions(entry, cfg(), itemVarStr, entry->paramName); } diff --git a/src/kconfig_compiler/KConfigXmlParser.cpp b/src/kconfig_compiler/KConfigXmlParser.cpp index 2f31beb9..045018de 100644 --- a/src/kconfig_compiler/KConfigXmlParser.cpp +++ b/src/kconfig_compiler/KConfigXmlParser.cpp @@ -176,6 +176,7 @@ void KConfigXmlParser::readParameterFromEntry(CfgEntry &readEntry, const QDomEle bool KConfigXmlParser::hasDefaultCode(CfgEntry &readEntry, const QDomElement &element) { + Q_UNUSED(readEntry) for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) { if (e.attribute(QStringLiteral("param")).isEmpty()) { if (e.attribute(QStringLiteral("code")) == QLatin1String("true")) { @@ -186,10 +187,11 @@ bool KConfigXmlParser::hasDefaultCode(CfgEntry &readEntry, const QDomElement &el return false; } - void KConfigXmlParser::readChoicesFromEntry(CfgEntry &readEntry, const QDomElement &e) { QList chlist; + const auto choiceNameRegex = QRegularExpression(QStringLiteral("\\w+")); + for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) { if (e2.tagName() != QLatin1String("choice")) { continue; @@ -198,7 +200,10 @@ void KConfigXmlParser::readChoicesFromEntry(CfgEntry &readEntry, const QDomEleme choice.name = e2.attribute(QStringLiteral("name")); if (choice.name.isEmpty()) { std::cerr << "Tag requires attribute 'name'." << std::endl; + } else if (!choiceNameRegex.match(choice.name).hasMatch()) { + std::cerr << "Tag attribute 'name' must be compatible with Enum naming. name was '" << qPrintable(choice.name) << "'. You can use attribute 'value' to pass any string as the choice value." << std::endl; } + choice.val = e2.attribute(QStringLiteral("value")); for (QDomElement e3 = e2.firstChildElement(); !e3.isNull(); e3 = e3.nextSiblingElement()) { if (e3.tagName() == QLatin1String("label")) { choice.label = e3.text(); diff --git a/src/kconfig_compiler/README.dox b/src/kconfig_compiler/README.dox index c17bd395..55e59736 100644 --- a/src/kconfig_compiler/README.dox +++ b/src/kconfig_compiler/README.dox @@ -354,7 +354,7 @@ this allows the same Enum value names to be used in different enums. For example - + \endverbatim @@ -369,6 +369,9 @@ will generate this public class containing the enum definition, inside the gener }; \endverbatim +Since 5.68, if present the value attribute will be used as the choice value written to the backend +instead of the name, allowing to write text incompatible with enum naming. + Alternatively, if GlobalEnums is set to true, all Enum items are defined as unnamed enums in the global scope of the generated class. In this case, all Enum values must have different names to avoid clashes. However, you can use a 'prefix' argument @@ -380,7 +383,7 @@ is set to true, the .kcfg entry - + \endverbatim diff --git a/src/kconfig_compiler/kcfg.xsd b/src/kconfig_compiler/kcfg.xsd index 77a335cd..11196d45 100644 --- a/src/kconfig_compiler/kcfg.xsd +++ b/src/kconfig_compiler/kcfg.xsd @@ -93,6 +93,7 @@ + -- cgit v1.2.1