##// END OF EJS Templates
Cleanup and minor UI fixes.
epatters -
Show More
@@ -22,7 +22,7 b' class ConsoleWidget(QtGui.QWidget):'
22 # The maximum number of lines of text before truncation.
22 # The maximum number of lines of text before truncation.
23 buffer_size = 500
23 buffer_size = 500
24
24
25 # Whether to use a CompletionWidget or plain text output for tab completion.
25 # Whether to use a list widget or plain text output for tab completion.
26 gui_completion = True
26 gui_completion = True
27
27
28 # Whether to override ShortcutEvents for the keybindings defined by this
28 # Whether to override ShortcutEvents for the keybindings defined by this
@@ -64,8 +64,11 b' class ConsoleWidget(QtGui.QWidget):'
64 """
64 """
65 super(ConsoleWidget, self).__init__(parent)
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 self._control = self._create_control(kind)
70 self._control = self._create_control(kind)
71 layout.addWidget(self._control)
69
72
70 # Initialize protected variables. Some variables contain useful state
73 # Initialize protected variables. Some variables contain useful state
71 # information for subclasses; they should be considered read-only.
74 # information for subclasses; they should be considered read-only.
@@ -88,9 +91,28 b' class ConsoleWidget(QtGui.QWidget):'
88 """ Reimplemented to ensure a console-like behavior in the underlying
91 """ Reimplemented to ensure a console-like behavior in the underlying
89 text widget.
92 text widget.
90 """
93 """
91 if obj == self._control:
94 # Re-map keys for all filtered widgets.
92 etype = event.type()
95 etype = event.type()
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 # Disable moving text by drag and drop.
116 # Disable moving text by drag and drop.
95 if etype == QtCore.QEvent.DragMove:
117 if etype == QtCore.QEvent.DragMove:
96 return True
118 return True
@@ -98,16 +120,6 b' class ConsoleWidget(QtGui.QWidget):'
98 elif etype == QtCore.QEvent.KeyPress:
120 elif etype == QtCore.QEvent.KeyPress:
99 return self._event_filter_keypress(event)
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 return super(ConsoleWidget, self).eventFilter(obj, event)
123 return super(ConsoleWidget, self).eventFilter(obj, event)
112
124
113 #---------------------------------------------------------------------------
125 #---------------------------------------------------------------------------
@@ -448,10 +460,8 b' class ConsoleWidget(QtGui.QWidget):'
448 return down
460 return down
449
461
450 def _create_control(self, kind):
462 def _create_control(self, kind):
451 """ Creates and sets the underlying text widget.
463 """ Creates and connects the underlying text widget.
452 """
464 """
453 layout = QtGui.QVBoxLayout(self)
454 layout.setMargin(0)
455 if kind == 'plain':
465 if kind == 'plain':
456 control = QtGui.QPlainTextEdit()
466 control = QtGui.QPlainTextEdit()
457 elif kind == 'rich':
467 elif kind == 'rich':
@@ -459,36 +469,24 b' class ConsoleWidget(QtGui.QWidget):'
459 control.setAcceptRichText(False)
469 control.setAcceptRichText(False)
460 else:
470 else:
461 raise ValueError("Kind %s unknown." % repr(kind))
471 raise ValueError("Kind %s unknown." % repr(kind))
462 layout.addWidget(control)
463
464 control.installEventFilter(self)
472 control.installEventFilter(self)
465 control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
473 control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
466 control.customContextMenuRequested.connect(self._show_context_menu)
474 control.customContextMenuRequested.connect(self._show_context_menu)
467 control.copyAvailable.connect(self.copy_available)
475 control.copyAvailable.connect(self.copy_available)
468 control.redoAvailable.connect(self.redo_available)
476 control.redoAvailable.connect(self.redo_available)
469 control.undoAvailable.connect(self.undo_available)
477 control.undoAvailable.connect(self.undo_available)
470
478 control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
471 return control
479 return control
472
480
473 def _event_filter_keypress(self, event):
481 def _event_filter_keypress(self, event):
474 """ Filter key events for the underlying text widget to create a
482 """ Filter key events for the underlying text widget to create a
475 console-like interface.
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 intercepted = False
485 intercepted = False
490 cursor = self._control.textCursor()
486 cursor = self._control.textCursor()
491 position = cursor.position()
487 position = cursor.position()
488 key = event.key()
489 ctrl_down = self._control_key_down(event.modifiers())
492 alt_down = event.modifiers() & QtCore.Qt.AltModifier
490 alt_down = event.modifiers() & QtCore.Qt.AltModifier
493 shift_down = event.modifiers() & QtCore.Qt.ShiftModifier
491 shift_down = event.modifiers() & QtCore.Qt.ShiftModifier
494
492
@@ -632,8 +630,9 b' class ConsoleWidget(QtGui.QWidget):'
632 # Note: this code is adapted from columnize 0.3.2.
630 # Note: this code is adapted from columnize 0.3.2.
633 # See http://code.google.com/p/pycolumnize/
631 # See http://code.google.com/p/pycolumnize/
634
632
635 font_metrics = QtGui.QFontMetrics(self.font)
633 width = self._control.viewport().width()
636 displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1)
634 char_width = QtGui.QFontMetrics(self.font).width(' ')
635 displaywidth = max(5, width / char_width)
637
636
638 # Some degenerate cases.
637 # Some degenerate cases.
639 size = len(items)
638 size = len(items)
General Comments 0
You need to be logged in to leave comments. Login now