9
9
QMessageBox ,
10
10
QHBoxLayout ,
11
11
)
12
- from PySide6 .QtGui import QAction
12
+ from PySide6 .QtGui import QAction , QTextCursor
13
13
from PySide6 .QtCore import QTimer , Qt
14
14
from charset_normalizer import from_bytes
15
15
from translation_worker import TranslationWorker
@@ -27,6 +27,10 @@ def __init__(self, llm_url):
27
27
self .translation_output = None
28
28
self .text_edit = None
29
29
self .worker = None
30
+ self .auto_replace_action = None
31
+ self .original_selection_start = 0
32
+ self .original_selection_end = 0
33
+
30
34
self .setWindowTitle ("TextAutoTranslate" )
31
35
self .resize (1000 , 600 )
32
36
self .languages = ["English" , "Spanish" , "French" , "German" , "Chinese" ]
@@ -36,7 +40,6 @@ def __init__(self, llm_url):
36
40
self .selection_timer .timeout .connect (self .process_selection )
37
41
self .translator_combo = None
38
42
39
- # Create central widget and main layout
40
43
central_widget = QWidget ()
41
44
self .setCentralWidget (central_widget )
42
45
self .main_layout = QGridLayout (central_widget )
@@ -77,6 +80,13 @@ def create_menus(self):
77
80
redo_action .setShortcut ("Ctrl+Y" )
78
81
edit_menu .addAction (redo_action )
79
82
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
+
80
90
# Help menu
81
91
help_menu = menu_bar .addMenu ("&Help" )
82
92
about_action = QAction ("&About" , self )
@@ -144,7 +154,6 @@ def init_ui(self):
144
154
self .main_layout .setRowStretch (2 , 1 )
145
155
146
156
def update_window_title (self ):
147
- """Update window title with current file path if available"""
148
157
if self .current_file_path :
149
158
self .setWindowTitle (f"TextAutoTranslate - { self .current_file_path } " )
150
159
else :
@@ -184,7 +193,6 @@ def open_file(self):
184
193
185
194
def save_file (self ):
186
195
if self .current_file_path :
187
- # Overwrite existing file
188
196
content = self .text_edit .toPlainText ()
189
197
try :
190
198
reply = QMessageBox .question (
@@ -201,7 +209,6 @@ def save_file(self):
201
209
except Exception as e :
202
210
QMessageBox .critical (self , "Error" , f"Failed to save file: { str (e )} " )
203
211
else :
204
- # Save as new file
205
212
file_path , _ = QFileDialog .getSaveFileName (
206
213
self , "Save File" , "" , "All Files (*)"
207
214
)
@@ -228,11 +235,28 @@ def process_selection(self):
228
235
229
236
if selected_text and selected_text != self .last_selection :
230
237
self .last_selection = selected_text
238
+ self .original_selection_start = cursor .selectionStart ()
239
+ self .original_selection_end = cursor .selectionEnd ()
231
240
self .start_translation_thread (selected_text )
232
241
233
242
def update_translation_output (self , translated_text ):
234
243
self .translation_output .setPlainText (translated_text )
235
244
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
+
236
260
def start_translation_thread (self , text ):
237
261
self .translation_output .setPlainText ("Translating..." )
238
262
translator = self .translator_combo .currentText ()
0 commit comments