From 1a28e3247b35d21cebaa18f9131a7547ad0ed6a1 2010-08-09 21:57:12 From: epatters Date: 2010-08-09 21:57:12 Subject: [PATCH] * Fleshed out IPythonWidget's style control. * Removed magic override code to reflect new magic design. * Misc. cleanup. --- diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py index aac9c09..93ccdf5 100644 --- a/IPython/frontend/qt/console/console_widget.py +++ b/IPython/frontend/qt/console/console_widget.py @@ -576,15 +576,15 @@ class ConsoleWidget(QtGui.QPlainTextEdit): if self.gui_completion: self._completion_widget.show_items(cursor, items) else: - text = self.format_as_columns(items) + text = self._format_as_columns(items) self._append_plain_text_keeping_prompt(text) - def format_as_columns(self, items, separator=' '): + def _format_as_columns(self, items, separator=' '): """ Transform a list of strings into a single string with columns. Parameters ---------- - items : sequence [str] + items : sequence of strings The strings to process. separator : str, optional [default is two spaces] @@ -600,10 +600,10 @@ class ConsoleWidget(QtGui.QPlainTextEdit): font_metrics = QtGui.QFontMetrics(self.font) displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1) - # Some degenerate cases + # Some degenerate cases. size = len(items) if size == 0: - return "\n" + return '\n' elif size == 1: return '%s\n' % str(items[0]) @@ -643,7 +643,7 @@ class ConsoleWidget(QtGui.QPlainTextEdit): del texts[-1] for col in range(len(texts)): texts[col] = texts[col].ljust(colwidths[col]) - string += "%s\n" % str(separator.join(texts)) + string += '%s\n' % str(separator.join(texts)) return string def _get_block_plain_text(self, block): diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py index ec34d66..ff16e38 100644 --- a/IPython/frontend/qt/console/ipython_widget.py +++ b/IPython/frontend/qt/console/ipython_widget.py @@ -10,7 +10,7 @@ class IPythonWidget(FrontendWidget): """ A FrontendWidget for an IPython kernel. """ - # The default stylesheet for prompts, colors, etc. + # The default stylesheet: black text on a white background. default_stylesheet = """ .error { color: red; } .in-prompt { color: navy; } @@ -19,6 +19,17 @@ class IPythonWidget(FrontendWidget): .out-prompt-number { font-weight: bold; } """ + # A dark stylesheet: white text on a black background. + dark_stylesheet = """ + QPlainTextEdit { background-color: black; color: white } + QFrame { border: 1px solid grey; } + .error { color: red; } + .in-prompt { color: lime; } + .in-prompt-number { color: lime; font-weight: bold; } + .out-prompt { color: red; } + .out-prompt-number { color: red; font-weight: bold; } + """ + #--------------------------------------------------------------------------- # 'QObject' interface #--------------------------------------------------------------------------- @@ -27,34 +38,10 @@ class IPythonWidget(FrontendWidget): super(IPythonWidget, self).__init__(parent) # Initialize protected variables. - self._magic_overrides = {} self._prompt_count = 0 # Set a default stylesheet. - self.set_style_sheet(self.default_stylesheet) - - #--------------------------------------------------------------------------- - # 'ConsoleWidget' abstract interface - #--------------------------------------------------------------------------- - - def _execute(self, source, hidden): - """ Reimplemented to override magic commands. - """ - magic_source = source.strip() - if magic_source.startswith('%'): - magic_source = magic_source[1:] - magic, sep, arguments = magic_source.partition(' ') - if not magic: - magic = magic_source - - callback = self._magic_overrides.get(magic) - if callback: - output = callback(arguments) - if output: - self.appendPlainText(output) - self._show_interpreter_prompt() - else: - super(IPythonWidget, self)._execute(source, hidden) + self.reset_style_sheet() #--------------------------------------------------------------------------- # 'FrontendWidget' interface @@ -119,26 +106,15 @@ class IPythonWidget(FrontendWidget): # 'IPythonWidget' interface #--------------------------------------------------------------------------- - def set_magic_override(self, magic, callback): - """ Overrides an IPython magic command. This magic will be intercepted - by the frontend rather than passed on to the kernel and 'callback' - will be called with a single argument: a string of argument(s) for - the magic. The callback can (optionally) return text to print to the - console. + def reset_style_sheet(self): + """ Sets the style sheet to the default style sheet. """ - self._magic_overrides[magic] = callback - - def remove_magic_override(self, magic): - """ Removes the override for the specified magic, if there is one. - """ - try: - del self._magic_overrides[magic] - except KeyError: - pass + self.set_style_sheet(self.default_stylesheet) def set_style_sheet(self, stylesheet): """ Sets the style sheet. """ + self.setStyleSheet(stylesheet) self.document().setDefaultStyleSheet(stylesheet) diff --git a/IPython/frontend/qt/console/pygments_highlighter.py b/IPython/frontend/qt/console/pygments_highlighter.py index 4b45dc9..d463616 100644 --- a/IPython/frontend/qt/console/pygments_highlighter.py +++ b/IPython/frontend/qt/console/pygments_highlighter.py @@ -66,7 +66,7 @@ def get_tokens_unprocessed(self, text, stack=('root',)): RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed -class BlockUserData(QtGui.QTextBlockUserData): +class PygmentsBlockUserData(QtGui.QTextBlockUserData): """ Storage for the user data associated with each line. """ @@ -81,7 +81,7 @@ class BlockUserData(QtGui.QTextBlockUserData): attrs = ['syntax_stack'] kwds = ', '.join([ '%s=%r' % (attr, getattr(self, attr)) for attr in attrs ]) - return 'BlockUserData(%s)' % kwds + return 'PygmentsBlockUserData(%s)' % kwds class PygmentsHighlighter(QtGui.QSyntaxHighlighter): @@ -117,7 +117,8 @@ class PygmentsHighlighter(QtGui.QSyntaxHighlighter): index += l if hasattr(self._lexer, '_saved_state_stack'): - data = BlockUserData(syntax_stack=self._lexer._saved_state_stack) + data = PygmentsBlockUserData( + syntax_stack=self._lexer._saved_state_stack) self.currentBlock().setUserData(data) # Clean up for the next go-round. del self._lexer._saved_state_stack @@ -174,6 +175,8 @@ class PygmentsHighlighter(QtGui.QSyntaxHighlighter): return result def _get_color(self, color): + """ Returns a QColor built from a Pygments color string. + """ qcolor = QtGui.QColor() qcolor.setRgb(int(color[:2], base=16), int(color[2:4], base=16),