def __init__(self, level, text, parent=None): super().__init__(text, parent) self.setAttribute(Qt.WA_StyledBackground, True) stylesheet = """ padding-top: 2px; padding-bottom: 2px; """ if level == usertypes.MessageLevel.error: stylesheet += """ background-color: {{ color['messages.bg.error'] }}; color: {{ color['messages.fg.error'] }}; font: {{ font['messages.error'] }}; border-bottom: 1px solid {{ color['messages.border.error'] }}; """ elif level == usertypes.MessageLevel.warning: stylesheet += """ background-color: {{ color['messages.bg.warning'] }}; color: {{ color['messages.fg.warning'] }}; font: {{ font['messages.warning'] }}; border-bottom: 1px solid {{ color['messages.border.warning'] }}; """ elif level == usertypes.MessageLevel.info: stylesheet += """ background-color: {{ color['messages.bg.info'] }}; color: {{ color['messages.fg.info'] }}; font: {{ font['messages.info'] }}; border-bottom: 1px solid {{ color['messages.border.info'] }} """ else: # pragma: no cover raise ValueError("Invalid level {!r}".format(level)) # We don't bother with set_register_stylesheet here as it's short-lived # anyways. self.setStyleSheet(style.get_stylesheet(stylesheet))
def set_mode_active(self, mode, val): """Setter for self.{insert,command,caret}_active. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. """ if mode == usertypes.KeyMode.insert: log.statusbar.debug("Setting insert_active to {}".format(val)) self._insert_active = val if mode == usertypes.KeyMode.command: log.statusbar.debug("Setting command_active to {}".format(val)) self._command_active = val elif mode == usertypes.KeyMode.caret: webview = objreg.get('tabbed-browser', scope='window', window=self._win_id).currentWidget() log.statusbar.debug("Setting caret_mode - val {}, selection " "{}".format(val, webview.selection_enabled)) if val: if webview.selection_enabled: self._set_mode_text("{} selection".format(mode.name)) self._caret_mode = CaretMode.selection else: self._set_mode_text(mode.name) self._caret_mode = CaretMode.on else: self._caret_mode = CaretMode.off self.setStyleSheet(style.get_stylesheet(self.STYLESHEET))
def set_mode_active(self, mode, val): """Setter for self.{insert,command,caret}_active. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. """ if mode == usertypes.KeyMode.insert: log.statusbar.debug("Setting insert flag to {}".format(val)) self._color_flags.insert = val if mode == usertypes.KeyMode.command: log.statusbar.debug("Setting command flag to {}".format(val)) self._color_flags.command = val elif mode in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]: log.statusbar.debug("Setting prompt flag to {}".format(val)) self._color_flags.prompt = val elif mode == usertypes.KeyMode.caret: tab = objreg.get('tabbed-browser', scope='window', window=self._win_id).currentWidget() log.statusbar.debug("Setting caret flag - val {}, selection " "{}".format(val, tab.caret.selection_enabled)) if val: if tab.caret.selection_enabled: self._set_mode_text("{} selection".format(mode.name)) self._color_flags.caret = ColorFlags.CaretMode.selection else: self._set_mode_text(mode.name) self._color_flags.caret = ColorFlags.CaretMode.on else: self._color_flags.caret = ColorFlags.CaretMode.off self.setStyleSheet(style.get_stylesheet(self.STYLESHEET))
def set_mode_active(self, mode, val): """Setter for self.{insert,command,caret}_active. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. """ if mode == usertypes.KeyMode.insert: log.statusbar.debug("Setting insert flag to {}".format(val)) self._color_flags.insert = val if mode == usertypes.KeyMode.command: log.statusbar.debug("Setting command flag to {}".format(val)) self._color_flags.command = val elif mode in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]: log.statusbar.debug("Setting prompt flag to {}".format(val)) self._color_flags.prompt = val elif mode == usertypes.KeyMode.caret: tab = self._current_tab() log.statusbar.debug("Setting caret flag - val {}, selection " "{}".format(val, tab.caret.selection_enabled)) if val: if tab.caret.selection_enabled: self._set_mode_text("{} selection".format(mode.name)) self._color_flags.caret = ColorFlags.CaretMode.selection else: self._set_mode_text(mode.name) self._color_flags.caret = ColorFlags.CaretMode.on else: self._color_flags.caret = ColorFlags.CaretMode.off self.setStyleSheet(style.get_stylesheet(self.STYLESHEET))
def __init__(self, level, text, replace, parent=None): super().__init__(text, parent) self.replace = replace self.setAttribute(Qt.WA_StyledBackground, True) stylesheet = """ padding-top: 2px; padding-bottom: 2px; """ if level == usertypes.MessageLevel.error: stylesheet += """ background-color: {{ color['messages.bg.error'] }}; color: {{ color['messages.fg.error'] }}; font: {{ font['messages.error'] }}; border-bottom: 1px solid {{ color['messages.border.error'] }}; """ elif level == usertypes.MessageLevel.warning: stylesheet += """ background-color: {{ color['messages.bg.warning'] }}; color: {{ color['messages.fg.warning'] }}; font: {{ font['messages.warning'] }}; border-bottom: 1px solid {{ color['messages.border.warning'] }}; """ elif level == usertypes.MessageLevel.info: stylesheet += """ background-color: {{ color['messages.bg.info'] }}; color: {{ color['messages.fg.info'] }}; font: {{ font['messages.info'] }}; border-bottom: 1px solid {{ color['messages.border.info'] }} """ else: # pragma: no cover raise ValueError("Invalid level {!r}".format(level)) # We don't bother with set_register_stylesheet here as it's short-lived # anyways. self.setStyleSheet(style.get_stylesheet(stylesheet))
def _set_prompt_active(self, val): """Setter for self.prompt_active. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. """ log.statusbar.debug("Setting prompt_active to {}".format(val)) self._prompt_active = val self.setStyleSheet(style.get_stylesheet(self.STYLESHEET))
def test_get_stylesheet(config_stub): config_stub.data = { 'colors': {'completion.bg': 'black'}, 'fonts': {'completion': 'foo'}, 'foo': {'bar': 'baz'}, } template = "{{ color['completion.bg'] }}\n{{ font['completion'] }}" rendered = style.get_stylesheet(template) assert rendered == 'background-color: black;\nfont: foo;'
def _update_url(self): """Update the displayed URL if the url or the hover url changed.""" if self._hover_url is not None: self.setText(self._hover_url) self._urltype = UrlType.hover elif self._normal_url is not None: self.setText(self._normal_url) self._urltype = self._normal_url_type else: self.setText('') self._urltype = UrlType.normal self.setStyleSheet(style.get_stylesheet(self.STYLESHEET))
def test_get_stylesheet(config_stub, template, expected): config_stub.data = { 'colors': { 'completion.bg': 'black', 'completion.fg': 'red', }, 'fonts': { 'completion': 'foo', }, 'foo': {'bar': 'baz'}, } rendered = style.get_stylesheet(template) assert rendered == expected
def _get_textdoc(self, index): """Create the QTextDocument of an item. Args: index: The QModelIndex of the item to draw. """ # FIXME we probably should do eliding here. See # qcommonstyle.cpp:viewItemDrawText # https://github.com/qutebrowser/qutebrowser/issues/118 text_option = QTextOption() if self._opt.features & QStyleOptionViewItem.WrapText: text_option.setWrapMode(QTextOption.WordWrap) else: text_option.setWrapMode(QTextOption.ManualWrap) text_option.setTextDirection(self._opt.direction) text_option.setAlignment( QStyle.visualAlignment(self._opt.direction, self._opt.displayAlignment)) if self._doc is not None: self._doc.deleteLater() self._doc = QTextDocument(self) self._doc.setDefaultFont(self._opt.font) self._doc.setDefaultTextOption(text_option) self._doc.setDefaultStyleSheet( style.get_stylesheet(""" .highlight { color: {{ color['completion.match.fg'] }}; } """)) self._doc.setDocumentMargin(2) if index.parent().isValid(): view = self.parent() pattern = view.pattern columns_to_filter = index.model().columns_to_filter(index) if index.column() in columns_to_filter and pattern: repl = r'<span class="highlight">\g<0></span>' text = re.sub(re.escape(pattern).replace(r'\ ', r'|'), repl, self._opt.text, flags=re.IGNORECASE) self._doc.setHtml(text) else: self._doc.setPlainText(self._opt.text) else: self._doc.setHtml('<span style="font: {};">{}</span>'.format( html.escape(config.get('fonts', 'completion.category')), html.escape(self._opt.text)))
def test_get_stylesheet(config_stub): config_stub.data = { 'colors': { 'completion.bg': 'black' }, 'fonts': { 'completion': 'foo' }, 'foo': { 'bar': 'baz' }, } template = "{{ color['completion.bg'] }}\n{{ font['completion'] }}" rendered = style.get_stylesheet(template) assert rendered == 'background-color: black;\nfont: foo;'
def _get_textdoc(self, index): """Create the QTextDocument of an item. Args: index: The QModelIndex of the item to draw. """ # FIXME we probably should do eliding here. See # qcommonstyle.cpp:viewItemDrawText # https://github.com/The-Compiler/qutebrowser/issues/118 text_option = QTextOption() if self._opt.features & QStyleOptionViewItem.WrapText: text_option.setWrapMode(QTextOption.WordWrap) else: text_option.setWrapMode(QTextOption.ManualWrap) text_option.setTextDirection(self._opt.direction) text_option.setAlignment(QStyle.visualAlignment( self._opt.direction, self._opt.displayAlignment)) if self._doc is not None: self._doc.deleteLater() self._doc = QTextDocument(self) self._doc.setDefaultFont(self._opt.font) self._doc.setDefaultTextOption(text_option) self._doc.setDefaultStyleSheet(style.get_stylesheet(""" .highlight { color: {{ color['completion.match.fg'] }}; } """)) self._doc.setDocumentMargin(2) if index.parent().isValid(): pattern = index.model().pattern columns_to_filter = index.model().srcmodel.columns_to_filter if index.column() in columns_to_filter and pattern: repl = r'<span class="highlight">\g<0></span>' text = re.sub(re.escape(pattern).replace(r'\ ', r'|'), repl, self._opt.text, flags=re.IGNORECASE) self._doc.setHtml(text) else: self._doc.setPlainText(self._opt.text) else: self._doc.setHtml( '<span style="font: {};">{}</span>'.format( html.escape(config.get('fonts', 'completion.category')), html.escape(self._opt.text)))
def _set_error(self, val): """Setter for self.error, so it can be used as Qt property. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. """ if self._error == val: # This gets called a lot (e.g. if the completion selection was # changed), and setStyleSheet is relatively expensive, so we ignore # this if there's nothing to change. return log.statusbar.debug("Setting error to {}".format(val)) self._error = val self.setStyleSheet(style.get_stylesheet(self.STYLESHEET)) if val: # If we got an error while command/prompt was shown, raise the text # widget. self._stack.setCurrentWidget(self.txt)
def _get_textdoc(self, index): """Create the QTextDocument of an item. Args: index: The QModelIndex of the item to draw. """ # FIXME we probably should do eliding here. See # qcommonstyle.cpp:viewItemDrawText text_option = QTextOption() if self._opt.features & QStyleOptionViewItem.WrapText: text_option.setWrapMode(QTextOption.WordWrap) else: text_option.setWrapMode(QTextOption.ManualWrap) text_option.setTextDirection(self._opt.direction) text_option.setAlignment(QStyle.visualAlignment( self._opt.direction, self._opt.displayAlignment)) self._doc = QTextDocument(self) if index.parent().isValid(): self._doc.setPlainText(self._opt.text) else: self._doc.setHtml('<b>{}</b>'.format(html.escape(self._opt.text))) self._doc.setDefaultFont(self._opt.font) self._doc.setDefaultTextOption(text_option) self._doc.setDefaultStyleSheet(style.get_stylesheet(""" .highlight { {{ color['completion.match.fg'] }} } """)) self._doc.setDocumentMargin(2) if index.column() == 0: marks = index.data(basecompletion.Role.marks) if marks is None: return for mark in marks: cur = QTextCursor(self._doc) cur.setPosition(mark[0]) cur.setPosition(mark[1], QTextCursor.KeepAnchor) txt = cur.selectedText() cur.removeSelectedText() cur.insertHtml('<span class="highlight">{}</span>'.format( html.escape(txt)))
def _get_textdoc(self, index): """Create the QTextDocument of an item. Args: index: The QModelIndex of the item to draw. """ # FIXME we probably should do eliding here. See # qcommonstyle.cpp:viewItemDrawText # https://github.com/The-Compiler/qutebrowser/issues/118 text_option = QTextOption() if self._opt.features & QStyleOptionViewItem.WrapText: text_option.setWrapMode(QTextOption.WordWrap) else: text_option.setWrapMode(QTextOption.ManualWrap) text_option.setTextDirection(self._opt.direction) text_option.setAlignment( QStyle.visualAlignment(self._opt.direction, self._opt.displayAlignment)) self._doc = QTextDocument(self) self._doc.setDefaultFont(self._opt.font) self._doc.setDefaultTextOption(text_option) self._doc.setDefaultStyleSheet( style.get_stylesheet(""" .highlight { {{ color['completion.match.fg'] }} } """)) self._doc.setDocumentMargin(2) if index.parent().isValid(): pattern = index.model().pattern if index.column() == 0 and pattern: repl = r'<span class="highlight">\g<0></span>' text = re.sub(re.escape(pattern), repl, self._opt.text, flags=re.IGNORECASE) self._doc.setHtml(text) else: self._doc.setPlainText(self._opt.text) else: self._doc.setHtml('<b>{}</b>'.format(html.escape(self._opt.text)))
def _set_severity(self, severity): """Set the severity for the current message. Re-set the stylesheet after setting the value, so everything gets updated by Qt properly. Args: severity: A Severity member. """ if self._severity == severity: # This gets called a lot (e.g. if the completion selection was # changed), and setStyleSheet is relatively expensive, so we ignore # this if there's nothing to change. return log.statusbar.debug("Setting severity to {}".format(severity)) self._severity = severity self.setStyleSheet(style.get_stylesheet(self.STYLESHEET)) if severity != Severity.normal: # If we got an error while command/prompt was shown, raise the text # widget. self._stack.setCurrentWidget(self.txt)
def _get_textdoc(self, index): """Create the QTextDocument of an item. Args: index: The QModelIndex of the item to draw. """ # FIXME we probably should do eliding here. See # qcommonstyle.cpp:viewItemDrawText # https://github.com/The-Compiler/qutebrowser/issues/118 text_option = QTextOption() if self._opt.features & QStyleOptionViewItem.WrapText: text_option.setWrapMode(QTextOption.WordWrap) else: text_option.setWrapMode(QTextOption.ManualWrap) text_option.setTextDirection(self._opt.direction) text_option.setAlignment(QStyle.visualAlignment( self._opt.direction, self._opt.displayAlignment)) self._doc = QTextDocument(self) self._doc.setDefaultFont(self._opt.font) self._doc.setDefaultTextOption(text_option) self._doc.setDefaultStyleSheet(style.get_stylesheet(""" .highlight { {{ color['completion.match.fg'] }} } """)) self._doc.setDocumentMargin(2) if index.parent().isValid(): pattern = index.model().pattern if index.column() == 0 and pattern: text = self._opt.text.replace( pattern, '<span class="highlight">{}</span>'.format(pattern)) self._doc.setHtml(text) else: self._doc.setPlainText(self._opt.text) else: self._doc.setHtml('<b>{}</b>'.format(html.escape(self._opt.text)))