def set_grep_text(self, text): if text: self._grep_rule = self._create_regexp(text) self._grep_format.setBackground(Qt.darkGreen) else: self._grep_format = QTextCharFormat() self._grep_rule = None
def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n") self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) f = QTextCharFormat() r = QRegExp() r.setMinimal(True) f.setFontWeight(QFont.Normal) f.setForeground(Qt.darkBlue) tagList = ["\\bignore_hosts\\b", "\\bsync_hosts\\b", "\\bignore_nodes\\b", "\\bsync_nodes\\b", "\\bignore_topics\\b", "\\bignore_publishers\\b", "\\bignore_topics\\b", "\\bsync_topics\\b", "\\bignore_subscribers\\b", "\\bsync_services\\b", "\\bsync_topics_on_demand\\b", "\\bsync_remote_nodes\\b"] for tag in tagList: r.setPattern(tag) self.rules.append((QRegExp(r), QTextCharFormat(f))) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) attrList = ["\\b\\*|\\*\\B|\\/\\*"] for attr in attrList: r.setPattern(attr) self.rules.append((QRegExp(r), QTextCharFormat(f)))
class SyncHighlighter(QSyntaxHighlighter): ''' Enabled the syntax highlightning for the sync interface. ''' def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n") self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) f = QTextCharFormat() r = QRegExp() r.setMinimal(True) f.setFontWeight(QFont.Normal) f.setForeground(Qt.darkBlue) tagList = [ "\\bignore_hosts\\b", "\\bsync_hosts\\b", "\\bignore_nodes\\b", "\\bsync_nodes\\b", "\\bignore_topics\\b", "\\bignore_publishers\\b", "\\bignore_topics\\b", "\\bsync_topics\\b", "\\bignore_subscribers\\b", "\\bsync_services\\b", "\\bsync_topics_on_demand\\b", "\\bsync_remote_nodes\\b" ] for tag in tagList: r.setPattern(tag) self.rules.append((QRegExp(r), QTextCharFormat(f))) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) attrList = ["\\b\\*|\\*\\B|\\/\\*"] for attr in attrList: r.setPattern(attr) self.rules.append((QRegExp(r), QTextCharFormat(f))) # f.setForeground(Qt.red) # f.setFontWeight(QFont.Bold) # attrList = ["\\s\\*"] # for attr in attrList: # r.setPattern(attr) # self.rules.append((QRegExp(r), QTextCharFormat(f))) def highlightBlock(self, text): for pattern, myformat in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() self.setFormat(index, length, myformat) index = pattern.indexIn(text, index + length) self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.commentStart.indexIn(text) if startIndex >= 0: commentLength = len(text) - startIndex self.setFormat(startIndex, commentLength, self.commentFormat)
class SyncHighlighter(QSyntaxHighlighter): ''' Enabled the syntax highlightning for the sync interface. ''' def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n") self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) f = QTextCharFormat() r = QRegExp() r.setMinimal(True) f.setFontWeight(QFont.Normal) f.setForeground(Qt.darkBlue) tagList = ["\\bignore_hosts\\b", "\\bsync_hosts\\b", "\\bignore_nodes\\b", "\\bsync_nodes\\b", "\\bignore_topics\\b", "\\bignore_publishers\\b", "\\bignore_topics\\b", "\\bsync_topics\\b", "\\bignore_subscribers\\b", "\\bsync_services\\b", "\\bsync_topics_on_demand\\b", "\\bsync_remote_nodes\\b"] for tag in tagList: r.setPattern(tag) self.rules.append((QRegExp(r), QTextCharFormat(f))) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) attrList = ["\\b\\*|\\*\\B|\\/\\*"] for attr in attrList: r.setPattern(attr) self.rules.append((QRegExp(r), QTextCharFormat(f))) # f.setForeground(Qt.red) # f.setFontWeight(QFont.Bold) # attrList = ["\\s\\*"] # for attr in attrList: # r.setPattern(attr) # self.rules.append((QRegExp(r), QTextCharFormat(f))) def highlightBlock(self, text): for pattern, myformat in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() self.setFormat(index, length, myformat) index = pattern.indexIn(text, index + length) self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.commentStart.indexIn(text) if startIndex >= 0: commentLength = len(text) - startIndex self.setFormat(startIndex, commentLength, self.commentFormat)
def _create_format(self, color, style=''): _format = QTextCharFormat() _format.setForeground(color) if 'bold' in style: _format.setFontWeight(QFont.Bold) else: _format.setFontWeight(QFont.Normal) if 'italic' in style: _format.setFontItalic(True) return _format
def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n|\r") self.default_format = QTextCharFormat() self.default_format.setForeground(QColor(24, 24, 24)) self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) tagList = ["\\btrue\\b", "\\bfalse\\b"] # create patterns for tags for tag in tagList: self.rules.append( (self._create_regexp(tag), self._create_format(Qt.blue))) # create pattern for digits self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127)))) # create pattern for params self.rules.append((self._create_regexp("\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue))) # create pattern for params self.rules.append( (self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue))) # create pattern for list signes self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold'))) # create pattern for ??? self.rules.append( (self._create_regexp("^---$"), self._create_format(Qt.darkRed))) # create pattern for braces self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen))) # create patterns for strings self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue))) # create patterns for substitutions self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127)))) # create patterns for DOCTYPE self.rules.append((self._create_regexp("<!DOCTYPE.*>"), self._create_format(Qt.lightGray))) self.rules.append((self._create_regexp("<\\?xml.*\\?>"), self._create_format(Qt.lightGray)))
def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n|\r") self.default_format = QTextCharFormat() self.default_format.setForeground(QColor(24, 24, 24)) self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) tagList = ["\\btrue\\b", "\\bfalse\\b"] # create patterns for tags for tag in tagList: self.rules.append((self._create_regexp(tag), self._create_format(Qt.blue))) # create pattern for digits self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127)))) # create pattern for params self.rules.append((self._create_regexp("^\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue))) # create pattern for params self.rules.append((self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue))) # create pattern for list signes self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold'))) # create pattern for ??? self.rules.append((self._create_regexp("^---$"), self._create_format(Qt.darkRed))) # create pattern for braces self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen))) # create patterns for strings self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue))) # create patterns for substitutions self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127)))) # create patterns for DOCTYPE self.rules.append((self._create_regexp("<!DOCTYPE.*>"), self._create_format(Qt.lightGray))) self.rules.append((self._create_regexp("<\\?xml.*\\?>"), self._create_format(Qt.lightGray)))
def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self._grep_format = QTextCharFormat() self._grep_rule = None self.rules = [] self.rules.append((self._create_regexp(r'.*\[DEBUG\].*', syntax=QRegExp.RegExp), self._create_format(QColor(57, 181, 74)))) self.rules.append((self._create_regexp(r'.*\[INFO\].*', syntax=QRegExp.RegExp), self._create_format(QColor('#FFFAFA')))) self.rules.append((self._create_regexp(r'.*\[WARN\].*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 199, 6)))) self.rules.append((self._create_regexp(r'.*WARNING.*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 199, 6)))) self.rules.append((self._create_regexp(r'.*\[ERROR\].*', syntax=QRegExp.RegExp), self._create_format(QColor(222, 56, 43)))) self.rules.append((self._create_regexp(r'.*\[FATAL\].*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 0, 0)))) #red
def format(color, style=''): """Return a QTextCharFormat with the given attributes. """ _color = QColor() if type(color) == QColor: _color = color else: _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
class TerminalFormats(QObject): ''' Defines qt formats for terminal colors. Use ubuntu colors from https://en.wikipedia.org/wiki/ANSI_escape_code ''' default_color = QColor('#FFFAFA') default_bg = QColor('#010101') re_code = re.compile(r"\x1B\[(?P<code>.*?)m") def __init__(self, parent=None): QObject.__init__(self, parent) self.current_format = QTextCharFormat() self.current_format.setForeground(self.default_color) self.formats = {} self.formats[0] = { 'setForeground': self.default_color, 'setBackground': self.default_bg, 'setFontWeight': QFont.Normal, 'setFontItalic': False, 'setFontUnderline': False } # Normal/Default (reset all attributes) self.formats[1] = { 'setFontWeight': QFont.Bold } # Bold/Bright (bold or increased intensity) self.formats[2] = { 'setFontWeight': QFont.Light } # Dim/Faint (decreased intensity) self.formats[3] = {'setFontItalic': True} # Italicized (italic on) self.formats[4] = { 'setFontUnderline': True, 'setUnderlineStyle': QTextCharFormat.SingleUnderline } # Underscore (single underlined) self.formats[5] = { 'setFontWeight': QFont.Bold } # Blink (slow, appears as Bold) self.formats[6] = { 'setFontWeight': QFont.Black } # Blink (rapid, appears as very Bold) self.formats[7] = { 'setForeground': ('background', ), 'setBackground': ('foreground', ) } # Reverse/Inverse (swap foreground and background) self.formats[8] = { 'setForeground': ('background', ) } # Concealed/Hidden/Invisible (usefull for passwords) self.formats[9] = {'setFontStrikeOut': True} # Crossed-out characters self.formats[10] = { 'setFont': QTextCharFormat().font() } # Primary (default) font # font styles self.formats[11] = {'setFont': ('font_style', 0)} self.formats[12] = {'setFont': ('font_style', 1)} self.formats[13] = {'setFont': ('font_style', 2)} self.formats[14] = {'setFont': ('font_style', 3)} self.formats[15] = {'setFont': ('font_style', 4)} self.formats[16] = {'setFont': ('font_style', 5)} self.formats[17] = {'setFont': ('font_style', 6)} self.formats[18] = {'setFont': ('font_style', 7)} self.formats[19] = {'setFont': ('font_style', 8)} # self.formats[20] = {} # Fraktur (unsupported) self.formats[21] = {'setFontWeight': QFont.Normal} # Set Bold off self.formats[22] = {'setFontWeight': QFont.Normal} # Set Dim off self.formats[23] = {'setFontItalic': False} self.formats[24] = { 'setFontUnderline': False, 'setUnderlineStyle': QTextCharFormat.NoUnderline } # Unset underlining self.formats[25] = {'setFontWeight': QFont.Normal} # Unset Blink/Bold # self.formats[26] = {} # Reserved self.formats[27] = { 'setBackground': ('foreground', ), 'setForeground': ('background', ) } # Positive (non-inverted) self.formats[28] = { 'setForeground': ('foreground', ), 'setBackground': ('background', ) } # Concealed/Hidden/Invisible (usefull for passwords) self.formats[29] = { 'setFontStrikeOut': False } # Crossed-out characters # foreground colors self.formats[30] = {'setForeground': QColor(1, 1, 1)} # Black self.formats[31] = {'setForeground': QColor(222, 56, 43)} # Red self.formats[32] = {'setForeground': QColor(57, 181, 74)} # Green self.formats[33] = {'setForeground': QColor(255, 199, 6)} # Yellow self.formats[34] = {'setForeground': QColor(0, 111, 184)} # Blue self.formats[35] = {'setForeground': QColor(118, 38, 113)} # Megenta self.formats[36] = {'setForeground': QColor(44, 181, 233)} # Cyan self.formats[37] = {'setForeground': QColor(204, 204, 204)} # White self.formats[39] = { 'setForeground': self.default_color } # Default foreground color self.formats[90] = { 'setForeground': QColor(128, 128, 128) } # Bright Black self.formats[91] = {'setForeground': QColor(255, 0, 0)} # Bright Red self.formats[92] = {'setForeground': QColor(0, 255, 0)} # Bright Green self.formats[93] = { 'setForeground': QColor(255, 255, 0) } # Bright Yellow self.formats[94] = {'setForeground': QColor(0, 0, 255)} # Bright Blue self.formats[95] = { 'setForeground': QColor(255, 0, 255) } # Bright Magenta self.formats[96] = { 'setForeground': QColor(0, 255, 255) } # Bright Cyan self.formats[97] = { 'setForeground': QColor(255, 255, 255) } # Bright White # background colors self.formats[40] = {'setBackground': QColor(1, 1, 1)} # Black self.formats[41] = {'setBackground': QColor(222, 56, 43)} # Red self.formats[42] = {'setBackground': QColor(57, 181, 74)} # Green self.formats[43] = {'setBackground': QColor(255, 199, 6)} # Yellow self.formats[44] = {'setBackground': QColor(0, 111, 184)} # Blue self.formats[45] = {'setBackground': QColor(118, 38, 113)} # Megenta self.formats[46] = {'setBackground': QColor(44, 181, 233)} # Cyan self.formats[47] = {'setBackground': QColor(204, 204, 204)} # White self.formats[49] = { 'setBackground': self.default_bg } # Default background color self.formats[100] = { 'setBackground': QColor(128, 128, 128) } # Bright Black self.formats[101] = {'setBackground': QColor(255, 0, 0)} # Bright Red self.formats[102] = { 'setBackground': QColor(0, 255, 0) } # Bright Green self.formats[103] = { 'setBackground': QColor(255, 255, 0) } # Bright Yellow self.formats[104] = {'setBackground': QColor(0, 0, 255)} # Bright Blue self.formats[105] = { 'setBackground': QColor(255, 0, 255) } # Bright Magenta self.formats[106] = { 'setBackground': QColor(0, 255, 255) } # Bright Cyan self.formats[107] = { 'setBackground': QColor(255, 255, 255) } # Bright White def _update_format(self, fmt, updates={}, font_helper=None): for attr, args in updates.items(): try: if isinstance(args, list): getattr(fmt, attr)(*args) elif isinstance(args, tuple): if font_helper is not None: # call getter method if len(attr) > 1: getattr(fmt, attr)(getattr(font_helper, attr[0])(attr[1])) else: getattr(fmt, attr)(getattr(font_helper, attr[0])()) else: getattr(fmt, attr)(args) except AttributeError: pass def insert_formated(self, cursor, text, char_format=None): cursor.beginEditBlock() current_char_format = char_format if current_char_format is None: current_char_format = cursor.charFormat() cidx = 0 for match in self.re_code.finditer(text): code = -1 try: code = int(match.group(1)) except Exception: pass cursor.insertText(text[cidx:match.start()], current_char_format) if code in self.formats: try: font_helper = FormatHelper(current_char_format) self._update_format(current_char_format, self.formats[code], font_helper=font_helper) except Exception as err: rospy.logwarn( "Failed update format for ANSI_escape_code %d: %s" % (code, err)) cidx = match.end() cursor.insertText(text[cidx:], current_char_format) cursor.movePosition(QTextCursor.End) cursor.setCharFormat(current_char_format) cursor.endEditBlock() return current_char_format
class YamlHighlighter(QSyntaxHighlighter): ''' Enabled the syntax highlightning for the yaml files. ''' def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n|\r") self.default_format = QTextCharFormat() self.default_format.setForeground(QColor(24, 24, 24)) self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) tagList = ["\\btrue\\b", "\\bfalse\\b"] # create patterns for tags for tag in tagList: self.rules.append( (self._create_regexp(tag), self._create_format(Qt.blue))) # create pattern for digits self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127)))) # create pattern for params self.rules.append((self._create_regexp("\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue))) # create pattern for params self.rules.append( (self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue))) # create pattern for list signes self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold'))) # create pattern for ??? self.rules.append( (self._create_regexp("^---$"), self._create_format(Qt.darkRed))) # create pattern for braces self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen))) # create patterns for strings self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue))) # create patterns for substitutions self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127)))) # create patterns for DOCTYPE self.rules.append((self._create_regexp("<!DOCTYPE.*>"), self._create_format(Qt.lightGray))) self.rules.append((self._create_regexp("<\\?xml.*\\?>"), self._create_format(Qt.lightGray))) def highlightBlock(self, text): self.setFormat(0, len(text), self.default_format) for pattern, form in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() self.setFormat(index, length, form) index = pattern.indexIn(text, index + length) # mark comment blocks self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.commentStart.indexIn(text) if startIndex >= 0: commentLength = len(text) - startIndex self.setFormat(startIndex, commentLength, self.commentFormat) def _create_regexp(self, pattern=''): _regexp = QRegExp() _regexp.setMinimal(True) _regexp.setPattern(pattern) return _regexp def _create_format(self, color, style=''): _format = QTextCharFormat() _format.setForeground(color) if 'bold' in style: _format.setFontWeight(QFont.Bold) else: _format.setFontWeight(QFont.Normal) if 'italic' in style: _format.setFontItalic(True) return _format
def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n") self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) f = QTextCharFormat() r = QRegExp() r.setMinimal(True) f.setFontWeight(QFont.Normal) f.setForeground(Qt.darkBlue) tagList = [ "\\bignore_hosts\\b", "\\bsync_hosts\\b", "\\bignore_nodes\\b", "\\bsync_nodes\\b", "\\bignore_topics\\b", "\\bignore_publishers\\b", "\\bignore_topics\\b", "\\bsync_topics\\b", "\\bignore_subscribers\\b", "\\bsync_services\\b", "\\bsync_topics_on_demand\\b", "\\bsync_remote_nodes\\b" ] for tag in tagList: r.setPattern(tag) self.rules.append((QRegExp(r), QTextCharFormat(f))) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) attrList = ["\\b\\*|\\*\\B|\\/\\*"] for attr in attrList: r.setPattern(attr) self.rules.append((QRegExp(r), QTextCharFormat(f)))
def highlightBlock(self, text): for pattern, form in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() frmt = form if self._in_hl_range(index, self._tag_hl_range): frmt = QTextCharFormat(form) if not self._end_tag_found: frmt.setForeground(Qt.red) else: frmt.setForeground(self._color_hl_tag) frmt.setFontWeight(QFont.Bold) self.setFormat(index, length, frmt) index = pattern.indexIn(text, index + length) # search for YAML comments index = self.yaml_comment_rule[0].indexIn(text) if index >= 0: self.setFormat(index, len(text) - index, self.yaml_comment_rule[1]) self._tag_hl_range = [] self.setCurrentBlockState(0) # detection for XML comments self._comments_idx = [] idx_start_cmt = 0 comment_length = 0 if self.previousBlockState( ) == -1 or not self.previousBlockState() & self.STATE_COMMENT: idx_start_cmt = self.comment_start.indexIn(text) while idx_start_cmt >= 0: idx_end = self.comment_end.indexIn(text, idx_start_cmt) comment_length = 0 if idx_end == -1: self.setCurrentBlockState(self.STATE_COMMENT) comment_length = len(text) - idx_start_cmt else: comment_length = idx_end - idx_start_cmt + self.comment_end.matchedLength( ) self._comments_idx.append((idx_start_cmt, comment_length)) self.setFormat(idx_start_cmt, comment_length, self.comment_format) idx_start_cmt = self.comment_start.indexIn( text, idx_start_cmt + comment_length) # format string and detection for multiline string idx_start = self.string_pattern.indexIn(text) if self.previousBlockState( ) != -1 and self.previousBlockState() & self.STATE_STRING: strlen = idx_start + self.string_pattern.matchedLength() if idx_start == -1: strlen = len(text) self.setCurrentBlockState(self.currentBlockState() + self.STATE_STRING) self.setFormat(0, strlen, self.string_format) idx_start = self.string_pattern.indexIn(text, strlen) idx_search = idx_start + 1 while idx_start >= 0: # skip the strings which are in the comments if not self._in_hl_range(idx_search, self._comments_idx): idx_end = self.string_pattern.indexIn(text, idx_search) strlen = 0 if not self._in_hl_range(idx_end, self._comments_idx): if idx_end == -1: self.setCurrentBlockState(self.currentBlockState() + self.STATE_STRING) strlen = len(text) - idx_start else: strlen = idx_end - idx_start + self.string_pattern.matchedLength( ) idx_search = idx_start + strlen self.setFormat(idx_start, strlen, self.string_format) idx_start = self.string_pattern.indexIn(text, idx_search) idx_search = idx_start + 1 else: idx_search = idx_end + 1 else: idx_start = self.string_pattern.indexIn(text, idx_search) idx_search = idx_start + 1 # mark arguments index = self.rule_arg[0].indexIn(text) while index >= 0: if not self._in_hl_range(index, self._comments_idx): length = self.rule_arg[0].matchedLength() self.setFormat(index, length, self.rule_arg[1]) index = self.rule_arg[0].indexIn(text, index + length) # mark deprecated parameter for pattern, form in self.dep_pattern: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() frmt = form if self._in_hl_range(index, self._tag_hl_range): frmt = QTextCharFormat(form) if not self._end_tag_found: frmt.setForeground(Qt.red) else: frmt.setForeground(self._color_hl_tag) frmt.setFontWeight(QFont.Bold) self.setFormat(index, length, frmt) index = pattern.indexIn(text, index + length)
class YamlHighlighter(QSyntaxHighlighter): ''' Enabled the syntax highlightning for the yaml files. ''' def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.rules = [] self.commentStart = QRegExp("#") self.commentEnd = QRegExp("\n|\r") self.default_format = QTextCharFormat() self.default_format.setForeground(QColor(24, 24, 24)) self.commentFormat = QTextCharFormat() self.commentFormat.setFontItalic(True) self.commentFormat.setForeground(Qt.darkGray) tagList = ["\\btrue\\b", "\\bfalse\\b"] # create patterns for tags for tag in tagList: self.rules.append((self._create_regexp(tag), self._create_format(Qt.blue))) # create pattern for digits self.rules.append((self._create_regexp("\\d+"), self._create_format(QColor(127, 64, 127)))) # create pattern for params self.rules.append((self._create_regexp("^\s*[_.\w]*\s*:"), self._create_format(Qt.darkBlue))) # create pattern for params self.rules.append((self._create_regexp(":\s*:[_\.\w]*$|:\s*\@[_\.\w]*$"), self._create_format(Qt.darkBlue))) # create pattern for list signes self.rules.append((self._create_regexp("^\s*-"), self._create_format(Qt.darkRed, 'bold'))) # create pattern for ??? self.rules.append((self._create_regexp("^---$"), self._create_format(Qt.darkRed))) # create pattern for braces self.rules.append((self._create_regexp("[\[\]\{\}\,]"), self._create_format(Qt.darkGreen))) # create patterns for strings self.rules.append((self._create_regexp("\".*\"|\'.*\'"), self._create_format(Qt.blue))) # create patterns for substitutions self.rules.append((self._create_regexp("\\$\\(.*\\)"), self._create_format(QColor(127, 64, 127)))) # create patterns for DOCTYPE self.rules.append((self._create_regexp("<!DOCTYPE.*>"), self._create_format(Qt.lightGray))) self.rules.append((self._create_regexp("<\\?xml.*\\?>"), self._create_format(Qt.lightGray))) def highlightBlock(self, text): self.setFormat(0, len(text), self.default_format) for pattern, form in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() self.setFormat(index, length, form) index = pattern.indexIn(text, index + length) # mark comment blocks self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.commentStart.indexIn(text) if startIndex >= 0: commentLength = len(text) - startIndex self.setFormat(startIndex, commentLength, self.commentFormat) def _create_regexp(self, pattern=''): _regexp = QRegExp() _regexp.setMinimal(True) _regexp.setPattern(pattern) return _regexp def _create_format(self, color, style=''): _format = QTextCharFormat() _format.setForeground(color) if 'bold' in style: _format.setFontWeight(QFont.Bold) else: _format.setFontWeight(QFont.Normal) if 'italic' in style: _format.setFontItalic(True) return _format
class ScreenHighlighter(QSyntaxHighlighter): ''' Enabled the syntax highlightning for the ROS log files. ''' def __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self._grep_format = QTextCharFormat() self._grep_rule = None self.rules = [] self.rules.append((self._create_regexp(r'.*\[DEBUG\].*', syntax=QRegExp.RegExp), self._create_format(QColor(57, 181, 74)))) self.rules.append((self._create_regexp(r'.*\[INFO\].*', syntax=QRegExp.RegExp), self._create_format(QColor('#FFFAFA')))) self.rules.append((self._create_regexp(r'.*\[WARN\].*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 199, 6)))) self.rules.append((self._create_regexp(r'.*WARNING.*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 199, 6)))) self.rules.append((self._create_regexp(r'.*\[ERROR\].*', syntax=QRegExp.RegExp), self._create_format(QColor(222, 56, 43)))) self.rules.append((self._create_regexp(r'.*\[FATAL\].*', syntax=QRegExp.RegExp), self._create_format(QColor(255, 0, 0)))) #red def _create_format(self, color, style=''): _format = QTextCharFormat() _format.setForeground(color) if 'bold' in style: _format.setFontWeight(QFont.Bold) else: _format.setFontWeight(QFont.Normal) if 'italic' in style: _format.setFontItalic(True) return _format def highlightBlock(self, text): for pattern, frmt in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() self.setFormat(index, length, frmt) index = pattern.indexIn(text, index + length) if self._grep_rule is not None: index = self._grep_rule.indexIn(text) while index >= 0: length = self._grep_rule.matchedLength() self.setFormat(index, length, self._grep_format) index = self._grep_rule.indexIn(text, index + length) def has_grep_text(self): return self._grep_rule is not None def set_grep_text(self, text): if text: self._grep_rule = self._create_regexp(text) self._grep_format.setBackground(Qt.darkGreen) else: self._grep_format = QTextCharFormat() self._grep_rule = None def contains_grep_text(self, text): if self._grep_rule is not None: return self._grep_rule.indexIn(text) >= 0 def _create_regexp(self, pattern='', cs=Qt.CaseInsensitive, syntax=QRegExp.Wildcard, minimal=False): _regexp = QRegExp(pattern, cs, syntax) _regexp.setMinimal(minimal) return _regexp
def __init__(self, parent=None): QObject.__init__(self, parent) self.current_format = QTextCharFormat() self.current_format.setForeground(self.default_color) self.formats = {} self.formats[0] = { 'setForeground': self.default_color, 'setBackground': self.default_bg, 'setFontWeight': QFont.Normal, 'setFontItalic': False, 'setFontUnderline': False } # Normal/Default (reset all attributes) self.formats[1] = { 'setFontWeight': QFont.Bold } # Bold/Bright (bold or increased intensity) self.formats[2] = { 'setFontWeight': QFont.Light } # Dim/Faint (decreased intensity) self.formats[3] = {'setFontItalic': True} # Italicized (italic on) self.formats[4] = { 'setFontUnderline': True, 'setUnderlineStyle': QTextCharFormat.SingleUnderline } # Underscore (single underlined) self.formats[5] = { 'setFontWeight': QFont.Bold } # Blink (slow, appears as Bold) self.formats[6] = { 'setFontWeight': QFont.Black } # Blink (rapid, appears as very Bold) self.formats[7] = { 'setForeground': ('background', ), 'setBackground': ('foreground', ) } # Reverse/Inverse (swap foreground and background) self.formats[8] = { 'setForeground': ('background', ) } # Concealed/Hidden/Invisible (usefull for passwords) self.formats[9] = {'setFontStrikeOut': True} # Crossed-out characters self.formats[10] = { 'setFont': QTextCharFormat().font() } # Primary (default) font # font styles self.formats[11] = {'setFont': ('font_style', 0)} self.formats[12] = {'setFont': ('font_style', 1)} self.formats[13] = {'setFont': ('font_style', 2)} self.formats[14] = {'setFont': ('font_style', 3)} self.formats[15] = {'setFont': ('font_style', 4)} self.formats[16] = {'setFont': ('font_style', 5)} self.formats[17] = {'setFont': ('font_style', 6)} self.formats[18] = {'setFont': ('font_style', 7)} self.formats[19] = {'setFont': ('font_style', 8)} # self.formats[20] = {} # Fraktur (unsupported) self.formats[21] = {'setFontWeight': QFont.Normal} # Set Bold off self.formats[22] = {'setFontWeight': QFont.Normal} # Set Dim off self.formats[23] = {'setFontItalic': False} self.formats[24] = { 'setFontUnderline': False, 'setUnderlineStyle': QTextCharFormat.NoUnderline } # Unset underlining self.formats[25] = {'setFontWeight': QFont.Normal} # Unset Blink/Bold # self.formats[26] = {} # Reserved self.formats[27] = { 'setBackground': ('foreground', ), 'setForeground': ('background', ) } # Positive (non-inverted) self.formats[28] = { 'setForeground': ('foreground', ), 'setBackground': ('background', ) } # Concealed/Hidden/Invisible (usefull for passwords) self.formats[29] = { 'setFontStrikeOut': False } # Crossed-out characters # foreground colors self.formats[30] = {'setForeground': QColor(1, 1, 1)} # Black self.formats[31] = {'setForeground': QColor(222, 56, 43)} # Red self.formats[32] = {'setForeground': QColor(57, 181, 74)} # Green self.formats[33] = {'setForeground': QColor(255, 199, 6)} # Yellow self.formats[34] = {'setForeground': QColor(0, 111, 184)} # Blue self.formats[35] = {'setForeground': QColor(118, 38, 113)} # Megenta self.formats[36] = {'setForeground': QColor(44, 181, 233)} # Cyan self.formats[37] = {'setForeground': QColor(204, 204, 204)} # White self.formats[39] = { 'setForeground': self.default_color } # Default foreground color self.formats[90] = { 'setForeground': QColor(128, 128, 128) } # Bright Black self.formats[91] = {'setForeground': QColor(255, 0, 0)} # Bright Red self.formats[92] = {'setForeground': QColor(0, 255, 0)} # Bright Green self.formats[93] = { 'setForeground': QColor(255, 255, 0) } # Bright Yellow self.formats[94] = {'setForeground': QColor(0, 0, 255)} # Bright Blue self.formats[95] = { 'setForeground': QColor(255, 0, 255) } # Bright Magenta self.formats[96] = { 'setForeground': QColor(0, 255, 255) } # Bright Cyan self.formats[97] = { 'setForeground': QColor(255, 255, 255) } # Bright White # background colors self.formats[40] = {'setBackground': QColor(1, 1, 1)} # Black self.formats[41] = {'setBackground': QColor(222, 56, 43)} # Red self.formats[42] = {'setBackground': QColor(57, 181, 74)} # Green self.formats[43] = {'setBackground': QColor(255, 199, 6)} # Yellow self.formats[44] = {'setBackground': QColor(0, 111, 184)} # Blue self.formats[45] = {'setBackground': QColor(118, 38, 113)} # Megenta self.formats[46] = {'setBackground': QColor(44, 181, 233)} # Cyan self.formats[47] = {'setBackground': QColor(204, 204, 204)} # White self.formats[49] = { 'setBackground': self.default_bg } # Default background color self.formats[100] = { 'setBackground': QColor(128, 128, 128) } # Bright Black self.formats[101] = {'setBackground': QColor(255, 0, 0)} # Bright Red self.formats[102] = { 'setBackground': QColor(0, 255, 0) } # Bright Green self.formats[103] = { 'setBackground': QColor(255, 255, 0) } # Bright Yellow self.formats[104] = {'setBackground': QColor(0, 0, 255)} # Bright Blue self.formats[105] = { 'setBackground': QColor(255, 0, 255) } # Bright Magenta self.formats[106] = { 'setBackground': QColor(0, 255, 255) } # Bright Cyan self.formats[107] = { 'setBackground': QColor(255, 255, 255) } # Bright White
def highlightBlock(self, text): for pattern, form in self.rules: index = pattern.indexIn(text) while index >= 0: length = pattern.matchedLength() frmt = form if self._in_hl_range(index, self._tag_hl_range): frmt = QTextCharFormat(form) if not self._end_tag_found: frmt.setForeground(Qt.red) frmt.setFontWeight(QFont.Bold) self.setFormat(index, length, frmt) index = pattern.indexIn(text, index + length) self._tag_hl_range = [] self.setCurrentBlockState(0) # detection for comments self._comments_idx = [] idx_start_cmt = 0 comment_length = 0 if self.previousBlockState() == -1 or not self.previousBlockState() & self.STATE_COMMENT: idx_start_cmt = self.comment_start.indexIn(text) while idx_start_cmt >= 0: idx_end = self.comment_end.indexIn(text, idx_start_cmt) comment_length = 0 if idx_end == -1: self.setCurrentBlockState(self.STATE_COMMENT) comment_length = len(text) - idx_start_cmt else: comment_length = idx_end - idx_start_cmt + self.comment_end.matchedLength() self._comments_idx.append((idx_start_cmt, comment_length)) self.setFormat(idx_start_cmt, comment_length, self.comment_format) idx_start_cmt = self.comment_start.indexIn(text, idx_start_cmt + comment_length) # format string and detection for multiline string idx_start = self.string_pattern.indexIn(text) if self.previousBlockState() != -1 and self.previousBlockState() & self.STATE_STRING: strlen = idx_start + self.string_pattern.matchedLength() if idx_start == -1: strlen = len(text) self.setCurrentBlockState(self.currentBlockState() + self.STATE_STRING) self.setFormat(0, strlen, self.string_format) idx_start = self.string_pattern.indexIn(text, strlen) idx_search = idx_start + 1 while idx_start >= 0: # skip the strings which are in the comments if not self._in_hl_range(idx_search, self._comments_idx): idx_end = self.string_pattern.indexIn(text, idx_search) strlen = 0 if not self._in_hl_range(idx_end, self._comments_idx): if idx_end == -1: self.setCurrentBlockState(self.currentBlockState() + self.STATE_STRING) strlen = len(text) - idx_start else: strlen = idx_end - idx_start + self.string_pattern.matchedLength() idx_search = idx_start + strlen self.setFormat(idx_start, strlen, self.string_format) idx_start = self.string_pattern.indexIn(text, idx_search) idx_search = idx_start + 1 else: idx_search = idx_end + 1 else: idx_start = self.string_pattern.indexIn(text, idx_search) idx_search = idx_start + 1 # mark arguments index = self.rule_arg[0].indexIn(text) while index >= 0: if not self._in_hl_range(index, self._comments_idx): length = self.rule_arg[0].matchedLength() self.setFormat(index, length, self.rule_arg[1]) index = self.rule_arg[0].indexIn(text, index + length)