Пример #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)))
Пример #2
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)))
Пример #3
0
 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
Пример #4
0
 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
Пример #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
Пример #6
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)
Пример #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)
                 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)