Update everything needed when config is changed

This commit is contained in:
Florian Bruhin 2014-04-10 23:30:45 +02:00
parent aa616e5ac7
commit 04807489b7
8 changed files with 70 additions and 22 deletions

View File

@ -47,6 +47,7 @@ from PyQt5.QtCore import pyqtSlot, QTimer, QEventLoop
import qutebrowser import qutebrowser
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
import qutebrowser.config.style as style
import qutebrowser.config.config as config import qutebrowser.config.config as config
import qutebrowser.network.qutescheme as qutescheme import qutebrowser.network.qutescheme as qutescheme
import qutebrowser.utils.message as message import qutebrowser.utils.message as message
@ -130,10 +131,11 @@ class QuteBrowser(QApplication):
self.mainwindow.status.keystring.setText) self.mainwindow.status.keystring.setText)
message.bridge.error.connect(self.mainwindow.status.disp_error) message.bridge.error.connect(self.mainwindow.status.disp_error)
message.bridge.info.connect(self.mainwindow.status.disp_tmp_text) message.bridge.info.connect(self.mainwindow.status.disp_tmp_text)
self.config.style_changed.connect(style.invalidate_caches)
self.config.changed.connect(self.mainwindow.tabs.on_config_changed) self.config.changed.connect(self.mainwindow.tabs.on_config_changed)
self.config.changed.connect( self.config.changed.connect(
self.mainwindow.completion.on_config_changed) self.mainwindow.completion.on_config_changed)
self.config.changed.connect(self.mainwindow.resize_completion) self.config.changed.connect(self.mainwindow.on_config_changed)
self.mainwindow.show() self.mainwindow.show()
self._python_hacks() self._python_hacks()

View File

@ -86,9 +86,12 @@ class Config(QObject):
Signals: Signals:
changed: Gets emitted when the config has changed. changed: Gets emitted when the config has changed.
Args: the changed section and option. Args: the changed section and option.
style_changed: When style caches need to be invalidated.
Args: the changed section and option.
""" """
changed = pyqtSignal(str, str) changed = pyqtSignal(str, str)
style_changed = pyqtSignal(str, str)
def __init__(self, configdir, fname, parent=None): def __init__(self, configdir, fname, parent=None):
super().__init__(parent) super().__init__(parent)
@ -287,6 +290,7 @@ class Config(QObject):
Emit: Emit:
changed: If the config was changed. changed: If the config was changed.
style_changed: When style caches need to be invalidated.
""" """
if value: if value:
value = self._interpolation.before_set(self, section, option, value = self._interpolation.before_set(self, section, option,
@ -300,6 +304,8 @@ class Config(QObject):
except KeyError: except KeyError:
raise NoOptionError(option, section) raise NoOptionError(option, section)
else: else:
if section in ['colors', 'fonts']:
self.style_changed.emit(section, option)
self.changed.emit(section, option) self.changed.emit(section, option)
def save(self): def save(self):

View File

@ -17,6 +17,8 @@
"""Utilities related to the look&feel of qutebrowser.""" """Utilities related to the look&feel of qutebrowser."""
from functools import partial
import qutebrowser.config.config as config import qutebrowser.config.config as config
_colordict = None _colordict = None
@ -40,6 +42,31 @@ def get_stylesheet(template):
return template.strip().format(color=_colordict, font=_fontdict) return template.strip().format(color=_colordict, font=_fontdict)
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.
"""
obj.setStyleSheet(get_stylesheet(obj.STYLESHEET))
config.config.changed.connect(partial(_update_stylesheet, obj))
def _update_stylesheet(obj, section, option):
"""Update the stylesheet for obj."""
# pylint: disable=unused-argument
obj.setStyleSheet(get_stylesheet(obj.STYLESHEET))
def invalidate_caches(section, option):
"""Invalidate cached dicts."""
# pylint: disable=unused-argument
global _colordict, _fontdict
if section == 'colors':
_colordict = None
elif section == 'fonts':
_fontdict = None
class ColorDict(dict): class ColorDict(dict):
"""A dict aimed at Qt stylesheet colors.""" """A dict aimed at Qt stylesheet colors."""

View File

@ -35,7 +35,7 @@ import qutebrowser.config.config as config
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
from qutebrowser.config.configdata import configdata from qutebrowser.config.configdata import configdata
from qutebrowser.models.completion import ROLE_MARKS from qutebrowser.models.completion import ROLE_MARKS
from qutebrowser.config.style import get_stylesheet from qutebrowser.config.style import set_register_stylesheet, get_stylesheet
from qutebrowser.commands.parsers import split_cmdline from qutebrowser.commands.parsers import split_cmdline
from qutebrowser.models.completionfilter import CompletionFilterModel from qutebrowser.models.completionfilter import CompletionFilterModel
from qutebrowser.models.commandcompletion import CommandCompletionModel from qutebrowser.models.commandcompletion import CommandCompletionModel
@ -55,7 +55,7 @@ class CompletionView(QTreeView):
Attributes: Attributes:
_model: The currently active filter model. _model: The currently active filter model.
_lastmodel: The model set in the last iteration. _lastmodel: The model set in the last iteration.
_STYLESHEET: The stylesheet template for the CompletionView. STYLESHEET: The stylesheet template for the CompletionView.
_completion_models: dict of available completion models. _completion_models: dict of available completion models.
_ignore_next: Whether to ignore the next cmd_text_changed signal. _ignore_next: Whether to ignore the next cmd_text_changed signal.
_enabled: Whether showing the CompletionView is enabled. _enabled: Whether showing the CompletionView is enabled.
@ -69,7 +69,7 @@ class CompletionView(QTreeView):
we're currently completing. we're currently completing.
""" """
_STYLESHEET = """ STYLESHEET = """
QTreeView {{ QTreeView {{
{font[completion]} {font[completion]}
{color[completion.fg]} {color[completion.fg]}
@ -119,7 +119,7 @@ class CompletionView(QTreeView):
self._delegate = _CompletionItemDelegate(self) self._delegate = _CompletionItemDelegate(self)
self.setItemDelegate(self._delegate) self.setItemDelegate(self._delegate)
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) set_register_stylesheet(self)
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum)
self.setHeaderHidden(True) self.setHeaderHidden(True)
self.setIndentation(0) self.setIndentation(0)

View File

@ -97,6 +97,11 @@ class MainWindow(QWidget):
#self.tabWidget.setCurrentIndex(0) #self.tabWidget.setCurrentIndex(0)
#QtCore.QMetaObject.connectSlotsByName(MainWindow) #QtCore.QMetaObject.connectSlotsByName(MainWindow)
def on_config_changed(self, section, option):
"""Resize completion if config changed."""
if section == 'general' and option == 'completion_height':
self.resize_completion()
def resize_completion(self): def resize_completion(self):
"""Adjust completion according to config.""" """Adjust completion according to config."""
confheight = str(config.config.get('general', 'completion_height')) confheight = str(config.config.get('general', 'completion_height'))

View File

@ -25,7 +25,7 @@ from PyQt5.QtWidgets import (QWidget, QLineEdit, QProgressBar, QLabel,
QShortcut) QShortcut)
from PyQt5.QtGui import QPainter, QKeySequence, QValidator from PyQt5.QtGui import QPainter, QKeySequence, QValidator
from qutebrowser.config.style import get_stylesheet from qutebrowser.config.style import set_register_stylesheet, get_stylesheet
import qutebrowser.commands.keys as keys import qutebrowser.commands.keys as keys
from qutebrowser.utils.url import urlstring from qutebrowser.utils.url import urlstring
from qutebrowser.utils.usertypes import NeighborList from qutebrowser.utils.usertypes import NeighborList
@ -47,7 +47,7 @@ class StatusBar(QWidget):
_stack: The QStackedLayout with cmd/txt widgets. _stack: The QStackedLayout with cmd/txt widgets.
_error: If there currently is an error, accessed through the error _error: If there currently is an error, accessed through the error
property. property.
_STYLESHEET: The stylesheet template. STYLESHEET: The stylesheet template.
Signals: Signals:
resized: Emitted when the statusbar has resized, so the completion resized: Emitted when the statusbar has resized, so the completion
@ -60,7 +60,7 @@ class StatusBar(QWidget):
resized = pyqtSignal('QRect') resized = pyqtSignal('QRect')
moved = pyqtSignal('QPoint') moved = pyqtSignal('QPoint')
_STYLESHEET = """ STYLESHEET = """
QWidget#StatusBar[error="false"] {{ QWidget#StatusBar[error="false"] {{
{color[statusbar.bg]} {color[statusbar.bg]}
}} }}
@ -79,7 +79,7 @@ class StatusBar(QWidget):
super().__init__(parent) super().__init__(parent)
self.setObjectName(self.__class__.__name__) self.setObjectName(self.__class__.__name__)
self.setAttribute(Qt.WA_StyledBackground) self.setAttribute(Qt.WA_StyledBackground)
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) set_register_stylesheet(self)
self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed) self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
@ -132,7 +132,7 @@ class StatusBar(QWidget):
updated by Qt properly. updated by Qt properly.
""" """
self._error = val self._error = val
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) self.setStyleSheet(get_stylesheet(self.STYLESHEET))
def _show_cmd_widget(self): def _show_cmd_widget(self):
"""Show command widget instead of temporary text.""" """Show command widget instead of temporary text."""
@ -429,11 +429,11 @@ class _Progress(QProgressBar):
"""The progress bar part of the status bar. """The progress bar part of the status bar.
Attributes: Attributes:
_STYLESHEET: The stylesheet template. STYLESHEET: The stylesheet template.
""" """
# FIXME for some reason, margin-left is not shown # FIXME for some reason, margin-left is not shown
_STYLESHEET = """ STYLESHEET = """
QProgressBar {{ QProgressBar {{
border-radius: 0px; border-radius: 0px;
border: 2px solid transparent; border: 2px solid transparent;
@ -448,7 +448,7 @@ class _Progress(QProgressBar):
def __init__(self, parent): def __init__(self, parent):
super().__init__(parent) super().__init__(parent)
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) set_register_stylesheet(self)
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Ignored) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Ignored)
self.setTextVisible(False) self.setTextVisible(False)
self.hide() self.hide()
@ -618,10 +618,10 @@ class _Url(TextBase):
_old_urltype: The type of the URL displayed before the hover URL. _old_urltype: The type of the URL displayed before the hover URL.
_urltype: The current URL type. One of normal/ok/error/warn/hover. _urltype: The current URL type. One of normal/ok/error/warn/hover.
Accessed via the urltype property. Accessed via the urltype property.
_STYLESHEET: The stylesheet template. STYLESHEET: The stylesheet template.
""" """
_STYLESHEET = """ STYLESHEET = """
QLabel#Url[urltype="normal"] {{ QLabel#Url[urltype="normal"] {{
{color[statusbar.url.fg]} {color[statusbar.url.fg]}
}} }}
@ -652,7 +652,7 @@ class _Url(TextBase):
""" """
super().__init__(bar, elidemode) super().__init__(bar, elidemode)
self.setObjectName(self.__class__.__name__) self.setObjectName(self.__class__.__name__)
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) set_register_stylesheet(self)
self._urltype = None self._urltype = None
self._old_urltype = None self._old_urltype = None
self._old_url = None self._old_url = None
@ -667,7 +667,7 @@ class _Url(TextBase):
def urltype(self, val): def urltype(self, val):
"""Setter for self.urltype, so it can be used as Qt property.""" """Setter for self.urltype, so it can be used as Qt property."""
self._urltype = val self._urltype = val
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) self.setStyleSheet(get_stylesheet(self.STYLESHEET))
@pyqtSlot(bool) @pyqtSlot(bool)
def on_loading_finished(self, ok): def on_loading_finished(self, ok):

View File

@ -17,11 +17,11 @@
"""The tab widget used for TabbedBrowser from browser.py.""" """The tab widget used for TabbedBrowser from browser.py."""
from PyQt5.QtCore import Qt from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import QTabWidget, QTabBar, QSizePolicy from PyQt5.QtWidgets import QTabWidget, QTabBar, QSizePolicy
import qutebrowser.config.config as config import qutebrowser.config.config as config
from qutebrowser.config.style import get_stylesheet from qutebrowser.config.style import set_register_stylesheet
from qutebrowser.utils.style import Style from qutebrowser.utils.style import Style
@ -30,13 +30,13 @@ class TabWidget(QTabWidget):
"""The tabwidget used for TabbedBrowser. """The tabwidget used for TabbedBrowser.
Attributes: Attributes:
_STYLESHEET: The stylesheet template to be used. STYLESHEET: The stylesheet template to be used.
""" """
# FIXME there is still some ugly 1px white stripe from somewhere if we do # FIXME there is still some ugly 1px white stripe from somewhere if we do
# background-color: grey for QTabBar... # background-color: grey for QTabBar...
_STYLESHEET = """ STYLESHEET = """
QTabWidget::pane {{ QTabWidget::pane {{
position: absolute; position: absolute;
top: 0px; top: 0px;
@ -68,7 +68,7 @@ class TabWidget(QTabWidget):
super().__init__(parent) super().__init__(parent)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.setStyle(Style(self.style())) self.setStyle(Style(self.style()))
self.setStyleSheet(get_stylesheet(self._STYLESHEET)) set_register_stylesheet(self)
self.setDocumentMode(True) self.setDocumentMode(True)
self.setElideMode(Qt.ElideRight) self.setElideMode(Qt.ElideRight)
self._init_config() self._init_config()
@ -96,3 +96,10 @@ class TabWidget(QTabWidget):
self.tabBar().setSelectionBehaviorOnRemove(select_conv[selstr]) self.tabBar().setSelectionBehaviorOnRemove(select_conv[selstr])
except KeyError: except KeyError:
pass pass
@pyqtSlot(str, str)
def on_config_changed(self, section, option):
"""Update attributes when config changed."""
# pylint: disable=unused-argument
if section == 'tabbar':
self._init_config()

View File

@ -142,6 +142,7 @@ class TabbedBrowser(TabWidget):
@pyqtSlot(str, str) @pyqtSlot(str, str)
def on_config_changed(self, section, option): def on_config_changed(self, section, option):
"""Update tab config when config was changed.""" """Update tab config when config was changed."""
super().on_config_changed(section, option)
for tab in self._tabs: for tab in self._tabs:
tab.on_config_changed(section, option) tab.on_config_changed(section, option)