class KPrefs

Class for handling preferences settings for an application. More...

Definition#include <klineakconfig/kprefs.h>
Inherited byKLPrefs
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Methods

Public Static Methods

Protected Methods

Private Members


Detailed Description

This class provides an interface to preferences settings. Preferences items can be registered by the addItem() function corresponding to the data type of the seetting. KPrefs then handles reading and writing of config files and setting of default values.

Normally you will subclass KPrefs, add data members for the preferences settings and register the members in the constructor of the subclass.

Example:


  class MyPrefs : public KPrefs {
    public:
      MyPrefs()
      {
        setCurrentGroup("MyGroup");
        addItemBool("MySetting1",&mMyBool,false);
        addItemColor("MySetting2",&mMyColor,QColor(1,2,3));
        
        setCurrentGroup("MyOtherGroup");
        addItemFont("MySetting3",&mMyFont,QFont("helvetica",12));
      }
      
      bool mMyBool;
      QColor mMyColor;
      QFont mMyFont;
  }

It might be convenient in many cases to make this subclass of KPrefs a singleton for global access from all over the application without passing references to the KPrefs object around.

You can set all values to default values by calling setDefaults(), write the data to the configuration file by calling writeConfig() and read the data from the configuration file by calling readConfig().

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 usrSetDefaults(), usrReadConfig() and usrWriteConfig().

Internally preferences settings are stored in instances of subclasses of KPrefsItem. You can also add KPrefsItem subclasses for your own types and call the generic addItem() to register them.

See also: KPrefsItem

 KPrefs (const QString &configname=QString::null)

KPrefs

Constructor.

Parameters:
confignamename of config file. If no name is given, the default config file as returned by kapp()->config() is used.

 ~KPrefs ()

~KPrefs

[virtual]

Destructor

void  setDefaults ()

setDefaults

Set preferences to default values. All registered items are set to their default values.

void  readConfig ()

readConfig

Read preferences from config file. All registered items are set to the values read from disk.

void  writeConfig ()

writeConfig

Write preferences to config file. The values of all registered items are written to disk.

void  setCurrentGroup (const QString &group)

setCurrentGroup

[static]

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".

void  addItem (KPrefsItem *)

addItem

Register a custom KPrefsItem.

void  addItemBool (const QString &key,bool *reference, bool defaultValue=false)

addItemBool

Register an item of type bool.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemInt (const QString &key,int *reference, int defaultValue=0)

addItemInt

Register an item of type int.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemColor (const QString &key,QColor *reference, const QColor &defaultValue=QColor(128,128,128))

addItemColor

Register an item of type QColor.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemFont (const QString &key,QFont *reference, const QFont &defaultValue=QFont("helvetica",12))

addItemFont

Register an item of type QFont.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemString (const QString &key,QString *reference, const QString &defaultValue="")

addItemString

Register an item of type QString.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemStringList (const QString &key,QStringList *reference, const QStringList &defaultValue=QStringList())

addItemStringList

Register an item of type QStringList.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  addItemIntList (const QString &key,QValueList *reference, const QValueList &defaultValue=QValueList())

addItemIntList

Register an item of type QValueList.

Parameters:
keyKey used in config file.
referencePointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValueDefault value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void  usrSetDefaults ()

usrSetDefaults

[protected virtual]

Implemented by subclasses that use special defaults.

void  usrReadConfig ()

usrReadConfig

[protected virtual]

Implemented by subclasses that read special config values.

void  usrWriteConfig ()

usrWriteConfig

[protected virtual]

Implemented by subclasses that write special config values.

KConfigconfig ()

config

[protected const]

Return the KConfig object used for reading and writing the settings.

static QString * mCurrentGroup

mCurrentGroup

[private]

KConfig * mConfig

mConfig

[private]

QPtrList<KPrefsItem> mItems

mItems

[private]