##// END OF EJS Templates
* Added Cut support to ConsoleWidget....
epatters -
Show More
@@ -234,6 +234,19 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
234 # 'ConsoleWidget' public interface
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 def can_paste(self):
250 def can_paste(self):
238 """ Returns whether text can be pasted from the clipboard.
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 self._control.copy()
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 def execute(self, source=None, hidden=False, interactive=False):
288 def execute(self, source=None, hidden=False, interactive=False):
268 """ Executes source or the input buffer, possibly prompting for more
289 """ Executes source or the input buffer, possibly prompting for more
269 input.
290 input.
@@ -460,7 +481,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
460 # FIXME: remove Consolas as a default on Linux once our font
481 # FIXME: remove Consolas as a default on Linux once our font
461 # selections are configurable by the user.
482 # selections are configurable by the user.
462 family, fallback = 'Consolas', 'Monospace'
483 family, fallback = 'Consolas', 'Monospace'
463
464 font = get_font(family, fallback)
484 font = get_font(family, fallback)
465 font.setPointSize(QtGui.qApp.font().pointSize())
485 font.setPointSize(QtGui.qApp.font().pointSize())
466 font.setStyleHint(QtGui.QFont.TypeWriter)
486 font.setStyleHint(QtGui.QFont.TypeWriter)
@@ -640,6 +660,34 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
640 self._control.setTextCursor(cursor)
660 self._control.setTextCursor(cursor)
641 self._text_completing_pos = current_pos
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 def _control_key_down(self, modifiers, include_command=True):
691 def _control_key_down(self, modifiers, include_command=True):
644 """ Given a KeyboardModifiers flags object, return whether the Control
692 """ Given a KeyboardModifiers flags object, return whether the Control
645 key is down.
693 key is down.
@@ -675,7 +723,7 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
675
723
676 # Connect signals.
724 # Connect signals.
677 control.cursorPositionChanged.connect(self._cursor_position_changed)
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 control.copyAvailable.connect(self.copy_available)
727 control.copyAvailable.connect(self.copy_available)
680 control.redoAvailable.connect(self.redo_available)
728 control.redoAvailable.connect(self.redo_available)
681 control.undoAvailable.connect(self.undo_available)
729 control.undoAvailable.connect(self.undo_available)
@@ -715,6 +763,10 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
715 self.copy()
763 self.copy()
716 intercepted = True
764 intercepted = True
717
765
766 elif event.matches(QtGui.QKeySequence.Cut):
767 self.cut()
768 intercepted = True
769
718 elif event.matches(QtGui.QKeySequence.Paste):
770 elif event.matches(QtGui.QKeySequence.Paste):
719 self.paste()
771 self.paste()
720 intercepted = True
772 intercepted = True
@@ -793,11 +845,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
793 self._page_control.setFocus()
845 self._page_control.setFocus()
794 intercept = True
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 elif key == QtCore.Qt.Key_Y:
848 elif key == QtCore.Qt.Key_Y:
802 self.paste()
849 self.paste()
803 intercepted = True
850 intercepted = True
@@ -1393,24 +1440,6 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
1393 """
1440 """
1394 self._control.setTextCursor(cursor)
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 def _show_prompt(self, prompt=None, html=False, newline=True):
1443 def _show_prompt(self, prompt=None, html=False, newline=True):
1415 """ Writes a new prompt at the end of the buffer.
1444 """ Writes a new prompt at the end of the buffer.
1416
1445
@@ -111,6 +111,7 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
111 self._bracket_matcher = BracketMatcher(self._control)
111 self._bracket_matcher = BracketMatcher(self._control)
112 self._call_tip_widget = CallTipWidget(self._control)
112 self._call_tip_widget = CallTipWidget(self._control)
113 self._completion_lexer = CompletionLexer(PythonLexer())
113 self._completion_lexer = CompletionLexer(PythonLexer())
114 self._copy_raw_action = QtGui.QAction('Copy (Raw Text)', None)
114 self._hidden = False
115 self._hidden = False
115 self._highlighter = FrontendHighlighter(self)
116 self._highlighter = FrontendHighlighter(self)
116 self._input_splitter = self._input_splitter_class(input_mode='block')
117 self._input_splitter = self._input_splitter_class(input_mode='block')
@@ -122,6 +123,16 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
122 self.tab_width = 4
123 self.tab_width = 4
123 self._set_continuation_prompt('... ')
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 # Connect signal handlers.
136 # Connect signal handlers.
126 document = self._control.document()
137 document = self._control.document()
127 document.contentsChange.connect(self._document_contents_change)
138 document.contentsChange.connect(self._document_contents_change)
@@ -196,6 +207,17 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
196 # 'ConsoleWidget' protected interface
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 def _event_filter_console_keypress(self, event):
221 def _event_filter_console_keypress(self, event):
200 """ Reimplemented to allow execution interruption.
222 """ Reimplemented to allow execution interruption.
201 """
223 """
@@ -324,6 +346,12 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
324 # 'FrontendWidget' public interface
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 def execute_file(self, path, hidden=False):
355 def execute_file(self, path, hidden=False):
328 """ Attempts to execute file with 'path'. If 'hidden', no output is
356 """ Attempts to execute file with 'path'. If 'hidden', no output is
329 shown.
357 shown.
@@ -30,13 +30,13 b' class RichIPythonWidget(IPythonWidget):'
30 # 'ConsoleWidget' protected interface
30 # 'ConsoleWidget' protected interface
31 #---------------------------------------------------------------------------
31 #---------------------------------------------------------------------------
32
32
33 def _show_context_menu(self, pos):
33 def _context_menu_make(self, pos):
34 """ Reimplemented to show a custom context menu for images.
34 """ Reimplemented to return a custom context menu for images.
35 """
35 """
36 format = self._control.cursorForPosition(pos).charFormat()
36 format = self._control.cursorForPosition(pos).charFormat()
37 name = format.stringProperty(QtGui.QTextFormat.ImageName)
37 name = format.stringProperty(QtGui.QTextFormat.ImageName)
38 if name.isEmpty():
38 if name.isEmpty():
39 super(RichIPythonWidget, self)._show_context_menu(pos)
39 menu = super(RichIPythonWidget, self)._context_menu_make(pos)
40 else:
40 else:
41 menu = QtGui.QMenu()
41 menu = QtGui.QMenu()
42
42
@@ -50,8 +50,7 b' class RichIPythonWidget(IPythonWidget):'
50 menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
50 menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
51 menu.addAction('Save SVG As...',
51 menu.addAction('Save SVG As...',
52 lambda: save_svg(svg, self._control))
52 lambda: save_svg(svg, self._control))
53
53 return menu
54 menu.exec_(self._control.mapToGlobal(pos))
55
54
56 #---------------------------------------------------------------------------
55 #---------------------------------------------------------------------------
57 # 'FrontendWidget' protected interface
56 # 'FrontendWidget' protected interface
@@ -16,14 +16,16 b' MetaQObject = type(QtCore.QObject)'
16
16
17 # You can switch the order of the parents here and it doesn't seem to matter.
17 # You can switch the order of the parents here and it doesn't seem to matter.
18 class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
18 class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
19 """ A metaclass that inherits from the metaclasses of both HasTraits and
19 """ A metaclass that inherits from the metaclasses of HasTraits and QObject.
20 QObject.
21
20
22 Using this metaclass allows a class to inherit from both HasTraits and
21 Using this metaclass allows a class to inherit from both HasTraits and
23 QObject. See QtKernelManager for an example.
22 QObject. See QtKernelManager for an example.
24 """
23 """
25 pass
24 pass
26
25
26 #-----------------------------------------------------------------------------
27 # Functions
28 #-----------------------------------------------------------------------------
27
29
28 def get_font(family, fallback=None):
30 def get_font(family, fallback=None):
29 """Return a font of the requested family, using fallback as alternative.
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