Show More
@@ -22,7 +22,7 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
22 | 22 | # The maximum number of lines of text before truncation. |
|
23 | 23 | buffer_size = 500 |
|
24 | 24 | |
|
25 |
# Whether to use a |
|
|
25 | # Whether to use a list widget or plain text output for tab completion. | |
|
26 | 26 | gui_completion = True |
|
27 | 27 | |
|
28 | 28 | # Whether to override ShortcutEvents for the keybindings defined by this |
@@ -64,8 +64,11 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
64 | 64 | """ |
|
65 | 65 | super(ConsoleWidget, self).__init__(parent) |
|
66 | 66 | |
|
67 | # Create the underlying text widget. | |
|
67 | # Create and set the underlying text widget. | |
|
68 | layout = QtGui.QVBoxLayout(self) | |
|
69 | layout.setMargin(0) | |
|
68 | 70 | self._control = self._create_control(kind) |
|
71 | layout.addWidget(self._control) | |
|
69 | 72 | |
|
70 | 73 | # Initialize protected variables. Some variables contain useful state |
|
71 | 74 | # information for subclasses; they should be considered read-only. |
@@ -88,9 +91,28 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
88 | 91 | """ Reimplemented to ensure a console-like behavior in the underlying |
|
89 | 92 | text widget. |
|
90 | 93 | """ |
|
91 | if obj == self._control: | |
|
94 | # Re-map keys for all filtered widgets. | |
|
92 | 95 |
|
|
96 | if etype == QtCore.QEvent.KeyPress and \ | |
|
97 | self._control_key_down(event.modifiers()) and \ | |
|
98 | event.key() in self._ctrl_down_remap: | |
|
99 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, | |
|
100 | self._ctrl_down_remap[event.key()], | |
|
101 | QtCore.Qt.NoModifier) | |
|
102 | QtGui.qApp.sendEvent(obj, new_event) | |
|
103 | return True | |
|
93 | 104 | |
|
105 | # Override shortucts for all filtered widgets. Note that on Mac OS it is | |
|
106 | # always unnecessary to override shortcuts, hence the check below (users | |
|
107 | # should just use the Control key instead of the Command key). | |
|
108 | elif etype == QtCore.QEvent.ShortcutOverride and \ | |
|
109 | sys.platform != 'darwin' and \ | |
|
110 | self._control_key_down(event.modifiers()) and \ | |
|
111 | event.key() in self._shortcuts: | |
|
112 | event.accept() | |
|
113 | return False | |
|
114 | ||
|
115 | elif obj == self._control: | |
|
94 | 116 | # Disable moving text by drag and drop. |
|
95 | 117 | if etype == QtCore.QEvent.DragMove: |
|
96 | 118 | return True |
@@ -98,16 +120,6 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
98 | 120 | elif etype == QtCore.QEvent.KeyPress: |
|
99 | 121 | return self._event_filter_keypress(event) |
|
100 | 122 | |
|
101 | # On Mac OS, it is always unnecessary to override shortcuts, hence | |
|
102 | # the check below. Users should just use the Control key instead of | |
|
103 | # the Command key. | |
|
104 | elif etype == QtCore.QEvent.ShortcutOverride: | |
|
105 | if sys.platform != 'darwin' and \ | |
|
106 | self._control_key_down(event.modifiers()) and \ | |
|
107 | event.key() in self._shortcuts: | |
|
108 | event.accept() | |
|
109 | return False | |
|
110 | ||
|
111 | 123 | return super(ConsoleWidget, self).eventFilter(obj, event) |
|
112 | 124 | |
|
113 | 125 | #--------------------------------------------------------------------------- |
@@ -448,10 +460,8 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
448 | 460 | return down |
|
449 | 461 | |
|
450 | 462 | def _create_control(self, kind): |
|
451 |
""" Creates and |
|
|
463 | """ Creates and connects the underlying text widget. | |
|
452 | 464 | """ |
|
453 | layout = QtGui.QVBoxLayout(self) | |
|
454 | layout.setMargin(0) | |
|
455 | 465 | if kind == 'plain': |
|
456 | 466 | control = QtGui.QPlainTextEdit() |
|
457 | 467 | elif kind == 'rich': |
@@ -459,36 +469,24 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
459 | 469 | control.setAcceptRichText(False) |
|
460 | 470 | else: |
|
461 | 471 | raise ValueError("Kind %s unknown." % repr(kind)) |
|
462 | layout.addWidget(control) | |
|
463 | ||
|
464 | 472 | control.installEventFilter(self) |
|
465 | 473 | control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
|
466 | 474 | control.customContextMenuRequested.connect(self._show_context_menu) |
|
467 | 475 | control.copyAvailable.connect(self.copy_available) |
|
468 | 476 | control.redoAvailable.connect(self.redo_available) |
|
469 | 477 | control.undoAvailable.connect(self.undo_available) |
|
470 | ||
|
478 | control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
|
471 | 479 | return control |
|
472 | 480 | |
|
473 | 481 | def _event_filter_keypress(self, event): |
|
474 | 482 | """ Filter key events for the underlying text widget to create a |
|
475 | 483 | console-like interface. |
|
476 | 484 | """ |
|
477 | key = event.key() | |
|
478 | ctrl_down = self._control_key_down(event.modifiers()) | |
|
479 | ||
|
480 | # If the key is remapped, return immediately. | |
|
481 | if ctrl_down and key in self._ctrl_down_remap: | |
|
482 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, | |
|
483 | self._ctrl_down_remap[key], | |
|
484 | QtCore.Qt.NoModifier) | |
|
485 | QtGui.qApp.sendEvent(self._control, new_event) | |
|
486 | return True | |
|
487 | ||
|
488 | # Otherwise, proceed normally and do not return early. | |
|
489 | 485 | intercepted = False |
|
490 | 486 | cursor = self._control.textCursor() |
|
491 | 487 | position = cursor.position() |
|
488 | key = event.key() | |
|
489 | ctrl_down = self._control_key_down(event.modifiers()) | |
|
492 | 490 | alt_down = event.modifiers() & QtCore.Qt.AltModifier |
|
493 | 491 | shift_down = event.modifiers() & QtCore.Qt.ShiftModifier |
|
494 | 492 | |
@@ -632,8 +630,9 b' class ConsoleWidget(QtGui.QWidget):' | |||
|
632 | 630 | # Note: this code is adapted from columnize 0.3.2. |
|
633 | 631 | # See http://code.google.com/p/pycolumnize/ |
|
634 | 632 | |
|
635 | font_metrics = QtGui.QFontMetrics(self.font) | |
|
636 | displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1) | |
|
633 | width = self._control.viewport().width() | |
|
634 | char_width = QtGui.QFontMetrics(self.font).width(' ') | |
|
635 | displaywidth = max(5, width / char_width) | |
|
637 | 636 | |
|
638 | 637 | # Some degenerate cases. |
|
639 | 638 | size = len(items) |
General Comments 0
You need to be logged in to leave comments.
Login now