qutebrowser/qutebrowser/widgets/tabwidget.py

441 lines
16 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-02-06 14:01:23 +01:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2014-07-15 14:20:10 +02:00
"""The tab widget used for TabbedBrowser from browser.py.
Module attributes:
PM_TabBarPadding: The PixelMetric value for TabBarStyle to get the padding
between items.
"""
2014-02-17 12:23:52 +01:00
2014-07-13 22:10:15 +02:00
import functools
from PyQt5.QtCore import Qt, QSize, QRect, QPoint, QTimer
2014-07-13 22:10:15 +02:00
from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle,
2014-09-23 22:02:21 +02:00
QStyle, QStylePainter, QStyleOptionTab)
2014-07-14 18:01:02 +02:00
from PyQt5.QtGui import QIcon, QPalette, QColor
2013-12-15 20:29:39 +01:00
2014-09-26 15:48:24 +02:00
from qutebrowser.utils import qtutils, objreg, utils
2014-08-26 19:10:14 +02:00
from qutebrowser.config import config
2014-01-28 12:21:00 +01:00
2014-01-28 23:04:02 +01:00
2014-07-15 14:20:10 +02:00
PM_TabBarPadding = QStyle.PM_CustomBase
2013-12-15 20:29:39 +01:00
class TabWidget(QTabWidget):
2014-02-07 20:21:50 +01:00
"""The tabwidget used for TabbedBrowser."""
2014-09-24 21:00:05 +02:00
def __init__(self, parent=None):
super().__init__(parent)
2014-07-14 13:34:44 +02:00
bar = TabBar()
self.setTabBar(bar)
2014-07-15 22:14:16 +02:00
bar.tabCloseRequested.connect(self.tabCloseRequested)
2014-01-30 22:29:01 +01:00
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
2013-12-15 20:29:39 +01:00
self.setDocumentMode(True)
2014-01-19 16:56:33 +01:00
self.setElideMode(Qt.ElideRight)
2014-07-15 14:20:10 +02:00
self.setUsesScrollButtons(True)
2014-07-14 13:34:44 +02:00
bar.setDrawBase(False)
self.init_config()
config.on_change(self.init_config, 'tabs')
2014-02-13 08:56:01 +01:00
def init_config(self):
2014-02-13 08:56:01 +01:00
"""Initialize attributes based on the config."""
position_conv = {
'north': QTabWidget.North,
'south': QTabWidget.South,
'west': QTabWidget.West,
'east': QTabWidget.East,
}
select_conv = {
'left': QTabBar.SelectLeftTab,
'right': QTabBar.SelectRightTab,
'previous': QTabBar.SelectPreviousTab,
}
2014-07-15 21:20:57 +02:00
tabbar = self.tabBar()
2014-08-06 08:10:32 +02:00
self.setMovable(config.get('tabs', 'movable'))
2014-07-15 22:14:16 +02:00
self.setTabsClosable(False)
2014-08-06 08:10:32 +02:00
posstr = config.get('tabs', 'position')
selstr = config.get('tabs', 'select-on-remove')
2014-07-15 21:20:57 +02:00
position = position_conv[posstr]
self.setTabPosition(position)
tabbar.vertical = position in (QTabWidget.West, QTabWidget.East)
tabbar.setSelectionBehaviorOnRemove(select_conv[selstr])
2014-07-16 14:10:46 +02:00
tabbar.refresh()
2014-05-23 04:12:18 +02:00
class TabBar(QTabBar):
2014-07-15 16:29:41 +02:00
"""Custom tabbar with our own style.
2014-07-15 22:14:16 +02:00
FIXME: Dragging tabs doesn't look as nice as it does in QTabBar. However,
fixing this would be a lot of effort, so we'll postpone it until we're
reimplementing drag&drop for other reasons.
2014-07-11 07:01:08 +02:00
2014-07-15 21:20:57 +02:00
Attributes:
vertical: When the tab bar is currently vertical.
2014-07-11 07:01:08 +02:00
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setStyle(TabBarStyle(self.style()))
self.set_font()
config.on_change(self.set_font, 'fonts', 'tabbar')
2014-07-15 21:20:57 +02:00
self.vertical = False
self.setAutoFillBackground(True)
self.set_colors()
config.on_change(self.set_colors, 'colors', 'tab.bg.bar')
QTimer.singleShot(0, self.autohide)
config.on_change(self.autohide, 'tabs', 'auto-hide')
def __repr__(self):
2014-09-26 15:48:24 +02:00
return utils.get_repr(self, count=self.count())
def autohide(self):
"""Auto-hide the tabbar if needed."""
auto_hide = config.get('tabs', 'auto-hide')
if auto_hide and self.count() == 1:
self.hide()
else:
self.show()
2014-07-16 14:10:46 +02:00
def refresh(self):
"""Properly repaint the tab bar and relayout tabs."""
# This is a horrible hack, but we need to do this so the underlaying Qt
# code sets layoutDirty so it actually relayouts the tabs.
self.setIconSize(self.iconSize())
2014-07-16 13:51:16 +02:00
def set_tab_indicator_color(self, idx, color):
"""Set the tab indicator color.
Args:
idx: The tab index.
color: A QColor.
"""
self.setTabData(idx, color)
self.update(self.tabRect(idx))
def set_font(self):
"""Set the tabbar font."""
self.setFont(config.get('fonts', 'tabbar'))
def set_colors(self):
"""Set the tabbar colors."""
p = self.palette()
p.setColor(QPalette.Window, config.get('colors', 'tab.bg.bar'))
self.setPalette(p)
2014-05-23 04:12:18 +02:00
def mousePressEvent(self, e):
"""Override mousePressEvent to close tabs if configured."""
2014-08-06 08:10:32 +02:00
button = config.get('tabs', 'close-mouse-button')
if (e.button() == Qt.RightButton and button == 'right' or
e.button() == Qt.MiddleButton and button == 'middle'):
idx = self.tabAt(e.pos())
if idx != -1:
e.accept()
self.tabCloseRequested.emit(idx)
return
super().mousePressEvent(e)
2014-06-10 14:30:31 +02:00
2014-07-11 16:54:54 +02:00
def minimumTabSizeHint(self, index):
"""Override minimumTabSizeHint because we want no hard minimum.
There are two problems with having a hard minimum tab size:
- When expanding is True, the window will expand without stopping
on some window managers.
- We don't want the main window to get bigger with many tabs. If
nothing else helps, we *do* want the tabs to get smaller instead
of enforcing a minimum window size.
Args:
index: The index of the tab to get a sizehint for.
Return:
A QSize.
"""
2014-07-15 14:20:10 +02:00
icon = self.tabIcon(index)
padding_count = 0
if not icon.isNull():
extent = self.style().pixelMetric(QStyle.PM_TabBarIconSize, None,
self)
icon_size = icon.actualSize(QSize(extent, extent))
padding_count += 1
else:
icon_size = QSize(0, 0)
padding_width = self.style().pixelMetric(PM_TabBarPadding, None, self)
height = self.fontMetrics().height()
width = (self.fontMetrics().size(0, '\u2026').width() +
2014-07-15 22:14:16 +02:00
icon_size.width() + padding_count * padding_width)
2014-07-15 14:20:10 +02:00
return QSize(width, height)
2014-07-11 16:54:54 +02:00
2014-07-15 14:20:10 +02:00
def tabSizeHint(self, index):
2014-06-10 14:30:31 +02:00
"""Override tabSizeHint so all tabs are the same size.
https://wiki.python.org/moin/PyQt/Customising%20tab%20bars
2014-07-14 18:48:38 +02:00
Args:
2014-07-15 14:20:10 +02:00
index: The index of the tab.
2014-07-14 18:48:38 +02:00
Return:
A QSize.
2014-06-10 14:30:31 +02:00
"""
2014-07-15 14:20:10 +02:00
minimum_size = self.minimumTabSizeHint(index)
2014-07-15 21:20:57 +02:00
height = self.fontMetrics().height()
if self.vertical:
2014-08-06 08:10:32 +02:00
confwidth = str(config.get('tabs', 'width'))
2014-07-15 21:20:57 +02:00
if confwidth.endswith('%'):
perc = int(confwidth.rstrip('%'))
2014-09-24 07:06:45 +02:00
width = objreg.get('main-window').width() * perc / 100
2014-07-15 21:20:57 +02:00
else:
width = int(confwidth)
size = QSize(max(minimum_size.width(), width), height)
elif self.count() == 0:
# This happens on startup on OS X.
# We return it directly rather than setting `size' because we don't
# want to ensure it's valid in this special case.
return QSize()
2014-07-15 21:20:57 +02:00
elif self.count() * minimum_size.width() > self.width():
2014-07-15 14:20:10 +02:00
# If we don't have enough space, we return the minimum size so we
# get scroll buttons as soon as needed.
size = minimum_size
else:
# If we *do* have enough space, tabs should occupy the whole window
# width.
size = QSize(self.width() / self.count(), height)
qtutils.ensure_valid(size)
2014-06-22 23:32:49 +02:00
return size
2014-07-13 22:10:15 +02:00
2014-07-14 18:01:02 +02:00
def paintEvent(self, _e):
"""Override paintEvent to draw the tabs like we want to."""
p = QStylePainter(self)
tab = QStyleOptionTab()
selected = self.currentIndex()
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')
2014-07-17 16:06:14 +02:00
tab.palette.setColor(QPalette.Window, color)
tab.palette.setColor(QPalette.WindowText,
2014-07-17 16:06:14 +02:00
config.get('colors', 'tab.fg'))
2014-07-16 13:51:16 +02:00
indicator_color = self.tabData(idx)
if indicator_color is None:
indicator_color = QColor()
tab.palette.setColor(QPalette.Base, indicator_color)
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)
2014-07-14 07:49:52 +02:00
2014-09-18 18:15:37 +02:00
def tabInserted(self, idx):
"""Show the tabbar if configured to hide and >1 tab is open."""
self.autohide()
2014-09-18 18:15:37 +02:00
super().tabInserted(idx)
def tabRemoved(self, idx):
"""Hide the tabbar if configured when only one tab is open."""
self.autohide()
2014-09-18 18:15:37 +02:00
super().tabRemoved(idx)
2014-07-13 22:10:15 +02:00
2014-07-14 13:34:44 +02:00
class TabBarStyle(QCommonStyle):
2014-07-13 22:10:15 +02:00
2014-07-14 13:34:44 +02:00
"""Qt style used by TabBar to fix some issues with the default one.
2014-07-13 22:10:15 +02:00
This fixes the following things:
- 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:
http://stackoverflow.com/a/17294081
https://code.google.com/p/makehuman/source/browse/trunk/makehuman/lib/qtgui.py
Attributes:
_style: The base/"parent" style.
"""
def __init__(self, style):
"""Initialize all functions we're not overriding.
This simply calls the corresponding function in self._style.
Args:
style: The base/"parent" style.
"""
self._style = style
2014-07-14 07:49:52 +02:00
for method in ('drawComplexControl', 'drawItemPixmap',
2014-07-13 22:10:15 +02:00
'generatedIconPixmap', 'hitTestComplexControl',
2014-07-15 14:20:10 +02:00
'itemPixmapRect', 'itemTextRect',
2014-07-14 23:31:45 +02:00
'polish', 'styleHint', 'subControlRect', 'unpolish',
2014-07-15 22:14:16 +02:00
'drawItemText', 'sizeFromContents', 'drawPrimitive'):
2014-07-13 22:10:15 +02:00
target = getattr(self._style, method)
setattr(self, method, functools.partial(target))
super().__init__()
2014-07-14 07:49:52 +02:00
def drawControl(self, element, opt, p, widget=None):
"""Override drawControl to draw odd tabs in a different color.
Draws the given element with the provided painter with the style
options specified by option.
Args:
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:
2014-07-16 13:51:16 +02:00
p.fillRect(opt.rect, opt.palette.window())
indicator_color = opt.palette.base().color()
2014-08-06 08:10:32 +02:00
indicator_width = config.get('tabs', 'indicator-width')
2014-07-16 13:51:16 +02:00
if indicator_color.isValid() and indicator_width != 0:
topleft = opt.rect.topLeft()
2014-08-06 08:10:32 +02:00
topleft += QPoint(config.get('tabs', 'indicator-space'), 2)
2014-07-16 13:51:16 +02:00
p.fillRect(topleft.x(), topleft.y(), indicator_width,
opt.rect.height() - 4, indicator_color)
# We use super() rather than self._style here because we don't want
# any sophisticated drawing.
super().drawControl(QStyle.CE_TabBarTabShape, opt, p, widget)
elif element == QStyle.CE_TabBarTabLabel:
text_rect, icon_rect = self._tab_layout(opt)
2014-07-14 18:01:02 +02:00
if not opt.icon.isNull():
qtutils.ensure_valid(icon_rect)
2014-07-14 18:01:02 +02:00
icon_mode = (QIcon.Normal if opt.state & QStyle.State_Enabled
else QIcon.Disabled)
icon_state = (QIcon.On if opt.state & QStyle.State_Selected
else QIcon.Off)
icon = opt.icon.pixmap(opt.iconSize, icon_mode, icon_state)
p.drawPixmap(icon_rect.x(), icon_rect.y(), icon)
self._style.drawItemText(p, text_rect,
Qt.AlignLeft | Qt.AlignVCenter,
opt.palette,
opt.state & QStyle.State_Enabled,
opt.text, QPalette.WindowText)
else:
# For any other elements we just delegate the work to our real
# style.
self._style.drawControl(element, opt, p, widget)
2014-07-14 18:01:02 +02:00
def pixelMetric(self, metric, option=None, widget=None):
"""Override pixelMetric to not shift the selected tab.
Args:
metric: PixelMetric
option: const QStyleOption *
widget: const QWidget *
Return:
An int.
"""
if (metric == QStyle.PM_TabBarTabShiftHorizontal or
metric == QStyle.PM_TabBarTabShiftVertical or
metric == QStyle.PM_TabBarTabHSpace or
metric == QStyle.PM_TabBarTabVSpace):
return 0
2014-07-15 14:20:10 +02:00
elif metric == PM_TabBarPadding:
return 4
2014-07-14 18:01:02 +02:00
else:
return self._style.pixelMetric(metric, option, widget)
2014-07-14 23:30:08 +02:00
def subElementRect(self, sr, opt, widget=None):
2014-07-14 18:01:02 +02:00
"""Override subElementRect to use our own _tab_layout implementation.
Args:
sr: SubElement
opt: QStyleOption
widget: QWidget
Return:
A QRect.
"""
if sr == QStyle.SE_TabBarTabText:
2014-07-14 23:30:08 +02:00
text_rect, _icon_rect = self._tab_layout(opt)
2014-07-14 18:01:02 +02:00
return text_rect
else:
2014-07-14 23:30:08 +02:00
return self._style.subElementRect(sr, opt, widget)
2014-07-14 18:01:02 +02:00
def _tab_layout(self, opt):
2014-07-14 18:01:02 +02:00
"""Compute the text/icon rect from the opt rect.
This is based on Qt's QCommonStylePrivate::tabLayout
(qtbase/src/widgets/styles/qcommonstyle.cpp) as we can't use the
private implementation.
Args:
opt: QStyleOptionTab
Return:
A (text_rect, icon_rect) tuple (both QRects).
"""
2014-07-15 14:20:10 +02:00
padding = self.pixelMetric(PM_TabBarPadding, opt)
2014-07-14 18:01:02 +02:00
icon_rect = QRect()
text_rect = QRect(opt.rect)
qtutils.ensure_valid(text_rect)
2014-08-06 08:10:32 +02:00
indicator_width = config.get('tabs', 'indicator-width')
2014-07-14 18:01:02 +02:00
text_rect.adjust(padding, 0, 0, 0)
if indicator_width != 0:
text_rect.adjust(indicator_width +
2014-08-06 08:10:32 +02:00
config.get('tabs', 'indicator-space'), 0, 0, 0)
2014-07-14 18:01:02 +02:00
if not opt.icon.isNull():
icon_rect = self._get_icon_rect(opt, text_rect)
2014-07-14 23:30:25 +02:00
text_rect.adjust(icon_rect.width() + padding, 0, 0, 0)
2014-07-14 18:01:02 +02:00
text_rect = self._style.visualRect(opt.direction, opt.rect, text_rect)
return (text_rect, icon_rect)
def _get_icon_rect(self, opt, text_rect):
"""Get a QRect for the icon to draw.
Args:
opt: QStyleOptionTab
text_rect: The QRect for the text.
Return:
A QRect.
"""
icon_size = opt.iconSize
if not icon_size.isValid():
2014-07-15 14:20:10 +02:00
icon_extent = self.pixelMetric(QStyle.PM_SmallIconSize)
2014-07-14 18:01:02 +02:00
icon_size = QSize(icon_extent, icon_extent)
icon_mode = (QIcon.Normal if opt.state & QStyle.State_Enabled
else QIcon.Disabled)
icon_state = (QIcon.On if opt.state & QStyle.State_Selected
else QIcon.Off)
tab_icon_size = opt.icon.actualSize(icon_size, icon_mode, icon_state)
tab_icon_size = QSize(min(tab_icon_size.width(), icon_size.width()),
min(tab_icon_size.height(), icon_size.height()))
icon_rect = QRect(text_rect.left(),
text_rect.center().y() - tab_icon_size.height() / 2,
tab_icon_size.width(), tab_icon_size.height())
icon_rect = self._style.visualRect(opt.direction, opt.rect, icon_rect)
qtutils.ensure_valid(icon_rect)
2014-07-14 18:01:02 +02:00
return icon_rect