Remove generated stylesheets again

We can just use jinja logic instead...
This commit is contained in:
Florian Bruhin 2016-10-03 19:31:01 +02:00
parent 1f011bdd5f
commit e3581a50ca
4 changed files with 45 additions and 85 deletions

View File

@ -46,7 +46,7 @@ def get_stylesheet(template_str):
config=objreg.get('config')) config=objreg.get('config'))
def set_register_stylesheet(obj, *, generator=None): def set_register_stylesheet(obj):
"""Set the stylesheet for an object based on it's STYLESHEET attribute. """Set the stylesheet for an object based on it's STYLESHEET attribute.
Also, register an update when the config is changed. Also, register an update when the config is changed.
@ -54,23 +54,20 @@ def set_register_stylesheet(obj, *, generator=None):
Args: Args:
obj: The object to set the stylesheet for and register. obj: The object to set the stylesheet for and register.
Must have a STYLESHEET attribute. Must have a STYLESHEET attribute.
generator: If set, call the given function to dynamically generate a
stylesheet instead.
""" """
stylesheet = generator() if generator is not None else obj.STYLESHEET qss = get_stylesheet(obj.STYLESHEET)
qss = get_stylesheet(stylesheet)
log.config.vdebug("stylesheet for {}: {}".format( log.config.vdebug("stylesheet for {}: {}".format(
obj.__class__.__name__, qss)) obj.__class__.__name__, qss))
obj.setStyleSheet(qss) obj.setStyleSheet(qss)
objreg.get('config').changed.connect( objreg.get('config').changed.connect(
functools.partial(_update_stylesheet, obj, generator=generator)) functools.partial(_update_stylesheet, obj))
def _update_stylesheet(obj, *, generator): def _update_stylesheet(obj):
"""Update the stylesheet for obj.""" """Update the stylesheet for obj."""
get_stylesheet.cache_clear()
if not sip.isdeleted(obj): if not sip.isdeleted(obj):
stylesheet = generator() if generator is not None else obj.STYLESHEET obj.setStyleSheet(get_stylesheet(obj.STYLESHEET))
obj.setStyleSheet(get_stylesheet(stylesheet))
class ColorDict(collections.UserDict): class ColorDict(collections.UserDict):

View File

@ -77,6 +77,30 @@ class PromptContainer(QWidget):
_win_id: The window ID this object is associated with. _win_id: The window ID this object is associated with.
""" """
STYLESHEET = """
QWidget#Prompt {
{% if config.get('ui', 'status-position') == 'top' %}
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
{% else %}
border-top-left-radius: 10px;
border-top-right-radius: 10px;
{% endif %}
}
QWidget {
/* FIXME
font: {{ font['keyhint'] }};
FIXME
*/
color: {{ color['statusbar.fg.prompt'] }};
background-color: {{ color['statusbar.bg.prompt'] }};
}
QLineEdit {
border: 1px solid grey;
}
"""
update_geometry = pyqtSignal() update_geometry = pyqtSignal()
def __init__(self, win_id, parent=None): def __init__(self, win_id, parent=None):
@ -86,8 +110,7 @@ class PromptContainer(QWidget):
self._layout = QVBoxLayout(self) self._layout = QVBoxLayout(self)
self._layout.setContentsMargins(10, 10, 10, 10) self._layout.setContentsMargins(10, 10, 10, 10)
self._prompt = None self._prompt = None
style.set_register_stylesheet(self, style.set_register_stylesheet(self)
generator=self._generate_stylesheet)
# FIXME review this # FIXME review this
self._shutting_down = False self._shutting_down = False
@ -99,35 +122,6 @@ class PromptContainer(QWidget):
return utils.get_repr(self, loops=len(self._loops), return utils.get_repr(self, loops=len(self._loops),
queue=len(self._queue), prompt=self._prompt) queue=len(self._queue), prompt=self._prompt)
def _generate_stylesheet(self):
"""Generate a stylesheet with the right edge rounded."""
stylesheet = """
QWidget#Prompt {
border-POSITION-left-radius: 10px;
border-POSITION-right-radius: 10px;
}
QWidget {
/* FIXME
font: {{ font['keyhint'] }};
FIXME
*/
color: {{ color['statusbar.fg.prompt'] }};
background-color: {{ color['statusbar.bg.prompt'] }};
}
QLineEdit {
border: 1px solid grey;
}
"""
position = config.get('ui', 'status-position')
if position == 'top':
return stylesheet.replace('POSITION', 'bottom')
elif position == 'bottom':
return stylesheet.replace('POSITION', 'top')
else:
raise ValueError("Invalid position {}!".format(position))
def _pop_later(self): def _pop_later(self):
"""Helper to call self._pop as soon as everything else is done.""" """Helper to call self._pop as soon as everything else is done."""
QTimer.singleShot(0, self._pop) QTimer.singleShot(0, self._pop)

View File

@ -45,6 +45,19 @@ class KeyHintView(QLabel):
update_geometry: Emitted when this widget should be resized/positioned. update_geometry: Emitted when this widget should be resized/positioned.
""" """
STYLESHEET = """
QLabel {
font: {{ font['keyhint'] }};
color: {{ color['keyhint.fg'] }};
background-color: {{ color['keyhint.bg'] }};
padding: 6px;
{% if config.get('ui', 'status-position') == 'top' %}
border-bottom-right-radius: 6px;
{% else %}
border-top-right-radius: 6px;
{% endif %}
}
"""
update_geometry = pyqtSignal() update_geometry = pyqtSignal()
def __init__(self, win_id, parent=None): def __init__(self, win_id, parent=None):
@ -56,8 +69,7 @@ class KeyHintView(QLabel):
self._show_timer = usertypes.Timer(self, 'keyhint_show') self._show_timer = usertypes.Timer(self, 'keyhint_show')
self._show_timer.setInterval(500) self._show_timer.setInterval(500)
self._show_timer.timeout.connect(self.show) self._show_timer.timeout.connect(self.show)
style.set_register_stylesheet(self, style.set_register_stylesheet(self)
generator=self._generate_stylesheet)
def __repr__(self): def __repr__(self):
return utils.get_repr(self, win_id=self._win_id) return utils.get_repr(self, win_id=self._win_id)
@ -67,22 +79,6 @@ class KeyHintView(QLabel):
self.update_geometry.emit() self.update_geometry.emit()
super().showEvent(e) super().showEvent(e)
def _generate_stylesheet(self):
"""Generate a stylesheet with the right edge rounded."""
stylesheet = """
QLabel {
font: {{ font['keyhint'] }};
color: {{ color['keyhint.fg'] }};
background-color: {{ color['keyhint.bg'] }};
padding: 6px;
border-EDGE-radius: 6px;
}
"""
if config.get('ui', 'status-position') == 'top':
return stylesheet.replace('EDGE', 'bottom-right')
else:
return stylesheet.replace('EDGE', 'top-right')
@pyqtSlot(str) @pyqtSlot(str)
def update_keyhint(self, modename, prefix): def update_keyhint(self, modename, prefix):
"""Show hints for the given prefix (or hide if prefix is empty). """Show hints for the given prefix (or hide if prefix is empty).

View File

@ -59,24 +59,6 @@ class Obj(QObject):
self.rendered_stylesheet = stylesheet self.rendered_stylesheet = stylesheet
class GeneratedObj(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.rendered_stylesheet = None
self._generated = False
def setStyleSheet(self, stylesheet):
self.rendered_stylesheet = stylesheet
def generate(self):
if not self._generated:
self._generated = True
return 'one'
else:
return 'two'
@pytest.mark.parametrize('delete', [True, False]) @pytest.mark.parametrize('delete', [True, False])
def test_set_register_stylesheet(delete, qtbot, config_stub, caplog): def test_set_register_stylesheet(delete, qtbot, config_stub, caplog):
config_stub.data = {'fonts': {'foo': 'bar'}, 'colors': {}} config_stub.data = {'fonts': {'foo': 'bar'}, 'colors': {}}
@ -105,15 +87,6 @@ def test_set_register_stylesheet(delete, qtbot, config_stub, caplog):
assert obj.rendered_stylesheet == expected assert obj.rendered_stylesheet == expected
def test_set_register_stylesheet_generator(qtbot, config_stub):
config_stub.data = {'fonts': {}, 'colors': {}}
obj = GeneratedObj()
style.set_register_stylesheet(obj, generator=obj.generate)
assert obj.rendered_stylesheet == 'one'
config_stub.changed.emit('foo', 'bar')
assert obj.rendered_stylesheet == 'two'
class TestColorDict: class TestColorDict:
@pytest.mark.parametrize('key, expected', [ @pytest.mark.parametrize('key, expected', [