Paint odd/even tabs in different colors

This commit is contained in:
Florian Bruhin 2014-07-14 15:30:36 +02:00
parent 256d7e90b9
commit 92d28a66d5
2 changed files with 62 additions and 37 deletions

View File

@ -856,9 +856,13 @@ DATA = OrderedDict([
SettingValue(types.Color(), 'white'), SettingValue(types.Color(), 'white'),
"Foreground color of tabs."), "Foreground color of tabs."),
('tab.bg', ('tab.bg.odd',
SettingValue(types.Color(), 'grey'), SettingValue(types.QtColor(), 'grey'),
"Background color of unselected tabs."), "Background color of unselected odd tabs."),
('tab.bg.even',
SettingValue(types.QtColor(), 'darkgrey'),
"Background color of unselected even tabs."),
('tab.bg.selected', ('tab.bg.selected',
SettingValue(types.Color(), 'black'), SettingValue(types.Color(), 'black'),

View File

@ -23,7 +23,7 @@ import functools
from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QSize from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QSize
from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle, from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle,
QStyle) QStyle, QStylePainter, QStyleOptionTab)
from PyQt5.QtGui import QIcon, QPixmap, QPalette, QColor from PyQt5.QtGui import QIcon, QPixmap, QPalette, QColor
import qutebrowser.config.config as config import qutebrowser.config.config as config
@ -67,24 +67,6 @@ class TabWidget(QTabWidget):
{font[tabbar]} {font[tabbar]}
{color[tab.bg.bar]} {color[tab.bg.bar]}
}} }}
QTabBar::tab {{
{color[tab.bg]}
{color[tab.fg]}
margin: 0px;
}}
QTabBar::tab:first, QTabBar::tab:middle {{
border-right: 2px solid {color[tab.seperator]};
}}
QTabBar::tab:only-one {{
border-right: 0px;
}}
QTabBar::tab:selected {{
{color[tab.bg.selected]}
}}
""" """
def __init__(self, parent): def __init__(self, parent):
@ -96,7 +78,6 @@ class TabWidget(QTabWidget):
self.setDocumentMode(True) self.setDocumentMode(True)
self.setElideMode(Qt.ElideRight) self.setElideMode(Qt.ElideRight)
bar.setDrawBase(False) bar.setDrawBase(False)
bar.setStyle(TabBarStyle(bar.style()))
self._init_config() self._init_config()
def _init_config(self): def _init_config(self):
@ -141,6 +122,10 @@ class TabBar(QTabBar):
tab_rightclicked = pyqtSignal(int) tab_rightclicked = pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self.setStyle(TabBarStyle(self.style()))
def __repr__(self): def __repr__(self):
return '<{} with {} tabs>'.format(self.__class__.__name__, return '<{} with {} tabs>'.format(self.__class__.__name__,
self.count()) self.count())
@ -182,17 +167,32 @@ class TabBar(QTabBar):
https://wiki.python.org/moin/PyQt/Customising%20tab%20bars https://wiki.python.org/moin/PyQt/Customising%20tab%20bars
""" """
height = super().tabSizeHint(index).height() height = self.fontMetrics().height()
size = QSize(self.width() / self.count(), height) size = QSize(self.width() / self.count(), height)
qt_ensure_valid(size) qt_ensure_valid(size)
return size return size
def initStyleOption(self, option, tabIndex): def paintEvent(self, e):
"""Override initStyleOption so we can mark odd tabs.""" """Override paintEvent to draw the tabs like we want to."""
super().initStyleOption(option, tabIndex) p = QStylePainter(self)
if tabIndex % 2: tab = QStyleOptionTab()
option |= 0x10000000 selected = self.currentIndex()
return option for idx in range(self.count()):
self.initStyleOption(tab, idx)
if idx == selected:
color = config.get('colors', 'tab.bg.selected')
elif idx % 2:
color = config.get('colors', 'tab.bg.odd')
else:
color = config.get('colors', 'tab.bg.even')
tab.palette.setColor(QPalette.Window, QColor(color))
tab.palette.setColor(QPalette.WindowText,
QColor(config.get('colors', 'tab.fg')))
if tab.rect.right() < 0 or tab.rect.left() > self.width():
# Don't bother drawing a tab if the entire tab is outside of
# the visible tab bar.
continue
p.drawControl(QStyle.CE_TabBarTab, tab)
class TabBarStyle(QCommonStyle): class TabBarStyle(QCommonStyle):
@ -285,10 +285,31 @@ class TabBarStyle(QCommonStyle):
enabled, text, textRole) enabled, text, textRole)
def drawControl(self, element, opt, p, widget=None): def drawControl(self, element, opt, p, widget=None):
"""Override drawControl to draw odd tabs in a different color.""" """Override drawControl to draw odd tabs in a different color.
if element != QStyle.CE_TabBarTab:
super().drawControl(element, opt, p, widget) Draws the given element with the provided painter with the style
return options specified by option.
raise Exception
opt.palette.setColor(QPalette.Base, QColor('red')) Args:
self._style.drawControl(element, opt, p, widget) element: ControlElement
option: const QStyleOption *
painter: QPainter *
widget: const QWidget *
"""
if element == QStyle.CE_TabBarTab:
# We override this so we can control TabBarTabShape/TabBarTabLabel.
self.drawControl(QStyle.CE_TabBarTabShape, opt, p, widget)
self.drawControl(QStyle.CE_TabBarTabLabel, opt, p, widget)
elif element == QStyle.CE_TabBarTabShape:
# We use super() rather than self._style here because we don't want
# any sophisticated drawing.
p.fillRect(opt.rect, opt.palette.window())
super().drawControl(QStyle.CE_TabBarTabShape, opt, p, widget)
elif element == QStyle.CE_TabBarTabLabel:
# We use super() rather than self._style here so our drawItemText()
# gets called.
super().drawControl(QStyle.CE_TabBarTabLabel, opt, p, widget)
else:
# For any other elements we just delegate the work to our real
# style.
self._style.drawControl(element, opt, p, widget)