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,771 views  Rating:
(1 rating)
Page 2 of 5

The QSyntaxHighlighter

Applying the QSyntaxHighlighter can be achieved very simply be feeding him with a parent as first argument, like below ;

class StdOut_Syntax(QSyntaxHighlighter):
    def __init__(self, parent):
        super(StdOut_Syntax, self).__init__(parent)

PyQt use the function highlightBlock to handle the syntax coloring, this function needs an input text - provided internally by the QTextEdit - in which we'll iterate for each regular expression we want to colorize, define a QTextCharFormat for the expression with the function setFormat, here is a simple example ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class StdOut_Syntax(QSyntaxHighlighter):
    def __init__(self, parent):
        super(StdOut_Syntax, self).__init__(parent)
 
    def highlightBlock(self, text):
        color = QColor(255, 125, 160)
        pattern = QRegExp(r'^//.+$')        # regexp pattern
        keyword = QTextCharFormat()
        keyword.setForeground(color)         # defining the aspect
        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, keyword) # we apply the format to the match
            index = pattern.indexIn(text, index + len)
        self.setCurrentBlockState(0)

 
Note ; The regular expression used above can be 'humanly translated' as ;
If the line starts with '//' until the end of the line
^ means the start of the line
.+ greedy expression match a maximum amount of characters, the '.' means any character
$ means the end of the line

 

 

 

 

Applying this class to our widget cmdsScrollFieldReporter with the following code we should get ;


Working color syntaxer class
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
from PySide.QtGui import *
from PySide.QtCore import *
from shiboken import wrapInstance
from maya.OpenMayaUI import MQtUtil
 
class StdOut_Syntax(QSyntaxHighlighter):
    def __init__(self, parent):
        super(StdOut_Syntax, self).__init__(parent)
        self.parent = parent
 
    def highlightBlock(self, text):
        color = QColor(255, 125, 160)
        pattern = QRegExp(r'^//.+$')        # regexp pattern
        keyword = QTextCharFormat()
        keyword.setForeground(color)         # defining the aspect
        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, keyword) # 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
    return StdOut_Syntax(se_edit)
wrap()

This means each time we'll meet the regular expression of "a line starting with // until the end of the line" will get a red color ! Thus ;


We see the light =)
Note ; A QTextEdit accepts only one QSyntaxHighlighter associated, if a second one is linked to the QTextEdit, result may be unpredictable, so the previous QSyntaxHighlighter must be removed from the QTextEdit children everytime you want to refresh it completely !

Now let's have a look how to "link" our QSyntaxHighlighter to Maya !