diff options
author | Florian Bruhin <me@the-compiler.org> | 2022-12-06 18:55:23 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2022-12-06 18:55:23 +0100 |
commit | cafbf96fdd7a835789d075f9d3d7c34f25a0f459 (patch) | |
tree | f18f74acb3652f825ac6003bfc643a3a0a3023f0 | |
parent | 6373959824529a0cfa18df53a9d02b0fbe7859a5 (diff) | |
download | qutebrowser-cafbf96fdd7a835789d075f9d3d7c34f25a0f459.tar.gz qutebrowser-cafbf96fdd7a835789d075f9d3d7c34f25a0f459.zip |
Use QProxyStyle for TabBarStyle
Closes #812
This does *not* seem to help with the crashes in #5385 or #5124, weirdly
enough, but it still seems simpler, so why not do it.
-rw-r--r-- | qutebrowser/mainwindow/tabwidget.py | 63 |
1 files changed, 14 insertions, 49 deletions
diff --git a/qutebrowser/mainwindow/tabwidget.py b/qutebrowser/mainwindow/tabwidget.py index b157fc07a..4d447ff48 100644 --- a/qutebrowser/mainwindow/tabwidget.py +++ b/qutebrowser/mainwindow/tabwidget.py @@ -26,9 +26,9 @@ from typing import Optional, Dict, Any from qutebrowser.qt.core import (pyqtSignal, pyqtSlot, Qt, QSize, QRect, QPoint, QTimer, QUrl) -from qutebrowser.qt.widgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle, +from qutebrowser.qt.widgets import (QTabWidget, QTabBar, QSizePolicy, QProxyStyle, QStyle, QStylePainter, QStyleOptionTab, - QStyleFactory) + QStyleFactory, QCommonStyle) from qutebrowser.qt.gui import QIcon, QPalette, QColor from qutebrowser.utils import qtutils, objreg, utils, usertypes, log @@ -775,7 +775,7 @@ class Layouts: indicator: QRect -class TabBarStyle(QCommonStyle): +class TabBarStyle(QProxyStyle): """Qt style used by TabBar to fix some issues with the default one. @@ -783,45 +783,10 @@ class TabBarStyle(QCommonStyle): - Remove the focus rectangle Ubuntu draws on tabs. - Force text to be left-aligned even though Qt has "centered" hardcoded. - - Unfortunately PyQt doesn't support QProxyStyle, so we need to do this the - hard way... - - Based on: - - https://stackoverflow.com/a/17294081 - https://code.google.com/p/makehuman/source/browse/trunk/makehuman/lib/qtgui.py """ ICON_PADDING = 4 - def __init__(self): - """Initialize all functions we're not overriding. - - This simply calls the corresponding function in self._style. - """ - self._style = QStyleFactory.create('Fusion') - for method in ['drawComplexControl', 'drawItemPixmap', - 'generatedIconPixmap', 'hitTestComplexControl', - 'itemPixmapRect', 'itemTextRect', 'polish', 'styleHint', - 'subControlRect', 'unpolish', 'drawItemText', - 'sizeFromContents', 'drawPrimitive']: - setattr(self, method, functools.partial(self._fusion_call, method)) - super().__init__() - - def _fusion_call(self, method: str, *args: Any) -> Any: - """Wrap a call to self._style to log RuntimeErrors. - - WORKAROUND for https://github.com/qutebrowser/qutebrowser/issues/5124 - """ - target = getattr(self._style, method) - try: - return target(*args) - except RuntimeError: - info = f"self._style.{method}{args}" - log.misc.warning(f"Got RuntimeError while calling {info}") - raise - def _draw_indicator(self, layouts, opt, p): """Draw the tab indicator. @@ -849,7 +814,7 @@ class TabBarStyle(QCommonStyle): icon_state = (QIcon.State.On if opt.state & QStyle.StateFlag.State_Selected else QIcon.State.Off) icon = opt.icon.pixmap(opt.iconSize, icon_mode, icon_state) - self._style.drawItemPixmap(p, layouts.icon, Qt.AlignmentFlag.AlignCenter, icon) + self.baseStyle().drawItemPixmap(p, layouts.icon, Qt.AlignmentFlag.AlignCenter, icon) def drawControl(self, element, opt, p, widget=None): """Override drawControl to draw odd tabs in a different color. @@ -866,7 +831,7 @@ class TabBarStyle(QCommonStyle): if element not in [QStyle.ControlElement.CE_TabBarTab, QStyle.ControlElement.CE_TabBarTabShape, QStyle.ControlElement.CE_TabBarTabLabel]: # Let the real style draw it. - self._style.drawControl(element, opt, p, widget) + self.baseStyle().drawControl(element, opt, p, widget) return layouts = self._tab_layout(opt) @@ -881,15 +846,15 @@ class TabBarStyle(QCommonStyle): elif element == QStyle.ControlElement.CE_TabBarTabShape: p.fillRect(opt.rect, opt.palette.window()) self._draw_indicator(layouts, opt, p) - # We use super() rather than self._style here because we don't want + # We use QCommonStyle rather than self.baseStyle() here because we don't want # any sophisticated drawing. - super().drawControl(QStyle.ControlElement.CE_TabBarTabShape, opt, p, widget) + QCommonStyle.drawControl(self, QStyle.ControlElement.CE_TabBarTabShape, opt, p, widget) elif element == QStyle.ControlElement.CE_TabBarTabLabel: if not opt.icon.isNull() and layouts.icon.isValid(): self._draw_icon(layouts, opt, p) alignment = (config.cache['tabs.title.alignment'] | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextHideMnemonic) - self._style.drawItemText(p, + self.baseStyle().drawItemText(p, layouts.text, int(alignment), opt.palette, @@ -917,7 +882,7 @@ class TabBarStyle(QCommonStyle): QStyle.PixelMetric.PM_TabBarScrollButtonWidth]: return 0 else: - return self._style.pixelMetric(metric, option, widget) + return self.baseStyle().pixelMetric(metric, option, widget) def subElementRect(self, sr, opt, widget=None): """Override subElementRect to use our own _tab_layout implementation. @@ -942,12 +907,12 @@ class TabBarStyle(QCommonStyle): # aligned properly. Otherwise, empty space will be shown after the # last tab even though the button width is set to 0 # - # Need to use super() because we also use super() to render + # Need to use QCommonStyle here because we also use it to render # element in drawControl(); otherwise, we may get bit by # style differences... - return super().subElementRect(sr, opt, widget) + return QCommonStyle.subElementRect(self, sr, opt, widget) else: - return self._style.subElementRect(sr, opt, widget) + return self.baseStyle().subElementRect(sr, opt, widget) def _tab_layout(self, opt): """Compute the text/icon rect from the opt rect. @@ -994,7 +959,7 @@ class TabBarStyle(QCommonStyle): text_rect.adjust( icon_rect.width() + TabBarStyle.ICON_PADDING, 0, 0, 0) - text_rect = self._style.visualRect(opt.direction, opt.rect, text_rect) + text_rect = self.baseStyle().visualRect(opt.direction, opt.rect, text_rect) return Layouts(text=text_rect, icon=icon_rect, indicator=indicator_rect) @@ -1029,5 +994,5 @@ class TabBarStyle(QCommonStyle): icon_top = text_rect.center().y() + 1 - tab_icon_size.height() // 2 icon_rect = QRect(QPoint(text_rect.left(), icon_top), tab_icon_size) - icon_rect = self._style.visualRect(opt.direction, opt.rect, icon_rect) + icon_rect = self.baseStyle().visualRect(opt.direction, opt.rect, icon_rect) return icon_rect |