napari.utils.events.Selection#

class napari.utils.events.Selection(data: Iterable[_T] = ())[source]#

Bases: EventedSet[_T]

A model of selected items, with active and current item.

There can only be one active and one current item, but there can be multiple selected items. An “active” item is defined as a single selected item (if multiple items are selected, there is no active item). The “current” item is mostly useful for (e.g.) keyboard actions: even with multiple items selected, you may only have one current item, and keyboard events (like up and down) can modify that current item. It’s possible to have a current item without an active item, but an active item will always be the current item.

An item can be the current item and selected at the same time. Qt views will ensure that there is always a current item as keyboard navigation, for example, requires a current item.

This pattern mimics current/selected items from Qt: https://doc.qt.io/qt-5/model-view-programming.html#current-item-and-selected-items

Parameters:

data (iterable, optional) – Elements to initialize the set with.

active#

The active item, if any. An active item is the one being edited.

Type:

Any, optional

_current#

The current item, if any. This is used primarily by GUI views when handling mouse/key events.

Type:

Any, optional

Events:
  • changed (added (Set[_T], removed: Set[_T])) – Emitted when the set changes, includes item(s) that have been added and/or removed from the set.

  • active (value (_T)) – emitted when the current item has changed.

  • _current (value (_T)) – emitted when the current item has changed. (Private event)

Methods

add(value)

Add an element to the set, if not already present.

clear([keep_current])

Clear the selection.

copy()

Return a shallow copy of this set.

difference([others])

Return set of all elements that are in this set but not other.

difference_update([others])

Remove all elements of another set from this set.

discard(value)

Remove an element from a set if it is a member.

intersection([others])

Return all elements that are in both sets as a new set.

intersection_update([others])

Remove all elements of in this set that are not present in other.

isdisjoint(other)

Return True if two sets have a null intersection.

issubset(others)

Returns whether another set contains this set or not

issuperset(others)

Returns whether this set contains another set or not

pop()

Return the popped value.

remove(value)

Remove an element.

select_only(obj)

Unselect everything but obj.

symmetric_difference(others)

Returns set of elements that are in exactly one of the sets

symmetric_difference_update(others)

Update set to the symmetric difference of itself and another.

toggle(obj)

Toggle selection state of obj.

union([others])

Return a set containing the union of sets

update([others])

Update this set with the union of this set and others

validate(v, field)

Pydantic validator.

Attributes

active

Return the currently active item or None.

Details

property active: _T | None#

Return the currently active item or None.

add(value: _T) None#

Add an element to the set, if not already present.

clear(keep_current: bool = False) None[source]#

Clear the selection.

copy() EventedSet[_T]#

Return a shallow copy of this set.

difference(others: Iterable[_T] = ()) EventedSet[_T]#

Return set of all elements that are in this set but not other.

difference_update(others: Iterable[_T] = ()) None#

Remove all elements of another set from this set.

discard(value: _T) None#

Remove an element from a set if it is a member.

If the element is not a member, do nothing.

intersection(others: Iterable[_T] = ()) EventedSet[_T]#

Return all elements that are in both sets as a new set.

intersection_update(others: Iterable[_T] = ()) None#

Remove all elements of in this set that are not present in other.

isdisjoint(other)#

Return True if two sets have a null intersection.

issubset(others: Iterable[_T]) bool#

Returns whether another set contains this set or not

issuperset(others: Iterable[_T]) bool#

Returns whether this set contains another set or not

pop()#

Return the popped value. Raise KeyError if empty.

remove(value)#

Remove an element. If not a member, raise a KeyError.

select_only(obj: _T) None[source]#

Unselect everything but obj. Add to selection if not present.

symmetric_difference(others: Iterable[_T]) EventedSet[_T]#

Returns set of elements that are in exactly one of the sets

symmetric_difference_update(others: Iterable[_T]) None#

Update set to the symmetric difference of itself and another.

This will remove any items in this set that are also in other, and add any items in others that are not present in this set.

toggle(obj: _T) None[source]#

Toggle selection state of obj.

union(others: Iterable[_T] = ()) EventedSet[_T]#

Return a set containing the union of sets

update(others: Iterable[_T] = ()) None#

Update this set with the union of this set and others

classmethod validate(v: Selection | dict, field: ModelField) Selection[source]#

Pydantic validator.