2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2015-01-03 15:51:31 +01:00
|
|
|
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-02-23 18:15:08 +01:00
|
|
|
#
|
|
|
|
# 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-08-28 09:51:54 +02:00
|
|
|
"""Utilities related to the look&feel of qutebrowser."""
|
2014-02-23 18:15:08 +01:00
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
import functools
|
2014-04-10 23:30:45 +02:00
|
|
|
|
2014-08-28 17:47:40 +02:00
|
|
|
import jinja2
|
2014-10-18 19:53:22 +02:00
|
|
|
import sip
|
2014-06-21 17:37:54 +02:00
|
|
|
from PyQt5.QtGui import QColor
|
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.config import config
|
2014-09-24 07:06:45 +02:00
|
|
|
from qutebrowser.utils import log, objreg
|
2014-02-23 18:15:08 +01:00
|
|
|
|
2014-05-05 17:56:14 +02:00
|
|
|
|
2014-08-28 09:51:54 +02:00
|
|
|
@functools.lru_cache(maxsize=16)
|
2014-08-28 17:47:40 +02:00
|
|
|
def get_stylesheet(template_str):
|
2014-02-23 18:15:08 +01:00
|
|
|
"""Format a stylesheet based on a template.
|
|
|
|
|
|
|
|
Args:
|
2014-08-28 17:47:40 +02:00
|
|
|
template_str: The stylesheet template as string.
|
2014-02-23 18:15:08 +01:00
|
|
|
|
|
|
|
Return:
|
|
|
|
The formatted template as string.
|
|
|
|
"""
|
2014-08-28 09:51:54 +02:00
|
|
|
colordict = ColorDict(config.section('colors'))
|
|
|
|
fontdict = FontDict(config.section('fonts'))
|
2014-08-28 17:47:40 +02:00
|
|
|
template = jinja2.Template(template_str)
|
|
|
|
return template.render(color=colordict, font=fontdict,
|
2014-09-24 07:06:45 +02:00
|
|
|
config=objreg.get('config'))
|
2014-02-23 18:15:08 +01:00
|
|
|
|
|
|
|
|
2014-04-10 23:30:45 +02:00
|
|
|
def set_register_stylesheet(obj):
|
|
|
|
"""Set the stylesheet for an object based on it's STYLESHEET attribute.
|
|
|
|
|
|
|
|
Also, register an update when the config is changed.
|
2014-04-17 17:44:27 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
obj: The object to set the stylesheet for and register.
|
|
|
|
Must have a STYLESHEET attribute.
|
2014-04-10 23:30:45 +02:00
|
|
|
"""
|
2014-06-13 07:38:10 +02:00
|
|
|
qss = get_stylesheet(obj.STYLESHEET)
|
2014-08-29 07:33:37 +02:00
|
|
|
log.style.vdebug("stylesheet for {}: {}".format(
|
|
|
|
obj.__class__.__name__, qss))
|
2014-06-13 07:38:10 +02:00
|
|
|
obj.setStyleSheet(qss)
|
2014-10-18 19:50:10 +02:00
|
|
|
objreg.get('config').changed.connect(
|
|
|
|
functools.partial(update_stylesheet, obj))
|
2014-04-10 23:30:45 +02:00
|
|
|
|
|
|
|
|
2014-09-28 11:27:52 +02:00
|
|
|
def update_stylesheet(obj):
|
2014-04-10 23:30:45 +02:00
|
|
|
"""Update the stylesheet for obj."""
|
2014-10-18 19:53:22 +02:00
|
|
|
if not sip.isdeleted(obj):
|
|
|
|
obj.setStyleSheet(get_stylesheet(obj.STYLESHEET))
|
2014-04-10 23:30:45 +02:00
|
|
|
|
|
|
|
|
2014-02-23 18:15:08 +01:00
|
|
|
class ColorDict(dict):
|
|
|
|
|
|
|
|
"""A dict aimed at Qt stylesheet colors."""
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Override dict __getitem__.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key: The key to get from the dict.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
If a value wasn't found, return an empty string.
|
|
|
|
(Color not defined, so no output in the stylesheet)
|
|
|
|
|
|
|
|
If the key has a .fg. element in it, return color: X;.
|
|
|
|
If the key has a .bg. element in it, return background-color: X;.
|
|
|
|
|
|
|
|
In all other cases, return the plain value.
|
|
|
|
"""
|
|
|
|
try:
|
2014-04-07 16:51:14 +02:00
|
|
|
val = super().__getitem__(key)
|
2014-09-16 08:20:19 +02:00
|
|
|
except KeyError:
|
|
|
|
log.style.exception("No color defined for {}!")
|
2014-02-23 18:15:08 +01:00
|
|
|
return ''
|
2014-06-21 17:37:54 +02:00
|
|
|
if isinstance(val, QColor):
|
|
|
|
# This could happen when accidentaly declarding something as
|
|
|
|
# QtColor instead of Color in the config, and it'd go unnoticed as
|
|
|
|
# the CSS is invalid then.
|
|
|
|
raise TypeError("QColor passed to ColorDict!")
|
2014-02-23 18:15:08 +01:00
|
|
|
if 'fg' in key.split('.'):
|
|
|
|
return 'color: {};'.format(val)
|
|
|
|
elif 'bg' in key.split('.'):
|
|
|
|
return 'background-color: {};'.format(val)
|
|
|
|
else:
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
|
|
class FontDict(dict):
|
|
|
|
|
|
|
|
"""A dict aimed at Qt stylesheet fonts."""
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Override dict __getitem__.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key: The key to get from the dict.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
If a value wasn't found, return an empty string.
|
|
|
|
(Color not defined, so no output in the stylesheet)
|
|
|
|
|
|
|
|
In all other cases, return font: <value>.
|
|
|
|
"""
|
|
|
|
try:
|
2014-04-07 16:51:14 +02:00
|
|
|
val = super().__getitem__(key)
|
2014-02-23 18:15:08 +01:00
|
|
|
except KeyError:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return 'font: {};'.format(val)
|