-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.py
74 lines (52 loc) · 2.15 KB
/
highlight.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
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPlainTextEdit, QVBoxLayout
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QColor, QRegExpValidator, QSyntaxHighlighter, QTextCharFormat
class SyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, parnet):
super().__init__(parnet)
self._highlight_lines= {}
def highlight_line(self, line_num, fnt):
if isinstance(line_num, int) and line_num >= 0 and isinstance(fnt, QTextCharFormat):
self._highlight_lines[line_num]=fnt
block = self.document().findBlockByLineNumber(line_num)
self.rehighlightBlock(block)
def clear_highlight(self):
self._highlight_lines={}
self.rehighlight()
def highlightBlock(self, text):
blockNumber = self.currentBlock().blockNumber()
fnt= self._highlight_lines.get(blockNumber)
if fnt is not None:
self.setFormat(0, len(text), fnt)
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 100)
mainLayout = QVBoxLayout()
validator=QRegExpValidator(QRegExp(r'[0-9 ]+'))
self.lineEdit = QLineEdit()
self.lineEdit.setStyleSheet('font-size: 30px; height: 50px')
self.lineEdit.setValidator(validator)
self.lineEdit.textChanged.connect(self.onTextChanged)
mainLayout.addWidget(self.lineEdit)
self.textEditor =QPlainTextEdit()
self.textEditor.setStyleSheet('font-size: 30px; color:green')
mainLayout.addWidget(self.textEditor)
for i in range(1,21):
self.textEditor.appendPlainText('line {0}'.format(i))
self.highlighter = SyntaxHighlighter(self.textEditor.document())
self.setLayout(mainLayout)
def onTextChanged(self, text):
fnt=QTextCharFormat()
fnt.setBackground(QColor('yellow'))
self.highlighter.clear_highlight()
try:
lineNumber = int(text)-1
self.highlighter.highlight_line(lineNumber, fnt)
except ValueError:
pass
app=QApplication(sys.argv)
demo=AppDemo()
demo.show()
sys.exit(app.exec_())