forked from Esukhia/dakje-desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEventHandler.py
109 lines (87 loc) · 3.55 KB
/
EventHandler.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
from PyQt5.QtWidgets import QPlainTextEdit, QMessageBox, QScrollBar, QComboBox
class EventHandler:
def __init__(self, parent):
self.parent = parent
self.changedWithoutSaving = False
self.curPos = 0
self.prePos = 0
self.preStart = None
self.preEnd = None
self.curStart = None
self.curEnd = None
def cursorPositionChanged(self):
textEdit = self.parent.textEdit
# 更新現在位置
cursor = textEdit.textCursor()
self.prePos = self.curPos
self.curPos = cursor.position()
self.preStart = self.curStart
self.preEnd = self.curEnd
if cursor.hasSelection():
self.curStart = cursor.selectionStart()
self.curEnd = cursor.selectionEnd()
else:
self.curStart = None
self.curEnd = None
def textChanged(self):
textEdit = self.parent.textEdit
wordManager = self.parent.wordManager
text = self.parent.textEdit.toPlainText()
if self.preEnd:
shift = self.curPos - self.preEnd
else:
shift = self.curPos - self.prePos
if not textEdit.toPlainText() or (
self.preStart == 0
and self.preEnd == len(textEdit.toPlainText())):
wordManager.setWords([])
# plain text mode:
if not self.parent.modeManager.isSpaceMode():
if self.preStart is not None:
wordManager.removeWords(self.preStart, self.preEnd)
for word in wordManager.getWords(start=self.prePos):
word.start += shift
else:
wordManager.removeWord(self.prePos)
for word in wordManager.getWords(start=self.prePos):
word.start += shift
# space mode:
# 空白模式時,因為不能重segment,預設只是改空白而已
else:
for word in wordManager.getWords(start=self.prePos):
word.start += shift
wordManager.reSegmentWord(self.curPos, self.prePos, text)
self.parent.highlightViewpoint()
self.changedWithoutSaving = True
self.checkTitle()
def checkTitle(self):
if self.changedWithoutSaving:
if not self.parent.windowTitle().endswith('*'):
self.parent.setWindowTitle(self.parent.windowTitle() + '*')
else:
if self.parent.windowTitle().endswith('*'):
self.parent.setWindowTitle(
self.parent.windowTitle()[:-1])
def mousePressEvent(self, event):
QPlainTextEdit.mousePressEvent(self.parent.textEdit, event)
if self.parent.modeManager.isTagMode():
self.parent.selectedWord = self.parent.wordManager.getPartOfSpeechWord(
self.parent.textEdit.textCursor().position())
if self.parent.selectedWord:
self.parent.changeTag(event.pos())
def wheelEvent(self, event):
QPlainTextEdit.wheelEvent(self.parent.textEdit, event)
self.parent.highlightViewpoint()
def checkSaving(self):
if self.changedWithoutSaving:
reply = QMessageBox.question(
self.parent, 'Save', 'Save the file?',
QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
self.parent.saveFile()
else:
pass
def sliderChange(self, event):
QScrollBar.mouseReleaseEvent(
self.parent.textEdit.verticalScrollBar(), event)
self.parent.highlightViewpoint()