Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion traitsui/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from .item import Item

UNITTESTING = False
# Reference to an EditorFactory object
factory_trait = Instance(EditorFactory)

Expand Down Expand Up @@ -164,7 +165,8 @@ def error(self, excp):
excp : Exception
The exception which occurred.
"""
pass
if UNITTESTING:
raise excp

def set_focus(self):
"""Assigns focus to the editor's underlying toolkit widget.
Expand Down
3 changes: 3 additions & 0 deletions traitsui/editor_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class EditorFactory(HasPrivateTraits):
#: The editor class to use for 'readonly' style views.
readonly_editor_class = Property()

#: Show the error dialog when an error occur.
show_error_dialog = Bool(True)

def __init__(self, *args, **traits):
"""Initializes the factory object."""
HasPrivateTraits.__init__(self, **traits)
Expand Down
23 changes: 9 additions & 14 deletions traitsui/editors/range_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

""" Defines the range editor factory for all traits user interface toolkits.
"""
import ast
import warnings

from types import CodeType
Expand Down Expand Up @@ -130,12 +131,6 @@ def init(self, handler=None):
self.high = eval(handler._high)
else:
self.high = handler._high
else:
if (self.low is None) and (self.low_name == ""):
self.low = 0.0

if (self.high is None) and (self.high_name == ""):
self.high = 1.0

def _get_low(self):
return self._low
Expand Down Expand Up @@ -217,25 +212,25 @@ def _get_simple_editor_class(self):
The type of editor depends on the type and extent of the range being
edited:

* One end of range is unspecified: RangeTextEditor
* **mode** is specified and not 'auto': editor corresponding to
**mode**
* One end of range is unspecified: RangeTextEditor
* Floating point range with extent > 100: LargeRangeSliderEditor
* Integer range or floating point range with extent <= 100:
SimpleSliderEditor
* All other cases: SimpleSpinEditor
"""
low, high, is_float = self._low_value, self._high_value, self.is_float

if self.mode != "auto":
return toolkit_object("range_editor:SimpleEditorMap")[self.mode]

if (low is None) or (high is None):
return toolkit_object("range_editor:RangeTextEditor")

if (not is_float) and (abs(high - low) > 1000000000):
return toolkit_object("range_editor:RangeTextEditor")

if self.mode != "auto":
return toolkit_object("range_editor:SimpleEditorMap")[self.mode]

if is_float and (abs(high - low) > 100):
return toolkit_object("range_editor:LargeRangeSliderEditor")

Expand All @@ -250,21 +245,21 @@ def _get_custom_editor_class(self):
The type of editor depends on the type and extent of the range being
edited:

* One end of range is unspecified: RangeTextEditor
* **mode** is specified and not 'auto': editor corresponding to
**mode**
* One end of range is unspecified: RangeTextEditor
* Floating point range: Same as "simple" style
* Integer range with extent > 15: Same as "simple" style
* Integer range with extent <= 15: CustomEnumEditor

"""
low, high, is_float = self._low_value, self._high_value, self.is_float
if (low is None) or (high is None):
return toolkit_object("range_editor:RangeTextEditor")

if self.mode != "auto":
return toolkit_object("range_editor:CustomEditorMap")[self.mode]

if (low is None) or (high is None):
return toolkit_object("range_editor:RangeTextEditor")

if is_float or (abs(high - low) > 15):
return self.simple_editor_class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

from numpy import sqrt
from numpy.random import random

from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'wx'
from traits.api import HasTraits, Property, Array
from traitsui.api import View, Item, TabularAdapter, TabularEditor

Expand All @@ -33,7 +34,7 @@ class ArrayAdapter(TabularAdapter):

font = 'Courier 10'
alignment = 'right'
format = '%.4f'
#format = '%.4f'

index_text = Property()
index_image = Property()
Expand Down Expand Up @@ -64,20 +65,20 @@ class ShowArray(HasTraits):
adapter=ArrayAdapter(),
auto_resize=True,
# Do not allow any kind of editing of the array:
editable=False,
operations=[],
editable=True,
operations=['edit'],
drag_move=False,
),
),
title='Array Viewer',
title='Array Viewer {}'.format(ETSConfig.toolkit),
width=0.3,
height=0.8,
resizable=True,
)


# Create the demo:
demo = ShowArray(data=random((100000, 3)))
demo = ShowArray(data=random((10, 3)))

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
Expand Down
38 changes: 24 additions & 14 deletions traitsui/examples/demo/Advanced/NumPy_array_view_editor_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,50 @@
"""

from numpy.random import random
from traits.api import HasTraits, Array
from traitsui.api import View, Item
from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'wx'
from traits.api import HasTraits, Array, Button
from traitsui.api import View, Item, VGroup
from traitsui.ui_editors.array_view_editor import ArrayViewEditor

# -- ShowArray demo class -------------------------------------------------


class ShowArray(HasTraits):

button = Button('Generate new array')
data = Array

view = View(
Item(
'data',
show_label=False,
editor=ArrayViewEditor(
titles=['x', 'y', 'z'],
format='%.4f',
# Font fails with wx in OSX;
# see traitsui issue #13:
# font = 'Arial 8'
),
),
VGroup(
Item('button'),
Item(
'data',
show_label=False,
editor=ArrayViewEditor(
titles=['x'], #'y', 'z'],
format='%.4f',

# Font fails with wx in OSX;
# see traitsui issue #13:
# font = 'Arial 8'
),
)),
title='Array Viewer',
width=0.3,
height=0.8,
resizable=True,
)

def _button_fired(self):
n = len(self.data)
self.data=random((2*n+1))


# -- Run the demo ---------------------------------------------------------

# Create the demo:
demo = ShowArray(data=random((100000, 3)))
demo = ShowArray(data=random((10)))

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
Expand Down
10 changes: 9 additions & 1 deletion traitsui/examples/demo/Advanced/Statusbar_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class TextEditor(HasPrivateTraits):

# The text being edited:
text = Str()
text2 = Str()

# The current length of the text being edited:
length = Property(observe='text')
length2 = Property(observe='text2')

# The current time:
time = Str()
Expand All @@ -58,14 +60,20 @@ class TextEditor(HasPrivateTraits):
resizable=True,
statusbar=[
StatusItem(name='length', width=0.5),
StatusItem(name='length2', width=0.5),
StatusItem(name='time', width=85),
],
)

# -- Property Implementations ---------------------------------------------

def _get_length(self):
return 'Length: %d characters' % len(self.text)

self.text2 = 'Length: %d characters' % len(self.text)
return self.text

def _get_length2(self):
return 'Length2: %d characters' % len(self.text2)

# -- Default Trait Values -------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def get_text_color(self, object):
configurable=False,
auto_size=False,
selected_indices='selected_player_indices',
orientation='horizontal',
columns=[
CheckboxColumn(name='in_lineup', label='In Lineup', width=0.12),
PlayerColumn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def sub(self, object):
editable=True,
sortable=False,
auto_size=False,
orientation='horizontal',
edit_view = '',
columns=[
ObjectColumn(name='name', editable=False, width=0.28),
AffectsAverageColumn(name='at_bats', label='AB'),
Expand Down
40 changes: 35 additions & 5 deletions traitsui/examples/demo/Standard_Editors/RangeEditor_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,48 @@
.. _RangeEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.range_editor.html#traitsui.editors.range_editor.RangeEditor
"""

from traits.api import HasTraits, Range
from traits.api import HasTraits, Range as _Range
from traitsui.api import Item, Group, View


# TODO: Update traits.api.Range with the following in order to avoid the popup-error-dialog.
# TODO: Remove redefinition of Range here once the traits.api.Range is updated.
class Range(_Range):
def create_editor(self):
""" Returns the default UI editor for the trait.
"""
# fixme: Needs to support a dynamic range editor.

auto_set = self.auto_set
if auto_set is None:
auto_set = True
show_error_dialog = self.show_error_dialog
if show_error_dialog is None:
show_error_dialog = True

from traitsui.api import RangeEditor
return RangeEditor(
self,
mode=self.mode or "auto",
cols=self.cols or 3,
auto_set=auto_set,
enter_set=self.enter_set or False,
low_label=self.low or "",
high_label=self.high or "",
low_name=self._low_name,
high_name=self._high_name,
show_error_dialog=show_error_dialog
)


class RangeEditorDemo(HasTraits):
"""Defines the RangeEditor demo class."""

# Define a trait for each of four range variants:
small_int_range = Range(1, 16)
medium_int_range = Range(1, 25)
large_int_range = Range(1, 150)
float_range = Range(0.0, 150.0)
small_int_range = Range(1, 16, show_error_dialog=False)
medium_int_range = Range(1, 25, show_error_dialog=False)
large_int_range = Range(1, 150, show_error_dialog=False)
float_range = Range(0.0, 150.0, show_error_dialog=False)

# RangeEditor display for narrow integer Range traits (< 17 wide):
int_range_group1 = Group(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Employee(HasTraits):
deletable=True,
sort_model=True,
auto_size=False,
orientation='vertical',
orientation='horizontal',
edit_view=View(
Group('first_name', 'last_name', 'age', 'phone', show_border=True),
resizable=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ def test_run_demo(self):
simple_float_slider.perform(KeyClick("Page Up"))
self.assertEqual(demo.float_range, 1.000)
simple_float_text = simple_float.locate(Textbox())
for _ in range(3):
displayed = simple_float_text.inspect(DisplayedText())
for _ in range(len(displayed) - 2):
simple_float_text.perform(KeyClick("Backspace"))
simple_float_text.perform(KeyClick("5"))
simple_float_text.perform(KeyClick("Enter"))
self.assertEqual(demo.float_range, 1.5)

custom_float_slider = custom_float.locate(Slider())
# after the trait is set to 1.5 above, the active range shown by
# the LargeRangeSliderEditor for the custom style is [0,11.500]
# so a page down is now a decrement of 1.15
# the LargeRangeSliderEditor for the custom style is [0,10.00]
# so a page down is now a decrement of 1.0
custom_float_slider.perform(KeyClick("Page Down"))
self.assertEqual(round(demo.float_range, 2), 0.35)
self.assertEqual(round(demo.float_range, 2), 0.5)
custom_float_text = custom_float.locate(Textbox())
for _ in range(5):
custom_float_text.perform(KeyClick("Backspace"))
Expand Down
3 changes: 3 additions & 0 deletions traitsui/extras/_demo_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,3 +1165,6 @@ def demo(
css_filename=css_filename,
),
).configure_traits()

if __name__ == '__main__':
demo()
Loading