Skip to content

Commit 0d6d455

Browse files
authored
Merge pull request #13 from Ati1707/main
add option to replace immediately after translating
2 parents 54b0ffd + 0b0b83d commit 0d6d455

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

app.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
QMessageBox,
1010
QHBoxLayout,
1111
)
12-
from PySide6.QtGui import QAction
12+
from PySide6.QtGui import QAction, QTextCursor
1313
from PySide6.QtCore import QTimer, Qt
1414
from charset_normalizer import from_bytes
1515
from translation_worker import TranslationWorker
@@ -27,6 +27,10 @@ def __init__(self, llm_url):
2727
self.translation_output = None
2828
self.text_edit = None
2929
self.worker = None
30+
self.auto_replace_action = None
31+
self.original_selection_start = 0
32+
self.original_selection_end = 0
33+
3034
self.setWindowTitle("TextAutoTranslate")
3135
self.resize(1000, 600)
3236
self.languages = ["English", "Spanish", "French", "German", "Chinese"]
@@ -36,7 +40,6 @@ def __init__(self, llm_url):
3640
self.selection_timer.timeout.connect(self.process_selection)
3741
self.translator_combo = None
3842

39-
# Create central widget and main layout
4043
central_widget = QWidget()
4144
self.setCentralWidget(central_widget)
4245
self.main_layout = QGridLayout(central_widget)
@@ -77,6 +80,13 @@ def create_menus(self):
7780
redo_action.setShortcut("Ctrl+Y")
7881
edit_menu.addAction(redo_action)
7982

83+
# Options menu
84+
options_menu = menu_bar.addMenu("&Options")
85+
self.auto_replace_action = QAction("Auto-replace selection", self)
86+
self.auto_replace_action.setCheckable(True)
87+
self.auto_replace_action.setChecked(False)
88+
options_menu.addAction(self.auto_replace_action)
89+
8090
# Help menu
8191
help_menu = menu_bar.addMenu("&Help")
8292
about_action = QAction("&About", self)
@@ -144,7 +154,6 @@ def init_ui(self):
144154
self.main_layout.setRowStretch(2, 1)
145155

146156
def update_window_title(self):
147-
"""Update window title with current file path if available"""
148157
if self.current_file_path:
149158
self.setWindowTitle(f"TextAutoTranslate - {self.current_file_path}")
150159
else:
@@ -184,7 +193,6 @@ def open_file(self):
184193

185194
def save_file(self):
186195
if self.current_file_path:
187-
# Overwrite existing file
188196
content = self.text_edit.toPlainText()
189197
try:
190198
reply = QMessageBox.question(
@@ -201,7 +209,6 @@ def save_file(self):
201209
except Exception as e:
202210
QMessageBox.critical(self, "Error", f"Failed to save file: {str(e)}")
203211
else:
204-
# Save as new file
205212
file_path, _ = QFileDialog.getSaveFileName(
206213
self, "Save File", "", "All Files (*)"
207214
)
@@ -228,11 +235,28 @@ def process_selection(self):
228235

229236
if selected_text and selected_text != self.last_selection:
230237
self.last_selection = selected_text
238+
self.original_selection_start = cursor.selectionStart()
239+
self.original_selection_end = cursor.selectionEnd()
231240
self.start_translation_thread(selected_text)
232241

233242
def update_translation_output(self, translated_text):
234243
self.translation_output.setPlainText(translated_text)
235244

245+
if self.auto_replace_action.isChecked():
246+
new_cursor = self.text_edit.textCursor()
247+
new_cursor.beginEditBlock()
248+
249+
new_cursor.setPosition(self.original_selection_start)
250+
new_cursor.setPosition(
251+
self.original_selection_end, QTextCursor.MoveMode.KeepAnchor
252+
)
253+
254+
new_cursor.insertText(translated_text)
255+
new_cursor.endEditBlock()
256+
257+
new_cursor.setPosition(self.original_selection_start + len(translated_text))
258+
self.text_edit.setTextCursor(new_cursor)
259+
236260
def start_translation_thread(self, text):
237261
self.translation_output.setPlainText("Translating...")
238262
translator = self.translator_combo.currentText()

0 commit comments

Comments
 (0)