def get_syntax_format(color=None, style=''): """ Returns a QTextCharFormat with the given attributes. """ _color = None if type(color) == str: _color = QColor() _color.setNamedColor(color) if type(color) == list: _color = QColor(*color) if color == 'green': _color = Qt.green _format = QTextCharFormat() if _color: _format.setForeground(_color) if 'bold' in style: _format.setFontWeight(QFont.Bold) if 'italic' in style: _format.setFontItalic(True) return _format
def format(color, style=''): """Return a QTextCharFormat with the given attributes. """ _color = QColor() _color.setNamedColor(color) _format = QTextCharFormat() _format.setForeground(_color) if 'bold' in style: _format.setFontWeight(QFont.Bold) if 'italic' in style: _format.setFontItalic(True) return _format
def lookup(self, key): """Return a QTextCharFormat with the given attributes. """ color, style = self.styles[key] _color = QColor() _color.setNamedColor(color) _format = QTextCharFormat() _format.setForeground(_color) if style: if 'bold' in style: _format.setFontWeight(QFont.Bold) if 'italic' in style: _format.setFontItalic(True) return _format
import re from Qt.QtCore import Qt from Qt.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont COMMENT_RE = re.compile(r"/\*.+\*/") COMMENT_FORMAT = QTextCharFormat() COMMENT_FORMAT.setFontItalic(True) COMMENT_FORMAT.setForeground(Qt.darkGray) JINJA_RE = re.compile(r"{{.+}}") JINJA_FORMAT = QTextCharFormat() JINJA_FORMAT.setFontWeight(QFont.Bold) JINJA_FORMAT.setForeground(Qt.darkMagenta) QCLASS_RE = re.compile(r"Q[\w]+") QCLASS_FORMAT = QTextCharFormat() QCLASS_FORMAT.setFontWeight(QFont.Bold) QCLASS_FORMAT.setForeground(Qt.darkGreen) QSUBELEMENT_RE = re.compile(r"::([\w-]+)") QSUBELEMENT_FORMAT = QTextCharFormat() QSUBELEMENT_FORMAT.setFontWeight(QFont.Bold) QSUBELEMENT_FORMAT.setForeground(Qt.darkBlue) PROPERTY_RE = re.compile(r"\[[^=]+=[^\]]+]") PROPERTY_FORMAT = QTextCharFormat() PROPERTY_FORMAT.setFontItalic(True) PROPERTY_FORMAT.setForeground(Qt.darkGreen) class TemplateHighlighter(QSyntaxHighlighter):
import re from Qt.QtCore import Qt from Qt.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont LINE_FILE_RE = re.compile(r"^\s\s\S.+") LINE_FILE_FORMAT = QTextCharFormat() LINE_FILE_FORMAT.setForeground(Qt.lightGray) LINE_CODE_RE = re.compile(r"^\s\s\s\s\S.+") LINE_CODE_FORMAT = QTextCharFormat() LINE_CODE_FORMAT.setFontWeight(QFont.Bold) FILE_RE = re.compile(r"\"[^\"]+\"") FILE_FORMAT = QTextCharFormat() FILE_FORMAT.setFontItalic(True) FILE_FORMAT.setForeground(Qt.black) CALL_RE = re.compile(r", in ([^$]+)$") CALL_FORMAT = QTextCharFormat() CALL_FORMAT.setFontWeight(QFont.Bold) CALL_FORMAT.setForeground(Qt.magenta) LINE_RE = re.compile(r"line \d+") LINE_FORMAT = QTextCharFormat() LINE_FORMAT.setForeground(Qt.green) class TracebackHighlighter(QSyntaxHighlighter): RULES = { LINE_FILE_RE: LINE_FILE_FORMAT,