Skip to content

Commit

Permalink
Support for RangeEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
kitchoi committed Aug 7, 2020
1 parent 5e0a9cd commit fee448d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,25 @@ def _get_factors(self):

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
from traitsui.testing.api import UITester, command
tester = UITester(delay=100)
with tester.create_ui(demo) as ui:
interactor = tester.find_by_name(ui, "max_n")
interactor.perform(command.KeyPress("Tab"))
interactor.perform(command.KeySequence("300"))
interactor.perform(command.KeyPress("Enter"))
interactor.perform(command.KeyPress("Shift-Tab"))
for _ in range(5):
interactor.perform(command.KeyPress("Right"))

interactor.perform(command.KeyPress("Tab"))
interactor.perform(command.KeySequence("100"))
interactor.perform(command.KeyPress("Enter"))
interactor.perform(command.KeyPress("Shift-Tab"))

for _ in range(5):
interactor.perform(command.KeyPress("Left"))

interactor.perform(command.KeyPress("Tab"))
interactor.perform(command.KeySequence("1"))
interactor.perform(command.KeyPress("Enter"))
35 changes: 35 additions & 0 deletions traitsui/testing/qt4/default_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TextEditor,
TreeEditor,
)
from traitsui.qt4.range_editor import SimpleSliderEditor
from traitsui.qt4.ui_panel import TabbedFoldGroupEditor
from traitsui.testing import command
from traitsui.testing import query
Expand Down Expand Up @@ -454,6 +455,28 @@ def list_editor_notebook_get_ui(interactor, action):
)


def key_sequence_simple_range_editor(interactor, action):
slider = interactor.editor.control.slider
text = interactor.editor.control.text
if text.hasFocus():
widget = text
else:
widget = slider
QTest.keyClicks(widget, action.sequence, delay=interactor.delay)


def key_press_simple_range_editor(interactor, action):
slider = interactor.editor.control.slider
text = interactor.editor.control.text
if text.hasFocus():
widget = text
else:
widget = slider
helpers.key_press(
widget, action.key, delay=interactor.delay
)


def get_default_registry():
""" Return the default registry of implementations for Qt editors.
Expand Down Expand Up @@ -659,4 +682,16 @@ def get_default_registry():
handler=validate_location_tabbed_group_editor,
)

# RangeEditor (slider)
registry.register(
editor_class=SimpleSliderEditor,
action_class=command.KeyPress,
handler=key_press_simple_range_editor,
)
registry.register(
editor_class=SimpleSliderEditor,
action_class=command.KeySequence,
handler=key_sequence_simple_range_editor,
)

return registry
24 changes: 22 additions & 2 deletions traitsui/testing/qt4/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from functools import reduce

from pyface.qt import QtCore
from pyface.qt.QtTest import QTest
from traitsui.qt4.key_event_to_name import key_map as _KEY_MAP
Expand All @@ -13,14 +15,32 @@ def get_displayed_text(widget):


def key_press(widget, key, delay=0):
if "-" in key:
*modifiers, key = key.split("-")
else:
modifiers = []

modifier_to_qt = {
"Ctrl": QtCore.Qt.ControlModifier,
"Alt": QtCore.Qt.AltModifier,
"Meta": QtCore.Qt.MetaModifier,
"Shift": QtCore.Qt.ShiftModifier,
}
qt_modifiers = [modifier_to_qt[modifier] for modifier in modifiers]
qt_modifier = reduce(
lambda x, y: x | y, qt_modifiers, QtCore.Qt.NoModifier
)

mapping = {name: event for event, name in _KEY_MAP.items()}
if key not in mapping:
raise ValueError(
"Unknown key {!r}. Expected one of these: {!r}".format(
key, sorted(mapping)
))
QTest.keyPress(
widget, mapping[key],
QTest.keyClick(
widget,
mapping[key],
qt_modifier,
delay=delay,
)

Expand Down

0 comments on commit fee448d

Please sign in to comment.