From 2b3bc44178d5b4d5fc916e4d59f5aa5ce9b6827c 2012-02-08 23:05:34
From: Min RK <benjaminrk@gmail.com>
Date: 2012-02-08 23:05:34
Subject: [PATCH] Merge pull request #1363 from minrk/qtstyle

Fix some minor color/style config issues in the qtconsole

The tabbed code changed a few things about loading styles and stylesheets in the qtconsole, which this resolves, among a couple other small color-related issues:

* tabs other than the first get colors
* IPythonWidget.style_sheet configurable is not clobbered by defaults
* style_sheet configurable would cause a crash, because _style_sheet_changed could be called before _control existed
* fix str->unicode token preventing stylesheets without syntax highlighting from working
* fix typo in the function used to detect whether a bgcolor is dark or light

---

diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py
index 3372daf..f4e7d3a 100644
--- a/IPython/frontend/qt/console/console_widget.py
+++ b/IPython/frontend/qt/console/console_widget.py
@@ -137,7 +137,12 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):
     font_changed = QtCore.Signal(QtGui.QFont)
 
     #------ Protected class variables ------------------------------------------
-
+    
+    # control handles
+    _control = None
+    _page_control = None
+    _splitter = None
+    
     # When the control key is down, these keys are mapped.
     _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left,
                          QtCore.Qt.Key_F : QtCore.Qt.Key_Right,
@@ -182,8 +187,6 @@ class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):
         layout = QtGui.QStackedLayout(self)
         layout.setContentsMargins(0, 0, 0, 0)
         self._control = self._create_control()
-        self._page_control = None
-        self._splitter = None
         if self.paging in ('hsplit', 'vsplit'):
             self._splitter = QtGui.QSplitter()
             if self.paging == 'hsplit':
diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py
index 81fddef..04490d6 100644
--- a/IPython/frontend/qt/console/ipython_widget.py
+++ b/IPython/frontend/qt/console/ipython_widget.py
@@ -533,12 +533,14 @@ class IPythonWidget(FrontendWidget):
         """ Set the style sheets of the underlying widgets.
         """
         self.setStyleSheet(self.style_sheet)
-        self._control.document().setDefaultStyleSheet(self.style_sheet)
-        if self._page_control:
+        if self._control is not None:
+            self._control.document().setDefaultStyleSheet(self.style_sheet)
+            bg_color = self._control.palette().window().color()
+            self._ansi_processor.set_background_color(bg_color)
+        
+        if self._page_control is not None:
             self._page_control.document().setDefaultStyleSheet(self.style_sheet)
 
-        bg_color = self._control.palette().window().color()
-        self._ansi_processor.set_background_color(bg_color)
 
 
     def _syntax_style_changed(self):
diff --git a/IPython/frontend/qt/console/pygments_highlighter.py b/IPython/frontend/qt/console/pygments_highlighter.py
index a80bcec..ef964d0 100644
--- a/IPython/frontend/qt/console/pygments_highlighter.py
+++ b/IPython/frontend/qt/console/pygments_highlighter.py
@@ -174,7 +174,7 @@ class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
     def _get_format_from_document(self, token, document):
         """ Returns a QTextCharFormat for token by
         """
-        code, html = self._formatter._format_lines([(token, 'dummy')]).next()
+        code, html = self._formatter._format_lines([(token, u'dummy')]).next()
         self._document.setHtml(html)
         return QtGui.QTextCursor(self._document).charFormat()
 
diff --git a/IPython/frontend/qt/console/qtconsoleapp.py b/IPython/frontend/qt/console/qtconsoleapp.py
index 72ebdb0..525ba91 100644
--- a/IPython/frontend/qt/console/qtconsoleapp.py
+++ b/IPython/frontend/qt/console/qtconsoleapp.py
@@ -190,6 +190,7 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
         kernel_manager.start_channels()
         widget = self.widget_factory(config=self.config,
                                    local_kernel=True)
+        self.init_colors(widget)
         widget.kernel_manager = kernel_manager
         widget._existing = False
         widget._may_close = True
@@ -212,6 +213,7 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
         kernel_manager.start_channels()
         widget = self.widget_factory(config=self.config,
                                 local_kernel=False)
+        self.init_colors(widget)
         widget._existing = True
         widget._may_close = False
         widget._confirm_exit = False
@@ -230,6 +232,7 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
         local_kernel = (not self.existing) or self.ip in LOCAL_IPS
         self.widget = self.widget_factory(config=self.config,
                                         local_kernel=local_kernel)
+        self.init_colors(self.widget)
         self.widget._existing = self.existing
         self.widget._may_close = not self.existing
         self.widget._confirm_exit = self.confirm_exit
@@ -246,7 +249,7 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
 
         self.window.setWindowTitle('Python' if self.pure else 'IPython')
 
-    def init_colors(self):
+    def init_colors(self, widget):
         """Configure the coloring of the widget"""
         # Note: This will be dramatically simplified when colors
         # are removed from the backend.
@@ -264,6 +267,10 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
             style = self.config.IPythonWidget.syntax_style
         except AttributeError:
             style = None
+        try:
+            sheet = self.config.IPythonWidget.style_sheet
+        except AttributeError:
+            sheet = None
 
         # find the value for colors:
         if colors:
@@ -284,30 +291,27 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
         else:
             colors=None
 
-        # Configure the style.
-        widget = self.widget
+        # Configure the style
         if style:
             widget.style_sheet = styles.sheet_from_template(style, colors)
             widget.syntax_style = style
             widget._syntax_style_changed()
             widget._style_sheet_changed()
         elif colors:
-            # use a default style
+            # use a default dark/light/bw style
             widget.set_default_style(colors=colors)
-        else:
-            # this is redundant for now, but allows the widget's
-            # defaults to change
-            widget.set_default_style()
 
         if self.stylesheet:
-            # we got an expicit stylesheet
+            # we got an explicit stylesheet
             if os.path.isfile(self.stylesheet):
                 with open(self.stylesheet) as f:
                     sheet = f.read()
-                widget.style_sheet = sheet
-                widget._style_sheet_changed()
             else:
-                raise IOError("Stylesheet %r not found."%self.stylesheet)
+                raise IOError("Stylesheet %r not found." % self.stylesheet)
+        if sheet:
+            widget.style_sheet = sheet
+            widget._style_sheet_changed()
+            
 
     def init_signal(self):
         """allow clean shutdown on sigint"""
@@ -327,7 +331,6 @@ class IPythonQtConsoleApp(BaseIPythonApplication, IPythonConsoleApp):
         super(IPythonQtConsoleApp, self).initialize(argv)
         IPythonConsoleApp.initialize(self,argv)
         self.init_qt_elements()
-        self.init_colors()
         self.init_signal()
 
     def start(self):
diff --git a/IPython/frontend/qt/console/styles.py b/IPython/frontend/qt/console/styles.py
index e92eaf0..c8e2ba7 100644
--- a/IPython/frontend/qt/console/styles.py
+++ b/IPython/frontend/qt/console/styles.py
@@ -64,8 +64,8 @@ def hex_to_rgb(color):
         return False
     try:
         r = int(color[:2],16)
-        g = int(color[:2],16)
-        b = int(color[:2],16)
+        g = int(color[2:4],16)
+        b = int(color[4:],16)
     except ValueError:
         return False
     else: