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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# (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 abstract EditorFactory class, which represents a factory for
creating the Editor objects used in a Traits-based user interface.
"""
import logging
from traits.api import (
HasPrivateTraits,
Callable,
Str,
Bool,
Any,
Property,
)
from .toolkit import toolkit_object
logger = logging.getLogger(__name__)
# -------------------------------------------------------------------------
# 'EditorFactory' abstract base class:
# -------------------------------------------------------------------------
class EditorFactory(HasPrivateTraits):
"""Represents a factory for creating the Editor objects in a Traits-based
user interface.
"""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: Function to use for string formatting
format_func = Callable()
#: Format string to use for formatting (used if **format_func** not set).
format_str = Str()
#: Is the editor being used to create table grid cells?
is_grid_cell = Bool(False)
#: Are created editors initially enabled?
enabled = Bool(True)
#: The extended trait name of the trait containing editor invalid state
#: status:
invalid = Str()
#: Text aligment to use in most readonly editors
#: Possible values: left, right, top, bottom, just, vcenter, hcenter,
#: center
#: Example: left,vcenter
text_alignment = Str()
#: The editor class to use for 'simple' style views.
simple_editor_class = Property()
#: The editor class to use for 'custom' style views.
custom_editor_class = Property()
#: The editor class to use for 'text' style views.
text_editor_class = Property()
#: The editor class to use for 'readonly' style views.
readonly_editor_class = Property()
def __init__(self, *args, **traits):
"""Initializes the factory object."""
HasPrivateTraits.__init__(self, **traits)
self.init(*args)
def init(self):
"""Performs any initialization needed after all constructor traits
have been set.
"""
pass
def named_value(self, name, ui):
"""Returns the value of a specified extended name of the form: name or
context_object_name.name[.name...]:
"""
names = name.split(".")
if len(names) == 1:
# fixme: This will produce incorrect values if the actual Item the
# factory is being used with does not use the default object='name'
# value, and the specified 'name' does not contain a '.'. The
# solution will probably involve providing the Item as an argument,
# but it is currently not available at the time this method needs
# to be called...
names.insert(0, "object")
value = ui.context[names[0]]
for name in names[1:]:
value = getattr(value, name)
return value
# -------------------------------------------------------------------------
# Methods that generate backend toolkit-specific editors.
# -------------------------------------------------------------------------
def simple_editor(self, ui, object, name, description, parent):
"""Generates an editor using the "simple" style."""
return self.simple_editor_class(
parent,
factory=self,
ui=ui,
object=object,
name=name,
description=description,
)
def custom_editor(self, ui, object, name, description, parent):
"""Generates an editor using the "custom" style."""
return self.custom_editor_class(
parent,
factory=self,
ui=ui,
object=object,
name=name,
description=description,
)
def text_editor(self, ui, object, name, description, parent):
"""Generates an editor using the "text" style."""
return self.text_editor_class(
parent,
factory=self,
ui=ui,
object=object,
name=name,
description=description,
)
def readonly_editor(self, ui, object, name, description, parent):
"""Generates an "editor" that is read-only."""
return self.readonly_editor_class(
parent,
factory=self,
ui=ui,
object=object,
name=name,
description=description,
)
# -------------------------------------------------------------------------
# Private methods
# -------------------------------------------------------------------------
@classmethod
def _get_toolkit_editor(cls, class_name):
"""
Returns the editor by name class_name in the backend package.
"""
editor_factory_modules = [
factory_class.__module__
for factory_class in cls.mro()
if issubclass(factory_class, EditorFactory)
]
for index, editor_module in enumerate(editor_factory_modules):
try:
editor_module_name = editor_module.split(".")[-1]
object_ref = ":".join([editor_module_name, class_name])
return toolkit_object(object_ref, True)
except RuntimeError as e:
msg = "Can't import toolkit_object '{}': {}"
logger.debug(msg.format(object_ref, e))
if index == len(editor_factory_modules) - 1:
raise e
return None
def string_value(self, value, format_func=None):
"""Returns the text representation of a specified object trait value.
If the **format_func** attribute is set on the editor factory, then
this method calls that function to do the formatting. If the
**format_str** attribute is set on the editor factory, then this
method uses that string for formatting. If neither attribute is
set, then this method just calls the appropriate text type to format.
"""
if self.format_func is not None:
return self.format_func(value)
if self.format_str != "":
return self.format_str % value
if format_func is not None:
return format_func(value)
return str(value)
# -------------------------------------------------------------------------
# Property getters
# -------------------------------------------------------------------------
def _get_simple_editor_class(self):
"""Returns the editor class to use for "simple" style views.
The default implementation tries to import the SimpleEditor class in
the editor file in the backend package, and if such a class is not to
found it returns the SimpleEditor class defined in editor_factory
module in the backend package.
"""
try:
SimpleEditor = self._get_toolkit_editor("SimpleEditor")
except Exception as e:
msg = "Can't import SimpleEditor for {}: {}"
logger.debug(msg.format(self.__class__, e))
SimpleEditor = toolkit_object("editor_factory:SimpleEditor")
return SimpleEditor
def _get_custom_editor_class(self):
"""Returns the editor class to use for "custom" style views.
The default implementation tries to import the CustomEditor class in
the editor file in the backend package, and if such a class is not to
found it returns simple_editor_class.
"""
try:
CustomEditor = self._get_toolkit_editor("CustomEditor")
except Exception as e:
msg = "Can't import CustomEditor for {}: {}"
logger.debug(msg.format(self.__class__, e))
CustomEditor = self.simple_editor_class
return CustomEditor
def _get_text_editor_class(self):
"""Returns the editor class to use for "text" style views.
The default implementation tries to import the TextEditor class in the
editor file in the backend package, and if such a class is not found
it returns the TextEditor class declared in the editor_factory module
in the backend package.
"""
try:
TextEditor = self._get_toolkit_editor("TextEditor")
except Exception as e:
msg = "Can't import TextEditor for {}: {}"
logger.debug(msg.format(self.__class__, e))
TextEditor = toolkit_object("editor_factory:TextEditor")
return TextEditor
def _get_readonly_editor_class(self):
"""Returns the editor class to use for "readonly" style views.
The default implementation tries to import the ReadonlyEditor class in
the editor file in the backend package, and if such a class is not
found it returns the ReadonlyEditor class declared in the
editor_factory module in the backend package.
"""
try:
ReadonlyEditor = self._get_toolkit_editor("ReadonlyEditor")
except Exception as e:
msg = "Can't import ReadonlyEditor for {}: {}"
logger.debug(msg.format(self.__class__, e))
ReadonlyEditor = toolkit_object("editor_factory:ReadonlyEditor")
return ReadonlyEditor
# -------------------------------------------------------------------------
# 'EditorWithListFactory' abstract base class:
# -------------------------------------------------------------------------
class EditorWithListFactory(EditorFactory):
"""Base class for factories of editors for objects that contain lists."""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: Values to enumerate (can be a list, tuple, dict, or a CTrait or
#: TraitHandler that is "mapped"):
values = Any()
#: Extended name of the trait on **object** containing the enumeration data
object = Str("object")
#: Name of the trait on 'object' containing the enumeration data
name = Str()
|