Example #1
0
    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 __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 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 _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
Example #5
0
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
 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
Example #7
0
 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)
Example #8
0
 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