forked from Esukhia/dakje-desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Mode.py
69 lines (55 loc) · 2.14 KB
/
Mode.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
class ModeManager:
def __init__(self, parent):
self.parent = parent
self._spaceModeOn = False
self._tagModeOn = False
def isTagMode(self):
return self._tagModeOn
def isSpaceMode(self):
return self._spaceModeOn
def checkIcon(self):
if self._spaceModeOn or self._tagModeOn:
self.parent.segmentAction.setEnabled(False)
else:
self.parent.segmentAction.setEnabled(True)
def setText(self, keepCursor=False):
if keepCursor:
cursor = self.parent.textEdit.textCursor()
position = cursor.position()
selectedWord = self.parent.wordManager.getWordByModeEnd(position)
distance = position - selectedWord.start
text = []
end = 0
for word in self.parent.wordManager.getWords():
start = end
end = start + len(word.content)
text.append(word.content)
word.start = start
if self._tagModeOn and word.content != '\n':
text.append('/')
text.append(word.partOfSpeech)
word.tagIsOn = True
end += word.partOfSpeechLen
else:
word.tagIsOn = False
if self._spaceModeOn:
text.append(' ')
end += 1
self.parent.textEdit.textChanged.disconnect()
self.parent.textEdit.setPlainText(''.join(text))
self.parent.textEdit.textChanged.connect(
self.parent.eventHandler.textChanged)
if keepCursor:
cursor.setPosition(selectedWord.start + distance)
self.parent.textEdit.setTextCursor(cursor)
def switchDisplayMode(self, mode):
if not self._spaceModeOn and not self._tagModeOn:
self.parent.segment(keepCursor=True)
if mode == 'Spaces':
self._spaceModeOn = not self._spaceModeOn
elif mode == 'Tags':
self._tagModeOn = not self._tagModeOn
self.setText(keepCursor=True)
self.parent.highlightViewpoint(
currentBlock=self.parent.textEdit.document().firstBlock())
self.checkIcon()