schrodinger.ui.qt.appframework2.settings module

class schrodinger.ui.qt.appframework2.settings.SettingsMixin(*args, **kwargs)

Bases: object

Mixin allows an object to save/restore its own state to/from a dictionary. A typical use case would be to collect the values of all the widgets in an options dialog as a dictionary. Example:

dlg = MyOptionsDialog()
saved_settings = dlg.getSettings()
dlg.applySettings(new_settings)

The settings are stored in a dictionary by each widget’s variable name. For widgets that are referenced from within another object, a nested dictionary will be created, the most common example of this being the panel.ui object. A typical settings dictionary may look like this:

{
    'ui': {
            'option_combo': 'Option A',
            'name_le': 'John',
            'remove_duplicate_checkbox': False,
            'num_copies_spinbox': 0
           },
    'other_name_le': 'Job 3'
}

Notice that dictionary keys are the string variable names, not the widgets themselves, and that panel.ui has its own sub-dictionary.

This mixin also supports the concept of aliases, which is a convenient way of accessing objects by a string or other identifier. Example:

self.setAlias(self.ui.my_input_asl_le, 'ASL')
# This is a shortcut for self.getAliasedSetting('ASL')
old_asl = self['ASL']
# This is a shortcut for self.setAliasedSetting('ASL')
self['ASL'] = new_asl

All the information about how to get values from the supported types is found in getObjValue() and setObjValue(). To extend this functionality for more types, either edit these two methods here or override these methods in the derived class, being sure to call the parent method in the else clause after testing for all new types. Always extend both the set and get functionality together.

You can also add support for this mixin to any class by implementing af2SettingsGetValue() and af2SettingsSetValue(). In this way, more complicated widgets or other objects can be automatically discovered.

__init__(*args, **kwargs)
setAlias(alias, obj, persistent=False)

Sets an alias to conveniently access an object.

Parameters
  • alias (hashable) – any hashable, but typically a string name

  • obj (object) – the actual object to be referenced

  • persistent (bool) – whether to make the setting persistent

setAliases(alias_dict, persistent=False)

Sets multiple aliases at once. Already used aliases are overwritten; other existing aliases are not affected.

Parameters
  • alias_dict (dict) – map of aliases to objects

  • persistent (bool) – whether to make the settings persistent

getAliasedSettings()
applyAliasedSettings(settings)

Applies any aliased settings with new values from the dictionary. Any aliases not present in the settings dictionary will be left unchanged.

Parameters

settings (dict) – a dictionary mapping aliases to new values to apply

getAliasedValue(alias)
setAliasedValue(alias, value)
setPersistent(alias=None)

Set options to be persistent. Any options to be made persistent must be aliased, since the alias is used to form the preference key. If no alias is specified, all aliased settings will be made persistent.

Parameters

alias (str or None) – the alias to save, or None

getPersistenceKey(alias)

Return a unique identifier for saving/restoring a setting in the preferences. Override this method to change the key scheme (this is necessary if creating a common resource which is shared by multiple panels).

Parameters

alias (str) – the alias for which we are generating a key

savePersistentOptions()

Store all persistent options to the preferences.

loadPersistentOptions()

Load all persistent options from the preferences.

getSettings(target=None, ignore_list=None)
applySettings(settings, target=None)
getObjValue(obj)
setObjValue(obj, value)
schrodinger.ui.qt.appframework2.settings.get_settings(target, ignore_list=None)

Recursively collects all settings.

Parameters
  • target (object) – the target object from which to collect from. Defaults to self. The target is normally only used in the recursive calls.

  • ignore_list (list of objects) – list of objects to ignore. Also used in recursive calls, to prevent circular reference traversal

Returns

the settings in a dict keyed by reference name. Nested references appear as dicts within the dict.

Return type

dict

schrodinger.ui.qt.appframework2.settings.apply_settings(settings, target)

Recursively applies any settings supplied in the settings argument.

schrodinger.ui.qt.appframework2.settings.get_obj_value(obj)

A generic function for getting the “value” of any supported object. This includes various types of QWidgets, any object that implements an af2SettingsGetValue method, or a tuple consisting of getter and setter functions.

Parameters

obj (object) – the object whose value to get

schrodinger.ui.qt.appframework2.settings.set_obj_value(obj, value)

A generic function for setting the “value” of any supported object. This includes various types of QWidgets, any object that implements an af2SettingsSetValue method, or a tuple consisting of getter and setter functions.

Parameters
  • obj (object) – the object whose value to set

  • value (the type must match whatever the object is expecting) – the value to set the object to

class schrodinger.ui.qt.appframework2.settings.AttributeSettingWrapper(parent_obj, attribute_name)

Bases: object

This allows any object attribute to be treated as a setting. This is useful for mapping an alias to an attribute.

__init__(parent_obj, attribute_name)
af2SettingsGetValue()
af2SettingsSetValue(value)
class schrodinger.ui.qt.appframework2.settings.PanelState(custom_state, auto_state)

Bases: object

A simple container to hold the panel state that is collected by the SettingsPanelMixin. Formerly, the state was held in a simple 2-tuple of (custom_state, auto_state).

__init__(custom_state, auto_state)
class schrodinger.ui.qt.appframework2.settings.SettingsPanelMixin(*args, **kwargs)

Bases: schrodinger.ui.qt.appframework2.settings.SettingsMixin

__init__(*args, **kwargs)
definePanelSettings()

Override this method to define the settings for the panel. The aliased settings provide an interface for saving/restoring panel state as well as for interacting with task/job runners that need to access the panel state in a way that is agnostic to the specifics of widget names and types.

Each panel setting is defined by a tuple that specifies the mapping of alias to panel setting. An optional third element in the tuple can be used to group settings by category. This allows multiple settings to share the same alias.

Each setting can either point to a specific object (usually a qt widget), or a pair of setter/getter functions.

If the mapped object is a string, this will be interpreted by af2 as referring to an attribute on the panel, and a AttributeSettingWrapper instance will automatically be created. For example, specifying the string ‘num_atoms’ will create a mapping to self.num_atoms which will simply get and set the value of that instance member.

Custom setter and getter functions should take the form getter(), returning a value that can be encoded/decoded by JSON, and setter(value), where the type of value is the same as the return type of the getter.

Commonly used objects/widgets should be handled automatically in settings.py. It’s worth considering whether it makes more sense to use a custom setter/getter here or add support for the widget in settings.py.

Returns

a list of tuples defining the custom settings.

Return type

list of tuples. Each tuple can be of type (str, object, str) or (str, (callable, callable), str) where the final str is optional.

Custom settings tuples consists of up to three elements:

  1. alias - a string identier for the setting. Ex. “box_centroid”

  2. either:

    1. an object of a type that is supported by settings.py or

    2. the string name of an existing panel attribute (i.e. member variable), or

    3. a (getter, setter) tuple. The getter should take no arguments, and the setter should take a single value.

  3. optionally, a group identifier. This can be useful if the panel runs two different jobs that both have a parameter with the same name but that needs to map to different widgets. If a setting has a group name, it will be ignored by runners unless the runner name matches the group name.

getPanelState()

Gets the current state of the panel in the form of a serializable dict. The state consists of the settings specified in definePanelSettings() as well as the automatically harvested settings.

setPanelState(state)

Resets the panel and then sets the panel to the specified state

Parameters

state (PanelState) – the panel state to set. This object should originate from a call to getPanelState()

writePanelState(filename=None)

Write the panel state to a JSON file

Parameters

filename (str) – the JSON filename. Defaults to “panelstate.json”

loadPanelState(filename=None)

Load the panel state from a JSON file

Parameters

filename (str) – the JSON filename. Defaults to “panelstate.json”

class schrodinger.ui.qt.appframework2.settings.BaseOptionsPanel(parent=None, **kwargs)

Bases: schrodinger.ui.qt.appframework2.settings.SettingsPanelMixin, schrodinger.ui.qt.appframework2.baseapp.ValidatedPanel

A base class for options dialogs that report all settings via a dictionary. This class descends from ValidatedPanel so it supports all the same validation system, including the @af2.validator decorators.

It shares common code with af2 panels, so setting self.ui, self.title, and self.help_topic all work the same way. It uses the same startup system, so setPanelOptions, setup, setDefaults, and layOut should be used in like fashion.

Appmethods (start, write, custom) are not supported.

To use, instantiate once and keep a reference. Call run() on the instance to open the panel. When the user is done, the panel will either return the settings dictionary (if user clicks ok) or None (if the user clicks cancel).

In place of run(), you may alternatively call open(), which will show the dialog as window modal and return immediately. The settings dictionary may be retrieved using getSettings() or getAliasedSettings().

__init__(parent=None, **kwargs)
Parameters
  • stop_before (int) – Exit the constructor before specified step.

  • parent (QWidget) – Parent widget, if any.

  • in_knime (bool) – Whether we are currently running under KNIME - a mode in which input selector is hidden, optionally a custom Workspace Structure is specified, and buttom bar has OK & Cancel buttons.

  • workspace_st_file (bool) – Structure to be returned by getWorkspaceStructure() when in_knime is True.

setPanelOptions()
setup()

Along with the usual af2 setup actions (instantiating widgets and other objects, connecting signals, etc), this is the recommended place for setting aliases.

setDefaults()
reset()
layOut()
accept()
run()

Show the dialog in modal mode. After dialog is closed, return None if user cancelled, or settings if user accepted.

open()

Open the dialog in window-modal mode, without blocking. This makes it possible for user to interact with the Workspace while dialog is open.

show(self)
reject()
onDialogDismissed()

Override this method with any logic that needs to run when the dialog is dismissed.

help()
property title
schrodinger.ui.qt.appframework2.settings.get_preference_handler()

Gets the af2 global prefence handler. This handler is used for storing persistent settings via this module.

schrodinger.ui.qt.appframework2.settings.get_persistent_value(key, default=<object object>)

Loads a saved value for the given key from the preferences.

Parameters
  • key (str) – the preference key

  • default – the default value to return if the key is not found. If a default is not specified, a KeyError will be raised if the key is not found.

schrodinger.ui.qt.appframework2.settings.set_persistent_value(key, value)

Save a value to the preferences.

Parameters
  • key (str) – the preference key

  • value (any type accepted by the preferences module) – the value to store

schrodinger.ui.qt.appframework2.settings.remove_preference_key(key)

Delete a persistent keypair from the preferences.

Parameters

key (str) – the preference key to remove

schrodinger.ui.qt.appframework2.settings.generate_preference_key(obj, tag)

Automatically generates a preference key based on the given object’s class name, module name, and a string tag.

Since persistant settings are intended to be used across sessions, keys are associated with class definitions, not instances.

Parameters
  • obj (object) – the object (usually a panel) with which the value is associated

  • tag (str) – a string identifier for the piece of data being stored