KConfig 5.109.0
|
Class for handling preferences settings for an application. More...
#include <KCoreConfigSkeleton>
Classes | |
class | ItemBool |
Class for handling a bool preferences item. More... | |
class | ItemDateTime |
Class for handling a QDateTime preferences item. More... | |
class | ItemDouble |
Class for handling a floating point preference item. More... | |
class | ItemEnum |
Class for handling enums. More... | |
class | ItemInt |
Class for handling a 32-bit integer preferences item. More... | |
class | ItemIntList |
Class for handling an integer list preferences item. More... | |
class | ItemLongLong |
Class for handling a 64-bit integer preferences item. More... | |
class | ItemPassword |
Class for handling a password preferences item. More... | |
class | ItemPath |
Class for handling a path preferences item. More... | |
class | ItemPathList |
Class for handling a path list preferences item. More... | |
class | ItemPoint |
Class for handling a QPoint preferences item. More... | |
class | ItemProperty |
Class for handling a QVariant preferences item. More... | |
class | ItemRect |
Class for handling a QRect preferences item. More... | |
class | ItemSize |
Class for handling a QSize preferences item. More... | |
class | ItemString |
Class for handling a string preferences item. More... | |
class | ItemStringList |
Class for handling a string list preferences item. More... | |
class | ItemUInt |
Class for handling an unsigned 32-bit integer preferences item. More... | |
class | ItemULongLong |
Class for handling unsigned 64-bit integer preferences item. More... | |
class | ItemUrl |
Class for handling a url preferences item. More... | |
class | ItemUrlList |
Class for handling a url list preferences item. More... | |
Public Types | |
typedef ItemLongLong | ItemInt64 |
typedef ItemULongLong | ItemUInt64 |
Signals | |
void | configChanged () |
This signal is emitted when the configuration change. | |
Public Slots | |
bool | save () |
Write preferences to config file. | |
void | writeConfig () |
Public Member Functions | |
KCoreConfigSkeleton (const QString &configname=QString(), QObject *parent=nullptr) | |
Constructor. | |
KCoreConfigSkeleton (KSharedConfig::Ptr config, QObject *parent=nullptr) | |
Constructor. | |
~KCoreConfigSkeleton () override | |
Destructor. | |
void | addItem (KConfigSkeletonItem *item, const QString &name=QString()) |
Register a custom KConfigSkeletonItem item with a given name . | |
ItemBool * | addItemBool (const QString &name, bool &reference, bool defaultValue=false, const QString &key=QString()) |
Register an item of type bool . | |
ItemDateTime * | addItemDateTime (const QString &name, QDateTime &reference, const QDateTime &defaultValue=QDateTime(), const QString &key=QString()) |
Register an item of type QDateTime. | |
ItemDouble * | addItemDouble (const QString &name, double &reference, double defaultValue=0.0, const QString &key=QString()) |
Register an item of type double . | |
ItemInt * | addItemInt (const QString &name, qint32 &reference, qint32 defaultValue=0, const QString &key=QString()) |
Register an item of type qint32 . | |
ItemLongLong * | addItemInt64 (const QString &name, qint64 &reference, qint64 defaultValue=0, const QString &key=QString()) |
ItemIntList * | addItemIntList (const QString &name, QList< int > &reference, const QList< int > &defaultValue=QList< int >(), const QString &key=QString()) |
Register an item of type QList<int>. | |
ItemLongLong * | addItemLongLong (const QString &name, qint64 &reference, qint64 defaultValue=0, const QString &key=QString()) |
Register an item of type qint64 . | |
ItemPassword * | addItemPassword (const QString &name, QString &reference, const QString &defaultValue=QLatin1String(""), const QString &key=QString()) |
Register a password item of type QString. | |
ItemPath * | addItemPath (const QString &name, QString &reference, const QString &defaultValue=QLatin1String(""), const QString &key=QString()) |
Register a path item of type QString. | |
ItemPoint * | addItemPoint (const QString &name, QPoint &reference, const QPoint &defaultValue=QPoint(), const QString &key=QString()) |
Register an item of type QPoint. | |
ItemProperty * | addItemProperty (const QString &name, QVariant &reference, const QVariant &defaultValue=QVariant(), const QString &key=QString()) |
Register a property item of type QVariant. | |
ItemRect * | addItemRect (const QString &name, QRect &reference, const QRect &defaultValue=QRect(), const QString &key=QString()) |
Register an item of type QRect. | |
ItemSize * | addItemSize (const QString &name, QSize &reference, const QSize &defaultValue=QSize(), const QString &key=QString()) |
Register an item of type QSize. | |
ItemString * | addItemString (const QString &name, QString &reference, const QString &defaultValue=QLatin1String(""), const QString &key=QString()) |
Register an item of type QString. | |
ItemStringList * | addItemStringList (const QString &name, QStringList &reference, const QStringList &defaultValue=QStringList(), const QString &key=QString()) |
Register an item of type QStringList. | |
ItemUInt * | addItemUInt (const QString &name, quint32 &reference, quint32 defaultValue=0, const QString &key=QString()) |
Register an item of type quint32 . | |
ItemULongLong * | addItemUInt64 (const QString &name, quint64 &reference, quint64 defaultValue=0, const QString &key=QString()) |
ItemULongLong * | addItemULongLong (const QString &name, quint64 &reference, quint64 defaultValue=0, const QString &key=QString()) |
Register an item of type quint64 . | |
void | clearItems () |
Removes and deletes all items. | |
KConfig * | config () |
Return the KConfig object used for reading and writing the settings. | |
const KConfig * | config () const |
Return the KConfig object used for reading and writing the settings. | |
QString | currentGroup () const |
Returns the current group used for addItem() calls. | |
KConfigSkeletonItem * | findItem (const QString &name) const |
Lookup item by name. | |
bool | isDefaults () const |
Indicates if all the registered items are set to their default value. | |
bool | isImmutable (const QString &name) const |
Return whether a certain item is immutable. | |
bool | isSaveNeeded () const |
Indicates if any registered item has a different value than the previously loaded value. | |
KConfigSkeletonItem::List | items () const |
Return list of items managed by this KCoreConfigSkeleton object. | |
void | load () |
Read preferences from config file. | |
void | read () |
Read preferences from the KConfig object. | |
void | readConfig () |
void | removeItem (const QString &name) |
Removes and deletes an item by name. | |
void | setCurrentGroup (const QString &group) |
Set the config file group for subsequent addItem() calls. | |
virtual void | setDefaults () |
Set all registered items to their default values. | |
void | setSharedConfig (KSharedConfig::Ptr pConfig) |
Set the KSharedConfig object used for reading and writing the settings. | |
KSharedConfig::Ptr | sharedConfig () const |
Return the KConfig object used for reading and writing the settings. | |
virtual bool | useDefaults (bool b) |
Specify whether this object should reflect the actual values or the default values. | |
Protected Member Functions | |
virtual void | usrRead () |
Perform the actual reading of the configuration file. | |
virtual void | usrReadConfig () |
virtual bool | usrSave () |
Perform the actual writing of the configuration file. | |
virtual void | usrSetDefaults () |
Perform the actual setting of default values. | |
virtual bool | usrUseDefaults (bool b) |
Implemented by subclasses that use special defaults. | |
virtual bool | usrWriteConfig () |
Class for handling preferences settings for an application.
This class provides an interface to preferences settings. Preferences items can be registered by the addItem() function corresponding to the data type of the setting. KCoreConfigSkeleton then handles reading and writing of config files and setting of default values.
Normally you will subclass KCoreConfigSkeleton, add data members for the preferences settings and register the members in the constructor of the subclass.
Example:
It might be convenient in many cases to make this subclass of KCoreConfigSkeleton a singleton for global access from all over the application without passing references to the KCoreConfigSkeleton object around.
You can write the data to the configuration file by calling save() and read the data from the configuration file by calling readConfig(). If you want to watch for config changes, use configChanged() signal.
If you have items, which are not covered by the existing addItem() functions you can add customized code for reading, writing and default setting by implementing the functions usrUseDefaults(), usrRead() and usrSave().
Internally preferences settings are stored in instances of subclasses of KConfigSkeletonItem. You can also add KConfigSkeletonItem subclasses for your own types and call the generic addItem() to register them.
In many cases you don't have to write the specific KCoreConfigSkeleton subclasses yourself, but you can use The KDE Configuration Compiler to automatically generate the C++ code from an XML description of the configuration options.
Use KConfigSkeleton if you need GUI types as well.
|
explicit |
Constructor.
configname | name of config file. If no name is given, the default config file as returned by KSharedConfig::openConfig() is used |
parent | the parent object (see QObject documentation) |
|
explicit |
Constructor.
config | configuration object to use |
parent | the parent object (see QObject documentation) |
|
override |
Destructor.
void KCoreConfigSkeleton::addItem | ( | KConfigSkeletonItem * | item, |
const QString & | name = QString() |
||
) |
Register a custom KConfigSkeletonItem item
with a given name
.
If name
is a null string, take the name from KConfigSkeletonItem::key().
KCoreConfigSkeleton takes ownership of item
.
ItemBool * KCoreConfigSkeleton::addItemBool | ( | const QString & | name, |
bool & | reference, | ||
bool | defaultValue = false , |
||
const QString & | key = QString() |
||
) |
Register an item of type bool
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemDateTime * KCoreConfigSkeleton::addItemDateTime | ( | const QString & | name, |
QDateTime & | reference, | ||
const QDateTime & | defaultValue = QDateTime() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QDateTime.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemDouble * KCoreConfigSkeleton::addItemDouble | ( | const QString & | name, |
double & | reference, | ||
double | defaultValue = 0.0 , |
||
const QString & | key = QString() |
||
) |
Register an item of type double
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemInt * KCoreConfigSkeleton::addItemInt | ( | const QString & | name, |
qint32 & | reference, | ||
qint32 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
Register an item of type qint32
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemLongLong * KCoreConfigSkeleton::addItemInt64 | ( | const QString & | name, |
qint64 & | reference, | ||
qint64 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
ItemIntList * KCoreConfigSkeleton::addItemIntList | ( | const QString & | name, |
QList< int > & | reference, | ||
const QList< int > & | defaultValue = QList< int >() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QList<int>.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemLongLong * KCoreConfigSkeleton::addItemLongLong | ( | const QString & | name, |
qint64 & | reference, | ||
qint64 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
Register an item of type qint64
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemPassword * KCoreConfigSkeleton::addItemPassword | ( | const QString & | name, |
QString & | reference, | ||
const QString & | defaultValue = QLatin1String("") , |
||
const QString & | key = QString() |
||
) |
Register a password item of type QString.
The string value is written encrypted to the config file.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemPath * KCoreConfigSkeleton::addItemPath | ( | const QString & | name, |
QString & | reference, | ||
const QString & | defaultValue = QLatin1String("") , |
||
const QString & | key = QString() |
||
) |
Register a path item of type QString.
The string value is interpreted as a path. This means, dollar expansion is activated for this value, so that e.g. $HOME
gets expanded.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemPoint * KCoreConfigSkeleton::addItemPoint | ( | const QString & | name, |
QPoint & | reference, | ||
const QPoint & | defaultValue = QPoint() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QPoint.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemProperty * KCoreConfigSkeleton::addItemProperty | ( | const QString & | name, |
QVariant & | reference, | ||
const QVariant & | defaultValue = QVariant() , |
||
const QString & | key = QString() |
||
) |
Register a property item of type QVariant.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemRect * KCoreConfigSkeleton::addItemRect | ( | const QString & | name, |
QRect & | reference, | ||
const QRect & | defaultValue = QRect() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QRect.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemSize * KCoreConfigSkeleton::addItemSize | ( | const QString & | name, |
QSize & | reference, | ||
const QSize & | defaultValue = QSize() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QSize.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemString * KCoreConfigSkeleton::addItemString | ( | const QString & | name, |
QString & | reference, | ||
const QString & | defaultValue = QLatin1String("") , |
||
const QString & | key = QString() |
||
) |
Register an item of type QString.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemStringList * KCoreConfigSkeleton::addItemStringList | ( | const QString & | name, |
QStringList & | reference, | ||
const QStringList & | defaultValue = QStringList() , |
||
const QString & | key = QString() |
||
) |
Register an item of type QStringList.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemUInt * KCoreConfigSkeleton::addItemUInt | ( | const QString & | name, |
quint32 & | reference, | ||
quint32 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
Register an item of type quint32
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
ItemULongLong * KCoreConfigSkeleton::addItemUInt64 | ( | const QString & | name, |
quint64 & | reference, | ||
quint64 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
ItemULongLong * KCoreConfigSkeleton::addItemULongLong | ( | const QString & | name, |
quint64 & | reference, | ||
quint64 | defaultValue = 0 , |
||
const QString & | key = QString() |
||
) |
Register an item of type quint64
.
name | Name used to identify this setting. Names must be unique. |
reference | Pointer to the variable, which is set by readConfig() calls and read by save() calls. |
defaultValue | Default value, which is used when the config file does not yet contain the key of this item. |
key | Key used in config file. If key is a null string, name is used as key. |
void KCoreConfigSkeleton::clearItems | ( | ) |
Removes and deletes all items.
KConfig * KCoreConfigSkeleton::config | ( | ) |
Return the KConfig object used for reading and writing the settings.
const KConfig * KCoreConfigSkeleton::config | ( | ) | const |
Return the KConfig object used for reading and writing the settings.
|
signal |
This signal is emitted when the configuration change.
QString KCoreConfigSkeleton::currentGroup | ( | ) | const |
Returns the current group used for addItem() calls.
KConfigSkeletonItem * KCoreConfigSkeleton::findItem | ( | const QString & | name | ) | const |
Lookup item by name.
bool KCoreConfigSkeleton::isDefaults | ( | ) | const |
Indicates if all the registered items are set to their default value.
bool KCoreConfigSkeleton::isImmutable | ( | const QString & | name | ) | const |
Return whether a certain item is immutable.
bool KCoreConfigSkeleton::isSaveNeeded | ( | ) | const |
Indicates if any registered item has a different value than the previously loaded value.
KConfigSkeletonItem::List KCoreConfigSkeleton::items | ( | ) | const |
Return list of items managed by this KCoreConfigSkeleton object.
void KCoreConfigSkeleton::load | ( | ) |
Read preferences from config file.
All registered items are set to the values read from disk. This method calls usrRead() after reading the settings of the registered items from the KConfig. You can override usrRead() in derived classes if you have special requirements.
void KCoreConfigSkeleton::read | ( | ) |
Read preferences from the KConfig object.
This method assumes that the KConfig object was previously loaded, i.e. it uses the in-memory values from KConfig without reloading from disk.
This method calls usrRead() after reading the settings of the registered items from the KConfig. You can override usrRead() in derived classes if you have special requirements.
|
inline |
void KCoreConfigSkeleton::removeItem | ( | const QString & | name | ) |
Removes and deletes an item by name.
name | the name of the item to remove |
|
slot |
Write preferences to config file.
The values of all registered items are written to disk. This method calls usrSave() after writing the settings from the registered items to the KConfig. You can override usrSave() in derived classes if you have special requirements.
void KCoreConfigSkeleton::setCurrentGroup | ( | const QString & | group | ) |
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".
|
virtual |
Set all registered items to their default values.
This method calls usrSetDefaults() after setting the defaults for the registered items. You can override usrSetDefaults() in derived classes if you have special requirements. If you need more fine-grained control of setting the default values of the registered items you can override setDefaults() in a derived class.
void KCoreConfigSkeleton::setSharedConfig | ( | KSharedConfig::Ptr | pConfig | ) |
Set the KSharedConfig object used for reading and writing the settings.
KSharedConfig::Ptr KCoreConfigSkeleton::sharedConfig | ( | ) | const |
Return the KConfig object used for reading and writing the settings.
|
virtual |
Specify whether this object should reflect the actual values or the default values.
This method is implemented by usrUseDefaults(), which can be overridden in derived classes if you have special requirements and can call usrUseDefaults() directly. If you don't have control whether useDefaults() or usrUseDefaults() is called override useDefaults() directly.
b | true to make this object reflect the default values, false to make it reflect the actual values. |
|
protectedvirtual |
Perform the actual reading of the configuration file.
Override in derived classes to read special config values. Called from read()
|
protectedvirtual |
|
protectedvirtual |
Perform the actual writing of the configuration file.
Override in derived classes to write special config values. Called from save()
Reimplemented in KConfigLoader.
|
protectedvirtual |
Perform the actual setting of default values.
Override in derived classes to set special default values. Called from setDefaults()
|
protectedvirtual |
Implemented by subclasses that use special defaults.
It replaces the default values with the actual values and vice versa. Called from useDefaults()
b | true to make this object reflect the default values, false to make it reflect the actual values. |
|
protectedvirtual |
|
inlineslot |