Skip to content

Commit

Permalink
Move format_func, format_str and invalid traits from factory to editor (
Browse files Browse the repository at this point in the history
#859)

* Move format_func and format_str from factory to editor

* Move invalid from factory to editor as invalid_trait_name
  • Loading branch information
ievacerny authored and kitchoi committed Jul 8, 2020
1 parent a7b6601 commit 8ef5755
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 53 deletions.
30 changes: 27 additions & 3 deletions traitsui/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ class Editor(HasPrivateTraits):
#: The trait the editor is editing (not its value, but the trait itself):
value_trait = Property()

#: Function to use for string formatting
format_func = Callable()

#: Format string to use for formatting (used if **format_func** is not set)
format_str = Str()

#: The extended trait name of the trait containing editor invalid state
#: status:
invalid_trait_name = Str()

#: The current editor invalid state status:
invalid = Bool(False)

Expand Down Expand Up @@ -183,7 +193,12 @@ def set_focus(self):
def string_value(self, value, format_func=None):
""" Returns the text representation of a specified object trait value.
This simply delegates to the factory's `string_value` method.
If the **format_func** attribute is set on the editor, then this method
calls that function to do the formatting. If the **format_str**
attribute is set on the editor, then this method uses that string for
formatting. If neither attribute is set, then this method just calls
the appropriate text type to format.
Sub-classes may choose to override the default implementation.
Parameters
Expand All @@ -193,7 +208,16 @@ def string_value(self, value, format_func=None):
format_func : callable or None
A function that takes a value and returns a string.
"""
return self.factory.string_value(value, format_func)
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)

def restore_prefs(self, prefs):
""" Restores saved user preference information for the editor.
Expand Down Expand Up @@ -470,7 +494,7 @@ def __init__(self, parent, **traits):
raise

# Synchronize the application invalid state status with the editor's:
self.sync_value(self.factory.invalid, "invalid", "from")
self.sync_value(self.invalid_trait_name, "invalid", "from")

# ------------------------------------------------------------------------
# private methods
Expand Down
32 changes: 12 additions & 20 deletions traitsui/editor_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def simple_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def custom_editor(self, ui, object, name, description, parent):
Expand All @@ -147,6 +150,9 @@ def custom_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def text_editor(self, ui, object, name, description, parent):
Expand All @@ -159,6 +165,9 @@ def text_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def readonly_editor(self, ui, object, name, description, parent):
Expand All @@ -171,6 +180,9 @@ def readonly_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

# -------------------------------------------------------------------------
Expand Down Expand Up @@ -199,26 +211,6 @@ def _get_toolkit_editor(cls, class_name):
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
# -------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions traitsui/editors/array_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def _one_dim_view(self, object, style, width, trait):
content = []
shape = object.shape
items = []
format_func = self.editor.factory.format_func
format_str = self.editor.factory.format_str
format_func = self.editor.format_func
format_str = self.editor.format_str
for i in range(shape[0]):
name = "f%d" % i
self.add_trait(
Expand Down Expand Up @@ -146,8 +146,8 @@ def _one_dim_view(self, object, style, width, trait):
def _two_dim_view(self, object, style, width, trait):
content = []
shape = object.shape
format_func = self.editor.factory.format_func
format_str = self.editor.factory.format_str
format_func = self.editor.format_func
format_str = self.editor.format_str
for i in range(shape[0]):
items = []
for j in range(shape[1]):
Expand Down
12 changes: 12 additions & 0 deletions traitsui/editors/csv_list_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ def simple_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def custom_editor(self, ui, object, name, description, parent):
Expand All @@ -370,6 +373,9 @@ def custom_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def text_editor(self, ui, object, name, description, parent):
Expand All @@ -383,6 +389,9 @@ def text_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)

def readonly_editor(self, ui, object, name, description, parent):
Expand All @@ -396,4 +405,7 @@ def readonly_editor(self, ui, object, name, description, parent):
object=object,
name=name,
description=description,
format_func=self.format_func,
format_str=self.format_str,
invalid_trait_name=self.invalid,
)
25 changes: 12 additions & 13 deletions traitsui/qt4/ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,25 +848,24 @@ def _add_items(self, content, outer=None):

editor_factory = ToolkitEditorFactory()

# If the item has formatting traits set them in the editor
# factory:
if item.format_func is not None:
editor_factory.format_func = item.format_func

if item.format_str != "":
editor_factory.format_str = item.format_str

# If the item has an invalid state extended trait name, set it
# in the editor factory:
if item.invalid != "":
editor_factory.invalid = item.invalid

# Create the requested type of editor from the editor factory:
factory_method = getattr(editor_factory, item.style + "_editor")
editor = factory_method(
ui, object, name, item.tooltip, None
).trait_set(item=item, object_name=item.object)

# If the item has formatting traits set them in the editor:
if item.format_func is not None:
editor.format_func = item.format_func

if item.format_str != "":
editor.format_str = item.format_str

# If the item has an invalid state extended trait name, set it
# in the editor:
if item.invalid != "":
editor.invalid_trait_name = item.invalid

# Tell the editor to actually build the editing widget. Note that
# "inner" is a layout. This shouldn't matter as individual editors
# shouldn't be using it as a parent anyway. The important thing is
Expand Down
25 changes: 12 additions & 13 deletions traitsui/wx/ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,19 +883,6 @@ def add_items(self, content, panel, sizer):

editor_factory = ToolkitEditorFactory()

# If the item has formatting traits set them in the editor
# factory:
if item.format_func is not None:
editor_factory.format_func = item.format_func

if item.format_str != "":
editor_factory.format_str = item.format_str

# If the item has an invalid state extended trait name, set it
# in the editor factory:
if item.invalid != "":
editor_factory.invalid = item.invalid

# Set up the background image (if used):
item_panel = panel

Expand All @@ -905,6 +892,18 @@ def add_items(self, content, panel, sizer):
ui, object, name, item.tooltip, item_panel
).trait_set(item=item, object_name=item.object)

# If the item has formatting traits set them in the editor:
if item.format_func is not None:
editor.format_func = item.format_func

if item.format_str != "":
editor.format_str = item.format_str

# If the item has an invalid state extended trait name, set it
# in the editor:
if item.invalid != "":
editor.invalid_trait_name = item.invalid

# Tell editor to actually build the editing widget:
editor.prepare(item_panel)

Expand Down

0 comments on commit 8ef5755

Please sign in to comment.