forked from Esukhia/dakje-desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
find.py
177 lines (117 loc) · 5.38 KB
/
find.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
# PYQT5 PyQt4’s QtGui module has been split into PyQt5’s QtGui, QtPrintSupport and QtWidgets modules
from PyQt5 import QtWidgets
from functools import wraps
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import Qt
import re
class FindDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.parent = parent
self.lastStart = 0
self.initUI()
def initUI(self):
# Button to search the document for something
findButton = QtWidgets.QPushButton("Find", self)
findButton.clicked.connect(self.find)
# Button to replace the last finding
replaceButton = QtWidgets.QPushButton("Replace", self)
replaceButton.clicked.connect(self.replace)
# Button to remove all findings
allButton = QtWidgets.QPushButton("Replace all", self)
allButton.clicked.connect(self.replaceAll)
# Normal mode - radio button
self.normalRadio = QtWidgets.QRadioButton("Normal", self)
# Regular Expression Mode - radio button
regexRadio = QtWidgets.QRadioButton("RegEx", self)
# The field into which to type the query
self.findField = QtWidgets.QPlainTextEdit(self)
self.findField.resize(250, 50)
# The field into which to type the text to replace the
# queried text
self.replaceField = QtWidgets.QPlainTextEdit(self)
self.replaceField.resize(250, 50)
layout = QtWidgets.QGridLayout()
layout.addWidget(self.findField, 1, 0, 1, 4)
layout.addWidget(self.normalRadio, 2, 2)
layout.addWidget(regexRadio, 2, 3)
layout.addWidget(findButton, 2, 0, 1, 2)
layout.addWidget(self.replaceField, 3, 0, 1, 4)
layout.addWidget(replaceButton, 4, 0, 1, 2)
layout.addWidget(allButton, 4, 2, 1, 2)
self.setGeometry(300, 300, 360, 250)
self.setWindowTitle("Find and Replace")
self.setLayout(layout)
# By default the normal mode is activated
self.normalRadio.setChecked(True)
def find(self):
self.parent.textEdit.cursorPositionChanged.disconnect()
# Grab the parent's text
text = self.parent.textEdit.toPlainText()
# And the text to find
query = self.findField.toPlainText()
if self.normalRadio.isChecked():
# Use normal string search to find the query from the
# last starting position
self.lastStart = text.find(query, self.lastStart + 1)
# If the find() method didn't return -1 (not found)
if self.lastStart >= 0:
end = self.lastStart + len(query)
self.moveCursor(self.lastStart, end)
else:
# Make the next search start from the begining again
self.lastStart = 0
self.parent.textEdit.moveCursor(QtGui.QTextCursor.End)
else:
# Compile the pattern
pattern = re.compile(query)
# The actual search
match = pattern.search(text, self.lastStart + 1)
if match:
self.lastStart = match.start()
self.moveCursor(self.lastStart, match.end())
else:
self.lastStart = 0
# We set the cursor to the end if the search was unsuccessful
self.parent.textEdit.moveCursor(QtGui.QTextCursor.End)
self.parent.textEdit.cursorPositionChanged.connect(
self.parent.eventHandler.cursorPositionChanged)
def replace(self, highlight=True):
self.parent.textEdit.cursorPositionChanged.disconnect()
self.parent.textEdit.textChanged.disconnect()
# Grab the text cursor
cursor = self.parent.textEdit.textCursor()
# Security
if cursor.hasSelection():
for word in self.parent.words:
if cursor.selectionStart() == word.partOfSpeechStart + 1 and \
cursor.selectionEnd() == word.partOfSpeechEnd + 1:
word.partOfSpeech = self.replaceField.toPlainText()
cursor.insertText(self.replaceField.toPlainText())
# And set the new cursor
self.parent.textEdit.setTextCursor(cursor)
self.parent.textEdit.cursorPositionChanged.connect(
self.parent.cursorIsChanged)
self.parent.textEdit.textChanged.connect(
self.parent.textIsChanged)
if highlight:
self.parent.highlight()
def replaceAll(self):
self.lastStart = 0
self.find()
# Replace and find until self.lastStart is 0 again
while self.lastStart:
self.replace(highlight=False)
self.find()
self.parent.highlight()
def moveCursor(self, start, end):
# We retrieve the QTextCursor object from the parent's QPlainTextEdit
cursor = self.parent.textEdit.textCursor()
# Then we set the position to the beginning of the last match
cursor.setPosition(start)
# Next we move the Cursor by over the match and pass the KeepAnchor parameter
# which will make the cursor select the the match's text
cursor.movePosition(QtGui.QTextCursor.Right,
QtGui.QTextCursor.KeepAnchor, end - start)
# And finally we set this new cursor as the parent's
self.parent.textEdit.setTextCursor(cursor)