##// END OF EJS Templates
* Added Cut support to ConsoleWidget....
epatters -
Show More
@@ -234,6 +234,19 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
234 234 # 'ConsoleWidget' public interface
235 235 #---------------------------------------------------------------------------
236 236
237 def can_cut(self):
238 """ Returns whether text can be cut to the clipboard.
239 """
240 cursor = self._control.textCursor()
241 return (cursor.hasSelection() and
242 self._in_buffer(cursor.anchor()) and
243 self._in_buffer(cursor.position()))
244
245 def can_copy(self):
246 """ Returns whether text can be copied to the clipboard.
247 """
248 return self._control.textCursor().hasSelection()
249
237 250 def can_paste(self):
238 251 """ Returns whether text can be pasted from the clipboard.
239 252 """
@@ -264,6 +277,14 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
264 277 """
265 278 self._control.copy()
266 279
280 def cut(self):
281 """ Copy the currently selected text to the clipboard and delete it
282 if it's inside the input buffer.
283 """
284 self.copy()
285 if self.can_cut():
286 self._control.textCursor().removeSelectedText()
287
267 288 def execute(self, source=None, hidden=False, interactive=False):
268 289 """ Executes source or the input buffer, possibly prompting for more
269 290 input.
@@ -460,7 +481,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
460 481 # FIXME: remove Consolas as a default on Linux once our font
461 482 # selections are configurable by the user.
462 483 family, fallback = 'Consolas', 'Monospace'
463
464 484 font = get_font(family, fallback)
465 485 font.setPointSize(QtGui.qApp.font().pointSize())
466 486 font.setStyleHint(QtGui.QFont.TypeWriter)
@@ -640,6 +660,34 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
640 660 self._control.setTextCursor(cursor)
641 661 self._text_completing_pos = current_pos
642 662
663 def _context_menu_make(self, pos):
664 """ Creates a context menu for the given QPoint (in widget coordinates).
665 """
666 menu = QtGui.QMenu()
667
668 cut_action = menu.addAction('Cut', self.cut)
669 cut_action.setEnabled(self.can_cut())
670 cut_action.setShortcut(QtGui.QKeySequence.Cut)
671
672 copy_action = menu.addAction('Copy', self.copy)
673 copy_action.setEnabled(self.can_copy())
674 copy_action.setShortcut(QtGui.QKeySequence.Copy)
675
676 paste_action = menu.addAction('Paste', self.paste)
677 paste_action.setEnabled(self.can_paste())
678 paste_action.setShortcut(QtGui.QKeySequence.Paste)
679
680 menu.addSeparator()
681 menu.addAction('Select All', self.select_all)
682
683 return menu
684
685 def _context_menu_show(self, pos):
686 """ Shows a context menu at the given QPoint (in widget coordinates).
687 """
688 menu = self._context_menu_make(pos)
689 menu.exec_(self._control.mapToGlobal(pos))
690
643 691 def _control_key_down(self, modifiers, include_command=True):
644 692 """ Given a KeyboardModifiers flags object, return whether the Control
645 693 key is down.
@@ -675,7 +723,7 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
675 723
676 724 # Connect signals.
677 725 control.cursorPositionChanged.connect(self._cursor_position_changed)
678 control.customContextMenuRequested.connect(self._show_context_menu)
726 control.customContextMenuRequested.connect(self._context_menu_show)
679 727 control.copyAvailable.connect(self.copy_available)
680 728 control.redoAvailable.connect(self.redo_available)
681 729 control.undoAvailable.connect(self.undo_available)
@@ -715,6 +763,10 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
715 763 self.copy()
716 764 intercepted = True
717 765
766 elif event.matches(QtGui.QKeySequence.Cut):
767 self.cut()
768 intercepted = True
769
718 770 elif event.matches(QtGui.QKeySequence.Paste):
719 771 self.paste()
720 772 intercepted = True
@@ -793,11 +845,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
793 845 self._page_control.setFocus()
794 846 intercept = True
795 847
796 elif key == QtCore.Qt.Key_X:
797 # FIXME: Instead of disabling cut completely, only allow it
798 # when safe.
799 intercepted = True
800
801 848 elif key == QtCore.Qt.Key_Y:
802 849 self.paste()
803 850 intercepted = True
@@ -1393,24 +1440,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
1393 1440 """
1394 1441 self._control.setTextCursor(cursor)
1395 1442
1396 def _show_context_menu(self, pos):
1397 """ Shows a context menu at the given QPoint (in widget coordinates).
1398 """
1399 menu = QtGui.QMenu()
1400
1401 copy_action = menu.addAction('Copy', self.copy)
1402 copy_action.setEnabled(self._get_cursor().hasSelection())
1403 copy_action.setShortcut(QtGui.QKeySequence.Copy)
1404
1405 paste_action = menu.addAction('Paste', self.paste)
1406 paste_action.setEnabled(self.can_paste())
1407 paste_action.setShortcut(QtGui.QKeySequence.Paste)
1408
1409 menu.addSeparator()
1410 menu.addAction('Select All', self.select_all)
1411
1412 menu.exec_(self._control.mapToGlobal(pos))
1413
1414 1443 def _show_prompt(self, prompt=None, html=False, newline=True):
1415 1444 """ Writes a new prompt at the end of the buffer.
1416 1445
@@ -111,6 +111,7 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
111 111 self._bracket_matcher = BracketMatcher(self._control)
112 112 self._call_tip_widget = CallTipWidget(self._control)
113 113 self._completion_lexer = CompletionLexer(PythonLexer())
114 self._copy_raw_action = QtGui.QAction('Copy (Raw Text)', None)
114 115 self._hidden = False
115 116 self._highlighter = FrontendHighlighter(self)
116 117 self._input_splitter = self._input_splitter_class(input_mode='block')
@@ -122,6 +123,16 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
122 123 self.tab_width = 4
123 124 self._set_continuation_prompt('... ')
124 125
126 # Configure actions.
127 action = self._copy_raw_action
128 key = QtCore.Qt.CTRL | QtCore.Qt.SHIFT | QtCore.Qt.Key_C
129 action.setEnabled(False)
130 action.setShortcut(QtGui.QKeySequence(key))
131 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
132 action.triggered.connect(self.copy_raw)
133 self.copy_available.connect(action.setEnabled)
134 self.addAction(action)
135
125 136 # Connect signal handlers.
126 137 document = self._control.document()
127 138 document.contentsChange.connect(self._document_contents_change)
@@ -196,6 +207,17 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
196 207 # 'ConsoleWidget' protected interface
197 208 #---------------------------------------------------------------------------
198 209
210 def _context_menu_make(self, pos):
211 """ Reimplemented to add an action for raw copy.
212 """
213 menu = super(FrontendWidget, self)._context_menu_make(pos)
214 for before_action in menu.actions():
215 if before_action.shortcut().matches(QtGui.QKeySequence.Paste) == \
216 QtGui.QKeySequence.ExactMatch:
217 menu.insertAction(before_action, self._copy_raw_action)
218 break
219 return menu
220
199 221 def _event_filter_console_keypress(self, event):
200 222 """ Reimplemented to allow execution interruption.
201 223 """
@@ -324,6 +346,12 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
324 346 # 'FrontendWidget' public interface
325 347 #---------------------------------------------------------------------------
326 348
349 def copy_raw(self):
350 """ Copy the currently selected text to the clipboard without attempting
351 to remove prompts or otherwise alter the text.
352 """
353 self._control.copy()
354
327 355 def execute_file(self, path, hidden=False):
328 356 """ Attempts to execute file with 'path'. If 'hidden', no output is
329 357 shown.
@@ -30,13 +30,13 b' class RichIPythonWidget(IPythonWidget):'
30 30 # 'ConsoleWidget' protected interface
31 31 #---------------------------------------------------------------------------
32 32
33 def _show_context_menu(self, pos):
34 """ Reimplemented to show a custom context menu for images.
33 def _context_menu_make(self, pos):
34 """ Reimplemented to return a custom context menu for images.
35 35 """
36 36 format = self._control.cursorForPosition(pos).charFormat()
37 37 name = format.stringProperty(QtGui.QTextFormat.ImageName)
38 38 if name.isEmpty():
39 super(RichIPythonWidget, self)._show_context_menu(pos)
39 menu = super(RichIPythonWidget, self)._context_menu_make(pos)
40 40 else:
41 41 menu = QtGui.QMenu()
42 42
@@ -50,8 +50,7 b' class RichIPythonWidget(IPythonWidget):'
50 50 menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
51 51 menu.addAction('Save SVG As...',
52 52 lambda: save_svg(svg, self._control))
53
54 menu.exec_(self._control.mapToGlobal(pos))
53 return menu
55 54
56 55 #---------------------------------------------------------------------------
57 56 # 'FrontendWidget' protected interface
@@ -16,14 +16,16 b' MetaQObject = type(QtCore.QObject)'
16 16
17 17 # You can switch the order of the parents here and it doesn't seem to matter.
18 18 class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
19 """ A metaclass that inherits from the metaclasses of both HasTraits and
20 QObject.
19 """ A metaclass that inherits from the metaclasses of HasTraits and QObject.
21 20
22 Using this metaclass allows a class to inherit from both HasTraits and
23 QObject. See QtKernelManager for an example.
21 Using this metaclass allows a class to inherit from both HasTraits and
22 QObject. See QtKernelManager for an example.
24 23 """
25 24 pass
26 25
26 #-----------------------------------------------------------------------------
27 # Functions
28 #-----------------------------------------------------------------------------
27 29
28 30 def get_font(family, fallback=None):
29 31 """Return a font of the requested family, using fallback as alternative.
General Comments 0
You need to be logged in to leave comments. Login now