示例#1
0
    def test_table_editor_select_cells(self):
        gui = GUI()
        object_list = ObjectListWithSelection(
            values=[ListItem(value=str(i**2)) for i in range(10)])
        object_list.selected_cells = [
            (object_list.values[5], "value"),
            (object_list.values[6], "other value"),
            (object_list.values[8], "value"),
        ]

        with store_exceptions_on_all_threads(), \
                create_ui(object_list, dict(view=select_cells_view)) as ui:
            editor = ui.get_editors("values")[0]
            gui.process_events()
            if is_current_backend_qt4():
                selected = editor.selected
            elif is_current_backend_wx():
                selected = editor.selected_cells

            press_ok_button(ui)
            gui.process_events()

        self.assertEqual(selected, [
            (object_list.values[5], "value"),
            (object_list.values[6], "other value"),
            (object_list.values[8], "value"),
        ])
    def test_list_str_editor_single_selection(self):
        with store_exceptions_on_all_threads():
            gui, editor = self.setup_gui(ListStrModel(), get_view())

            if is_current_backend_qt4():  # No initial selection
                self.assertEqual(editor.selected_index, -1)
                self.assertEqual(editor.selected, None)
            elif is_current_backend_wx():  # First element selected initially
                self.assertEqual(editor.selected_index, 0)
                self.assertEqual(editor.selected, "one")

            set_selected_single(editor, 1)
            gui.process_events()

            self.assertEqual(editor.selected_index, 1)
            self.assertEqual(editor.selected, "two")

            set_selected_single(editor, 2)
            gui.process_events()

            self.assertEqual(editor.selected_index, 2)
            self.assertEqual(editor.selected, "three")

            clear_selection(editor)
            gui.process_events()

            self.assertEqual(editor.selected_index, -1)
            self.assertEqual(editor.selected, None)
示例#3
0
    def test_simple_set_editor_deleted_valid_values(self):
        editor_factory = SetEditor(values=["one", "two", "three", "four"])
        view = View(UItem(
            "value",
            editor=editor_factory,
            style="simple",
        ))
        list_edit = ListModel()

        with store_exceptions_on_all_threads():
            gui, editor = self.setup_gui(list_edit, view)

            self.assertEqual(get_list_items(editor._unused), ["four", "three"])
            self.assertEqual(get_list_items(editor._used), ["one", "two"])

            editor_factory.values = ["two", "three", "four"]
            gui.process_events()

            self.assertEqual(get_list_items(editor._unused), ["four", "three"])
            # FIXME issue enthought/traitsui#840
            if is_current_backend_wx():
                with self.assertRaises(AssertionError):
                    self.assertEqual(get_list_items(editor._used), ["two"])
                self.assertEqual(get_list_items(editor._used), ["one", "two"])
            else:
                self.assertEqual(get_list_items(editor._used), ["two"])
            self.assertEqual(list_edit.value, ["two"])
示例#4
0
def get_button_text(button):
    """ Return the button text given a button control """
    if is_current_backend_wx():
        return button.GetLabel()

    elif is_current_backend_qt4():
        return button.text()
示例#5
0
def test_table_editor_select_cells():
    gui = GUI()
    object_list = ObjectListWithSelection(
        values=[ListItem(value=str(i**2)) for i in range(10)]
    )
    object_list.selected_cells = [
        (object_list.values[5], 'value'),
        (object_list.values[6], 'other value'),
        (object_list.values[8], 'value'),
    ]

    with store_exceptions_on_all_threads():
        ui = object_list.edit_traits(view=select_cells_view)
        editor = ui.get_editors('values')[0]
        gui.process_events()
        if is_current_backend_qt4():
            selected = editor.selected
        elif is_current_backend_wx():
            selected = editor.selected_cells

        press_ok_button(ui)
        gui.process_events()

    assert selected == [
        (object_list.values[5], 'value'),
        (object_list.values[6], 'other value'),
        (object_list.values[8], 'value'),
    ]
示例#6
0
def get_button_text(button):
    """ Return the button text given a button control """
    if is_current_backend_wx():
        return button.GetLabel()

    elif is_current_backend_qt4():
        return button.text()
示例#7
0
    def test_csv_editor_external_append(self):
        # Behavior: CSV editor is notified when an element is appended to the
        # list externally

        def _wx_get_text_value(ui):
            txt_ctrl = ui.control.FindWindowByName("text", ui.control)
            return txt_ctrl.GetValue()

        def _qt_get_text_value(ui):
            from pyface import qt

            txt_ctrl = ui.control.findChild(qt.QtGui.QLineEdit)
            return txt_ctrl.text()

        list_of_floats = ListOfFloats(data=[1.0])
        csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats)
        with store_exceptions_on_all_threads(), create_ui(csv_view) as ui:

            # add element to list, make sure that editor knows about it
            list_of_floats.data.append(3.14)

            # get current value from editor
            if is_current_backend_wx():
                value_str = _wx_get_text_value(ui)
            elif is_current_backend_qt4():
                value_str = _qt_get_text_value(ui)

            expected = csv_list_editor._format_list_str([1.0, 3.14])
            self.assertEqual(value_str, expected)

            press_ok_button(ui)
示例#8
0
    def test_radio_editor_mapping_values(self):
        # FIXME issue enthought/traitsui#842
        if is_current_backend_wx():
            import wx

            with self.assertRaises(wx._core.wxAssertionError):
                self.check_enum_mappings_value_change("custom", "radio")
        else:
            self.check_enum_mappings_value_change("custom", "radio")
示例#9
0
    def test_custom_editor_mapping_name_tuple(self):
        # FIXME issue enthought/traitsui#842
        if is_current_backend_wx():
            import wx

            with self.assertRaises(wx._core.wxAssertionError):
                self.check_checklist_mappings_tuple_name_change("custom")
        else:
            self.check_checklist_mappings_tuple_name_change("custom")
 def test_list_str_editor_refresh_editor(self):
     # Smoke test for refresh_editor/refresh_
     with store_exceptions_on_all_threads():
         gui, editor = self.setup_gui(ListStrModel(), get_view())
         if is_current_backend_qt4():
             editor.refresh_editor()
         elif is_current_backend_wx():
             editor._refresh()
         gui.process_events()
示例#11
0
def get_combobox_text(combobox):
    """ Return the text given a combobox control """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            return combobox.GetString(combobox.GetSelection())
        else:
            return combobox.GetValue()
    elif is_current_backend_qt4():
        return combobox.currentText()
示例#12
0
def get_combobox_text(combobox):
    """ Return the text given a combobox control. """
    if is_current_backend_wx():
        return combobox.GetString(combobox.GetSelection())

    elif is_current_backend_qt4():
        return combobox.currentText()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#13
0
def get_combobox_text(combobox):
    """ Return the text given a combobox control """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            return combobox.GetString(combobox.GetSelection())
        else:
            return combobox.GetValue()
    elif is_current_backend_qt4():
        return combobox.currentText()
示例#14
0
def get_list_widget_text(list_widget):
    """ Return the text of currently selected item in given list widget. """
    if is_current_backend_wx():
        selected_item_idx = list_widget.GetSelection()
        return list_widget.GetString(selected_item_idx)

    elif is_current_backend_qt4():
        return list_widget.currentItem().text()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#15
0
def get_button_control(control, button_idx):
    """ Get button control from a specified parent control given a button index.
    Assumes all sizer children (wx) or layout items (qt) are buttons.
    """
    if is_current_backend_wx():
        return control.GetSizer().GetChildren()[button_idx].GetWindow()

    elif is_current_backend_qt4():
        return control.layout().itemAt(button_idx).widget()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
    def test_list_str_editor_single_selection_changed(self):
        with store_exceptions_on_all_threads():
            gui, editor = self.setup_gui(ListStrModel(), get_view())

            if is_current_backend_qt4():  # No initial selection
                self.assertEqual(get_selected_indices(editor), [])
            elif is_current_backend_wx():  # First element selected initially
                self.assertEqual(get_selected_indices(editor), [0])

            editor.selected_index = 1
            gui.process_events()

            self.assertEqual(get_selected_indices(editor), [1])
            self.assertEqual(editor.selected, "two")

            editor.selected = "three"
            gui.process_events()

            self.assertEqual(get_selected_indices(editor), [2])
            self.assertEqual(editor.selected_index, 2)

            # Selected set to invalid value doesn't change anything
            editor.selected = "four"
            gui.process_events()

            self.assertEqual(get_selected_indices(editor), [2])
            self.assertEqual(editor.selected_index, 2)

            # Selected index changed to
            editor.selected_index = -1
            gui.process_events()

            if is_current_backend_qt4():
                # -1 clears selection
                self.assertEqual(get_selected_indices(editor), [])
                self.assertEqual(editor.selected, None)
            elif is_current_backend_wx():
                # Visually selects everything but doesn't update `selected`
                self.assertEqual(editor.selected, "four")
                self.assertEqual(get_selected_indices(editor), [0, 1, 2])
示例#17
0
def finish_combobox_text_entry(combobox):
    """ Finish text entry given combobox. """
    if is_current_backend_wx():
        import wx

        event = wx.CommandEvent(wx.EVT_TEXT_ENTER.typeId, combobox.GetId())
        wx.PostEvent(combobox, event)

    elif is_current_backend_qt4():
        combobox.lineEdit().editingFinished.emit()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#18
0
def set_list_widget_selected_index(list_widget, idx):
    """ Set the choice index given a list widget control and index number. """
    if is_current_backend_wx():
        import wx

        list_widget.SetSelection(idx)
        event = wx.CommandEvent(wx.EVT_LISTBOX.typeId, list_widget.GetId())
        wx.PostEvent(list_widget, event)

    elif is_current_backend_qt4():
        list_widget.setCurrentRow(idx)

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
def set_selected_single(editor, index):
    """ Selects a specified item in an editor with multi_select=False.
    """
    if is_current_backend_wx():
        editor.control.Select(index)

    elif is_current_backend_qt4():
        from pyface.qt.QtGui import QItemSelectionModel

        smodel = editor.list_view.selectionModel()
        mi = editor.model.index(index)
        smodel.select(mi, QItemSelectionModel.ClearAndSelect)

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#20
0
def set_text_in_line_edit(line_edit, text):
    """ Set text in text widget and complete editing. """
    if is_current_backend_wx():
        import wx

        line_edit.SetValue(text)
        event = wx.CommandEvent(wx.EVT_TEXT_ENTER.typeId, line_edit.GetId())
        wx.PostEvent(line_edit, event)

    elif is_current_backend_qt4():
        line_edit.setText(text)
        line_edit.editingFinished.emit()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#21
0
    def setup_gui(self, model, view):
        gui = GUI()
        ui = model.edit_traits(view=view)
        self.addCleanup(ui.dispose)

        gui.process_events()
        editor = ui.get_editors("value")[0]
        line_edit = editor.control

        # FIXME issue enthought/traitsui#851
        if is_current_backend_wx():
            import wx
            self.addCleanup(line_edit.Unbind, wx.EVT_KILL_FOCUS)

        return gui, editor, line_edit
示例#22
0
def set_combobox_index(combobox, idx):
    """ Set the choice index given a combobox control and index number """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            event_type = wx.EVT_CHOICE.typeId
        else:
            event_type = wx.EVT_COMBOBOX.typeId
        event = wx.CommandEvent(event_type, combobox.GetId())
        text = combobox.GetString(idx)
        event.SetString(text)
        event.SetInt(idx)
        wx.PostEvent(combobox, event)

    elif is_current_backend_qt4():
        combobox.setCurrentIndex(idx)
示例#23
0
def set_combobox_index(combobox, idx):
    """ Set the choice index given a combobox control and index number """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            event_type = wx.EVT_CHOICE.typeId
        else:
            event_type = wx.EVT_COMBOBOX.typeId
        event = wx.CommandEvent(event_type, combobox.GetId())
        text = combobox.GetString(idx)
        event.SetString(text)
        event.SetInt(idx)
        wx.PostEvent(combobox, event)

    elif is_current_backend_qt4():
        combobox.setCurrentIndex(idx)
示例#24
0
def get_list_items(list_widget):
    """ Return a list of strings from the list widget. """
    items = []

    if is_current_backend_wx():
        for i in range(list_widget.GetCount()):
            items.append(list_widget.GetString(i))

    elif is_current_backend_qt4():
        for i in range(list_widget.count()):
            items.append(list_widget.item(i).text())

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")

    return items
示例#25
0
def click_radio_button(widget, button_idx):
    """ Simulate a radio button click given widget and button number. Assumes
    all sizer children (wx) or layout items (qt) are buttons."""
    if is_current_backend_wx():
        import wx

        sizer_items = widget.GetSizer().GetChildren()
        button = sizer_items[button_idx].GetWindow()
        event = wx.CommandEvent(wx.EVT_RADIOBUTTON.typeId, button.GetId())
        event.SetEventObject(button)
        wx.PostEvent(widget, event)

    elif is_current_backend_qt4():
        widget.layout().itemAt(button_idx).widget().click()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#26
0
def click_on_image(image_control):
    """ Click on the image controlled by given image_control."""
    if is_current_backend_wx():
        import wx

        event_down = wx.MouseEvent(wx.EVT_LEFT_DOWN.typeId)
        wx.PostEvent(image_control, event_down)
        event_up = wx.MouseEvent(wx.EVT_LEFT_UP.typeId)
        event_up.SetX(0)
        event_up.SetY(0)
        wx.PostEvent(image_control, event_up)

    elif is_current_backend_qt4():
        image_control.click()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#27
0
def clear_selection(editor):
    """ Clears existing selection.
    """
    if is_current_backend_wx():
        import wx

        currently_selected = get_selected_rows(editor)
        # Deselect all currently selected items
        for selected_row in currently_selected:
            editor.control.SetItemState(selected_row, 0,
                                        wx.LIST_STATE_SELECTED)

    elif is_current_backend_qt4():
        editor.control.selectionModel().clearSelection()

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#28
0
def set_selected_single(editor, row):
    """ Selects a specified row in an editor with multi_select=False.
    """
    if is_current_backend_wx():
        editor.control.Select(row)

    elif is_current_backend_qt4():
        from pyface.qt.QtGui import QItemSelectionModel

        smodel = editor.control.selectionModel()
        mi = editor.model.index(row, 0)
        # Add `Rows` flag to select the whole row
        smodel.select(
            mi, QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows)

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#29
0
def set_combobox_text(combobox, text):
    """ Set the text given a combobox control """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            event_type = wx.EVT_CHOICE.typeId
            event = wx.CommandEvent(event_type, combobox.GetId())
            event.SetString(text)
            wx.PostEvent(combobox, event)
        else:
            combobox.SetValue(text)
            event_type = wx.EVT_COMBOBOX.typeId
            event = wx.CommandEvent(event_type, combobox.GetId())
            event.SetString(text)
            wx.PostEvent(combobox, event)

    elif is_current_backend_qt4():
        combobox.setEditText(text)
示例#30
0
def set_combobox_text(combobox, text):
    """ Set the text given a combobox control """
    if is_current_backend_wx():
        import wx
        if isinstance(combobox, wx.Choice):
            event_type = wx.EVT_CHOICE.typeId
            event = wx.CommandEvent(event_type, combobox.GetId())
            event.SetString(text)
            wx.PostEvent(combobox, event)
        else:
            combobox.SetValue(text)
            event_type = wx.EVT_COMBOBOX.typeId
            event = wx.CommandEvent(event_type, combobox.GetId())
            event.SetString(text)
            wx.PostEvent(combobox, event)

    elif is_current_backend_qt4():
        combobox.setEditText(text)
示例#31
0
    def test_table_editor_select_row_index(self):
        gui = GUI()
        object_list = ObjectListWithSelection(
            values=[ListItem(value=str(i**2)) for i in range(10)])
        object_list.selected_index = 5

        with store_exceptions_on_all_threads(), \
                create_ui(object_list, dict(view=select_row_index_view)) as ui:
            editor = ui.get_editors("values")[0]
            gui.process_events()
            if is_current_backend_qt4():
                selected = editor.selected_indices
            elif is_current_backend_wx():
                selected = editor.selected_row_index

            press_ok_button(ui)
            gui.process_events()

        self.assertEqual(selected, 5)
def right_click_item(control, index):
    """ Right clicks on the specified item.
    """

    if is_current_backend_wx():
        import wx

        event = wx.ListEvent(
            wx.EVT_LIST_ITEM_RIGHT_CLICK.typeId, control.GetId()
        )
        event.SetIndex(index)
        wx.PostEvent(control, event)

    elif is_current_backend_qt4():
        # Couldn't figure out how to close the context menu programatically
        raise unittest.SkipTest("Test not implemented for this toolkit")

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#33
0
def test_table_editor_select_cell_indices():
    gui = GUI()
    object_list = ObjectListWithSelection(
        values=[ListItem(value=str(i**2)) for i in range(10)])
    object_list.selected_cell_indices = [(5, 0), (6, 1), (8, 0)]

    with store_exceptions_on_all_threads():
        ui = object_list.edit_traits(view=select_cell_indices_view)
        editor = ui.get_editors("values")[0]
        gui.process_events()
        if is_current_backend_qt4():
            selected = editor.selected_indices
        elif is_current_backend_wx():
            selected = editor.selected_cell_indices

        press_ok_button(ui)
        gui.process_events()

    assert selected == [(5, 0), (6, 1), (8, 0)]
示例#34
0
def double_click_on_item(editor, item_idx, in_used=False):
    """ Simulate a double click on an item in a specified list.

    The function deselects all items in both used and unused lists, then
    selects an item at index item_idx either in the used list (if
    in_used=True) or in the unused list. Finally the function simulates a
    double click on the selected item.
    """
    unused_list = editor._unused
    used_list = editor._used

    if is_current_backend_wx():
        import wx

        # First deselect all items
        for i in range(unused_list.GetCount()):
            unused_list.Deselect(i)
        for i in range(used_list.GetCount()):
            used_list.Deselect(i)
        # Select the item in the correct list
        list_with_selection = used_list if in_used else unused_list
        list_with_selection.SetSelection(item_idx)

        event = wx.CommandEvent(wx.EVT_LISTBOX_DCLICK.typeId,
                                list_with_selection.GetId())
        wx.PostEvent(editor.control, event)

    elif is_current_backend_qt4():
        for i in range(unused_list.count()):
            status = (not in_used) and (item_idx == i)
            unused_list.item(i).setSelected(status)

        for i in range(used_list.count()):
            status = (in_used) and (item_idx == i)
            used_list.item(i).setSelected(status)

        if in_used:
            used_list.itemDoubleClicked.emit(used_list.item(item_idx))
        else:
            unused_list.itemDoubleClicked.emit(unused_list.item(item_idx))

    else:
        raise unittest.SkipTest("Test not implemented for this toolkit")
示例#35
0
def test_table_editor_select_column_index():
    gui = GUI()
    object_list = ObjectListWithSelection(
        values=[ListItem(value=str(i**2)) for i in range(10)]
    )
    object_list.selected_index = 1

    with store_exceptions_on_all_threads():
        ui = object_list.edit_traits(view=select_column_index_view)
        editor = ui.get_editors('values')[0]
        gui.process_events()
        if is_current_backend_qt4():
            selected = editor.selected_indices
        elif is_current_backend_wx():
            selected = editor.selected_column_index

        press_ok_button(ui)
        gui.process_events()

    assert selected == 1