diff options
-rw-r--r-- | autotests/kconfigskeletontest.cpp | 27 | ||||
-rw-r--r-- | src/core/kcoreconfigskeleton.cpp | 45 | ||||
-rw-r--r-- | src/core/kcoreconfigskeleton.h | 37 | ||||
-rw-r--r-- | src/core/kcoreconfigskeleton_p.h | 4 |
4 files changed, 113 insertions, 0 deletions
diff --git a/autotests/kconfigskeletontest.cpp b/autotests/kconfigskeletontest.cpp index 3e37f6c5..1971da78 100644 --- a/autotests/kconfigskeletontest.cpp +++ b/autotests/kconfigskeletontest.cpp @@ -56,6 +56,9 @@ void KConfigSkeletonTest::init() QCOMPARE(mMyColor, DEFAULT_SETTING2); QCOMPARE(mMyFont, DEFAULT_SETTING3); QCOMPARE(mMyString, DEFAULT_SETTING4); + + QVERIFY(s->isDefaults()); + QVERIFY(!s->isSaveNeeded()); } void KConfigSkeletonTest::cleanup() @@ -70,15 +73,27 @@ void KConfigSkeletonTest::testSimple() mMyFont = WRITE_SETTING3; mMyString = WRITE_SETTING4; + QVERIFY(s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + s->save(); + QVERIFY(!s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + mMyBool = false; mMyColor = QColor(); mMyString.clear(); mMyFont = QFont(); + QVERIFY(s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + s->read(); + QVERIFY(!s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + QCOMPARE(mMyBool, WRITE_SETTING1); QCOMPARE(mMyColor, WRITE_SETTING2); QCOMPARE(mMyFont, WRITE_SETTING3); @@ -112,16 +127,28 @@ void KConfigSkeletonTest::testDefaults() mMyFont = WRITE_SETTING3; mMyString = WRITE_SETTING4; + QVERIFY(s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + s->save(); + QVERIFY(!s->isSaveNeeded()); + QVERIFY(!s->isDefaults()); + s->setDefaults(); + QVERIFY(s->isSaveNeeded()); + QVERIFY(s->isDefaults()); + QCOMPARE(mMyBool, DEFAULT_SETTING1); QCOMPARE(mMyColor, DEFAULT_SETTING2); QCOMPARE(mMyFont, DEFAULT_SETTING3); QCOMPARE(mMyString, DEFAULT_SETTING4); s->save(); + + QVERIFY(!s->isSaveNeeded()); + QVERIFY(s->isDefaults()); } void KConfigSkeletonTest::testKConfigDirty() diff --git a/src/core/kcoreconfigskeleton.cpp b/src/core/kcoreconfigskeleton.cpp index b3eb0019..34c58ff6 100644 --- a/src/core/kcoreconfigskeleton.cpp +++ b/src/core/kcoreconfigskeleton.cpp @@ -135,11 +135,31 @@ bool KConfigSkeletonItem::isImmutable() const return d->mIsImmutable; } +bool KConfigSkeletonItem::isDefault() const +{ + return d->mIsDefaultImpl(); +} + +bool KConfigSkeletonItem::isSaveNeeded() const +{ + return d->mIsSaveNeededImpl(); +} + void KConfigSkeletonItem::readImmutability(const KConfigGroup &group) { d->mIsImmutable = group.isEntryImmutable(mKey); } +void KConfigSkeletonItem::setIsDefaultImpl(const std::function<bool ()> &impl) +{ + d->mIsDefaultImpl = impl; +} + +void KConfigSkeletonItem::setIsSaveNeededImpl(const std::function<bool ()> &impl) +{ + d->mIsSaveNeededImpl = impl; +} + KCoreConfigSkeleton::ItemString::ItemString(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue, @@ -1091,6 +1111,28 @@ void KCoreConfigSkeleton::read() usrRead(); } +bool KCoreConfigSkeleton::isDefaults() const +{ + KConfigSkeletonItem::List::ConstIterator it; + for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { + if (!(*it)->isDefault()) { + return false; + } + } + return true; +} + +bool KCoreConfigSkeleton::isSaveNeeded() const +{ + KConfigSkeletonItem::List::ConstIterator it; + for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { + if ((*it)->isSaveNeeded()) { + return true; + } + } + return false; +} + bool KCoreConfigSkeleton::save() { //qDebug(); @@ -1394,6 +1436,9 @@ KConfigCompilerSignallingItem::KConfigCompilerSignallingItem(KConfigSkeletonItem Q_ASSERT(mTargetFunction); Q_ASSERT(mItem); Q_ASSERT(mObject); + + setIsDefaultImpl([this] { return mItem->isDefault(); }); + setIsSaveNeededImpl([this] { return mItem->isSaveNeeded(); }); } KConfigCompilerSignallingItem::~KConfigCompilerSignallingItem() diff --git a/src/core/kcoreconfigskeleton.h b/src/core/kcoreconfigskeleton.h index 771d8cc9..6e792efd 100644 --- a/src/core/kcoreconfigskeleton.h +++ b/src/core/kcoreconfigskeleton.h @@ -210,6 +210,21 @@ public: */ bool isImmutable() const; + /** + * Indicates if the item is set to its default value. + * + * @since 5.64 + */ + bool isDefault() const; + + /** + * Indicates if the item has a different value than the + * previously loaded value. + * + * @since 5.64 + */ + bool isSaveNeeded() const; + protected: /** * sets mIsImmutable to true if mKey in config is immutable @@ -221,6 +236,11 @@ protected: QString mKey; ///< The config key for this item QString mName; ///< The name of this item + // HACK: Necessary to avoid introducing new virtuals in KConfigSkeletonItem + // KF6: Use proper pure virtuals in KConfigSkeletonItem + void setIsDefaultImpl(const std::function<bool()> &impl); + void setIsSaveNeededImpl(const std::function<bool()> &impl); + private: KConfigSkeletonItemPrivate *const d; }; @@ -240,6 +260,8 @@ public: : KConfigSkeletonItem(_group, _key), mReference(reference), mDefault(defaultValue), mLoadedValue(defaultValue) { + setIsDefaultImpl([this] { return mReference == mDefault; }); + setIsSaveNeededImpl([this] { return mReference != mLoadedValue; }); } /** @@ -1078,6 +1100,21 @@ public: void read(); /** + * Indicates if all the registered items are set to their default value. + * + * @since 5.64 + */ + bool isDefaults() const; + + /** + * Indicates if any registered item has a different value than the + * previously loaded value. + * + * @since 5.64 + */ + bool isSaveNeeded() const; + + /** * Set the config file group for subsequent addItem() calls. It is valid * until setCurrentGroup() is called with a new argument. Call this before * you add any items. The default value is "No Group". diff --git a/src/core/kcoreconfigskeleton_p.h b/src/core/kcoreconfigskeleton_p.h index 41005c6f..623bfa03 100644 --- a/src/core/kcoreconfigskeleton_p.h +++ b/src/core/kcoreconfigskeleton_p.h @@ -60,6 +60,10 @@ public: QString mLabel; ///< The label for this item QString mToolTip; ///< The ToolTip text for this item QString mWhatsThis; ///< The What's This text for this item + + // HACK: Necessary to avoid introducing new virtuals in KConfigSkeletonItem + std::function<bool()> mIsDefaultImpl; + std::function<bool()> mIsSaveNeededImpl; }; #endif |