sleap.gui.state

Module with object for storing and accessing gui state variables.

Each project open in the GUI will have its own instance of GuiState, as will any video player (QtVideoPlayer widget) which shows different images than in the main app GUI (e.g., QtImageDirectoryWidget used for visualizing results during training).

The state object makes it easier to separate code which updates state (e.g., sets current frame or current video) and code which updates the GUI in response to state-change.

The state object is effectively a dictionary which allows you to bind functions to keys so that the functions each get called when the value for that key changes (or is initially set).

Note that there’s no type checking, e.g., to ensure that state[“video”] is being set to a Video object. This is a potential source of bugs since callbacks connected to some key will often assume that value will always be of some specific type.

class sleap.gui.state.GuiState[source]

Class for passing persistent gui state variables.

Arbitrary variables can be set, bools can be toggled, and callbacks can be automatically triggered on variable changes.

This allows us to separate controls (which set state variables) and views (which can update themselves when the relevant state variables change).

connect(key: str, callbacks: Union[Callable, List[Callable]])[source]

Connects one or more callbacks for state variable.

Callbacks are called (triggered) whenever the state is changed, i.e., when the value for some key is set either (i) initially or (ii) to a different value than the current value.

This is analogous to connecting a function to a Qt slot.

Callback should take a single arg, which will be the current (new) value of whatever state var is triggering the callback.

emit(key: str)[source]

Trigger callbacks for state variable.

This calls each callback for the specified key, without needing to change the value of the key.

This is analogous to emitting a Qt signal.

get(key: str, default=<object object>) → Any[source]

Getter with support for default value.

increment(key: str, step: int = 1, mod: int = 1, default: int = 0)[source]

Increment numeric value for specified key.

Parameters
  • key – The key.

  • step – What to add to current value.

  • mod – Wrap value (i.e., apply modulus) if not 1.

  • default – Set value to this if there’s no current value for key.

Returns

None.

increment_in_list(key: str, value_list: list, reverse: bool = False)[source]

Advance to subsequent (or prior) value in list.

When current value for key is not found in list, the value is set to the first (or last, if reverse) item in list.

Parameters
  • key – The key.

  • value_list – List of values of any type which supports equality check.

  • reverse – Whether to use next or previous item in value list.

Returns

None.

set(key: str, value: Any)[source]

Functional version of setter (for use in lambdas).

toggle(key: str, default: bool = False)[source]

Toggle boolean value for specified key.