Пример #1
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        if key_sequence.matches(QKeySequence.Save):
            self.save()

        return super(SaveableAdvancedCodeWidget, self).keyPressEvent(event)
Пример #2
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        if key_sequence.matches(QKeySequence.Save):
            self.save()

        return super(SaveableAdvancedCodeWidget, self).keyPressEvent(event)
Пример #3
0
    def __init__(self, parent=None):
        super(QtTermEntryWidget, self).__init__(parent)

        self._termWidget = parent
        font = QFont("Monaco")
        font.setStyleHint(font.TypeWriter, font.PreferDefault)
        self.setFont(font)

        self._lineNumber = QtTermEntryLineNumberWidget(self)
        self._highlighter = PythonHighlighter(self)

        self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
        self.updateRequest.connect(self.updateLineNumberArea)
        self.cursorPositionChanged.connect(self.highlightCurrentLine)

        self.updateLineNumberAreaWidth(0)
        self.highlightCurrentLine()

        self.executeAction = QAction('Execute Python', self)
        self.executeAction.setShortcut(QKeySequence("Ctrl+Return"))
        self.executeAction.triggered.connect(self.execute)
        self.addAction(self.executeAction)

        self.syntaxError.connect(self.displaySyntaxError)

        self.stdoutRedirect = OutputRedirect()

        self._locals = {}
Пример #4
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        if key_sequence.matches(QKeySequence.Find):
            self.enable_find()
        elif key_sequence.matches(QKeySequence.Replace):
            self.enable_replace()
        elif key_sequence.matches(QKeySequence(QtCore.Qt.Key_Escape)):
            if self.active_find_widget:
                self.find.hide()
                self.replace.hide()
                self.code.setFocus()
                self.previous_find_widget = self.active_find_widget
                self.active_find_widget = None

        return super(AdvancedCodeWidget, self).keyPressEvent(event)
Пример #5
0
    def _obtain_qshortcut(self, shortcut, widget, scope, remove=False):
        '''
        Helper method to get a QShortcut from a shortcut.
        :param str|MouseShortcut shortcut:
        '''
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        from pyvmmonitor_qt.qt.QtWidgets import QShortcut
        from pyvmmonitor_qt.qt.QtCore import Qt

        assert isinstance(
            shortcut,
            (str, QKeySequence,
             MouseShortcut)), 'Did not expect: %s' % (shortcut.__class__, )

        if shortcut.__class__ == MouseShortcut:
            cache_key = (shortcut, scope)
        else:
            key_sequence = QKeySequence(shortcut)
            cache_key = (key_sequence.toString(), scope)

        ret = self._qshortcuts.get(cache_key)
        if ret is not None:
            if remove:
                del self._qshortcuts[cache_key]
            return ret

        if remove:
            return None

        if shortcut.__class__ == MouseShortcut:
            ret = _MouseQShortcut(shortcut)
            self._qshortcuts[cache_key] = ret
            return ret

        qshortcut = QShortcut(key_sequence, widget)
        if scope != IQtCommandsManager.DEFAULT_SCOPE:
            qshortcut.setContext(Qt.WidgetWithChildrenShortcut)
        # qshortcut.setContext(Qt.WidgetShortcut)
        # qshortcut.setContext(Qt.WindowShortcut)
        # qshortcut.setContext(Qt.ApplicationShortcut)
        self._qshortcuts[cache_key] = qshortcut

        return qshortcut
Пример #6
0
    def _obtain_qshortcut(self, shortcut):
        '''
        Helper method to get a QShortcut from a shortcut.
        :param str shortcut:
        '''
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        from pyvmmonitor_qt.qt.QtWidgets import QShortcut

        if shortcut.__class__ == MouseShortcut:
            ret = _MouseQShortcut(shortcut)
            cache_key = shortcut
            self._qshortcuts[cache_key] = ret
            return cache_key, ret

        key_sequence = QKeySequence(shortcut)
        cache_key = key_sequence.toString()
        qshortcut = self._qshortcuts.get(cache_key)
        if qshortcut is None:
            widget = self._widget()
            qshortcut = QShortcut(key_sequence, widget)
            self._qshortcuts[cache_key] = qshortcut
        return cache_key, qshortcut
Пример #7
0
def test_shortcuts_in_app(qtapi, _shortcuts_main_window):
    from pyvmmonitor_qt.commands.qt_commands_manager import create_default_qt_commands_manager
    from pyvmmonitor_qt.qt.QtCore import Qt
    from pyvmmonitor_qt.qt.QtGui import QKeySequence
    from pyvmmonitor_qt.qt_event_loop import process_events
    try:
        main_window = _shortcuts_main_window()
        main_window.show()
        process_events()

        activated = []

        def on_copy():
            activated.append('on_copy')

        qt_commands_manager = create_default_qt_commands_manager(main_window)
        # : :type qt_commands_manager: _DefaultQtShortcutsManager

        qt_commands_manager.register_command('copy', 'Copy')
        qt_commands_manager.set_command_handler('copy', on_copy)

        qt_commands_manager.set_shortcut('copy', QKeySequence('Ctrl+C'))
        qtapi.keyPress(main_window, 'c', Qt.KeyboardModifier.ControlModifier)

        assert activated == ['on_copy']
        del activated[:]

        qt_commands_manager.remove_shortcut('copy', 'Ctrl+C')
        qtapi.keyPress(main_window, 'c', Qt.KeyboardModifier.ControlModifier)
        assert activated == []

        qt_commands_manager.set_shortcut('copy', 'Ctrl+D')
        qtapi.keyPress(main_window, 'd', Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_copy']
        del activated[:]

        # Both active (Ctrl+C, Ctrl+D)
        qt_commands_manager.set_shortcut('copy', 'Ctrl+C')
        qtapi.keyPress(main_window, 'd', Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_copy']
        del activated[:]
        qtapi.keyPress(main_window, 'c', Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_copy']
        del activated[:]

        qt_commands_manager.remove_shortcut('copy', 'Ctrl+C')
        qtapi.keyPress(main_window, 'c', Qt.KeyboardModifier.ControlModifier)
        assert activated == []
    finally:
        main_window = None
Пример #8
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        if key_sequence.matches(QKeySequence.Find):
            self.enable_find()
        elif key_sequence.matches(QKeySequence.Replace):
            self.enable_replace()
        elif key_sequence.matches(QKeySequence(QtCore.Qt.Key_Escape)):
            if self.active_find_widget:
                self.find.hide()
                self.replace.hide()
                self.code.setFocus()
                self.previous_find_widget = self.active_find_widget
                self.active_find_widget = None

        return super(AdvancedCodeWidget, self).keyPressEvent(event)
Пример #9
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        self.keyPressEvent_action(event)  # FIXME: see above

        # If the cursor is in the middle of the first line, pressing the "up"
        # key causes the cursor to go to the start of the first line, i.e. the
        # beginning of the document. Likewise, if the cursor is somewhere in the
        # last line, the "down" key causes it to go to the end.
        cursor = self.textCursor()
        if key_sequence.matches(QKeySequence(QtCore.Qt.Key_Up)):
            cursor.movePosition(QTextCursor.StartOfLine)
            if cursor.atStart():
                self.setTextCursor(cursor)
                event.accept()
        elif key_sequence.matches(QKeySequence(QtCore.Qt.Key_Down)):
            cursor.movePosition(QTextCursor.EndOfLine)
            if cursor.atEnd():
                self.setTextCursor(cursor)
                event.accept()

        elif self.auto_indent and \
                key_sequence.matches(QKeySequence(QtCore.Qt.Key_Return)) or \
                key_sequence.matches(QKeySequence(QtCore.Qt.Key_Enter)):
            event.accept()
            return self.autoindent_newline()
        elif key_sequence.matches(self.indent_key):
            event.accept()
            return self.block_indent()
        elif key_sequence.matches(self.unindent_key):
            event.accept()
            return self.block_unindent()
        elif key_sequence.matches(self.comment_key):
            event.accept()
            return self.block_comment()
        elif key_sequence.matches(self.goto_line_key):
            event.accept()
            return self.go_to_line()
        elif key_sequence.matches(self.home_key):
            event.accept()
            return self.exec_home_key()
        elif self.auto_indent and self.smart_backspace and \
                key_sequence.matches(self.backspace_key) and \
                self._backspace_should_unindent():
            event.accept()
            return self.block_unindent()

        return super(CodeWidget, self).keyPressEvent(event)
Пример #10
0
    def __init__(self,
                 parent,
                 should_highlight_current_line=True,
                 font=None,
                 lexer=None):
        from pyvmmonitor_qt.qt.QtGui import QColor
        from pyvmmonitor_qt.qt.QtGui import QFont

        super(CodeWidget, self).__init__(parent)

        self.highlighter = PygmentsHighlighter(self.document(), lexer)
        self.line_number_widget = LineNumberWidget(self)
        self.status_widget = StatusGutterWidget(self)

        if font is None:
            # Set a decent fixed width font for this platform.
            font = QFont()
            if sys.platform == 'win32':
                # Prefer Consolas, but fall back to Courier if necessary.
                font.setFamily('Consolas')
                if not font.exactMatch():
                    font.setFamily('Courier')
            elif sys.platform == 'darwin':
                font.setFamily('Monaco')
            else:
                font.setFamily('Monospace')
            font.setStyleHint(QFont.TypeWriter)
        self.set_font(font)

        # Whether we should highlight the current line or not.
        self.should_highlight_current_line = should_highlight_current_line

        # What that highlight color should be.
        self.line_highlight_color = QColor(QtCore.Qt.yellow).lighter(160)

        # Auto-indentation behavior
        self.auto_indent = True
        self.smart_backspace = True

        # Tab settings
        self.tabs_as_spaces = True
        self.tab_width = 4

        self.indent_character = ':'
        self.comment_character = '#'

        # Set up gutter widget and current line highlighting
        self.blockCountChanged.connect(self.update_line_number_width)
        self.updateRequest.connect(self.update_line_numbers)
        self.cursorPositionChanged.connect(self.highlight_current_line)

        self.update_line_number_width()
        self.highlight_current_line()

        # Don't wrap text
        self.setLineWrapMode(QPlainTextEdit.NoWrap)

        # Key bindings
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        self.indent_key = QKeySequence(QtCore.Qt.Key_Tab)
        self.unindent_key = QKeySequence(QtCore.Qt.SHIFT +
                                         QtCore.Qt.Key_Backtab)
        self.comment_key = QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Slash)
        self.backspace_key = QKeySequence(QtCore.Qt.Key_Backspace)
        self.goto_line_key = QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_G)
        self.home_key = QKeySequence(QtCore.Qt.Key_Home)
Пример #11
0
def test_widget_scopes(qtapi, _shortcuts_main_window):
    '''

    '''
    from pyvmmonitor_qt.commands.qt_commands_manager import create_default_qt_commands_manager
    from pyvmmonitor_qt.qt.QtCore import Qt
    from pyvmmonitor_qt.qt.QtGui import QKeySequence
    from pyvmmonitor_qt.qt_event_loop import process_events
    from pyvmmonitor_qt.qt_utils import assert_focus_within_timeout
    try:
        main_window = _shortcuts_main_window()
        main_window.show()

        process_events()

        activated = []

        def on_g():
            activated.append('on_g')

        def on_d():
            activated.append('on_d')

        def on_b():
            activated.append('on_b')

        qt_commands_manager = create_default_qt_commands_manager(main_window)
        qt_commands_manager.register_scope('line_edit1')
        qt_commands_manager.register_scope('line_edit2')
        # : :type qt_commands_manager: _DefaultQtShortcutsManager

        qt_commands_manager.register_command('cmd_g', 'CMD_G')
        qt_commands_manager.set_command_handler('cmd_g', on_g)
        qt_commands_manager.set_shortcut('cmd_g', QKeySequence('Ctrl+G'))

        qt_commands_manager.register_command('cmd_d', 'CMD_D')
        qt_commands_manager.set_command_handler('cmd_d', on_d, 'line_edit1')
        qt_commands_manager.set_shortcut('cmd_d',
                                         QKeySequence('Ctrl+D'),
                                         scope='line_edit1')

        qt_commands_manager.register_command('cmd_b', 'CMD_B')
        qt_commands_manager.set_command_handler('cmd_b', on_b, 'line_edit2')
        qt_commands_manager.set_shortcut('cmd_b',
                                         QKeySequence('Ctrl+B'),
                                         scope='line_edit2')

        qt_commands_manager.set_scope_widget('line_edit2',
                                             main_window.line_edit2)
        qt_commands_manager.set_scope_widget('line_edit1',
                                             main_window.line_edit1)

        qtapi.keyPress(main_window, 'G', Qt.KeyboardModifier.ControlModifier)
        qtapi.keyPress(main_window.line_edit1, 'G',
                       Qt.KeyboardModifier.ControlModifier)
        qtapi.keyPress(main_window.line_edit2, 'G',
                       Qt.KeyboardModifier.ControlModifier)

        assert activated == ['on_g', 'on_g', 'on_g']
        del activated[:]

        qtapi.keyPress(main_window, 'B', Qt.KeyboardModifier.ControlModifier)
        assert activated == []
        assert_focus_within_timeout(main_window.line_edit1)
        qtapi.keyPress(main_window.line_edit1, 'B',
                       Qt.KeyboardModifier.ControlModifier)
        assert activated == []
        assert_focus_within_timeout(main_window.line_edit2)
        qtapi.keyPress(main_window.line_edit2, 'B',
                       Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_b']
        del activated[:]

        qtapi.keyPress(main_window, 'D', Qt.KeyboardModifier.ControlModifier)
        assert activated == []
        assert_focus_within_timeout(main_window.line_edit1)
        qtapi.keyPress(main_window.line_edit1, 'D',
                       Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_d']
        assert_focus_within_timeout(main_window.line_edit2)
        qtapi.keyPress(main_window.line_edit2, 'D',
                       Qt.KeyboardModifier.ControlModifier)
        assert activated == ['on_d']
        del activated[:]

    finally:
        main_window = None
Пример #12
0
    def keyPressEvent(self, event):
        from pyvmmonitor_qt.qt.QtGui import QKeySequence
        key_sequence = QKeySequence(event.key() + int(event.modifiers()))

        self.keyPressEvent_action(event)  # FIXME: see above

        # If the cursor is in the middle of the first line, pressing the "up"
        # key causes the cursor to go to the start of the first line, i.e. the
        # beginning of the document. Likewise, if the cursor is somewhere in the
        # last line, the "down" key causes it to go to the end.
        cursor = self.textCursor()
        if key_sequence.matches(QKeySequence(QtCore.Qt.Key_Up)):
            cursor.movePosition(QTextCursor.StartOfLine)
            if cursor.atStart():
                self.setTextCursor(cursor)
                event.accept()
        elif key_sequence.matches(QKeySequence(QtCore.Qt.Key_Down)):
            cursor.movePosition(QTextCursor.EndOfLine)
            if cursor.atEnd():
                self.setTextCursor(cursor)
                event.accept()

        elif self.auto_indent and \
                key_sequence.matches(QKeySequence(QtCore.Qt.Key_Return)) or \
                key_sequence.matches(QKeySequence(QtCore.Qt.Key_Enter)):
            event.accept()
            return self.autoindent_newline()
        elif key_sequence.matches(self.indent_key):
            event.accept()
            return self.block_indent()
        elif key_sequence.matches(self.unindent_key):
            event.accept()
            return self.block_unindent()
        elif key_sequence.matches(self.comment_key):
            event.accept()
            return self.block_comment()
        elif key_sequence.matches(self.goto_line_key):
            event.accept()
            return self.go_to_line()
        elif key_sequence.matches(self.home_key):
            event.accept()
            return self.exec_home_key()
        elif self.auto_indent and self.smart_backspace and \
                key_sequence.matches(self.backspace_key) and \
                self._backspace_should_unindent():
            event.accept()
            return self.block_unindent()

        return super(CodeWidget, self).keyPressEvent(event)