diff options
author | Méven Car <meven29@gmail.com> | 2020-03-05 10:02:46 +0100 |
---|---|---|
committer | Méven Car <meven.car@enioka.com> | 2020-03-08 19:02:19 +0100 |
commit | ec207330d5bd61799a47092bf555a523ab000f93 (patch) | |
tree | 1d81fd6d70ff14dcad535cc79c8d40c641a1c676 /src/core | |
parent | a38f3d91d9dc6717010f01893ae26c8015b6bb05 (diff) | |
download | kconfig-ec207330d5bd61799a47092bf555a523ab000f93.tar.gz kconfig-ec207330d5bd61799a47092bf555a523ab000f93.tar.bz2 |
KconfigXT: Add a value attribute to Enum field choices
Summary:
Allow to write choices such as :
```
<choices>
<choice name="enum_name" value="I can't containt (anything)"></choice>
<choice name="normal_choice"></choice>
</choices>
```
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
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/kcoreconfigskeleton.cpp | 18 | ||||
-rw-r--r-- | src/core/kcoreconfigskeleton.h | 11 | ||||
-rw-r--r-- | src/core/kcoreconfigskeleton_p.h | 1 |
3 files changed, 28 insertions, 2 deletions
diff --git a/src/core/kcoreconfigskeleton.cpp b/src/core/kcoreconfigskeleton.cpp index 5672a850..575ee721 100644 --- a/src/core/kcoreconfigskeleton.cpp +++ b/src/core/kcoreconfigskeleton.cpp @@ -590,6 +590,19 @@ void KCoreConfigSkeleton::ItemLongLong::setMaxValue(qint64 v) mMax = v; } +QString KCoreConfigSkeleton::ItemEnum::valueForChoice(const QString &name) const +{ + // HACK for BC concerns + // TODO KF6: remove KConfigSkeletonItemPrivate::mValues and add a value field to KCoreConfigSkeleton::ItemEnum::Choice + const auto inHash = d_ptr->mValues.value(name); + return !inHash.isEmpty() ? inHash : name; +} + +void KCoreConfigSkeleton::ItemEnum::setValueForChoice(const QString &name, const QString &value) +{ + d_ptr->mValues.insert(name, value); +} + KCoreConfigSkeleton::ItemEnum::ItemEnum(const QString &_group, const QString &_key, qint32 &reference, const QList<Choice> &choices, @@ -609,7 +622,8 @@ void KCoreConfigSkeleton::ItemEnum::readConfig(KConfig *config) QString tmp = cg.readEntry(mKey, QString()).toLower(); for (QList<Choice>::ConstIterator it = mChoices.constBegin(); it != mChoices.constEnd(); ++it, ++i) { - if ((*it).name.toLower() == tmp) { + QString choiceName = (*it).name; + if (valueForChoice(choiceName).toLower() == tmp) { mReference = i; break; } @@ -630,7 +644,7 @@ void KCoreConfigSkeleton::ItemEnum::writeConfig(KConfig *config) if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else if ((mReference >= 0) && (mReference < mChoices.count())) { - cg.writeEntry(mKey, mChoices[mReference].name, writeFlags()); + cg.writeEntry(mKey, valueForChoice(mChoices.at(mReference).name), writeFlags()); } else { cg.writeEntry(mKey, mReference, writeFlags()); } diff --git a/src/core/kcoreconfigskeleton.h b/src/core/kcoreconfigskeleton.h index 02473fb4..9d84583e 100644 --- a/src/core/kcoreconfigskeleton.h +++ b/src/core/kcoreconfigskeleton.h @@ -802,9 +802,20 @@ public: void writeConfig(KConfig *config) override; // Source compatibility with 4.x + // TODO KF6 remove typedef Choice Choice2; QList<Choice> choices2() const; + /** + * Returns the value for for the choice with the given name + */ + QString valueForChoice(const QString &name) const; + + /** + * Stores a choice value for name + */ + void setValueForChoice(const QString &name, const QString &valueForChoice); + private: QList<Choice> mChoices; }; diff --git a/src/core/kcoreconfigskeleton_p.h b/src/core/kcoreconfigskeleton_p.h index 8ba64db7..fcc9bc13 100644 --- a/src/core/kcoreconfigskeleton_p.h +++ b/src/core/kcoreconfigskeleton_p.h @@ -62,6 +62,7 @@ public: QString mToolTip; ///< The ToolTip text for this item QString mWhatsThis; ///< The What's This text for this item KConfigGroup mConfigGroup; ///< KConfigGroup, allow to read/write item in nested groups + QHash<QString, QString> mValues; /// The values used for ItemEnum's choices, name -> value (if set) // HACK: Necessary to avoid introducing new virtuals in KConfigSkeletonItem std::function<bool()> mIsDefaultImpl; |