File: q_list_view.py

package info (click to toggle)
napari 0.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,036 kB
  • sloc: python: 112,264; xml: 72; makefile: 44; sh: 5
file content (51 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Example of using low-level `QtListView` with SelectableEventedList

:class:`napari.utils.events.SelectableEventedList` is a mutable sequence that
emits events when modified.  It also has a selection model (tracking which
items are selected).

:class:`napari._qt.containers.QtListView` adapts the `EventedList` to the
QAbstractItemModel/QAbstractItemView interface used by the QtFramework.  This
allows you to create an interactive GUI view onto a python model that stays
up to date, and can modify the python object... while maintining the python
object as the single "source of truth".
"""
import napari
from napari._qt.containers import QtListView
from napari.qt import get_qapp
from napari.utils.events import SelectableEventedList

get_qapp()


class MyObject:
    """generic object."""

    def __init__(self, name) -> None:
        self.name = name

    def __str__(self):
        return self.name


# create our evented list
root = SelectableEventedList([MyObject(x) for x in 'abcdefg'])
# create Qt view onto the list
view = QtListView(root)
# show the view
view.show()


# spy on events
root.events.reordered.connect(lambda e: print('reordered to: ', e.value))
root.selection.events.changed.connect(
    lambda e: print(
        f'selection changed.  added: {e.added}, removed: {e.removed}'
    )
)
root.selection.events._current.connect(
    lambda e: print(f'current item changed to: {e.value}')
)

if __name__ == '__main__':
    napari.run()