enthought / traitsui
1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2005, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
#  Author: David C. Morrill
14
#  Date:   10/14/2004
15
#
16
# ------------------------------------------------------------------------------
17

18 4
""" Defines common traits used within the traits.ui package.
19
"""
20

21 4
from pyface.ui_traits import (
22
    Alignment,
23
    Border,
24
    HasBorder,
25
    HasMargin,
26
    Image,
27
    Margin,
28
    Position,
29
    convert_bitmap,
30
    convert_image,
31
)
32 4
from traits.api import (
33
    Any,
34
    Delegate,
35
    Enum,
36
    Expression,
37
    Float,
38
    HasStrictTraits,
39
    List,
40
    Range,
41
    Str,
42
    TraitError,
43
    TraitType,
44
)
45

46 4
from .helper import PrefixList, SequenceTypes
47

48
# -------------------------------------------------------------------------
49
#  Trait definitions:
50
# -------------------------------------------------------------------------
51

52
# Orientation trait:
53 4
Orientation = PrefixList(("vertical", "horizontal"))
54

55
# Styles for user interface elements:
56 4
EditorStyle = style_trait = PrefixList(
57
    ("simple", "custom", "text", "readonly"), cols=4)
58

59
# Group layout trait:
60 4
Layout = PrefixList(("normal", "split", "tabbed", "flow", "fold"))
61

62
# Trait for the default object being edited:
63 4
AnObject = Expression("object")
64

65
# The default dock style to use:
66 4
DockStyle = dock_style_trait = Enum(
67
    "fixed",
68
    "horizontal",
69
    "vertical",
70
    "tab",
71
    desc="the default docking style to use",
72
)
73

74
# The category of elements dragged out of the view:
75 4
ExportType = Str(desc="the category of elements dragged out of the view")
76

77
# Delegate a trait value to the object's **container** trait:
78 4
ContainerDelegate = container_delegate = Delegate(
79
    "container", listenable=False
80
)
81

82
# An identifier for the external help context:
83 4
HelpId = help_id_trait = Str(desc="the external help context identifier")
84

85
# A button to add to a view:
86 4
AButton = Any()
87
# AButton = Trait( '', Str, Instance( 'traitsui.menu.Action' ) )
88

89
# The set of buttons to add to the view:
90 4
Buttons = List(
91
    AButton, desc="the action buttons to add to the bottom of the view"
92
)
93

94
# View trait specified by name or instance:
95 4
AView = Any()
96
# AView = Trait( '', Str, Instance( 'traitsui.view.View' ) )
97

98
# FIXME: on AButton and AView: TraitCompound handlers with deferred-import
99
# Instance traits are just broken. The Instance trait tries to update the
100
# top-level CTrait's fast_validate table when the import is resolved. However,
101
# sometimes the CTrait gets copied for unknown reasons and the copy's
102
# fast_validate table is not updated although the TraitCompound's
103
# slow_validates table is modified.
104

105
# -------------------------------------------------------------------------
106
#  'StatusItem' class:
107
# -------------------------------------------------------------------------
108

109

110 4
class StatusItem(HasStrictTraits):
111

112
    #: The name of the trait the status information will be synched with:
113 4
    name = Str("status")
114

115
    #: The width of the status field. The possible values are:
116
    #:
117
    #:   - abs( width )  > 1.0: Width of the field in pixels = abs( width )
118
    #:   - abs( width ) <= 1.0: Relative width of the field when compared to
119
    #:                          the other relative width fields.
120 4
    width = Float(0.5)
121

122 4
    def __init__(self, value=None, **traits):
123
        """ Initializes the item object.
124
        """
125 4
        super(StatusItem, self).__init__(**traits)
126

127 4
        if value is not None:
128 4
            self.name = value
129

130

131
# -------------------------------------------------------------------------
132
#  'ViewStatus' trait:
133
# -------------------------------------------------------------------------
134

135

136 4
class ViewStatus(TraitType):
137
    """ Defines a trait whose value must be a single StatusItem instance or a
138
        list of StatusItem instances.
139
    """
140

141
    #: Define the default value for the trait:
142 4
    default_value = None
143

144
    #: A description of the type of value this trait accepts:
145 4
    info_text = (
146
        "None, a string, a single StatusItem instance, or a list or "
147
        "tuple of strings and/or StatusItem instances"
148
    )
149

150 4
    def validate(self, object, name, value):
151
        """ Validates that a specified value is valid for this trait.
152
        """
153 4
        if isinstance(value, str):
154 4
            return [StatusItem(name=value)]
155

156 4
        if isinstance(value, StatusItem):
157 0
            return [value]
158

159 4
        if value is None:
160 0
            return value
161

162 0
        result = []
163 4
        if isinstance(value, SequenceTypes):
164 4
            for item in value:
165 4
                if isinstance(item, str):
166 0
                    result.append(StatusItem(name=item))
167 4
                elif isinstance(item, StatusItem):
168 0
                    result.append(item)
169
                else:
170 0
                    break
171
            else:
172 0
                return result
173

174 0
        self.error(object, name, value)
175

176

177
# -------------------------------------------------------------------------
178
#  'ATheme' trait:
179
# -------------------------------------------------------------------------
180

181

182 4
def convert_theme(value, level=3):
183
    """ Converts a specified value to a Theme if possible.
184
    """
185 4
    if not isinstance(value, str):
186 4
        return value
187

188 4
    if (value[:1] == "@") and (value.find(":") >= 2):
189 0
        try:
190 0
            from .image.image import ImageLibrary
191

192 0
            info = ImageLibrary.image_info(value)
193 0
        except:
194 0
            info = None
195

196 4
        if info is not None:
197 0
            return info.theme
198

199 0
    from .theme import Theme
200

201 0
    return Theme(image=convert_image(value, level + 1))
202

203

204 4
class ATheme(TraitType):
205
    """ Defines a trait whose value must be a traits UI Theme or a string that
206
        can be converted to one.
207
    """
208

209
    #: Define the default value for the trait:
210 4
    default_value = None
211

212
    #: A description of the type of value this trait accepts:
213 4
    info_text = "a Theme or string that can be used to define one"
214

215 4
    def __init__(self, value=None, **metadata):
216
        """ Creates an ATheme trait.
217

218
        Parameters
219
        ----------
220
        value : string or Theme
221
            The default value for the ATheme, either a Theme object, or a
222
            string from which a Theme object can be derived.
223
        """
224 4
        super(ATheme, self).__init__(convert_theme(value), **metadata)
225

226 4
    def validate(self, object, name, value):
227
        """ Validates that a specified value is valid for this trait.
228
        """
229 0
        from .theme import Theme
230

231 4
        if value is None:
232 0
            return None
233

234 0
        new_value = convert_theme(value, 4)
235 4
        if isinstance(new_value, Theme):
236 0
            return new_value
237

238 0
        self.error(object, name, value)
239

240

241
# -------------------------------------------------------------------------------
242
#  Other trait definitions:
243
# -------------------------------------------------------------------------
244

245
# The spacing between two items:
246 4
Spacing = Range(-32, 32, 3)

Read our documentation on viewing source code .

Loading