How to use PtSettings

The best way to learn how to use PtSettings is to first look at the demos, and then use the PtSettingsExpert to generate new units and read the source documentation. This should teach you all you need to know, and more, about how to use PtSettings.

What will it look like ?

Definition: Each object in the structure is called a settings group. It is the equivalent of a registry key, a section in an .ini file or an XML node.

Definition: A setting is a stored value that is accessed by using a published property in a settings group.

The declaration of settings groups can look like this:

Example 1. The example is based on code taken from Demo1.
  TUserSettings = class(TSettingsGroup)
  private
    fID : integer;
    fName : string;
  published
    property ID    : integer read fID    write fID;
    property Name  : string  read fName  write fName;
  end;

  TSettings = class(TSettingsGroup)
  private
    fUser : TUserSettings;
    fGUI : TGUISettings;
  public
    constructor Create(const AName: string; const AParent: TSettingsGroup = nil);
    property User : TUserSettings read fUser;
    property GUI : TGUISettings read fGUI;
    published
    ... // Settings that belong to the Settings object
  end;

The best ways to see what it should look like are to take a look at the demos or generate a settings unit.

How to load and save settings

Definition: A storage could be an .ini file, the registry, an XML document or any other type of storage you may choose to implement, that can be used to store your settings. A storage object is an object that is responsible for accessing a storage.

Storages implement the following interface :
  ISettingsStorage = interface
    procedure Load(const Group: TSettingsGroup);
    procedure Save(const Group: TSettingsGroup);
    procedure LoadAll(const Group: TSettingsGroup);
    procedure SaveAll(const Group: TSettingsGroup);
    procedure DeleteAll(const Group: TSettingsGroup);
  end;

  • Load and Save only apply to the settings group given in the parameter.
  • LoadAll and SaveAll apply to the settings group given in the parameter and will recurse to all child groups as well.
  • DeleteAll will delete the settings group (and all of its children) from the storage. The settings objects in the Delphi application will not be affected.
  • Example 2. (continues on from Example 1)
      var
        Storage: ISettingsStorage;


    1) Storage.Load( Settings );
    This will read only the Settings object from storage (and not Settings.User or Settings.GUI) :

    2) Storage.SaveAll( Settings );
    This will save all properties in Settings and sub groups including Settings.User and Settings.GUI. In other words, this will save all the settings.

    3) Storage.DeleteAll( Settings.GUI );
    This will delete the settings group Settings.GUI from the storage, but will not affect the actual settings group object Settings.GUI.

    4) Storage.DeleteAll( nil );
    This deletes the settings storage itself (ex. delete the .ini file). However to prevent tragedies, the registry storage TRegSettingsStorage does not accept nil.