def test_key_click_disabled(self):
        textbox = QtGui.QLineEdit()
        textbox.setEnabled(False)
        change_slot = mock.Mock()
        textbox.editingFinished.connect(change_slot)

        with self.assertRaises(Disabled):
            _interaction_helpers.key_click_qwidget(
                textbox, command.KeyClick("Enter"), 0)
        self.assertEqual(change_slot.call_count, 0)
Example #2
0
def register(registry):
    """Registry location and interaction handlers for EnumEditor.

    Parameters
    ----------
    registry : InteractionRegistry
    """
    _IndexedListEditor.register(registry)
    _IndexedRadioEditor.register(registry)
    _IndexedSimpleEditor.register(registry)

    simple_editor_text_handlers = [
        (
            KeyClick,
            (
                lambda wrapper, interaction: _interaction_helpers.key_click_qwidget(
                    control=wrapper._target.control,
                    interaction=interaction,
                    delay=wrapper.delay,
                )
            ),
        ),
        (
            KeySequence,
            (
                lambda wrapper, interaction: _interaction_helpers.key_sequence_qwidget(
                    control=wrapper._target.control,
                    interaction=interaction,
                    delay=wrapper.delay,
                )
            ),
        ),
        (
            DisplayedText,
            lambda wrapper, _: wrapper._target.control.currentText(),
        ),
        (
            SelectedText,
            lambda wrapper, _: wrapper._target.control.currentText(),
        ),
    ]

    for interaction_class, handler in simple_editor_text_handlers:
        registry.register_interaction(
            target_class=SimpleEditor,
            interaction_class=interaction_class,
            handler=handler,
        )

    registry.register_interaction(
        target_class=RadioEditor,
        interaction_class=SelectedText,
        handler=radio_selected_text_handler,
    )

    registry.register_interaction(
        target_class=ListEditor,
        interaction_class=SelectedText,
        handler=lambda wrapper, _: wrapper._target.control.currentItem().text(),
    )
def register_editable_textbox_handlers(registry, target_class, widget_getter):
    """ Register common interactions for an editable textbox (in Qt)

    Parameters
    ----------
    registry : TargetRegistry
        The registry being registered to.
    target_class : subclass of type
        The type of target being wrapped in a UIWrapper on which the
        interaction will be performed.
    widget_getter : callable(wrapper: UIWrapper) -> QWidget
        A callable to return a Qt widget for editing text, i.e. QLineEdit
        or QTextEdit.
    """
    handlers = [
        (KeySequence, (lambda wrapper, interaction: _interaction_helpers.
                       key_sequence_textbox(widget_getter(wrapper),
                                            interaction, wrapper.delay))),
        (KeyClick,
         (lambda wrapper, interaction: _interaction_helpers.key_click_qwidget(
             widget_getter(wrapper), interaction, wrapper.delay))),
        (MouseClick,
         (lambda wrapper, _: _interaction_helpers.mouse_click_qwidget(
             widget_getter(wrapper), wrapper.delay))),
        (DisplayedText, lambda wrapper, _: _interaction_helpers.
         displayed_text_qobject(widget_getter(wrapper))),
    ]
    for interaction_class, handler in handlers:
        registry.register_interaction(
            target_class=target_class,
            interaction_class=interaction_class,
            handler=handler,
        )
Example #4
0
    def test_key_click(self):
        textbox = QtGui.QLineEdit()
        change_slot = mock.Mock()
        textbox.editingFinished.connect(change_slot)

        # sanity check on editingFinished signal
        _interaction_helpers.key_sequence_qwidget(textbox,
                                                  command.KeySequence("abc"),
                                                  0)
        self.assertEqual(change_slot.call_count, 0)

        _interaction_helpers.key_click_qwidget(textbox,
                                               command.KeyClick("Enter"), 0)
        self.assertEqual(change_slot.call_count, 1)

        # test on a different Qwidget object - QtGui.QTextEdit()
        textbox = QtGui.QTextEdit()
        change_slot = mock.Mock()
        # Now "Enter" should not finish editing, but instead go to next line
        textbox.textChanged.connect(change_slot)
        _interaction_helpers.key_click_qwidget(textbox,
                                               command.KeyClick("Enter"), 0)
        self.assertEqual(change_slot.call_count, 1)
        self.assertEqual(textbox.toPlainText(), "\n")

        # for a QLabel, one can try a key click and nothing will happen
        textbox = QtGui.QLabel()
        _interaction_helpers.key_click_qwidget(textbox, command.KeyClick("A"),
                                               0)
        self.assertEqual(textbox.text(), "")
Example #5
0
    def test_key_click(self):
        textbox = QtGui.QLineEdit()
        change_slot = mock.Mock()
        textbox.editingFinished.connect(lambda: change_slot())

        # sanity check on editingFinished signal
        _interaction_helpers.key_sequence_qwidget(textbox,
                                                  command.KeySequence("abc"),
                                                  0)
        self.assertEqual(change_slot.call_count, 0)

        _interaction_helpers.key_click_qwidget(textbox,
                                               command.KeyClick("Enter"), 0)
        self.assertEqual(change_slot.call_count, 1)

        # test on a different Qwidget object - QtGui.QTextEdit()
        textbox = QtGui.QTextEdit()
        change_slot = mock.Mock()
        # Now "Enter" should not finish editing, but instead go to next line
        textbox.textChanged.connect(lambda: change_slot())
        _interaction_helpers.key_click_qwidget(textbox,
                                               command.KeyClick("Enter"), 0)
        # The textChanged event appears to be fired twice instead of once
        # on Windows/PySide6, for reasons as yet undetermined. But for our
        # purposes it's good enough that it's fired at all.
        # xref: enthought/traitsui#1895
        change_slot.assert_called()
        self.assertEqual(textbox.toPlainText(), "\n")

        # for a QLabel, one can try a key click and nothing will happen
        textbox = QtGui.QLabel()
        _interaction_helpers.key_click_qwidget(textbox, command.KeyClick("A"),
                                               0)
        self.assertEqual(textbox.text(), "")