##// END OF EJS Templates
* Added API for setting the font of a ConsoleWidget....
epatters -
Show More
@@ -1,5 +1,6 b''
1 # Standard library imports
1 # Standard library imports
2 import re
2 import re
3 import sys
3
4
4 # System library imports
5 # System library imports
5 from PyQt4 import QtCore, QtGui
6 from PyQt4 import QtCore, QtGui
@@ -118,7 +119,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
118 def __init__(self, parent=None):
119 def __init__(self, parent=None):
119 QtGui.QPlainTextEdit.__init__(self, parent)
120 QtGui.QPlainTextEdit.__init__(self, parent)
120
121
121 # Initialize public and protected variables
122 # Initialize public and protected variables.
122 self.ansi_codes = True
123 self.ansi_codes = True
123 self.buffer_size = 500
124 self.buffer_size = 500
124 self.gui_completion = True
125 self.gui_completion = True
@@ -130,14 +131,10 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
130 self._prompt_pos = 0
131 self._prompt_pos = 0
131 self._reading = False
132 self._reading = False
132
133
133 # Set a monospaced font
134 # Set a monospaced font.
134 point_size = QtGui.QApplication.font().pointSize()
135 self.reset_font()
135 font = QtGui.QFont('Monospace', point_size)
136 font.setStyleHint(QtGui.QFont.TypeWriter)
137 self._completion_widget.setFont(font)
138 self.document().setDefaultFont(font)
139
136
140 # Define a custom context menu
137 # Define a custom context menu.
141 self._context_menu = QtGui.QMenu(self)
138 self._context_menu = QtGui.QMenu(self)
142
139
143 copy_action = QtGui.QAction('Copy', self)
140 copy_action = QtGui.QAction('Copy', self)
@@ -362,7 +359,9 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
362 return self._execute(interactive=interactive)
359 return self._execute(interactive=interactive)
363
360
364 def _get_input_buffer(self):
361 def _get_input_buffer(self):
365 # If we're executing, the input buffer may not even exist anymore due
362 """ The text that the user has entered entered at the current prompt.
363 """
364 # If we're executing, the input buffer may not even exist anymore due to
366 # the limit imposed by 'buffer_size'. Therefore, we store it.
365 # the limit imposed by 'buffer_size'. Therefore, we store it.
367 if self._executing:
366 if self._executing:
368 return self._executing_input_buffer
367 return self._executing_input_buffer
@@ -374,17 +373,19 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
374 # out the Unicode line break characters that Qt insists on inserting.
373 # out the Unicode line break characters that Qt insists on inserting.
375 input_buffer = str(cursor.selection().toPlainText())
374 input_buffer = str(cursor.selection().toPlainText())
376
375
377 # Strip out continuation prompts
376 # Strip out continuation prompts.
378 return input_buffer.replace('\n' + self._continuation_prompt, '\n')
377 return input_buffer.replace('\n' + self._continuation_prompt, '\n')
379
378
380 def _set_input_buffer(self, string):
379 def _set_input_buffer(self, string):
381 # Add continuation prompts where necessary
380 """ Replaces the text in the input buffer with 'string'.
381 """
382 # Add continuation prompts where necessary.
382 lines = string.splitlines()
383 lines = string.splitlines()
383 for i in xrange(1, len(lines)):
384 for i in xrange(1, len(lines)):
384 lines[i] = self._continuation_prompt + lines[i]
385 lines[i] = self._continuation_prompt + lines[i]
385 string = '\n'.join(lines)
386 string = '\n'.join(lines)
386
387
387 # Replace buffer with new text
388 # Replace buffer with new text.
388 cursor = self._get_end_cursor()
389 cursor = self._get_end_cursor()
389 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
390 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
390 cursor.insertText(string)
391 cursor.insertText(string)
@@ -393,6 +394,9 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
393 input_buffer = property(_get_input_buffer, _set_input_buffer)
394 input_buffer = property(_get_input_buffer, _set_input_buffer)
394
395
395 def _get_input_buffer_cursor_line(self):
396 def _get_input_buffer_cursor_line(self):
397 """ The text in the line of the input buffer in which the user's cursor
398 rests. Returns a string if there is such a line; otherwise, None.
399 """
396 if self._executing:
400 if self._executing:
397 return None
401 return None
398 cursor = self.textCursor()
402 cursor = self.textCursor()
@@ -407,6 +411,32 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
407
411
408 input_buffer_cursor_line = property(_get_input_buffer_cursor_line)
412 input_buffer_cursor_line = property(_get_input_buffer_cursor_line)
409
413
414 def _get_font(self):
415 """ The base font being used by the ConsoleWidget.
416 """
417 return self.document().defaultFont()
418
419 def _set_font(self, font):
420 """ Sets the base font for the ConsoleWidget to the specified QFont.
421 """
422 self._completion_widget.setFont(font)
423 self.document().setDefaultFont(font)
424
425 font = property(_get_font, _set_font)
426
427 def reset_font(self):
428 """ Sets the font to the default fixed-width font for this platform.
429 """
430 if sys.platform == 'win32':
431 name = 'Courier'
432 elif sys.platform == 'darwin':
433 name = 'Monaco'
434 else:
435 name = 'Monospace'
436 font = QtGui.QFont(name, QtGui.qApp.font().pointSize())
437 font.setStyleHint(QtGui.QFont.TypeWriter)
438 self._set_font(font)
439
410 #---------------------------------------------------------------------------
440 #---------------------------------------------------------------------------
411 # 'ConsoleWidget' abstract interface
441 # 'ConsoleWidget' abstract interface
412 #---------------------------------------------------------------------------
442 #---------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now