File: context_value.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 (127 lines) | stat: -rw-r--r-- 3,749 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
# (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 some helper classes and traits used to define 'bindable' editor
values.  These classes and associated traits are designed to simplify
writing editors by allowing the editor factory to specify a context value
instance for attributes and have the value on the editor be synchronized
with the corresponsing value rom the context.

The factory should look something like this::

    class MyEditorFactory(EditorFactory):

        #: The minimum value.
        minimum = CVInt

        #: The suffix for the data.
        suffix = CVType(Str)

The editor class needs to have traits which correspond to the context value
traits, and should be able to react to changes to them::

    class MyEditor(Editor):

        #: The minimum value.
        minimum = Int()

        #: The suffix for the data.
        suffix = Str()

This can then be used in views, with values either as constants or as
instances of :class:`ContextValue` (abbreviated as ``CV``)::

    class MyObject(HasTraits):

        #: An important value.
        my_value = Str()

        #: The minimum value.
        my_minimum = Int(10)

        traits_view = View(
            Item(
                'my_value',
                editor=MyEditorFactory(
                    minimum=CV('my_minimum'),
                    suffix='...',
                ),
            )
        )
"""


from traits.api import HasStrictTraits, Instance, Str, Int, Float, Union


class ContextValue(HasStrictTraits):
    """Defines the name of a context value that can be bound to an editor

    Resolution of the name follows the same rules as for context values in
    Item objects: if there is no dot in it then it is treated as an
    attribute of the 'object' context value, other wise the first part
    specifies the object in the context and the rest are dotted attribute
    look-ups.
    """

    #: The extended trait name of the value that can be bound to the editor
    #: (e.g. 'selection' or 'handler.selection'):
    name = Str()

    # ------------------------------------------------------------------------
    # object Interface
    # ------------------------------------------------------------------------

    def __init__(self, name):
        super().__init__(name=name)


#: Define a shorthand name for a ContextValue:
CV = ContextValue


# Trait definitions useful in defining bindable editor traits ---------------


def CVType(type, **metadata):
    """Factory that creates a union of a trait type and a ContextValue trait.

    This also sets up one-way synchronization to the editor if no
    other synchronization is specified.

    Parameters
    ----------
    type : trait type
        The trait type that is expected for constant values.
    **metadata
        Additional metadata for the trait.

    Returns
    -------
    cv_type_trait : trait
        A trait which can either hold a constant of the specified
        type or an instance of the ContextValue class.
    """
    metadata.setdefault("sync_value", "to")
    return Union(type, InstanceOfContextValue, **metadata)


#: Shorthand for an Instance of ContextValue trait.
InstanceOfContextValue = Instance(ContextValue, allow_none=False)

#: Int or Context value trait
CVInt = CVType(Int)

#: Float or Context value trait
CVFloat = CVType(Float)

#: Str or Context value trait
CVStr = CVType(Str)