Adverti horiz upsell
Color syntaxing the Script Editor Reporter
Color syntaxing the Script Editor Reporter
mlouala, updated 2017-04-16 11:20:08 UTC 5,762 views  Rating:
(1 rating)
Page 4 of 5

Finalizing

Here is a bit more complex example to show you advanced use of the regular expressions

Our final color syntaxer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from PySide.QtGui import *
from PySide.QtCore import *
from shiboken import wrapInstance
from maya.OpenMayaUI import MQtUtil
 
class Rule():
    def __init__(self, fg_color, pattern='', bg_color=None, bold=False, italic=False):
        self.pattern = QRegExp(pattern)
        self.form = QTextCharFormat()
        self.form.setForeground(QColor(*fg_color))
        if bg_color:
            self.form.setBackground(QColor(*bg_color))
        font = QFont('Courier New', 9)
        font.setBold(bold)
        font.setItalic(italic)
        self.form.setFont(font)
 
class StdOut_Syntax(QSyntaxHighlighter):
    def __init__(self, parent, rules):
        super(StdOut_Syntax, self).__init__(parent)
        self.parent = parent
        self.rules = rules
 
    def highlightBlock(self, text):
        # applying each rules
        for rule in self.rules:
            pattern = rule.pattern        # regexp pattern
            index = pattern.indexIn(text)
            while index >= 0:
                # loop through the text to find matches
                len = pattern.matchedLength() # length of the match
                self.setFormat(index, len, rule.form) # we apply the format to the match
                index = pattern.indexIn(text, index + len)
            self.setCurrentBlockState(0)
 
def wrap():
    i = 1
    while i:
        try:
            se_edit = wrapInstance(long(MQtUtil.findControl('cmdScrollFieldReporter%i' % i)),
                                                            QTextEdit)
            # we remove the old syntax and raise an exception to get out of the while
            assert se_edit.findChild(QSyntaxHighlighter).deleteLater()
        except TypeError:
            i += 1     # if we don't find the widget we increment '
        except (AttributeError, AssertionError):
            break
 
    rules = [Rule((212, 160, 125), r'^//.+$', bold=True),
             Rule((185, 125, 255), r'^#.+$', italic=True),
             Rule((255, 175, 44), r'^(#|//).*(error|Error).+$')]
 
    StdOut = StdOut_Syntax(se_edit, rules)
    return StdOut
StdOut = wrap()

 A simple translation of the above regular expressions would be ;

  • line starting with // until the end of the line
  • line starting with # until the end of the line
  • line starting with # OR // and contains the word error or Error until the end of the line

Here is the result (move the mouse above the image to see result);

  
Coloring the QTextEdit

 

You can test new rules in real time with the two following lines ; write your rules then copy them in your syntax.py to have them everytime you launch Maya !

StdOut.rules.append(Rule((255,255,255), r'^.*?Result.*?$', bold=True))
StdOut.rehighlight()

Will add a rule to colorize each line which contains the word Result in white bold