File: list_editor.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (215 lines) | stat: -rw-r--r-- 6,802 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# (C) Copyright 2004-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!

""" Defines the list editor factory for the traits user interface toolkits..
"""

from traits.api import (
    Any,
    BaseTraitHandler,
    Bool,
    Callable,
    Dict,
    Enum,
    HasTraits,
    Instance,
    Int,
    Property,
    PrototypedFrom,
    Range,
    Str,
    Tuple,
)

from traitsui.editor_factory import EditorFactory
from traitsui.helper import DockStyle
from traitsui.item import Item
from traitsui.toolkit import toolkit_object
from traitsui.ui_traits import style_trait, AView
from traitsui.view import View

# -------------------------------------------------------------------------
#  Trait definitions:
# -------------------------------------------------------------------------

#: Trait whose value is a BaseTraitHandler object
handler_trait = Instance(BaseTraitHandler)

#: The visible number of rows displayed
rows_trait = Range(1, 50, 5, desc="the number of list rows to display")

#: The visible number of columns displayed
columns_trait = Range(1, 10, 1, desc="the number of list columns to display")

editor_trait = Instance(EditorFactory)


class ListEditor(EditorFactory):
    """Editor factory for list editors."""

    # -------------------------------------------------------------------------
    #  Trait definitions:
    # -------------------------------------------------------------------------

    #: The editor to use for each list item:
    editor = editor_trait

    #: Can the list be reorganized, or have items added and deleted.
    mutable = Bool(True)

    #: Should the scrollbars be displayed if the list is too long.
    scrollable = Bool(True, sync_value=True)

    #: The style of editor to use for each item:
    style = style_trait

    #: The trait handler for each list item:
    trait_handler = handler_trait

    #: The number of list rows to display:
    rows = rows_trait

    #: The number of list columns to create:
    columns = columns_trait

    #: Use a notebook for a custom view?
    use_notebook = Bool(False)

    #: Show a right-click context menu for the notebook tabs?  (Qt only)
    show_notebook_menu = Bool(False)

    #: Factory that will be called to create and add a new element to this
    #: list. If None, the default value for the trait of interest is used.
    item_factory = Callable()

    #: Tuple of positional arguments to be passed to the default factory
    #: callable when creating new elements
    item_factory_args = Tuple()

    #: Dictionary of keyword arguments to be passed to the default factory
    #: callable when creating new elements
    item_factory_kwargs = Dict()

    # -- Notebook Specific Traits ---------------------------------------------

    #: Are notebook items deletable?
    deletable = Bool(False)

    #: The extended name of the trait on each page object which should be used
    #: to determine whether or not an individual page should be deletable.
    deletable_trait = Str()

    #: FIXME: Currently, this trait is used only in the wx backend.
    #: The DockWindow graphical theme
    dock_theme = Any()

    #: FIXME: Currently, this trait is used only in the wx backend.
    #: Dock page style to use for each DockControl:
    dock_style = DockStyle

    #: Export class for each item in a notebook:
    export = Str()

    #: Name of the view to use in notebook mode:
    view = AView

    #: The type of UI to construct ('panel', 'subpanel', etc)
    ui_kind = Enum("subpanel", "panel")

    #: A factory function that can be used to define that actual object to be
    #: edited (i.e. view_object = factory( object )):
    factory = Callable()

    #: Extended name to use for each notebook page. It can be either the actual
    #: name or the name of an attribute on the object in the form:
    #: '.name[.name...]'
    page_name = Str()

    #: Name of the [object.]trait[.trait...] to synchronize notebook page
    #: selection with:
    selected = Str()

    # -------------------------------------------------------------------------
    #  Traits view definition:
    # -------------------------------------------------------------------------

    traits_view = View(
        [
            ["use_notebook{Use a notebook in a custom view}", "|[Style]"],
            [
                Item("page_name", enabled_when="object.use_notebook"),
                Item("view", enabled_when="object.use_notebook"),
                "|[Notebook options]",
            ],
            [
                Item("rows", enabled_when="not object.use_notebook"),
                "|[Number of list rows to display]<>",
            ],
        ]
    )

    # -------------------------------------------------------------------------
    #  'Editor' factory methods:
    # -------------------------------------------------------------------------

    def _get_custom_editor_class(self):
        if self.use_notebook:
            return toolkit_object("list_editor:NotebookEditor")
        return toolkit_object("list_editor:CustomEditor")


# -------------------------------------------------------------------------
#  'ListItemProxy' class:
#   This class is used to update the list editors when the object changes
#   external to the editor.
# -------------------------------------------------------------------------


class ListItemProxy(HasTraits):

    #: The list proxy:
    list = Property()

    #: The item proxies index into the original list:
    index = Int()

    #: Delegate all other traits to the original object:
    _ = PrototypedFrom("_zzz_object")

    #: Define all of the private internal use values (the funny names are an
    #: attempt to avoid name collisions with delegated trait names):
    _zzz_inited = Any()
    _zzz_object = Any()
    _zzz_name = Any()

    def __init__(self, object, name, index, trait, value):
        super().__init__()

        self._zzz_inited = False
        self._zzz_object = object
        self._zzz_name = name
        self.index = index

        if trait is not None:
            self.add_trait("value", trait)
            self.value = value

        self._zzz_inited = self.index < len(self.list)

    def _get_list(self):
        return getattr(self._zzz_object, self._zzz_name)

    def _value_changed(self, old_value, new_value):
        if self._zzz_inited:
            self.list[self.index] = new_value


# This alias is deprecated and will be removed in TraitsUI 8.
ToolkitEditorFactory = ListEditor