From 3e498ded19da76f3ea67081c79b3e336440f9669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Tue, 21 May 2024 01:08:23 +0800 Subject: [PATCH] auto --- LunaTranslator/LunaTranslator/gui/showword.py | 36 +++++++++++++---- LunaTranslator/LunaTranslator/myutils/hwnd.py | 39 ++++++++++++------- .../files/defaultconfig/config.json | 7 ++-- LunaTranslator/files/lang/ar.json | 4 +- LunaTranslator/files/lang/cht.json | 4 +- LunaTranslator/files/lang/en.json | 4 +- LunaTranslator/files/lang/es.json | 4 +- LunaTranslator/files/lang/fr.json | 4 +- LunaTranslator/files/lang/it.json | 4 +- LunaTranslator/files/lang/ja.json | 4 +- LunaTranslator/files/lang/ko.json | 4 +- LunaTranslator/files/lang/pl.json | 4 +- LunaTranslator/files/lang/ru.json | 4 +- LunaTranslator/files/lang/th.json | 4 +- LunaTranslator/files/lang/tr.json | 4 +- LunaTranslator/files/lang/uk.json | 4 +- LunaTranslator/files/lang/vi.json | 4 +- LunaTranslator/files/lang/zh.json | 6 ++- 18 files changed, 103 insertions(+), 41 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/showword.py b/LunaTranslator/LunaTranslator/gui/showword.py index 4d785fef..c3c775af 100644 --- a/LunaTranslator/LunaTranslator/gui/showword.py +++ b/LunaTranslator/LunaTranslator/gui/showword.py @@ -14,6 +14,8 @@ from PyQt5.QtWidgets import ( QTabBar, QLabel, ) +from myutils.hwnd import grabwindow + from urllib.parse import quote from PyQt5.QtGui import QPixmap, QImage from traceback import print_exc @@ -107,7 +109,21 @@ class statusbutton(QPushButton): self.statuschanged2.emit((self.idx) % len(self.colors)) self.seticon() - +class autoremovelineedit(QLineEdit): + def check(self): + last=self.text() + if os.path.exists(last) and os.path.isfile(last): + norm_dir1 = os.path.normpath(last) + norm_dir2 = os.path.normpath(os.path.abspath('./cache')) + print(norm_dir1,norm_dir2) + if norm_dir1.startswith(norm_dir2): + os.remove(last) + def setText(self,s): + self.check() + super().setText(s) + def clear(self): + self.check() + class AnkiWindow(QWidget): __ocrsettext = pyqtSignal(str) refreshhtml = pyqtSignal() @@ -371,6 +387,10 @@ class AnkiWindow(QWidget): _TR("自动TTS"), getsimpleswitch(globalconfig["ankiconnect"], "autoruntts"), ) + layout.addRow( + _TR("自动截图"), + getsimpleswitch(globalconfig["ankiconnect"], "autocrop"), + ) layout.addRow( _TR("录音时模拟按键_1"), @@ -422,11 +442,11 @@ class AnkiWindow(QWidget): cropbutton = QPushButton(qtawesome.icon("fa.crop"), "") cropbutton.clicked.connect(self.crop) - self.audiopath = QLineEdit() + self.audiopath = autoremovelineedit() self.audiopath.setReadOnly(True) - self.audiopath_sentence = QLineEdit() + self.audiopath_sentence = autoremovelineedit() self.audiopath_sentence.setReadOnly(True) - self.editpath = QLineEdit() + self.editpath = autoremovelineedit() self.editpath.setReadOnly(True) self.viewimagelabel = QLabel() self.editpath.textChanged.connect(self.wrappedpixmap) @@ -563,7 +583,6 @@ class AnkiWindow(QWidget): self.editpath.clear() self.audiopath.clear() self.audiopath_sentence.clear() - def errorwrap(self): try: self.addanki() @@ -813,12 +832,15 @@ class searchwordW(closeashidewindow): sentence = self.searchtext.text() + sentence self.searchtext.setText(sentence) - self.ankiwindow.example.setPlainText(gobject.baseobject.currenttext) self.search(sentence) + + self.ankiwindow.example.setPlainText(gobject.baseobject.currenttext) if globalconfig["ankiconnect"]["autoruntts"]: self.ankiwindow.langdu() self.ankiwindow.langdu2() - + + if globalconfig["ankiconnect"]["autocrop"]: + grabwindow(self.ankiwindow.editpath.setText) def search(self, sentence): sentence = sentence.strip() if sentence == "": diff --git a/LunaTranslator/LunaTranslator/myutils/hwnd.py b/LunaTranslator/LunaTranslator/myutils/hwnd.py index bca61ff7..57655145 100644 --- a/LunaTranslator/LunaTranslator/myutils/hwnd.py +++ b/LunaTranslator/LunaTranslator/myutils/hwnd.py @@ -9,7 +9,7 @@ from myutils.wrapper import threader @threader -def grabwindow(): +def grabwindow(callback=None): fnamebase = "./cache/screenshot/{}".format(0) try: @@ -33,6 +33,8 @@ def grabwindow(): @threader def _(): winrtutils._winrt_capture_window(fname + "_winrt_magpie.png", hwnd) + if callback and os.path.exists(fname + "_winrt_magpie.png"): + callback(os.path.abspath(fname + "_winrt_magpie.png")) _() hwnd = windows.FindWindow("LosslessScaling", None) @@ -41,32 +43,39 @@ def grabwindow(): @threader def _(): winrtutils._winrt_capture_window(fname + "_winrt_lossless.png", hwnd) + if callback and os.path.exists(fname + "_winrt_lossless.png"): + callback(os.path.abspath(fname + "_winrt_lossless.png")) _() - hwnd = windows.GetForegroundWindow() try: - if hwnd == int(gobject.baseobject.translation_ui.winId()): - hwnd = gobject.baseobject.textsource.hwnd + hwnd = gobject.baseobject.textsource.hwnd except: - pass + hwnd = windows.GetForegroundWindow() - @threader - def _(): - winrtutils._winrt_capture_window(fname + "_winrt.png", hwnd) - - _() _ = windows.GetClientRect(hwnd) rate = dynamic_rate(hwnd, _) w, h = int(_[2] / rate), int(_[3] / rate) - print(_) - print(h, w, rate) p = QApplication.primaryScreen().grabWindow(hwnd, 0, 0, w, h) p = p.toImage().copy(0, 0, w, h) if not p.allGray(): p.save(fname + "_gdi.png") - gobject.baseobject.translation_ui.displaystatus.emit( - "saved to " + fname, "red", True, True - ) + if callback and os.path.exists(fname + "_gdi.png"): + callback(os.path.abspath(fname + "_gdi.png")) + + if not callback: + + gobject.baseobject.translation_ui.displaystatus.emit( + "saved to " + fname, "red", True, True + ) + + @threader + def _(): + winrtutils._winrt_capture_window(fname + "_winrt.png", hwnd) + if callback and os.path.exists(fname + "_winrt.png"): + callback(os.path.abspath(fname + "_winrt.png")) + + if p.allGray() or (not callback): + _() def dynamic_rate(hwnd, rect): diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index dd8edba4..bb95318e 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -193,7 +193,8 @@ "use":false, "keycode":120 } - } + }, + "autocrop":false }, "ankiwindow": [ 100, @@ -363,7 +364,7 @@ }, "grabwindow": { "use": false, - "tip": "前景窗口截屏", + "tip": "窗口截图", "icon": "fa.camera" }, "resize": { @@ -608,7 +609,7 @@ }, "_21": { "use": false, - "name": "前景窗口截屏", + "name": "窗口截图", "keystring": "" }, "_22": { diff --git a/LunaTranslator/files/lang/ar.json b/LunaTranslator/files/lang/ar.json index bb244180..a3e9d326 100644 --- a/LunaTranslator/files/lang/ar.json +++ b/LunaTranslator/files/lang/ar.json @@ -791,5 +791,7 @@ "编码": "ترميز", "自动TTS": "التلقائي تحويل النص إلى كلام", "使用webview显示": "عرض باستخدام WebView", - "录音时模拟按键": "التناظرية مفتاح التسجيل" + "录音时模拟按键": "التناظرية مفتاح التسجيل", + "窗口截图": "نافذة لقطة", + "自动截图": "لقطة التلقائي" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/cht.json b/LunaTranslator/files/lang/cht.json index c9484b76..b5b06718 100644 --- a/LunaTranslator/files/lang/cht.json +++ b/LunaTranslator/files/lang/cht.json @@ -791,5 +791,7 @@ "编码": "編碼", "自动TTS": "自動TTS", "使用webview显示": "使用webview顯示", - "录音时模拟按键": "錄音時類比按鍵" + "录音时模拟按键": "錄音時類比按鍵", + "窗口截图": "視窗截圖", + "自动截图": "自動截圖" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/en.json b/LunaTranslator/files/lang/en.json index 3a8adfe8..b635d391 100644 --- a/LunaTranslator/files/lang/en.json +++ b/LunaTranslator/files/lang/en.json @@ -791,5 +791,7 @@ "编码": "coding", "自动TTS": "Automatic TTS", "使用webview显示": "Display using webview", - "录音时模拟按键": "Simulate buttons during recording" + "录音时模拟按键": "Simulate buttons during recording", + "窗口截图": "Window screenshot", + "自动截图": "Automatic screenshot" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/es.json b/LunaTranslator/files/lang/es.json index 0073cb55..71698619 100644 --- a/LunaTranslator/files/lang/es.json +++ b/LunaTranslator/files/lang/es.json @@ -791,5 +791,7 @@ "编码": "Codificación", "自动TTS": "TTS automático", "使用webview显示": "Mostrar con Webview", - "录音时模拟按键": "Teclas de simulación durante la grabación" + "录音时模拟按键": "Teclas de simulación durante la grabación", + "窗口截图": "Captura de pantalla de la ventana", + "自动截图": "Captura de pantalla automática" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/fr.json b/LunaTranslator/files/lang/fr.json index 18c2c276..d3eef893 100644 --- a/LunaTranslator/files/lang/fr.json +++ b/LunaTranslator/files/lang/fr.json @@ -791,5 +791,7 @@ "编码": "Codage", "自动TTS": "Tts automatique", "使用webview显示": "Afficher avec webview", - "录音时模拟按键": "Simulation des touches lors de l'enregistrement" + "录音时模拟按键": "Simulation des touches lors de l'enregistrement", + "窗口截图": "Capture d'écran de la fenêtre", + "自动截图": "Capture d'écran automatique" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/it.json b/LunaTranslator/files/lang/it.json index 5849361f..1a7b2016 100644 --- a/LunaTranslator/files/lang/it.json +++ b/LunaTranslator/files/lang/it.json @@ -791,5 +791,7 @@ "编码": "codifica", "自动TTS": "TTS automatico", "使用webview显示": "Visualizzazione tramite webview", - "录音时模拟按键": "Simula pulsanti durante la registrazione" + "录音时模拟按键": "Simula pulsanti durante la registrazione", + "窗口截图": "Schermata finestra", + "自动截图": "Schermata automatica" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ja.json b/LunaTranslator/files/lang/ja.json index 29994974..f76cdbb4 100644 --- a/LunaTranslator/files/lang/ja.json +++ b/LunaTranslator/files/lang/ja.json @@ -791,5 +791,7 @@ "编码": "エンコード", "自动TTS": "自動TTS", "使用webview显示": "webview表示の使用", - "录音时模拟按键": "録音時アナログボタン" + "录音时模拟按键": "録音時アナログボタン", + "窗口截图": "ウィンドウのスクリーンショット", + "自动截图": "自動スクリーンショット" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ko.json b/LunaTranslator/files/lang/ko.json index e032080e..3d8eff14 100644 --- a/LunaTranslator/files/lang/ko.json +++ b/LunaTranslator/files/lang/ko.json @@ -791,5 +791,7 @@ "编码": "인코딩", "自动TTS": "자동 TTS", "使用webview显示": "웹뷰를 사용하여 표시", - "录音时模拟按键": "녹음 시 시뮬레이션 키" + "录音时模拟按键": "녹음 시 시뮬레이션 키", + "窗口截图": "창 캡처", + "自动截图": "자동 캡처" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/pl.json b/LunaTranslator/files/lang/pl.json index 44315c1c..6ab369bb 100644 --- a/LunaTranslator/files/lang/pl.json +++ b/LunaTranslator/files/lang/pl.json @@ -791,5 +791,7 @@ "编码": "kodowanie", "自动TTS": "Automatyczny TTS", "使用webview显示": "Wyświetlanie przy użyciu widoku internetowego", - "录音时模拟按键": "Symulowanie przycisków podczas nagrywania" + "录音时模拟按键": "Symulowanie przycisków podczas nagrywania", + "窗口截图": "Zrzut ekranu okna", + "自动截图": "Automatyczny zrzut ekranu" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ru.json b/LunaTranslator/files/lang/ru.json index 2270681c..ec29ee78 100644 --- a/LunaTranslator/files/lang/ru.json +++ b/LunaTranslator/files/lang/ru.json @@ -791,5 +791,7 @@ "编码": "Код", "自动TTS": "Автоматический TTS", "使用webview显示": "Использовать webview", - "录音时模拟按键": "Клавиша моделирования при записи" + "录音时模拟按键": "Клавиша моделирования при записи", + "窗口截图": "Снимок окна", + "自动截图": "Автоматический снимок экрана" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/th.json b/LunaTranslator/files/lang/th.json index c2a61db1..348c30a5 100644 --- a/LunaTranslator/files/lang/th.json +++ b/LunaTranslator/files/lang/th.json @@ -791,5 +791,7 @@ "编码": "การเข้ารหัส", "自动TTS": "TTS อัตโนมัติ", "使用webview显示": "ใช้ webview เพื่อแสดงผล", - "录音时模拟按键": "การจำลองปุ่มกดเมื่อบันทึก" + "录音时模拟按键": "การจำลองปุ่มกดเมื่อบันทึก", + "窗口截图": "ภาพหน้าจอของหน้าต่าง", + "自动截图": "ภาพหน้าจออัตโนมัติ" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/tr.json b/LunaTranslator/files/lang/tr.json index ad02321d..272f0d3c 100644 --- a/LunaTranslator/files/lang/tr.json +++ b/LunaTranslator/files/lang/tr.json @@ -791,5 +791,7 @@ "编码": "coding", "自动TTS": "Otomatik TTS", "使用webview显示": "Web görüntüsünü kullanarak göster", - "录音时模拟按键": "Kaydedirken düğmeleri simüle edin" + "录音时模拟按键": "Kaydedirken düğmeleri simüle edin", + "窗口截图": "Window screenshot", + "自动截图": "Otomatik ekran fotoğrafı" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/uk.json b/LunaTranslator/files/lang/uk.json index fb087a83..b154d053 100644 --- a/LunaTranslator/files/lang/uk.json +++ b/LunaTranslator/files/lang/uk.json @@ -791,5 +791,7 @@ "编码": "кодування", "自动TTS": "Автоматичний TTS", "使用webview显示": "Показувати за допомогою веб- перегляду", - "录音时模拟按键": "Імітувати кнопки під час запису" + "录音时模拟按键": "Імітувати кнопки під час запису", + "窗口截图": "Знімок вікна", + "自动截图": "Автоматичний знімок екрана" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/vi.json b/LunaTranslator/files/lang/vi.json index 63228821..b5530944 100644 --- a/LunaTranslator/files/lang/vi.json +++ b/LunaTranslator/files/lang/vi.json @@ -791,5 +791,7 @@ "编码": "Mã hóa", "自动TTS": "Tự động TTS", "使用webview显示": "Sử dụng WebView để hiển thị", - "录音时模拟按键": "Mô phỏng các phím khi ghi âm" + "录音时模拟按键": "Mô phỏng các phím khi ghi âm", + "窗口截图": "Ảnh chụp cửa sổ", + "自动截图": "Ảnh chụp màn hình tự động" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/zh.json b/LunaTranslator/files/lang/zh.json index de0ed88e..c9252fda 100644 --- a/LunaTranslator/files/lang/zh.json +++ b/LunaTranslator/files/lang/zh.json @@ -87,7 +87,7 @@ "附加音源": "", "图标2": "", "如果你感觉该软件对你有帮助,欢迎微信扫码赞助,谢谢~": "", - "前景窗口截屏": "", + "窗口截图": "", "OCR最短间隔时间(s)": "", "自动获取系统代理": "", "是否使用快捷键": "", @@ -791,5 +791,7 @@ "编码": "", "自动TTS": "", "使用webview显示": "", - "录音时模拟按键": "" + "录音时模拟按键": "", + "前景窗口截屏": "", + "自动截图": "" } \ No newline at end of file