def mouse_click_item_view(model, view, index, delay): """ Perform mouse click on the given QAbstractItemModel (model) and QAbstractItemView (view) with the given row and column. Parameters ---------- model : QAbstractItemModel Model from which QModelIndex will be obtained view : QAbstractItemView View from which the widget identified by the index will be found and key sequence be performed. index : QModelIndex Raises ------ LookupError If the index cannot be located. Note that the index error provides more """ check_q_model_index_valid(index) rect = view.visualRect(index) QTest.mouseClick( view.viewport(), QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, rect.center(), delay=delay, )
def key_click(widget, key, delay): """ Performs a key click of the given key on the given widget after a delay. Parameters ---------- widget : Qwidget The Qt widget to be key clicked. key : str Standardized (pyface) name for a keyboard event. e.g. "Enter", "Tab", "Space", "0", "1", "A", ... Note: modifiers (e.g. Shift, Alt, etc. are not currently supported) delay : int Time delay (in ms) in which the key click will be performed. """ 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.keyClick( widget, mapping[key], QtCore.Qt.NoModifier, delay=delay, )
def mouse_click_qwidget(control, delay): """Performs a mouce click on a Qt widget. Parameters ---------- control : Qwidget The Qt widget to be clicked. delay : int Time delay (in ms) in which click will be performed. """ # for QAbstractButtons we do not use QTest.mouseClick as it assumes the # center of the widget as the location to be clicked, which may be # incorrect. For QAbstractButtons we can simply call their click method. if isinstance(control, QtGui.QAbstractButton): if delay > 0: QTest.qSleep(delay) control.click() elif control is not None: QTest.mouseClick( control, QtCore.Qt.MouseButton.LeftButton, delay=delay, ) else: raise ValueError("control is None")
def mouse_click_tab_index(tab_widget, index, delay): """ Performs a mouse click on a tab at an index in a QtGui.QTabWidget. Parameters ---------- tab_widget : QtGui.QTabWidget The tab widget containing the tab to be clicked. index : int The index of the tab to be clicked. delay : int Time delay (in ms) in which click will be performed. Raises ------ IndexError If the index is out of range. """ if not 0 <= index < tab_widget.count(): raise IndexError(index) tabbar = tab_widget.tabBar() rect = tabbar.tabRect(index) QTest.mouseClick( tabbar, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, rect.center(), delay=delay, )
def right_click_center(editor): """Right click the middle of the widget.""" if is_wx(): import wx control = editor.control event = wx.ListEvent( wx.EVT_LIST_ITEM_RIGHT_CLICK.typeId, control.GetId() ) wx.PostEvent(control, event) elif is_qt(): from pyface.qt import QtCore from pyface.qt.QtTest import QTest view = editor.list_view rect = view.rect() QTest.mouseClick( view.viewport(), QtCore.Qt.MouseButton.RightButton, QtCore.Qt.KeyboardModifier.NoModifier, rect.center(), ) else: raise unittest.SkipTest("Test not implemented for this toolkit")
def key_sequence_item_view(model, view, index, sequence, delay=0): """ Perform Key Sequence on the given QAbstractItemModel (model) and QAbstractItemView (view) with the given row and column. Parameters ---------- model : QAbstractItemModel Model from which QModelIndex will be obtained view : QAbstractItemView View from which the widget identified by the index will be found and key sequence be performed. index : QModelIndex sequence : str Sequence of characters to be inserted to the widget identifed by the row and column. Raises ------ Disabled If the widget cannot be edited. LookupError If the index cannot be located. Note that the index error provides more """ check_q_model_index_valid(index) widget = view.indexWidget(index) if widget is None: raise Disabled( "No editable widget for item at row {!r} and column {!r}".format( index.row(), index.column())) QTest.keyClicks(widget, sequence, delay=delay)
def qt_mouse_click_web_view(view, delay): """ Perform a mouse click at the center of the web view. Note that the page is allowed time to load before and after the mouse click. Parameters ---------- view : QWebView or QWebEngineView """ from pyface.qt import QtCore from pyface.qt.QtTest import QTest qt_allow_view_to_load(view) if view.focusProxy() is not None: # QWebEngineView widget = view.focusProxy() else: # QWebView widget = view try: QTest.mouseClick( widget, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, delay=delay, ) finally: qt_allow_view_to_load(view)
def right_click_item(editor, index): """Right clicks on the specified item.""" if is_wx(): import wx control = editor.control event = wx.ListEvent( wx.EVT_LIST_ITEM_RIGHT_CLICK.typeId, control.GetId() ) event.SetIndex(index) wx.PostEvent(control, event) elif is_qt(): from pyface.qt import QtCore from pyface.qt.QtTest import QTest view = editor.list_view q_model_index = view.model().index(index, 0) view.scrollTo(q_model_index) rect = view.visualRect(q_model_index) QTest.mouseClick( view.viewport(), QtCore.Qt.MouseButton.RightButton, QtCore.Qt.KeyboardModifier.NoModifier, rect.center(), ) else: raise unittest.SkipTest("Test not implemented for this toolkit")
def click_key_seq(widget, key_seq): if not isinstance(key_seq, QtGui.QKeySequence): key_seq = QtGui.QKeySequence(key_seq) try: # QKeySequence on python3-pyside does not have `len` first_key = key_seq[0] except IndexError: return False key = QtCore.Qt.Key(first_key & ~QtCore.Qt.KeyboardModifierMask) modifier = QtCore.Qt.KeyboardModifier(first_key & QtCore.Qt.KeyboardModifierMask) QTest.keyClick(widget, key, modifier) return True
def click_key_seq(widget, key_seq): if not isinstance(key_seq, QtGui.QKeySequence): key_seq = QtGui.QKeySequence(key_seq) try: # QKeySequence on python3-pyside does not have `len` first_key = key_seq[0] except IndexError: return False key = QtCore.Qt.Key(first_key & ~QtCore.Qt.KeyboardModifierMask) modifier = QtCore.Qt.KeyboardModifier( first_key & QtCore.Qt.KeyboardModifierMask) QTest.keyClick(widget, key, modifier) return True
def mouse_click_qwidget(control, delay): """ Performs a mouce click on a Qt widget. Parameters ---------- control : Qwidget The Qt widget to be clicked. delay : int Time delay (in ms) in which click will be performed. """ QTest.mouseClick( control, QtCore.Qt.LeftButton, delay=delay, )
def mouse_click_qlayout(layout, index, delay=0): """ Performs a mouse click on a widget at an index in a QLayout. Parameters ---------- layout : Qlayout The layout containing the widget to be clicked index : int The index of the widget in the layout to be clicked """ if not 0 <= index < layout.count(): raise IndexError(index) widget = layout.itemAt(index).widget() QTest.mouseClick( widget, QtCore.Qt.LeftButton, delay=delay, )
def key_sequence_qwidget(control, interaction, delay): """ Performs simulated typing of a sequence of keys on the given widget after a delay. Parameters ---------- control : Qwidget The Qt widget to be acted on. interaction : instance of command.KeySequence The interaction object holding the sequence of key inputs to be simulated being typed delay : int Time delay (in ms) in which each key click in the sequence will be performed. """ if not control.isEnabled(): raise Disabled("{!r} is disabled.".format(control)) QTest.keyClicks(control, interaction.sequence, delay=delay)
def check(typed, expected): cursor = cw.textCursor() cursor.setPosition(0) cw.setTextCursor(cursor) QTest.keyClicks(cw, typed) self.assertEqual(cw.toPlainText(), expected)