forked from nuwainfo/tibetaneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.py
123 lines (96 loc) · 4.65 KB
/
highlighter.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
from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont
def getText(words, mode):
if mode not in ('plainText', 'taggedText'):
raise NotImplementedError()
result = ''
for w in words:
if mode == 'plainText':
w.plainTextModePosition = len(result)
result += w.content
else:
w.taggedModePosition = len(result)
result += w.content + '/' + w.partOfSpeech
return result
def getWord(position, words, mode):
if mode not in ('plainText', 'taggedText'):
raise NotImplementedError()
for w in words:
if mode == 'plainText':
start = w.plainTextModePosition
end = w.plainTextModePosition + len(w.content)
else:
start = w.taggedModePosition
end = w.taggedModePosition + len(w.content) + w.partOfSpeechLen
if start <= position < end:
return w
class Highlighter(QSyntaxHighlighter):
def __init__(self, parent=None, mainWindow=None):
super().__init__(parent)
self.mainWindow = mainWindow
self.textFormatManager = self.mainWindow.textFormatManager
self.highLightBlockOn = False
self.formatList = []
def highlightBlock(self, text):
if not self.highLightBlockOn:
return
wordFormatDict = self.textFormatManager.getWordFormatDict()
defaultFormat = self.textFormatManager.getDefaultFormat()
partOfSpeechFormat = self.textFormatManager.getPartOfSpeechFormat()
currentBlock = self.currentBlock()
for textFormat in self.mainWindow.textFormatManager.getFormats():
textFormat.counterDict[str(currentBlock.blockNumber())] = 0
self.mainWindow.textFormatManager.getDefaultFormat().counterDict[
str(currentBlock.blockNumber())] = 0
self.mainWindow.textFormatManager.getPartOfSpeechFormat().counterDict[
str(currentBlock.blockNumber())] = 0
start = currentBlock.position()
end = start + currentBlock.length()
wordsToHighlight = self.mainWindow.wordManager.getWords(start, end)
for word in wordsToHighlight:
textFormat = wordFormatDict.get(word.content, defaultFormat)
self.setFormat(word.start - start, word.length, textFormat)
textFormat.counterDict[str(currentBlock.blockNumber())] += 1
if word.tagIsOn:
self.setFormat(word.end - start, word.partOfSpeechLen,
partOfSpeechFormat)
for w in wordsToHighlight:
w.highlighted = {}
plainText = getText(wordsToHighlight, 'plainText')
taggedText = getText(wordsToHighlight, 'taggedText')
for textFormat in self.textFormatManager.getFormats():
if textFormat.type == 'level':
for pattern in textFormat.regexList:
expression = QRegExp(pattern)
index = expression.indexIn(plainText)
while index >= 0:
length = expression.matchedLength()
for i in range(length):
word = getWord(index + i, wordsToHighlight, 'plainText')
if word:
word.highlighted[index - word.plainTextModePosition + i] = textFormat
index = expression.indexIn(plainText, index + length)
else:
for pattern in textFormat.regexList:
expression = QRegExp(pattern)
index = expression.indexIn(taggedText)
while index >= 0:
length = expression.matchedLength()
for i in range(length):
word = getWord(index + i, wordsToHighlight, 'taggedText')
if word:
word.highlighted[index - word.taggedModePosition + i] = textFormat
index = expression.indexIn(taggedText, index + length)
for word in wordsToHighlight:
for index, textFormat in word.highlighted.items():
if self.mainWindow.modeManager.isTagMode():
if index >= word.length + word.partOfSpeechLen:
break
else:
if index >= word.length:
break
self.setFormat(word.start - start + index, 1, textFormat)
textFormat = word.needHighlighted()
if textFormat:
self.setFormat(word.start - start, word.length, textFormat)
self.mainWindow.counterWidget.refresh()