diff --git a/autotests/kconfig_compiler/CMakeLists.txt b/autotests/kconfig_compiler/CMakeLists.txt --- a/autotests/kconfig_compiler/CMakeLists.txt +++ b/autotests/kconfig_compiler/CMakeLists.txt @@ -189,6 +189,15 @@ ecm_add_test(TEST_NAME test_signal ${test_signal_SRCS}) target_link_libraries(test_signal KF5::ConfigGui) + +########### next target ############### + +set(test_notifiers_SRCS test_notifiers_main.cpp ) +gen_kcfg_test_source(test_notifiers test_notifiers_SRCS) +ecm_add_test(TEST_NAME test_notifiers ${test_notifiers_SRCS}) +target_link_libraries(test_notifiers KF5::ConfigGui) + + ########### next target ############### set(kconfigcompiler_test_signals_SRCS kconfigcompiler_test_signals.cpp) diff --git a/autotests/kconfig_compiler/kconfigcompiler_test.cpp b/autotests/kconfig_compiler/kconfigcompiler_test.cpp --- a/autotests/kconfig_compiler/kconfigcompiler_test.cpp +++ b/autotests/kconfig_compiler/kconfigcompiler_test.cpp @@ -48,6 +48,7 @@ "test_dpointer.cpp", "test_dpointer.h", "test_qdebugcategory.cpp", "test_qdebugcategory.h", "test_signal.cpp", "test_signal.h", + "test_notifiers.cpp", "test_notifiers.h", "test_translation_kde.cpp", "test_translation_kde.h", "test_translation_kde_domain.cpp", "test_translation_kde_domain.h", "test_translation_qt.cpp", "test_translation_qt.h", diff --git a/autotests/kconfig_compiler/test_notifiers.h.ref b/autotests/kconfig_compiler/test_notifiers.h.ref new file mode 100644 --- /dev/null +++ b/autotests/kconfig_compiler/test_notifiers.h.ref @@ -0,0 +1,94 @@ +// This file is generated by kconfig_compiler_kf5 from test_notifiers.kcfg. +// All changes you do to this file will be lost. +#ifndef TESTNOTIFIERS_H +#define TESTNOTIFIERS_H + +#include +#include +#include + +class TestNotifiers : public KConfigSkeleton +{ + public: + + TestNotifiers( int Number ); + ~TestNotifiers(); + + /** + Set Block colors. + */ + void setColor( const QColor & v ) + { + if (!isImmutable( QStringLiteral( "Color" ) )) + mColor = v; + } + + /** + Get Block colors. + */ + QColor color() const + { + return mColor; + } + + /** + Set foo bar + */ + void setFooBar( const QString & v ) + { + if (!isImmutable( QStringLiteral( "FooBar" ) )) + mFooBar = v; + } + + /** + Get foo bar + */ + QString fooBar() const + { + return mFooBar; + } + + /** + Set Age + */ + void setAge( int v ) + { + if (v < 8) + { + qDebug() << "setAge: value " << v << " is less than the minimum value of 8"; + v = 8; + } + + if (v > 88) + { + qDebug() << "setAge: value " << v << " is greater than the maximum value of 88"; + v = 88; + } + + if (!isImmutable( QStringLiteral( "Age" ) )) + mAge = v; + } + + /** + Get Age + */ + int age() const + { + return mAge; + } + + protected: + int mParamNumber; + + // Foo + QColor mColor; + + // Bar$(Number) + QString mFooBar; + int mAge; + + private: +}; + +#endif + diff --git a/autotests/kconfig_compiler/test_notifiers.cpp.ref b/autotests/kconfig_compiler/test_notifiers.cpp.ref new file mode 100644 --- /dev/null +++ b/autotests/kconfig_compiler/test_notifiers.cpp.ref @@ -0,0 +1,34 @@ +// This file is generated by kconfig_compiler_kf5 from test_notifiers.kcfg. +// All changes you do to this file will be lost. + +#include "test_notifiers.h" + +TestNotifiers::TestNotifiers( int Number ) + : KConfigSkeleton( QStringLiteral( "test7rc" ) ) + , mParamNumber(Number) +{ + setCurrentGroup( QStringLiteral( "Foo" ) ); + + KConfigSkeleton::ItemColor *itemColor; + itemColor = new KConfigSkeleton::ItemColor( currentGroup(), QStringLiteral( "color #%1" ).arg( mParamNumber ), mColor, QColor( "red" ) ); + itemColor->setWriteFlags(KConfigBase::Notify); + addItem( itemColor, QStringLiteral( "Color" ) ); + + setCurrentGroup( QStringLiteral( "Bar%1" ).arg( mParamNumber ) ); + + KConfigSkeleton::ItemString *itemFooBar; + itemFooBar = new KConfigSkeleton::ItemString( currentGroup(), QStringLiteral( "foo bar" ), mFooBar ); + itemFooBar->setWriteFlags(KConfigBase::Notify); + addItem( itemFooBar, QStringLiteral( "FooBar" ) ); + KConfigSkeleton::ItemInt *itemAge; + itemAge = new KConfigSkeleton::ItemInt( currentGroup(), QStringLiteral( "Age" ), mAge, 35 ); + itemAge->setMinValue(8); + itemAge->setMaxValue(88); + itemAge->setWriteFlags(KConfigBase::Notify); + addItem( itemAge, QStringLiteral( "Age" ) ); +} + +TestNotifiers::~TestNotifiers() +{ +} + diff --git a/autotests/kconfig_compiler/test_notifiers.kcfg b/autotests/kconfig_compiler/test_notifiers.kcfg new file mode 100644 --- /dev/null +++ b/autotests/kconfig_compiler/test_notifiers.kcfg @@ -0,0 +1,25 @@ + + + + + + + + + + red + + + + + + 35 + 8 + 88 + + + + diff --git a/autotests/kconfig_compiler/test_notifiers.kcfgc b/autotests/kconfig_compiler/test_notifiers.kcfgc new file mode 100644 --- /dev/null +++ b/autotests/kconfig_compiler/test_notifiers.kcfgc @@ -0,0 +1,8 @@ +# Code generation options for kconfig_compiler_kf5 +File=test_notifiers.kcfg +ClassName=TestNotifiers +Singleton=false +Mutators=true +GlobalEnums=true +ItemAccessors=false +Notifiers=true diff --git a/autotests/kconfig_compiler/test_notifiers_main.cpp b/autotests/kconfig_compiler/test_notifiers_main.cpp new file mode 100644 --- /dev/null +++ b/autotests/kconfig_compiler/test_notifiers_main.cpp @@ -0,0 +1,32 @@ +/* +Copyright (c) 2019 Kai Uwe Broulik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "test_notifiers.h" +#include + +int main(int argc, char **argv) +{ + QGuiApplication app(argc, argv); + Q_UNUSED(app); + TestNotifiers *t = new TestNotifiers(42); + delete t; + return 0; +} diff --git a/src/core/kcoreconfigskeleton.h b/src/core/kcoreconfigskeleton.h --- a/src/core/kcoreconfigskeleton.h +++ b/src/core/kcoreconfigskeleton.h @@ -132,6 +132,19 @@ */ QString whatsThis() const; + /** + The write flags to be used when writing configuration. + @since 5.58 + */ + void setWriteFlags(KConfigBase::WriteConfigFlags flags); + + /** + Return write flags to be used when writing configuration. + They should be passed to every call of writeEntry() and revertToDefault(). + @since 5.58 + */ + KConfigBase::WriteConfigFlags writeFlags() const; + /** * This function is called by @ref KCoreConfigSkeleton to read the value for this setting * from a config file. @@ -141,6 +154,7 @@ /** * This function is called by @ref KCoreConfigSkeleton to write the value of this setting * to a config file. + * Make sure to pass writeFlags() to every call of writeEntry() and revertToDefault(). */ virtual void writeConfig(KConfig *) = 0; @@ -274,9 +288,9 @@ if (mReference != mLoadedValue) { // Is this needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else { - cg.writeEntry(mKey, mReference); + cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } diff --git a/src/core/kcoreconfigskeleton.cpp b/src/core/kcoreconfigskeleton.cpp --- a/src/core/kcoreconfigskeleton.cpp +++ b/src/core/kcoreconfigskeleton.cpp @@ -110,6 +110,16 @@ return d->mWhatsThis; } +void KConfigSkeletonItem::setWriteFlags(KConfigBase::WriteConfigFlags flags) +{ + d->mWriteFlags = flags; +} + +KConfigBase::WriteConfigFlags KConfigSkeletonItem::writeFlags() const +{ + return d->mWriteFlags; +} + QVariant KConfigSkeletonItem::minValue() const { return QVariant(); @@ -144,13 +154,13 @@ if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else if (mType == Path) { - cg.writePathEntry(mKey, mReference); + cg.writePathEntry(mKey, mReference, writeFlags()); } else if (mType == Password) { - cg.writeEntry(mKey, obscuredString(mReference)); + cg.writeEntry(mKey, obscuredString(mReference), writeFlags()); } else { - cg.writeEntry(mKey, mReference); + cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } @@ -215,9 +225,9 @@ if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else { - cg.writeEntry(mKey, mReference.toString()); + cg.writeEntry(mKey, mReference.toString(), writeFlags()); } mLoadedValue = mReference; } @@ -479,11 +489,11 @@ if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else if ((mReference >= 0) && (mReference < mChoices.count())) { - cg.writeEntry(mKey, mChoices[mReference].name); + cg.writeEntry(mKey, mChoices[mReference].name, writeFlags()); } else { - cg.writeEntry(mKey, mReference); + cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } @@ -879,10 +889,10 @@ if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else { QStringList sl = mReference; - cg.writePathEntry(mKey, sl); + cg.writePathEntry(mKey, sl, writeFlags()); } mLoadedValue = mReference; } @@ -921,13 +931,13 @@ if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { - cg.revertToDefault(mKey); + cg.revertToDefault(mKey, writeFlags()); } else { QStringList strList; for (const QUrl &url : qAsConst(mReference)) { strList.append(url.toString()); } - cg.writeEntry(mKey, strList); + cg.writeEntry(mKey, strList, writeFlags()); } mLoadedValue = mReference; } diff --git a/src/core/kcoreconfigskeleton_p.h b/src/core/kcoreconfigskeleton_p.h --- a/src/core/kcoreconfigskeleton_p.h +++ b/src/core/kcoreconfigskeleton_p.h @@ -52,8 +52,10 @@ public: KConfigSkeletonItemPrivate() : mIsImmutable(true) + , mWriteFlags(KConfigBase::Normal) {} bool mIsImmutable; ///< Indicates this item is immutable + KConfigBase::WriteConfigFlags mWriteFlags; ///< The flags to pass to calls of writeEntry() and revertToDefault() QString mLabel; ///< The label for this item QString mToolTip; ///< The ToolTip text for this item diff --git a/src/kconfig_compiler/kconfig_compiler.cpp b/src/kconfig_compiler/kconfig_compiler.cpp --- a/src/kconfig_compiler/kconfig_compiler.cpp +++ b/src/kconfig_compiler/kconfig_compiler.cpp @@ -94,6 +94,8 @@ setUserTexts = codegenConfig.value(QStringLiteral("SetUserTexts"), false).toBool(); defaultGetters = codegenConfig.value(QStringLiteral("DefaultValueGetters"), QStringList()).toStringList(); allDefaultGetters = (defaultGetters.count() == 1) && (defaultGetters.at(0).toLower() == QLatin1String("true")); + notifiers = codegenConfig.value(QStringLiteral("Notifiers"), QStringList()).toStringList(); + allNotifiers = ((notifiers.count() == 1) && (notifiers.at(0).toLower() == QLatin1String("true"))); globalEnums = codegenConfig.value(QStringLiteral("GlobalEnums"), false).toBool(); useEnumTypes = codegenConfig.value(QStringLiteral("UseEnumTypes"), false).toBool(); const QString trString = codegenConfig.value(QStringLiteral("TranslationSystem")).toString().toLower(); @@ -132,6 +134,7 @@ QStringList sourceIncludes; QStringList mutators; QStringList defaultGetters; + QStringList notifiers; QString qCategoryLoggingName; QString headerExtension; QString sourceExtension; @@ -142,6 +145,7 @@ bool globalEnums; bool useEnumTypes; bool itemAccessors; + bool allNotifiers; TranslationSystem translationSystem; QString translationDomain; bool generateProperties; @@ -2484,6 +2488,10 @@ cpp << userTextsFunctions((*itEntry), cfg); } + if (cfg.allNotifiers || cfg.notifiers.contains((*itEntry)->name())) { + cpp << " " << itemPath(*itEntry, cfg) << "->setWriteFlags(KConfigBase::Notify);" << endl; + } + cpp << " addItem( " << itemPath(*itEntry, cfg); QString quotedName = (*itEntry)->name(); addQuotes(quotedName);