Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    def get_style(self, color, bold=False):
        brush = QBrush(QColor(*color))
        f = QTextCharFormat()
        if bold:
            f.setFontWeight(QFont.Bold)
        f.setForeground(brush)

        return f
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    def initialize(self):

        # numeric color
        self._numericFormat.setForeground(QColor('#9ACD32'))

        # mel command options started with -
        melOpsFormat = QTextCharFormat()
        melOpsFormat.setForeground(QColor('#B8860B'))
        self.__rules.append((re.compile('-[a-zA-Z]+\\b'), melOpsFormat))

        # keywords color
        self._keywordColor = QColor(0, 128, 255)

        self._numeric()
        self._keywordFormat()
        self._cmdsFunctionFormat()

        # maya api format
        mapiFormat = QTextCharFormat()
        mapiFormat.setForeground(self._keywordColor)
        self.__rules.append((re.compile('\\bM\\w+\\b'), mapiFormat))
        # Qt
        self.__rules.append((re.compile('\\bQ\\w+\\b'), mapiFormat))

        # quotation
        self._quotationFormat.setForeground(Qt.green)
        # quote: ""
        self.__rules.append((re.compile('".*"'), self._quotationFormat))
        # single quotes for python: ''
        self.__rules.append((re.compile("'.*'"), self._quotationFormat))

        # sing line comment
        # orange red
        self._commentFormat.setForeground(QColor(255, 128, 64))
        # // mel comment
        self.__rules.append((re.compile('//[^\n]*'), self._commentFormat))
        # # python comment
        self.__rules.append((re.compile('#[^\n]*'), self._commentFormat))

        # function and class format
        funcFormat = QTextCharFormat()
        funcFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('\\b(\\w+)\(.*\):'), funcFormat))

        # mel warning
        warningFormat = QTextCharFormat()
        warningFormat.setForeground(QColor('#FF9ACD32'))
        warningFormat.setBackground(Qt.yellow)
        warningFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('// Warning:[^\n]*//'), warningFormat))

        # mel error
        errorFormat = QTextCharFormat()
        errorFormat.setForeground(QColor('#FF9ACD32'))
        errorFormat.setBackground(Qt.red)
        errorFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('// Error:[^\n]*//'), errorFormat))

        self.setDocument(self.parent.document())
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def _keywordFormat(self):
        '''set up keyword format'''
        # mel keyword
        melKeywords = [
            'false', 'float', 'int', 'matrix', 'off', 'on', 'string', 'true',
            'vector', 'yes', 'alias', 'case', 'catch', 'break', 'case',
            'continue', 'default', 'do', 'else', 'for', 'if', 'in', 'while',
            'alias', 'case', 'catch', 'global', 'proc', 'return', 'source',
            'switch'
        ]
        # python keyword
        pyKeywords = keyword.kwlist + ['False', 'True', 'None']

        keywords = {}.fromkeys(melKeywords)
        keywords.update({}.fromkeys(pyKeywords))
        # keyword format
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(self._keywordColor)
        keywordFormat.setFontWeight(QFont.Bold)
        kwtext = '\\b(' + "|".join(keywords) + ')\\b'
        self.__rules.append((re.compile(kwtext), keywordFormat))
Ejemplo n.º 7
0
    def _cmdsFunctionFormat(self):
        '''set up maya.cmds functions'''
        mayaBinDir = os.path.dirname(sys.executable)
        cmdsList = os.path.join(mayaBinDir, 'commandList')
        functions = '\\b('
        with open(cmdsList) as phile:
            for line in phile:
                functions += line.split(' ')[0] + '|'

        # global MEL procedures
        melProcedures = cmds.melInfo()
        maxlen = 1400
        stop = len(melProcedures) / maxlen
        melProc = []
        melProc.append('\\b(' + '|'.join(melProcedures[:maxlen]) + ')\\b')
        for i in range(1, stop - 1):
            start = maxlen * i
            end = maxlen * (i + 1)
            melProc.append('\\b(' + '|'.join(melProcedures[start:end]) +
                           ')\\b')
        melProc.append('\\b(' + '|'.join(melProcedures[maxlen * stop:]) +
                       ')\\b')

        # TODO: should update it when a plug-in was load.
        # function from plug-ins
        plugins = cmds.pluginInfo(query=1, listPlugins=1)
        for plugin in plugins:
            funcFromPlugin = cmds.pluginInfo(plugin, query=1, command=1)
            if funcFromPlugin:
                functions += '|'.join(funcFromPlugin)
        functions = functions[:-1] + ')\\b'

        # function format
        funcFormat = QTextCharFormat()
        funcFormat.setForeground(self._keywordColor)
        self.__rules.append((re.compile(functions), funcFormat))
        for mp in melProc:
            self.__rules.append((re.compile(mp), funcFormat))
Ejemplo n.º 8
0
class Highlighter(QSyntaxHighlighter):
    """syntax highlighter"""
    def __init__(self, parent=None):
        super(Highlighter, self).__init__(parent)
        self.parent = parent
        self.__rules = []
        self._numericFormat = QTextCharFormat()
        self._quotationFormat = QTextCharFormat()
        self._commentFormat = QTextCharFormat()

        # Quotes
        self._singleQuotes = QRegExp("'''")
        self._doubleQuotes = QRegExp('"""')

        # mel multi-line comment: /*  */
        self._melMLComStart = re.compile('/\\*')
        self._melMLComEnd = re.compile('\\*/')

    def initialize(self):

        # numeric color
        self._numericFormat.setForeground(QColor('#9ACD32'))

        # mel command options started with -
        melOpsFormat = QTextCharFormat()
        melOpsFormat.setForeground(QColor('#B8860B'))
        self.__rules.append((re.compile('-[a-zA-Z]+\\b'), melOpsFormat))

        # keywords color
        self._keywordColor = QColor(0, 128, 255)

        self._numeric()
        self._keywordFormat()
        self._cmdsFunctionFormat()

        # maya api format
        mapiFormat = QTextCharFormat()
        mapiFormat.setForeground(self._keywordColor)
        self.__rules.append((re.compile('\\bM\\w+\\b'), mapiFormat))
        # Qt
        self.__rules.append((re.compile('\\bQ\\w+\\b'), mapiFormat))

        # quotation
        self._quotationFormat.setForeground(Qt.green)
        # quote: ""
        self.__rules.append((re.compile('".*"'), self._quotationFormat))
        # single quotes for python: ''
        self.__rules.append((re.compile("'.*'"), self._quotationFormat))

        # sing line comment
        # orange red
        self._commentFormat.setForeground(QColor(255, 128, 64))
        # // mel comment
        self.__rules.append((re.compile('//[^\n]*'), self._commentFormat))
        # # python comment
        self.__rules.append((re.compile('#[^\n]*'), self._commentFormat))

        # function and class format
        funcFormat = QTextCharFormat()
        funcFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('\\b(\\w+)\(.*\):'), funcFormat))

        # mel warning
        warningFormat = QTextCharFormat()
        warningFormat.setForeground(QColor('#FF9ACD32'))
        warningFormat.setBackground(Qt.yellow)
        warningFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('// Warning:[^\n]*//'), warningFormat))

        # mel error
        errorFormat = QTextCharFormat()
        errorFormat.setForeground(QColor('#FF9ACD32'))
        errorFormat.setBackground(Qt.red)
        errorFormat.setFontWeight(QFont.Bold)
        self.__rules.append((re.compile('// Error:[^\n]*//'), errorFormat))

        self.setDocument(self.parent.document())

    def _numeric(self):
        '''set up numeric format'''
        num_01 = re.compile('\\b[+-]?[0-9]+[lL]?\\b')
        num_02 = re.compile('\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b')
        num_03 = re.compile(
            '\\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b')
        num_regs = (num_01, num_02, num_03)
        for nr in num_regs:
            self.__rules.append((nr, self._numericFormat))

    def _keywordFormat(self):
        '''set up keyword format'''
        # mel keyword
        melKeywords = [
            'false', 'float', 'int', 'matrix', 'off', 'on', 'string', 'true',
            'vector', 'yes', 'alias', 'case', 'catch', 'break', 'case',
            'continue', 'default', 'do', 'else', 'for', 'if', 'in', 'while',
            'alias', 'case', 'catch', 'global', 'proc', 'return', 'source',
            'switch'
        ]
        # python keyword
        pyKeywords = keyword.kwlist + ['False', 'True', 'None']

        keywords = {}.fromkeys(melKeywords)
        keywords.update({}.fromkeys(pyKeywords))
        # keyword format
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(self._keywordColor)
        keywordFormat.setFontWeight(QFont.Bold)
        kwtext = '\\b(' + "|".join(keywords) + ')\\b'
        self.__rules.append((re.compile(kwtext), keywordFormat))

    def _cmdsFunctionFormat(self):
        '''set up maya.cmds functions'''
        mayaBinDir = os.path.dirname(sys.executable)
        cmdsList = os.path.join(mayaBinDir, 'commandList')
        functions = '\\b('
        with open(cmdsList) as phile:
            for line in phile:
                functions += line.split(' ')[0] + '|'

        # global MEL procedures
        melProcedures = cmds.melInfo()
        maxlen = 1400
        stop = len(melProcedures) / maxlen
        melProc = []
        melProc.append('\\b(' + '|'.join(melProcedures[:maxlen]) + ')\\b')
        for i in range(1, stop - 1):
            start = maxlen * i
            end = maxlen * (i + 1)
            melProc.append('\\b(' + '|'.join(melProcedures[start:end]) +
                           ')\\b')
        melProc.append('\\b(' + '|'.join(melProcedures[maxlen * stop:]) +
                       ')\\b')

        # TODO: should update it when a plug-in was load.
        # function from plug-ins
        plugins = cmds.pluginInfo(query=1, listPlugins=1)
        for plugin in plugins:
            funcFromPlugin = cmds.pluginInfo(plugin, query=1, command=1)
            if funcFromPlugin:
                functions += '|'.join(funcFromPlugin)
        functions = functions[:-1] + ')\\b'

        # function format
        funcFormat = QTextCharFormat()
        funcFormat.setForeground(self._keywordColor)
        self.__rules.append((re.compile(functions), funcFormat))
        for mp in melProc:
            self.__rules.append((re.compile(mp), funcFormat))

    def _melMLCommentFormat(self, text):
        '''set up mel multi-line comment: /*  */'''
        startIndex = 0
        commentLen = 0
        self.setCurrentBlockState(0)
        if self.previousBlockState() != 1:
            searchStart = self._melMLComStart.search(text)
            if searchStart:
                startIndex = searchStart.start()
                searchEnd = self._melMLComEnd.search(text)
                if searchEnd:
                    commentLen = searchEnd.end() - startIndex
                else:
                    self.setCurrentBlockState(1)
                    commentLen = len(text) - startIndex
        else:
            searchEnd = self._melMLComEnd.search(text)
            if searchEnd:
                commentLen = searchEnd.end()
            else:
                self.setCurrentBlockState(1)
                commentLen = len(text)
        if commentLen > 0:
            self.setFormat(startIndex, commentLen, self._commentFormat)

    def quotesFormat(self, text, regExp, state):
        '''set up single or double quotes strings format'''
        if self.previousBlockState() == state:
            startIndex = 0
            add = 0
        else:
            startIndex = regExp.indexIn(text)
            add = regExp.matchedLength()

        while startIndex >= 0:
            end = regExp.indexIn(text, startIndex + add)
            if end >= add:
                quotesLen = end - startIndex + regExp.matchedLength()
                self.setCurrentBlockState(0)
            else:
                self.setCurrentBlockState(state)
                quotesLen = len(text) - startIndex + add
            self.setFormat(startIndex, quotesLen, self._quotationFormat)
            startIndex = regExp.indexIn(text, startIndex + quotesLen)

        if self.currentBlockState() == state:
            return True
        else:
            return False

    def highlightBlock(self, text):
        '''highlight text'''
        for regExp, tformat in self.__rules:
            match = regExp.search(text)
            while match:
                self.setFormat(match.start(),
                               match.end() - match.start(), tformat)
                match = regExp.search(text, match.end())

        # blocks
        self._melMLCommentFormat(text)
        quotesState = self.quotesFormat(text, self._singleQuotes, 2)
        if not quotesState:
            self.quotesFormat(text, self._doubleQuotes, 3)
Ejemplo n.º 9
0
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):
Ejemplo n.º 10
0
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,