File: list_box.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (152 lines) | stat: -rw-r--r-- 4,628 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
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
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A simple list box widget with a model-view architecture. """

import warnings

import wx

from traits.api import Event, Instance, Int

from pyface.list_box_model import ListBoxModel
from .layout_widget import LayoutWidget


class ListBox(LayoutWidget):
    """ A simple list box widget with a model-view architecture. """

    # The model that provides the data for the list box.
    model = Instance(ListBoxModel)

    # The objects currently selected in the list.
    selection = Int(-1)

    # Events.

    # An item has been activated.
    item_activated = Event()

    # Default style.
    STYLE = wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_NEEDED_SB

    def __init__(self, parent=None, **traits):
        """ Creates a new list box. """

        create = traits.pop('create', True)

        # Base-class constructors.
        super().__init__(parent=parent, **traits)

        # Create the widget!
        if create:
            self.create()
            warnings.warn(
                "automatic widget creation is deprecated and will be removed "
                "in a future Pyface version, code should not pass the create "
                "parameter and should instead call create() explicitly",
                DeprecationWarning,
                stacklevel=2,
            )

    def create(self, parent=None):
        super().create(parent=parent)

        self._populate()

        # Listen for changes to the model.
        self.model.observe(self._on_model_changed, "list_changed")

    def dispose(self):
        self.model.observe(
            self._on_model_changed, "list_changed", remove=True
        )
        self.model.dispose()

    # ------------------------------------------------------------------------
    # 'ListBox' interface.
    # ------------------------------------------------------------------------

    def refresh(self):
        """ Refreshes the list box. """

        # For now we just clear out the entire list.
        self.control.Clear()

        # Populate the list.
        self._populate()

    # ------------------------------------------------------------------------
    # wx event handlers.
    # ------------------------------------------------------------------------

    def _on_item_selected(self, event):
        """ Called when an item in the list is selected. """

        listbox = event.GetEventObject()

        self.selection = listbox.GetSelection()

    def _on_item_activated(self, event):
        """ Called when an item in the list is activated. """

        listbox = event.GetEventObject()
        index = listbox.GetSelection()

        # Trait event notification.
        self.item_activated = index

    # ------------------------------------------------------------------------
    # Trait handlers.
    # ------------------------------------------------------------------------

    # Static ---------------------------------------------------------------

    def _selection_changed(self, index):
        """ Called when the selected item is changed. """

        if index != -1:
            self.control.SetSelection(index)

    # Dynamic -------------------------------------------------------------#

    def _on_model_changed(self, event):
        """ Called when the model has changed. """

        # For now we just clear out the entire list.
        self.refresh()

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def _create_control(self, parent):
        """ Creates the widget. """

        control = wx.ListBox(parent, -1, style=self.STYLE)

        # Wire it up!
        control.Bind(
            wx.EVT_LISTBOX, self._on_item_selected, id=self.control.GetId()
        )
        control.Bind(
            wx.EVT_LISTBOX_DCLICK,
            self._on_item_activated,
            id=self.control.GetId(),
        )

        # Populate the list.
        return control

    def _populate(self):
        """ Populates the list box. """

        for index in range(self.model.get_item_count()):
            label, item = self.model.get_item_at(index)
            self.control.Append(label, item)