Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 48c18a8

Browse files
author
Raven
committed
Restore old FindReplaceDialog class from experimental version
1 parent 719887d commit 48c18a8

File tree

1 file changed

+51
-73
lines changed

1 file changed

+51
-73
lines changed

scratchpad.py

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -120,79 +120,57 @@ def get_preferred_font():
120120

121121

122122
class FindReplaceDialog(QDialog):
123-
def __init__(self, editor, parent=None):
124-
super().__init__(parent)
125-
self.editor = editor
126-
self.setWindowTitle("Find and Replace")
127-
icon = load_icon('scratchpad.png')
128-
self.setWindowIcon(icon if isinstance(icon, QIcon) else QIcon())
129-
self.layout = QVBoxLayout(self)
130-
self.find_label = QLabel("Find:")
131-
self.find_input = QLineEdit(self)
132-
self.layout.addWidget(self.find_label)
133-
self.layout.addWidget(self.find_input)
134-
self.replace_label = QLabel("Replace with:")
135-
self.replace_input = QLineEdit(self)
136-
self.layout.addWidget(self.replace_label)
137-
self.layout.addWidget(self.replace_input)
138-
self.button_layout = QHBoxLayout()
139-
self.find_button = QPushButton("Find Next", self)
140-
self.replace_button = QPushButton("Replace", self)
141-
self.replace_all_button = QPushButton("Replace All", self)
142-
self.button_layout.addWidget(self.find_button)
143-
self.button_layout.addWidget(self.replace_button)
144-
self.button_layout.addWidget(self.replace_all_button)
145-
self.layout.addLayout(self.button_layout)
146-
self.find_button.clicked.connect(self.find_next)
147-
self.replace_button.clicked.connect(self.replace)
148-
self.replace_all_button.clicked.connect(self.replace_all)
149-
self.setLayout(self.layout)
150-
151-
def find_next(self):
152-
ed = self.editor
153-
text = self.find_input.text().strip()
154-
if not text:
155-
QMessageBox.warning(self, "Empty Search", "Please enter text to find.")
156-
return
157-
found = ed.findFirst(text, False, False, False, True, True)
158-
if not found:
159-
QMessageBox.information(self, "Not Found", "No more occurrences found.")
160-
161-
def replace(self):
162-
ed = self.editor
163-
find_text = self.find_input.text()
164-
replace_text = self.replace_input.text()
165-
if not find_text:
166-
return
167-
if ed.hasSelectedText() and ed.selectedText() == find_text:
168-
ed.replaceSelectedText(replace_text)
169-
ed.findFirst(find_text, False, False, False, True, True)
170-
return
171-
if not ed.findFirst(find_text, False, False, False, True, True):
172-
return
173-
ed.replaceSelectedText(replace_text)
174-
ed.findFirst(find_text, False, False, False, True, True)
175-
176-
def replace_all(self):
177-
ed = self.editor
178-
find_text = self.find_input.text()
179-
replace_text = self.replace_input.text()
180-
if not find_text:
181-
return
182-
ed.beginUndoAction()
183-
try:
184-
line, index = 0, 0
185-
count_guard = 0
186-
max_iters = 1_000_000
187-
while count_guard < max_iters:
188-
found = ed.findFirst(find_text, False, False, False, False, True, line, index, True, False)
189-
if not found:
190-
break
191-
ed.replaceSelectedText(replace_text)
192-
line, index = ed.getCursorPosition()
193-
count_guard += 1
194-
finally:
195-
ed.endUndoAction()
123+
def __init__(self, text_edit):
124+
super().__init__()
125+
self.text_edit = text_edit
126+
self.setWindowTitle("Find and Replace")
127+
self.setWindowIcon(load_icon('scratchpad.png'))
128+
self.layout = QVBoxLayout(self)
129+
self.find_label = QLabel("Find:")
130+
self.find_input = QLineEdit(self)
131+
self.layout.addWidget(self.find_label)
132+
self.layout.addWidget(self.find_input)
133+
self.replace_label = QLabel("Replace with:")
134+
self.replace_input = QLineEdit(self)
135+
self.layout.addWidget(self.replace_label)
136+
self.layout.addWidget(self.replace_input)
137+
self.button_layout = QHBoxLayout()
138+
self.find_button = QPushButton("Find Next", self)
139+
self.replace_button = QPushButton("Replace", self)
140+
self.replace_all_button = QPushButton("Replace All", self)
141+
self.button_layout.addWidget(self.find_button)
142+
self.button_layout.addWidget(self.replace_button)
143+
self.button_layout.addWidget(self.replace_all_button)
144+
self.layout.addLayout(self.button_layout)
145+
self.find_button.clicked.connect(self.find_next)
146+
self.replace_button.clicked.connect(self.replace)
147+
self.replace_all_button.clicked.connect(self.replace_all)
148+
self.setLayout(self.layout)
149+
self.current_index = 0
150+
151+
def find_next(self):
152+
text_to_find = self.find_input.text().strip()
153+
if text_to_find:
154+
options = QTextDocument.FindFlags()
155+
found = self.text_edit.find(text_to_find, options)
156+
if not found:
157+
QMessageBox.information(self, "Not Found", "No more occurrences found.")
158+
else:
159+
QMessageBox.warning(self, "Empty Search", "Please enter text to find.")
160+
161+
def replace(self):
162+
text_to_find = self.find_input.text()
163+
text_to_replace = self.replace_input.text()
164+
if text_to_find and text_to_replace:
165+
content = self.text_edit.toPlainText()
166+
self.text_edit.setPlainText(content.replace(text_to_find, text_to_replace, 1))
167+
168+
def replace_all(self):
169+
text_to_find = self.find_input.text()
170+
text_to_replace = self.replace_input.text()
171+
if text_to_find and text_to_replace:
172+
content = self.text_edit.toPlainText()
173+
self.text_edit.setPlainText(content.replace(text_to_find, text_to_replace))
196174

197175

198176

0 commit comments

Comments
 (0)