티스토리 뷰

개인적인 툴을 제작 하던 중에, 파이썬 소스코드를 읽어와서 QPlainTextEdit 로 보여주어야 할 일이 생겨서 코드를 만들었습니다. (만들었다기 보단 인터넷에 있는걸 합쳐놨어요. 두개가 분리되있어서 조금 귀찮긴 하더라고요)


출처(view line, selection color) : 

 https://stackoverflow.com/questions/40386194/create-text-area-textedit-with-line-number-in-pyqt

출처(syntax highlighting) : 

   https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting



codeeditor.py 를 qtdesigner 에서 promote 시켜서 사용 하시면 됩니다.<그림2 참조, 모르시면 댓글남기시면... 언젠간 답변 드릴 수도?>   http://4uwingnet.tistory.com/8 에 포스팅이 완료되었습니다. 참조하세요.


<codeeditor.py 실행 화면>


< qtdesigner 에서 QPlainTextEdit을 CodeEditor로 promote 시킨 화면>





< codeeditor.py >
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
original author : 
__author__ = https://stackoverflow.com/users/839338/dan-dev
"""
 
__author__ = "Seounghun, Chung <4uwingnet@naver.com>"
__license__ = "License:BSD 3Clause"
 
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import syntax
 
class LineNumberArea(QWidget):
 
 
    def __init__(self, editor):
        super().__init__(editor)
        self.myeditor = editor
 
 
    def sizeHint(self):
        return Qsize(self.editor.lineNumberAreaWidth(), 0)
 
 
    def paintEvent(self, event):
        self.myeditor.lineNumberAreaPaintEvent(event)
 
 
class CodeEditor(QPlainTextEdit):
    def __init__(self, parent = None):
        super(CodeEditor,self).__init__(parent)
        self.highlight = syntax.PythonHighlighter(self.document())
        
        self.lineNumberArea = LineNumberArea(self)
 
        self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
        self.updateRequest.connect(self.updateLineNumberArea)
        self.cursorPositionChanged.connect(self.highlightCurrentLine)
        
        self.updateLineNumberAreaWidth(0)
 
 
    def lineNumberAreaWidth(self):
        digits = 1
        count = max(1, self.blockCount())
        while count >= 10:
            count /= 10
            digits += 1
        space = 3 + self.fontMetrics().width('9'* digits
        return space
 
 
    def updateLineNumberAreaWidth(self, _):
        self.setViewportMargins(self.lineNumberAreaWidth(), 000)
 
 
    def updateLineNumberArea(self, rect, dy):
 
        if dy:
            self.lineNumberArea.scroll(0, dy)
        else:
            self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(),
                       rect.height())
 
        if rect.contains(self.viewport().rect()):
            self.updateLineNumberAreaWidth(0)
 
 
    def resizeEvent(self, event):
        super().resizeEvent(event)
 
        cr = self.contentsRect();
        self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(),
                    self.lineNumberAreaWidth(), cr.height()))
 
 
    def lineNumberAreaPaintEvent(self, event):
        mypainter = QPainter(self.lineNumberArea)
 
        mypainter.fillRect(event.rect(), Qt.lightGray)
 
        block = self.firstVisibleBlock()
        blockNumber = block.blockNumber()
        top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
        bottom = top + self.blockBoundingRect(block).height()
 
        # Just to make sure I use the right font
        height = self.fontMetrics().height()
        while block.isValid() and (top <= event.rect().bottom()):
            if block.isVisible() and (bottom >= event.rect().top()):
                number = str(blockNumber + 1)
                mypainter.setPen(Qt.black)
                mypainter.drawText(0, top, self.lineNumberArea.width(), height,
                 Qt.AlignRight, number)
 
            block = block.next()
            top = bottom
            bottom = top + self.blockBoundingRect(block).height()
            blockNumber += 1
 
 
    def highlightCurrentLine(self):
        extraSelections = []
 
        if not self.isReadOnly():
            selection = QTextEdit.ExtraSelection()
 
            lineColor = QColor(Qt.yellow).lighter(185)
 
            selection.format.setBackground(lineColor)
            selection.format.setProperty(QTextFormat.FullWidthSelection, True)
            selection.cursor = self.textCursor()
            selection.cursor.clearSelection()
            extraSelections.append(selection)
        self.setExtraSelections(extraSelections)
 
if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
 
    app = QApplication(sys.argv)
    codeEditor = CodeEditor()
    with open("syntax.py","r") as f:
        codeEditor.setPlainText(f.read())
    codeEditor.show()    
    sys.exit(app.exec_())        
cs



< syntax.py >

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
original author : 
__author__ = https://wiki.python.org/moin/PyQt/Python%20syntax%20highlighting
"""
__author__ = "Seounghun, Chung <4uwingnet@naver.com>"
__license__ = "License:BSD 3Clause"
 
 
import sys
 
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
from PyQt5.QtWidgets import *
 
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
 
 
# Syntax styles that can be shared by all languages
STYLES = {
    'keyword'format('blue'),
    'operator'format('red'),
    'brace'format('Gray'),
    'defclass'format('black''bold'),
    'string'format('magenta'),
    'string2'format('darkMagenta'),
    'comment'format('darkGreen''italic'),
    'self'format('black''italic'),
    'numbers'format('brown'),
}
 
 
class PythonHighlighter (QSyntaxHighlighter):
    """Syntax highlighter for the Python language.
    """
    # Python keywords
    keywords = [
        'and''assert''break''class''continue''def',
        'del''elif''else''except''exec''finally',
        'for''from''global''if''import''in',
        'is''lambda''not''or''pass''print',
        'raise''return''try''while''yield',
        'None''True''False',
    ]
 
    # Python operators
    operators = [
        '=',
        # Comparison
        '==''!=''<''<=''>''>=',
        # Arithmetic
        '\+''-''\*''/''//''\%''\*\*',
        # In-place
        '\+=''-=''\*=''/=''\%=',
        # Bitwise
        '\^''\|''\&''\~''>>''<<',
    ]
 
    # Python braces
    braces = [
        '\{''\}''\(''\)''\[''\]',
    ]
    def __init__(self, document):
        QSyntaxHighlighter.__init__(self, document)
 
        # Multi-line strings (expression, flag, style)
        # FIXME: The triple-quotes in these two lines will mess up the
        # syntax highlighting from this point onward
        self.tri_single = (QRegExp("'''"), 1, STYLES['string2'])
        self.tri_double = (QRegExp('"""'), 2, STYLES['string2'])
 
        rules = []
 
        # Keyword, operator, and brace rules
        rules += [(r'\b%s\b' % w, 0, STYLES['keyword'])
            for w in PythonHighlighter.keywords]
        rules += [(r'%s' % o, 0, STYLES['operator'])
            for o in PythonHighlighter.operators]
        rules += [(r'%s' % b, 0, STYLES['brace'])
            for b in PythonHighlighter.braces]
 
        # All other rules
        rules += [
            # 'self'
            (r'\bself\b'0, STYLES['self']),
 
            # Double-quoted string, possibly containing escape sequences
            (r'"[^"\\]*(\\.[^"\\]*)*"'0, STYLES['string']),
            # Single-quoted string, possibly containing escape sequences
            (r"'[^'\\]*(\\.[^'\\]*)*'"0, STYLES['string']),
 
            # 'def' followed by an identifier
            (r'\bdef\b\s*(\w+)'1, STYLES['defclass']),
            # 'class' followed by an identifier
            (r'\bclass\b\s*(\w+)'1, STYLES['defclass']),
 
            # From '#' until a newline
            (r'#[^\n]*'0, STYLES['comment']),
 
            # Numeric literals
            (r'\b[+-]?[0-9]+[lL]?\b'0, STYLES['numbers']),
            (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b'0, STYLES['numbers']),
            (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b'0, STYLES['numbers']),
        ]
 
        # Build a QRegExp for each pattern
        self.rules = [(QRegExp(pat), index, fmt)
            for (pat, index, fmt) in rules]
 
 
    def highlightBlock(self, text):
        """Apply syntax highlighting to the given block of text.
        """
        # Do other syntax formatting
        for expression, nth, format in self.rules:
            index = expression.indexIn(text, 0)
 
            while index >= 0:
                # We actually want the index of the nth match
                index = expression.pos(nth)
                length = len(expression.cap(nth))
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)
 
        self.setCurrentBlockState(0)
 
        # Do multi-line strings
        in_multiline = self.match_multiline(text, *self.tri_single)
        if not in_multiline:
            in_multiline = self.match_multiline(text, *self.tri_double)
 
 
    def match_multiline(self, text, delimiter, in_state, style):
        """Do highlighting of multi-line strings. ``delimiter`` should be a
        ``QRegExp`` for triple-single-quotes or triple-double-quotes, and
        ``in_state`` should be a unique integer to represent the corresponding
        state changes when inside those strings. Returns True if we're still
        inside a multi-line string when this function is finished.
        """
        # If inside triple-single quotes, start at 0
        if self.previousBlockState() == in_state:
            start = 0
            add = 0
        # Otherwise, look for the delimiter on this line
        else:
            start = delimiter.indexIn(text)
            # Move past this match
            add = delimiter.matchedLength()
 
        # As long as there's a delimiter match on this line...
        while start >= 0:
            # Look for the ending delimiter
            end = delimiter.indexIn(text, start + add)
            # Ending delimiter on this line?
            if end >= add:
                length = end - start + add + delimiter.matchedLength()
                self.setCurrentBlockState(0)
            # No; multi-line string
            else:
                self.setCurrentBlockState(in_state)
                length = len(text) - start + add
            # Apply formatting
            self.setFormat(start, length, style)
            # Look for the next match
            start = delimiter.indexIn(text, start + length)
 
        # Return True if still inside a multi-line string, False otherwise
        if self.currentBlockState() == in_state:
            return True
        else:
            return False
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함