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