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()
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'), ]
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_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)
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)
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()
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()
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()
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")
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()
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")
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])
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")
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")
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")
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)
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)
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
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")
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")
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")
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")
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)
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)
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)]
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")
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")
def set_selected_multiple(editor, indices): """ Clears old selection and selects specified items in an editor with multi_select=True. """ if is_current_backend_wx(): clear_selection(editor) for index in indices: editor.control.Select(index) elif is_current_backend_qt4(): from pyface.qt.QtGui import QItemSelectionModel clear_selection(editor) smodel = editor.list_view.selectionModel() for index in indices: mi = editor.model.index(index) smodel.select(mi, QItemSelectionModel.Select) else: raise unittest.SkipTest("Test not implemented for this toolkit")
def test_simple_evaluate_editor_object_no_auto_set(self): view = get_evaluate_view("simple", auto_set=False) enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, combobox = self.setup_gui(enum_edit, view) self.assertEqual(enum_edit.value, "one") set_combobox_text(combobox, "two") gui.process_events() # wx modifies the value without the need to finish entry if is_current_backend_qt4(): self.assertEqual(enum_edit.value, "one") finish_combobox_text_entry(combobox) gui.process_events() self.assertEqual(enum_edit.value, "two")
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
def get_all_button_selected_status(control): """ Return a list with selected (wx) or checked (qt) button status under given control. Assumes all sizer children (wx) or layout items (qt) are buttons. """ button_status = [] if is_current_backend_wx(): for item in control.GetSizer().GetChildren(): button_status.append(item.GetWindow().Selected()) elif is_current_backend_qt4(): layout = control.layout() for i in range(layout.count()): button_status.append(layout.itemAt(i).widget().isChecked()) else: raise unittest.SkipTest("Test not implemented for this toolkit") return button_status
def set_combobox_index(editor, idx): """ Set the choice index of a combobox control given editor and index number. """ if is_current_backend_wx(): import wx choice = editor.control choice.SetSelection(idx) event = wx.CommandEvent(wx.EVT_CHOICE.typeId, choice.GetId()) event.SetString(choice.GetString(idx)) wx.PostEvent(choice, event) elif is_current_backend_qt4(): # Cannot initiate update programatically because of `activated` # event. At least check that it updates as expected when done # manually editor.update_object(idx) else: raise unittest.SkipTest("Test not implemented for this toolkit")