state
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.
Classes:
| Name | Description |
|---|---|
GuiState |
Class for passing persistent gui state variables. |
GuiState
¶
Bases: object
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).
Methods:
| Name | Description |
|---|---|
__contains__ |
Does state contain key? |
__delitem__ |
Removes key from state. Doesn't trigger callbacks. |
__getitem__ |
Gets value for key, or None if no value. |
__setitem__ |
Sets value for key, triggering any callbacks bound to key. |
connect |
Connects one or more callbacks for state variable. |
emit |
Trigger callbacks for state variable. |
get |
Getter with support for default value. |
increment |
Increment numeric value for specified key. |
increment_in_list |
Advance to subsequent (or prior) value in list. |
set |
Functional version of setter (for use in lambdas). |
toggle |
Toggle boolean value for specified key. |
Source code in sleap/gui/state.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
__contains__(key)
¶
Does state contain key?
Source code in sleap/gui/state.py
63 64 65 | |
__delitem__(key)
¶
Removes key from state. Doesn't trigger callbacks.
Source code in sleap/gui/state.py
67 68 69 70 | |
__getitem__(key)
¶
Gets value for key, or None if no value.
Source code in sleap/gui/state.py
52 53 54 | |
__setitem__(key, value)
¶
Sets value for key, triggering any callbacks bound to key.
Source code in sleap/gui/state.py
56 57 58 59 60 61 | |
connect(key, callbacks)
¶
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.
Source code in sleap/gui/state.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
emit(key)
¶
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.
Source code in sleap/gui/state.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
get(key, default=NO_ARG)
¶
Getter with support for default value.
Source code in sleap/gui/state.py
72 73 74 75 76 | |
increment(key, step=1, mod=None, default=0)
¶
Increment numeric value for specified key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
GSVarType
|
The key. |
required |
step
|
int
|
What to add to current value. |
1
|
mod
|
Optional[int]
|
Wrap value (i.e., apply modulus) if not None. |
None
|
default
|
int
|
Set value to this if there's no current value for key. |
0
|
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in sleap/gui/state.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
increment_in_list(key, value_list, reverse=False)
¶
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:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
GSVarType
|
The key. |
required |
value_list
|
list
|
List of values of any type which supports equality check. |
required |
reverse
|
bool
|
Whether to use next or previous item in value list. |
False
|
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in sleap/gui/state.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
set(key, value)
¶
Functional version of setter (for use in lambdas).
Source code in sleap/gui/state.py
78 79 80 | |
toggle(key, default=False)
¶
Toggle boolean value for specified key.
Source code in sleap/gui/state.py
82 83 84 | |