##// END OF EJS Templates
* Fleshed out IPythonWidget's style control....
epatters -
Show More
@@ -576,15 +576,15 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
576 if self.gui_completion:
576 if self.gui_completion:
577 self._completion_widget.show_items(cursor, items)
577 self._completion_widget.show_items(cursor, items)
578 else:
578 else:
579 text = self.format_as_columns(items)
579 text = self._format_as_columns(items)
580 self._append_plain_text_keeping_prompt(text)
580 self._append_plain_text_keeping_prompt(text)
581
581
582 def format_as_columns(self, items, separator=' '):
582 def _format_as_columns(self, items, separator=' '):
583 """ Transform a list of strings into a single string with columns.
583 """ Transform a list of strings into a single string with columns.
584
584
585 Parameters
585 Parameters
586 ----------
586 ----------
587 items : sequence [str]
587 items : sequence of strings
588 The strings to process.
588 The strings to process.
589
589
590 separator : str, optional [default is two spaces]
590 separator : str, optional [default is two spaces]
@@ -600,10 +600,10 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
600 font_metrics = QtGui.QFontMetrics(self.font)
600 font_metrics = QtGui.QFontMetrics(self.font)
601 displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1)
601 displaywidth = max(5, (self.width() / font_metrics.width(' ')) - 1)
602
602
603 # Some degenerate cases
603 # Some degenerate cases.
604 size = len(items)
604 size = len(items)
605 if size == 0:
605 if size == 0:
606 return "\n"
606 return '\n'
607 elif size == 1:
607 elif size == 1:
608 return '%s\n' % str(items[0])
608 return '%s\n' % str(items[0])
609
609
@@ -643,7 +643,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
643 del texts[-1]
643 del texts[-1]
644 for col in range(len(texts)):
644 for col in range(len(texts)):
645 texts[col] = texts[col].ljust(colwidths[col])
645 texts[col] = texts[col].ljust(colwidths[col])
646 string += "%s\n" % str(separator.join(texts))
646 string += '%s\n' % str(separator.join(texts))
647 return string
647 return string
648
648
649 def _get_block_plain_text(self, block):
649 def _get_block_plain_text(self, block):
@@ -10,7 +10,7 b' class IPythonWidget(FrontendWidget):'
10 """ A FrontendWidget for an IPython kernel.
10 """ A FrontendWidget for an IPython kernel.
11 """
11 """
12
12
13 # The default stylesheet for prompts, colors, etc.
13 # The default stylesheet: black text on a white background.
14 default_stylesheet = """
14 default_stylesheet = """
15 .error { color: red; }
15 .error { color: red; }
16 .in-prompt { color: navy; }
16 .in-prompt { color: navy; }
@@ -19,6 +19,17 b' class IPythonWidget(FrontendWidget):'
19 .out-prompt-number { font-weight: bold; }
19 .out-prompt-number { font-weight: bold; }
20 """
20 """
21
21
22 # A dark stylesheet: white text on a black background.
23 dark_stylesheet = """
24 QPlainTextEdit { background-color: black; color: white }
25 QFrame { border: 1px solid grey; }
26 .error { color: red; }
27 .in-prompt { color: lime; }
28 .in-prompt-number { color: lime; font-weight: bold; }
29 .out-prompt { color: red; }
30 .out-prompt-number { color: red; font-weight: bold; }
31 """
32
22 #---------------------------------------------------------------------------
33 #---------------------------------------------------------------------------
23 # 'QObject' interface
34 # 'QObject' interface
24 #---------------------------------------------------------------------------
35 #---------------------------------------------------------------------------
@@ -27,34 +38,10 b' class IPythonWidget(FrontendWidget):'
27 super(IPythonWidget, self).__init__(parent)
38 super(IPythonWidget, self).__init__(parent)
28
39
29 # Initialize protected variables.
40 # Initialize protected variables.
30 self._magic_overrides = {}
31 self._prompt_count = 0
41 self._prompt_count = 0
32
42
33 # Set a default stylesheet.
43 # Set a default stylesheet.
34 self.set_style_sheet(self.default_stylesheet)
44 self.reset_style_sheet()
35
36 #---------------------------------------------------------------------------
37 # 'ConsoleWidget' abstract interface
38 #---------------------------------------------------------------------------
39
40 def _execute(self, source, hidden):
41 """ Reimplemented to override magic commands.
42 """
43 magic_source = source.strip()
44 if magic_source.startswith('%'):
45 magic_source = magic_source[1:]
46 magic, sep, arguments = magic_source.partition(' ')
47 if not magic:
48 magic = magic_source
49
50 callback = self._magic_overrides.get(magic)
51 if callback:
52 output = callback(arguments)
53 if output:
54 self.appendPlainText(output)
55 self._show_interpreter_prompt()
56 else:
57 super(IPythonWidget, self)._execute(source, hidden)
58
45
59 #---------------------------------------------------------------------------
46 #---------------------------------------------------------------------------
60 # 'FrontendWidget' interface
47 # 'FrontendWidget' interface
@@ -119,26 +106,15 b' class IPythonWidget(FrontendWidget):'
119 # 'IPythonWidget' interface
106 # 'IPythonWidget' interface
120 #---------------------------------------------------------------------------
107 #---------------------------------------------------------------------------
121
108
122 def set_magic_override(self, magic, callback):
109 def reset_style_sheet(self):
123 """ Overrides an IPython magic command. This magic will be intercepted
110 """ Sets the style sheet to the default style sheet.
124 by the frontend rather than passed on to the kernel and 'callback'
125 will be called with a single argument: a string of argument(s) for
126 the magic. The callback can (optionally) return text to print to the
127 console.
128 """
111 """
129 self._magic_overrides[magic] = callback
112 self.set_style_sheet(self.default_stylesheet)
130
131 def remove_magic_override(self, magic):
132 """ Removes the override for the specified magic, if there is one.
133 """
134 try:
135 del self._magic_overrides[magic]
136 except KeyError:
137 pass
138
113
139 def set_style_sheet(self, stylesheet):
114 def set_style_sheet(self, stylesheet):
140 """ Sets the style sheet.
115 """ Sets the style sheet.
141 """
116 """
117 self.setStyleSheet(stylesheet)
142 self.document().setDefaultStyleSheet(stylesheet)
118 self.document().setDefaultStyleSheet(stylesheet)
143
119
144
120
@@ -66,7 +66,7 b" def get_tokens_unprocessed(self, text, stack=('root',)):"
66 RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed
66 RegexLexer.get_tokens_unprocessed = get_tokens_unprocessed
67
67
68
68
69 class BlockUserData(QtGui.QTextBlockUserData):
69 class PygmentsBlockUserData(QtGui.QTextBlockUserData):
70 """ Storage for the user data associated with each line.
70 """ Storage for the user data associated with each line.
71 """
71 """
72
72
@@ -81,7 +81,7 b' class BlockUserData(QtGui.QTextBlockUserData):'
81 attrs = ['syntax_stack']
81 attrs = ['syntax_stack']
82 kwds = ', '.join([ '%s=%r' % (attr, getattr(self, attr))
82 kwds = ', '.join([ '%s=%r' % (attr, getattr(self, attr))
83 for attr in attrs ])
83 for attr in attrs ])
84 return 'BlockUserData(%s)' % kwds
84 return 'PygmentsBlockUserData(%s)' % kwds
85
85
86
86
87 class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
87 class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
@@ -117,7 +117,8 b' class PygmentsHighlighter(QtGui.QSyntaxHighlighter):'
117 index += l
117 index += l
118
118
119 if hasattr(self._lexer, '_saved_state_stack'):
119 if hasattr(self._lexer, '_saved_state_stack'):
120 data = BlockUserData(syntax_stack=self._lexer._saved_state_stack)
120 data = PygmentsBlockUserData(
121 syntax_stack=self._lexer._saved_state_stack)
121 self.currentBlock().setUserData(data)
122 self.currentBlock().setUserData(data)
122 # Clean up for the next go-round.
123 # Clean up for the next go-round.
123 del self._lexer._saved_state_stack
124 del self._lexer._saved_state_stack
@@ -174,6 +175,8 b' class PygmentsHighlighter(QtGui.QSyntaxHighlighter):'
174 return result
175 return result
175
176
176 def _get_color(self, color):
177 def _get_color(self, color):
178 """ Returns a QColor built from a Pygments color string.
179 """
177 qcolor = QtGui.QColor()
180 qcolor = QtGui.QColor()
178 qcolor.setRgb(int(color[:2], base=16),
181 qcolor.setRgb(int(color[:2], base=16),
179 int(color[2:4], base=16),
182 int(color[2:4], base=16),
General Comments 0
You need to be logged in to leave comments. Login now