Add generator argument to set_register_stylesheet

This commit is contained in:
Florian Bruhin 2016-09-22 17:29:03 +02:00
parent 750dfd98af
commit e04e6c51d1
2 changed files with 36 additions and 5 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): def set_register_stylesheet(obj, *, generator=None):
"""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,19 +54,23 @@ def set_register_stylesheet(obj):
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.
""" """
qss = get_stylesheet(obj.STYLESHEET) stylesheet = generator() if generator is not None else 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)) functools.partial(_update_stylesheet, obj, generator=generator))
def update_stylesheet(obj): def _update_stylesheet(obj, *, generator):
"""Update the stylesheet for obj.""" """Update the stylesheet for obj."""
if not sip.isdeleted(obj): if not sip.isdeleted(obj):
obj.setStyleSheet(get_stylesheet(obj.STYLESHEET)) stylesheet = generator() if generator is not None else obj.STYLESHEET
obj.setStyleSheet(get_stylesheet(stylesheet))
class ColorDict(collections.UserDict): class ColorDict(collections.UserDict):

View File

@ -59,6 +59,24 @@ 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': {}}
@ -87,6 +105,15 @@ 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', [