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

12 8
from traitsui.qt4.enum_editor import (
13
    ListEditor,
14
    RadioEditor,
15
    SimpleEditor,
16
)
17 8
from traitsui.testing.tester import command, locator, query
18 8
from traitsui.testing.tester.common_ui_targets import _BaseSourceWithLocation
19 8
from traitsui.testing.tester.qt4 import helpers
20 8
from traitsui.testing.tester.editors.layout import column_major_to_row_major
21

22

23 8
class _IndexedListEditor(_BaseSourceWithLocation):
24
    """ Wrapper class for EnumListEditor and Index.
25
    """
26 8
    source_class = ListEditor
27 8
    locator_class = locator.Index
28 11
    handlers = [
29
        (command.MouseClick, (lambda wrapper, _: helpers.mouse_click_item_view(
30
            model=wrapper.target.source.control.model(),
31
            view=wrapper.target.source.control,
32
            index=wrapper.target.source.control.model().index(
33
                wrapper.target.location.index, 0),
34
            delay=wrapper.delay))),
35
    ]
36

37

38 8
class _IndexedRadioEditor(_BaseSourceWithLocation):
39
    """ Wrapper class for EnumRadioEditor and Index.
40
    """
41 8
    source_class = RadioEditor
42 8
    locator_class = locator.Index
43 11
    handlers = [
44
        (command.MouseClick, (lambda wrapper, _: helpers.mouse_click_qlayout(
45
            layout=wrapper.target.source.control.layout(),
46
            index=convert_index(
47
                layout=wrapper.target.source.control.layout(),
48
                index=wrapper.target.location.index,
49
                row_major=wrapper.target.source.row_major
50
            ),
51
            delay=wrapper.delay))),
52
    ]
53

54

55 8
def convert_index(layout, index, row_major):
56
    """ Helper function to convert an index for a QGridLayout so that the
57
    index counts over the grid in the correct direction.
58
    The grid is always populated in row major order. The row_major trait of a
59
    Radio Enum Editor simply changes what elements are assigned to each entry
60
    in the grid, so that when displayed, they appear in column major order.
61
    Qlayouts are indexed in the order they are populated, so to access
62
    the correct element we may need to convert a column-major based index
63
    into a row-major one.
64

65
    Parameters
66
    ----------
67
    layout : QGridLayout
68
        The layout of interest
69
    index : int
70
        the index of interest
71
    row_major : bool
72
        whether or not the grid entries are organized in row major order
73
    """
74 11
    if row_major:
75 8
        return index
76
    else:
77 8
        n = layout.count()
78 8
        num_cols = layout.columnCount()
79 8
        num_rows = layout.rowCount()
80 8
        return column_major_to_row_major(index, n, num_rows, num_cols)
81

82

83 8
class _IndexedSimpleEditor(_BaseSourceWithLocation):
84
    """ Wrapper class for Simple EnumEditor and Index.
85
    """
86 8
    source_class = SimpleEditor
87 8
    locator_class = locator.Index
88 11
    handlers = [
89
        (command.MouseClick, (lambda wrapper, _: helpers.mouse_click_combobox(
90
            combobox=wrapper.target.source.control,
91
            index=wrapper.target.location.index,
92
            delay=wrapper.delay))),
93
    ]
94

95

96 8
def radio_selected_text_handler(wrapper, interaction):
97
    """ Handler function used to query SelectedText for EnumRadioEditor.
98

99
    Parameters
100
    ----------
101
    wrapper : UIWrapper
102
        The UIWrapper containing that object with text to be displayed.
103
    interaction : query.SelectedText
104
        Unused in this function but included to match the expected format of a
105
        handler.  Should only be query.SelectedText
106
    """
107 8
    control = wrapper.target.control
108 11
    for index in range(control.layout().count()):
109 11
        if control.layout().itemAt(index).widget().isChecked():
110 8
            return control.layout().itemAt(index).widget().text()
111 5
    return None
112

113

114 8
def register(registry):
115
    """ Registry location and interaction handlers for EnumEditor.
116

117
    Parameters
118
    ----------
119
    registry : InteractionRegistry
120
    """
121 8
    _IndexedListEditor.register(registry)
122 8
    _IndexedRadioEditor.register(registry)
123 8
    _IndexedSimpleEditor.register(registry)
124

125 11
    simple_editor_text_handlers = [
126
        (command.KeyClick,
127
            (lambda wrapper, interaction: helpers.key_click_qwidget(
128
                control=wrapper.target.control,
129
                interaction=interaction,
130
                delay=wrapper.delay))),
131
        (command.KeySequence,
132
            (lambda wrapper, interaction: helpers.key_sequence_qwidget(
133
                control=wrapper.target.control,
134
                interaction=interaction,
135
                delay=wrapper.delay))),
136
        (query.DisplayedText,
137
            lambda wrapper, _: wrapper.target.control.currentText()),
138
        (query.SelectedText,
139
            lambda wrapper, _: wrapper.target.control.currentText())
140
    ]
141

142 11
    for interaction_class, handler in simple_editor_text_handlers:
143 8
        registry.register_handler(
144
            target_class=SimpleEditor,
145
            interaction_class=interaction_class,
146
            handler=handler
147
        )
148

149 8
    registry.register_handler(
150
        target_class=RadioEditor,
151
        interaction_class=query.SelectedText,
152
        handler=radio_selected_text_handler,
153
    )
154

155 11
    registry.register_handler(
156
        target_class=ListEditor,
157
        interaction_class=query.SelectedText,
158
        handler=lambda wrapper, _: wrapper.target.control.currentItem().text(),
159
    )

Read our documentation on viewing source code .

Loading