Class Settings
- Namespace
- CADability
- Assembly
- CADability.dll
This class is used to give access and store information that influences global behavior of the CADability system. There is a static variable GlobalSettings. This is the only use of settings in CADability. This class behaves as a hierarchical dictionary. The keys are strings, which may have the form "mainkey.subkey" The values are objects, i.e. any kind of data. If the objects implement the IShowProperty interface they are displayed in the global setting tab of the controlcenter. There are some classes like ColorSetting, IntegerProperty, DoubleProperty, StringProperty which can be used as a setting value. If you instead simply use a double or string value the setting will be only available to programming code but not to the user in the ControlCenter.
public class Settings : IShowPropertyImpl, IShowProperty, IPropertyEntry, ISerializable, ISettingChanged, IAttributeListContainer, IDeserializationCallback, IJsonSerialize, IJsonSerializeDone
- Inheritance
-
Settings
- Implements
- Inherited Members
Constructors
Settings()
Creates an empty settings object. Sub settings are created automatically by AddSetting(string, object) with a dotted name; the deserialization also uses this constructor.
public Settings()
Settings(SerializationInfo, StreamingContext)
Constructor required by deserialization
protected Settings(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoSerializationInfo
contextStreamingContextStreamingContext
Settings(string)
Creates an empty settings object with the given resource id, which determines the label and the tooltip when the settings are shown in the control center (see resourceIdInternal).
public Settings(string ResourceID)
Parameters
ResourceIDstringThe resource id for the StringTable
Fields
myName
Internal state: the name (key) of this Settings object within the Settings object that contains it as sub settings, null for a root object like the global settings. It qualifies the names passed to SettingChangedEvent ("mainkey.subkey").
protected string myName
Field Value
Properties
ColorList
The colours (ColorList) stored in these settings under the key "ColorList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public ColorList ColorList { get; }
Property Value
DimensionStyleList
The dimension styles (DimensionStyleList) stored in these settings under the key "DimensionStyleList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public DimensionStyleList DimensionStyleList { get; }
Property Value
EntryType
Overrides EntryType, returns GroupTitle.
public override ShowPropertyEntryType EntryType { get; }
Property Value
GlobalSettings
The global settings contain many different settings or configurations for the program execution. The settings are displayed in the "global" tab of the controlcenter. User code may add or remove settings. AddSetting(string, object).
public static Settings GlobalSettings { get; set; }
Property Value
HatchStyleList
The hatch styles (HatchStyleList) stored in these settings under the key "HatchStyleList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public HatchStyleList HatchStyleList { get; }
Property Value
HelpLink
Implementation of HelpLink, returns resourceIdInternal.
public override string HelpLink { get; }
Property Value
LayerList
The layers (LayerList) stored in these settings under the key "LayerList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public LayerList LayerList { get; }
Property Value
LinePatternList
The line patterns (LinePatternList) stored in these settings under the key "LinePatternList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public LinePatternList LinePatternList { get; }
Property Value
LineWidthList
The line widths (LineWidthList) stored in these settings under the key "LineWidthList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public LineWidthList LineWidthList { get; }
Property Value
Modified
Whether the settings have been changed since they were loaded or saved. All modifying methods and the changes made in the control center set it; writing the settings resets it. The global settings are saved when the frame is disposed, regardless of this flag.
public bool Modified { get; set; }
Property Value
StyleList
The styles (StyleList) stored in these settings under the key "StyleList", or null if there is none. The global settings contain the default attribute lists, which a new Project clones as its initial lists.
public StyleList StyleList { get; }
Property Value
SubEntries
The sub entries shown below this entry when it is opened in the property page. The default implementation has none and returns an empty array.
public override IShowProperty[] SubEntries { get; }
Property Value
SubEntriesCount
The number of SubEntries, 0 for an entry that cannot be opened. The default implementation returns 0.
public override int SubEntriesCount { get; }
Property Value
Methods
AddSetting(string, object)
Adds a setting with the given name and value, replacing an existing setting of that name. A name of the form "mainkey.subkey" puts the value into the sub settings "mainkey", which are created if necessary. Values that implement ISettingChanged are observed so that their changes raise SettingChangedEvent, attribute lists (IAttributeList) get this object as their owner. Raises SettingChangedEvent with the new value and marks the settings as modified. The settings keep the order in which they were added, which is the order of display in the control center.
public void AddSetting(string Name, object Value)
Parameters
NamestringThe name (key) of the setting, dotted for sub settings
ValueobjectThe value. An object implementing IShowProperty (e.g. BooleanProperty, IntegerProperty, DoubleProperty, ColorSetting, MultipleChoiceSetting) is shown and can be edited in the control center; a plain value (bool, int, double, string) is only accessible by code
Added(IPropertyPage)
Implementation of Added(IPropertyPage): memorizes the property page in propertyPage. Unlike Added(IPropertyTreeView) it does not raise the state changed event. Override it to subscribe to events while the entry is displayed, and call the base implementation.
public override void Added(IPropertyPage propertyTreeView)
Parameters
propertyTreeViewIPropertyPage
AllowOpenCascadeMultiThreading(bool)
Obsolete, does nothing. It remains from the time when CADability used OpenCascade as its geometry kernel.
public static void AllowOpenCascadeMultiThreading(bool allow)
Parameters
allowboolNot used
ContainsSetting(string)
Checks whether a setting with the given name exists (dotted names address sub settings).
public bool ContainsSetting(string Name)
Parameters
NamestringThe name of the setting
Returns
- bool
true if the setting exists
Dispose()
Releases the global settings: if this object is the GlobalSettings, it unsubscribes from the events of its values and sets GlobalSettings to null, so that they are reloaded from the file on the next access. For other settings objects it does nothing. The frame calls it when it is disposed, after saving the settings.
public void Dispose()
GetAllKeys()
Returns the names of all settings, one entry per setting in display order; the entries of a sub settings object follow its own entry with dotted names (e.g. "Snap.Circle30"), so every entry is a valid name for GetValue(string). Dispose() uses it to unsubscribe from all settings.
public string[] GetAllKeys()
Returns
- string[]
One name per setting
GetBoolValue(object)
Converts the given object into a bool. Values of type bool can be stored in the settings in two ways: as a primitive bool and as a BooleanProperty, which additionally knows how it can be changed interactively by the user in a property grid. If the given object has another type, an InvalidCastException is thrown.
public static bool GetBoolValue(object TheValue)
Parameters
TheValueobjectAn object, usually taken from the settings
Returns
- bool
The bool value of the object
GetBoolValue(string, bool)
Returns the setting with the given name as a bool. The setting may be stored as a bool or as a BooleanProperty.
public bool GetBoolValue(string name, bool defaultValue)
Parameters
namestringThe name of the setting (dotted for sub settings)
defaultValueboolThe value to return if the setting does not exist or has another type
Returns
- bool
The value of the setting or
defaultValue
GetDoubleValue(object)
Converts an object taken from the settings into a double. A double and a DoubleProperty are accepted; any other object is cast to double, which throws an InvalidCastException when that is not possible.
public static double GetDoubleValue(object theValue)
Parameters
theValueobjectAn object, usually taken from the settings
Returns
- double
The double value of the object
GetDoubleValue(string, double)
Returns the setting with the given name as a double. The setting may be stored as a double or as a DoubleProperty.
public double GetDoubleValue(string name, double defaultValue)
Parameters
namestringThe name of the setting (dotted for sub settings)
defaultValuedoubleThe value to return if the setting does not exist or has another type
Returns
- double
The value of the setting or
defaultValue
GetIntValue(object)
Converts an object taken from the settings into an int. An int, an IntegerProperty and a MultipleChoiceSetting (the index of the current selection) are accepted; any other object is cast to int, which throws an InvalidCastException when that is not possible.
public static int GetIntValue(object TheValue)
Parameters
TheValueobjectAn object, usually taken from the settings
Returns
- int
The int value of the object
GetIntValue(string, int)
Returns the setting with the given name as an int. The setting may be stored as an int, a double (truncated), an IntegerProperty or a MultipleChoiceSetting (the index of the current selection).
public int GetIntValue(string name, int defaultValue)
Parameters
namestringThe name of the setting (dotted for sub settings)
defaultValueintThe value to return if the setting does not exist or has another type
Returns
- int
The value of the setting or
defaultValue
GetObjectData(IJsonWriteData)
Implements GetObjectData(IJsonWriteData): writes the settings as name/value pairs in display order (sub settings recursively), the resource id and the name of this settings object. Resets Modified.
public virtual void GetObjectData(IJsonWriteData data)
Parameters
dataIJsonWriteDataReceives the data
GetObjectData(SerializationInfo, StreamingContext)
Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.
public void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInforeceives the data
contextStreamingContextthe destination of the serialization
GetStringValue(string, string)
Returns the setting with the given name as a string. A StringProperty yields its text, any other existing value is converted with ToString().
public string GetStringValue(string name, string defaultValue)
Parameters
namestringThe name of the setting (dotted for sub settings)
defaultValuestringThe value to return if the setting does not exist
Returns
- string
The value of the setting or
defaultValue
GetSubSetting(string)
Returns the sub settings (the group) with the given name, e.g. "Snap" or "Printing" for groups of the global settings. Dotted names address nested groups.
public Settings GetSubSetting(string Name)
Parameters
NamestringThe name of the group
Returns
- Settings
The sub settings, or null if there is no such setting or it is not a Settings object
GetValue(string)
Returns the value of the setting with the given name. A dotted name ("mainkey.subkey") addresses a setting in the sub settings "mainkey". The value is the object as it was added: a plain value or a property object like BooleanProperty; the typed methods GetBoolValue(string, bool), GetIntValue(string, int), GetDoubleValue(string, double) and GetStringValue(string, string) unwrap it.
public object GetValue(string Name)
Parameters
NamestringThe name of the setting
Returns
- object
The value, or null if there is no such setting
LoadGlobalSettings(string)
Reads the global settings from the given JSON file and makes them the GlobalSettings, disposing the previous ones. The active language of the StringTable is set from the "Language" setting of the file before the file is read a second time, so that language dependent choices are built correctly. Exceptions (file not found, invalid content) are propagated. Normally the settings are loaded automatically when GlobalSettings is first used, which falls back to default settings if the file cannot be read and adds settings that are missing in the file; this method alone does not add them.
public static void LoadGlobalSettings(string FileName)
Parameters
FileNamestringThe name of the file to read
OnDeserialization()
Completes the deserialization (binary or JSON): builds the lookup table of the settings from the stored ordered list (or, for very old files, the list from the table), subscribes to the change events of the values, makes this object the owner of the attribute lists it contains and resets Modified. Does nothing when called a second time.
protected void OnDeserialization()
RearrangeSetting(string, string, bool)
Changes the order of the settings, which is the order of display in the control center: the setting
toMove is placed directly before or after the setting moveTo. Both names
must be direct entries of this settings object (not dotted); if one of them does not exist, nothing happens.
public void RearrangeSetting(string toMove, string moveTo, bool setBefore)
Parameters
toMovestringThe name of the setting to move
moveTostringThe name of the setting that marks the new position
setBeforebooltrue to place it before
moveTo, false to place it after
RefreshSubentries()
Discards the cached property entries of this settings object and refreshes its display in the control center, if it is shown there. Call it after adding or removing settings while the settings are displayed.
public void RefreshSubentries()
RemoveSetting(string)
Removes the setting with the given name (dotted names address sub settings). Raises SettingChangedEvent with a null value before the setting is removed. Nothing happens if there is no such setting.
public void RemoveSetting(string Name)
Parameters
NamestringThe name of the setting
Removed(IPropertyPage)
Implementation of Removed(IPropertyPage): forgets the property page. Override it to unsubscribe from events, and call the base implementation.
public override void Removed(IPropertyPage propertyTreeView)
Parameters
propertyTreeViewIPropertyPage
SaveGlobalSettings()
Writes the global settings to the global settings file (see SettingsGlobalFileName), unless neverSaveGlobalSettings is set. The frame calls it when it is disposed, so that changes made in the control center persist.
public static void SaveGlobalSettings()
SaveGlobalSettings(string)
Writes the global settings (GlobalSettings) in JSON format to the given file, overwriting an existing file. SaveGlobalSettings() uses the standard file.
public static void SaveGlobalSettings(string FileName)
Parameters
FileNamestringThe name of the file to write
SerializationDone(JsonSerialize)
Implements SerializationDone(JsonSerialize): called when all objects of the file have been read; completes the deserialization (OnDeserialization()).
public virtual void SerializationDone(JsonSerialize jsonSerialize)
Parameters
jsonSerializeJsonSerializeThe serializer
SetObjectData(IJsonReadData)
Implements SetObjectData(IJsonReadData): reads the data written by GetObjectData(IJsonWriteData) and registers for SerializationDone(JsonSerialize), where the lookup table and the event subscriptions are built.
public virtual void SetObjectData(IJsonReadData data)
Parameters
dataIJsonReadDataThe data to read from
SetValue(string, object)
Sets the value of the setting with the given name (dotted names address sub settings, which are created if necessary). A setting that does not exist is added (AddSetting(string, object)). If the existing setting is a property object (BooleanProperty, IntegerProperty, DoubleProperty, ColorSetting, MultipleChoiceSetting), the plain value (bool, int, double, Color, index of the selection) is set into that object, otherwise the value is replaced. A null value is ignored. Raises SettingChangedEvent and marks the settings as modified.
public void SetValue(string name, object newValue)
Parameters
Exceptions
- SettingsException
if the type of the new value does not fit the existing setting, or the name is invalid
ShutDown()
GlobalSettings has registered the Application.ApplicationExit event. This call saves the settings and unregisters the event.
public static void ShutDown()
Events
SettingChangedEvent
Raised when a setting has been added, removed or changed, by the methods of this class or interactively in the control center. The handler receives the name of the setting, qualified with the names of the sub settings ("mainkey.subkey"), and the new value (null when the setting was removed). CADability itself listens to this event of the GlobalSettings, e.g. the frame and the views.
public event SettingChangedDelegate SettingChangedEvent