From 18c9608c3d3529b4ce19852553ae86337dd630ec 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: Fri, 23 Feb 2024 11:50:25 +0800 Subject: [PATCH] issues/527 issues/528 --- .../LunaTranslator/LunaTranslator.py | 3 +- .../LunaTranslator/LunaTranslator_main.py | 2 +- .../LunaTranslator/gui/rangeselect.py | 29 +++++++++++++++---- .../LunaTranslator/gui/settingpage_ocr.py | 2 ++ .../LunaTranslator/gui/translatorUI.py | 2 +- .../LunaTranslator/gui/usefulwidget.py | 5 +++- .../LunaTranslator/textsource/ocrtext.py | 18 ++++++++---- .../files/defaultconfig/config.json | 2 ++ .../files/defaultconfig/static_data.json | 2 +- 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 | 4 ++- 24 files changed, 92 insertions(+), 33 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index 6c1c7289..df6b5a67 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -38,11 +38,10 @@ class _autolock: def __del__(self): self.lock.release() class MAINUI() : - def __init__(self,app) -> None: + def __init__(self) -> None: super().__init__() self.lasttranslatorindex=0 self.usefultranslators=0 - self.app=app self.translators={} self.cishus={} self.reader=None diff --git a/LunaTranslator/LunaTranslator/LunaTranslator_main.py b/LunaTranslator/LunaTranslator/LunaTranslator_main.py index 98acec7d..1e695479 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator_main.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator_main.py @@ -74,7 +74,7 @@ if __name__ == "__main__" : getQMessageBox(None,"错误","当前路径读写权限不足,请使用管理员权限运行!") os._exit(0) - gobject.baseobject=MAINUI(app) + gobject.baseobject=MAINUI() gobject.baseobject.checklang() gobject.baseobject.aa() app.exit(app.exec_()) diff --git a/LunaTranslator/LunaTranslator/gui/rangeselect.py b/LunaTranslator/LunaTranslator/gui/rangeselect.py index 822fa551..fd94d09a 100644 --- a/LunaTranslator/LunaTranslator/gui/rangeselect.py +++ b/LunaTranslator/LunaTranslator/gui/rangeselect.py @@ -1,7 +1,7 @@ -from PyQt5.QtWidgets import QWidget,QDesktopWidget,QMainWindow,QLabel,QPushButton,QStatusBar,QDialog,QApplication +from PyQt5.QtWidgets import QMenu,QDesktopWidget,QMainWindow,QLabel,QAction,QStatusBar,QDialog,QApplication from PyQt5.QtGui import QBitmap,QPainter,QPen,QColor,QFont,QMouseEvent,QCursor from PyQt5.QtCore import Qt,QPoint,QRect,QEvent,pyqtSignal - +from myutils.config import _TR import gobject from myutils.config import globalconfig from gui.resizeablemainwindow import Mainw @@ -19,9 +19,19 @@ class rangeadjust(Mainw) : self._rect=None self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) self.setAttribute(Qt.WA_TranslucentBackground) - + self.setContextMenuPolicy(Qt.CustomContextMenu) + self.customContextMenuRequested.connect(self.showmenu) for s in self.cornerGrips: s.raise_() + def showmenu(self,p): + menu=QMenu(self ) + close=QAction(_TR("关闭")) + menu.addAction(close) + action=menu.exec(self.mapToGlobal(p)) + if action==close: + self._rect=None + self.close() + def setstyle(self): self.label.setStyleSheet(" border:%spx solid %s; background-color: rgba(0,0,0, 0.01)" %(globalconfig['ocrrangewidth'],globalconfig['ocrrangecolor'] )) def mouseMoveEvent(self, e ) : @@ -39,7 +49,8 @@ class rangeadjust(Mainw) : self._endPos = None def moveEvent(self,e): rect = self.geometry() - self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])] + if self._rect: + self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])] def enterEvent(self, QEvent) : self.drag_label.setStyleSheet("background-color:rgba(0,0,0, 0.1)") def leaveEvent(self, QEvent): @@ -48,11 +59,17 @@ class rangeadjust(Mainw) : self.label.setGeometry(0, 0, self.width(), self.height()) rect = self.geometry() - self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])] + if self._rect: + self._rect=[(rect.left()+globalconfig['ocrrangewidth'],rect.top()+globalconfig['ocrrangewidth']),(rect.right()-globalconfig['ocrrangewidth'],rect.bottom()-globalconfig['ocrrangewidth'])] super(rangeadjust, self).resizeEvent(a0) def getrect(self): return self._rect - + def setrect(self,rect): + self._rect=rect + if rect: + (x1,y1),(x2,y2)=rect + self.setGeometry(x1-globalconfig['ocrrangewidth'],y1-globalconfig['ocrrangewidth'],x2-x1+2*globalconfig['ocrrangewidth'],y2-y1+2*globalconfig['ocrrangewidth']) + self.show() class rangeselct(QMainWindow) : def __init__(self, parent ) : diff --git a/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py b/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py index 9727ed52..416fcda3 100644 --- a/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py +++ b/LunaTranslator/LunaTranslator/gui/settingpage_ocr.py @@ -50,6 +50,8 @@ def getocrgrid(self) : [], [(("多重区域模式"),12),getsimpleswitch(globalconfig ,'multiregion')], + [(("记忆选定区域"),12),getsimpleswitch(globalconfig ,'rememberocrregions')], + [], [(("OCR范围框颜色"),12),(getcolorbutton(globalconfig,'ocrrangecolor',callback=lambda : selectcolor(self,globalconfig,'ocrrangecolor',self.ocrrangecolor_button,callback=lambda :gobject.baseobject.textsource.setstyle()),name='ocrrangecolor_button',parent=self),1)], [(("OCR范围框宽度"),12),(getspinbox(1,100,globalconfig,'ocrrangewidth' ,callback=lambda x: gobject.baseobject.textsource.setstyle() ),4)], [(("选取OCR范围后立即进行一次识别"),12),getsimpleswitch(globalconfig ,'ocrafterrangeselect')], diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index 7e88c829..e3f0691a 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -669,12 +669,12 @@ class QUnFrameWindow(resizableframeless): self.tray.hide() self.tray = None self.hide() - saveallconfig() if gobject.baseobject.textsource: gobject.baseobject.textsource=None + saveallconfig() endsubprocs() os._exit(0) \ No newline at end of file diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 2d646b4b..fcc4fdc0 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -382,7 +382,10 @@ def selectcolor(parent,configdict,configkey,button,item=None,name=None,callback= configdict[configkey]=color.name() if callback: - callback() + try: + callback() + except: + print_exc() def getboxlayout(widgets,lc=QHBoxLayout): cp_layout=lc() diff --git a/LunaTranslator/LunaTranslator/textsource/ocrtext.py b/LunaTranslator/LunaTranslator/textsource/ocrtext.py index 40b346dd..3c36c11e 100644 --- a/LunaTranslator/LunaTranslator/textsource/ocrtext.py +++ b/LunaTranslator/LunaTranslator/textsource/ocrtext.py @@ -45,6 +45,11 @@ class ocrtext(basetext): self.range_ui = [] self.timestamp=time.time() super(ocrtext,self ).__init__('0','ocr') + if globalconfig['rememberocrregions']: + for region in globalconfig['ocrregions']: + if region: + self.newrangeadjustor() + self.setrect(region) def newrangeadjustor(self): if len(self.range_ui)==0 or globalconfig['multiregion']: self.range_ui.append(rangeadjust(gobject.baseobject.translation_ui)) @@ -56,13 +61,13 @@ class ocrtext(basetext): for _r in self.range_ui: _r.move(_r.pos().x()+ x,_r.pos().y()+ y) def setrect(self,rect): - (x1,y1),(x2,y2)=rect - self.range_ui[-1].setGeometry(x1-globalconfig['ocrrangewidth'],y1-globalconfig['ocrrangewidth'],x2-x1+2*globalconfig['ocrrangewidth'],y2-y1+2*globalconfig['ocrrangewidth']) - self.range_ui[-1].show() + self.range_ui[-1].setrect(rect) def setstyle(self): [_.setstyle() for _ in self.range_ui] def showhiderangeui(self,b): - [_.setVisible(b) for _ in self.range_ui] + for _ in self.range_ui: + if _.getrect(): + _.setVisible(b) def gettextthread(self ): if all([_.getrect() is None for _ in self.range_ui]): time.sleep(1) @@ -155,6 +160,7 @@ class ocrtext(basetext): def end(self): + globalconfig['ocrregions']=[_.getrect() for _ in self.range_ui] + [_.close() for _ in self.range_ui] super().end() - ocr_end() - [_.close() for _ in self.range_ui] \ No newline at end of file + ocr_end() \ No newline at end of file diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index 3a226001..909ff3fb 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -97,6 +97,8 @@ 500 ], "multiregion":false, + "rememberocrregions":false, + "ocrregions":[], "textboxreadonly":true, "ocr_stable_sim": 0, "ocr_diff_sim": 0.95, diff --git a/LunaTranslator/files/defaultconfig/static_data.json b/LunaTranslator/files/defaultconfig/static_data.json index 33b4e7f5..a6665d2c 100644 --- a/LunaTranslator/files/defaultconfig/static_data.json +++ b/LunaTranslator/files/defaultconfig/static_data.json @@ -1,5 +1,5 @@ { - "version":"v2.37.0", + "version":"v2.37.1", "language_list_show":["简体中文","日本語","English","Русский язык","Español","한국어","Français","繁體中文","Tiếng Việt","Türkçe","Polski","Українська Мова","Italiano","اللغة العربية","ภาษาไทย"] , "language_list_translator":["简体中文","日文","英文","俄语","西班牙语","韩语","法语","繁体中文","越南语","土耳其语","波兰语","乌克兰语","意大利语","阿拉伯语","泰语"], "language_list_translator_inner":["zh", "ja", "en","ru","es","ko","fr","cht","vi","tr","pl","uk","it","ar","th"], diff --git a/LunaTranslator/files/lang/ar.json b/LunaTranslator/files/lang/ar.json index a4116264..18f181af 100644 --- a/LunaTranslator/files/lang/ar.json +++ b/LunaTranslator/files/lang/ar.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "استخدام النموذج الياباني بدلا من ذلك ، قد لا تعمل بشكل جيد", "保持非重复字符": "الحفاظ على الطابع غير المتكرر", "选择文本窗口中文本框只读": "حدد مربع النص للقراءة فقط في نافذة النص", - "多重区域模式": "وضع منطقة متعددة" + "多重区域模式": "وضع منطقة متعددة", + "记忆选定区域": "اختيار الذاكرة", + "关闭": "غلق" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/cht.json b/LunaTranslator/files/lang/cht.json index c7bb4390..8e72e97e 100644 --- a/LunaTranslator/files/lang/cht.json +++ b/LunaTranslator/files/lang/cht.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "正在使用日語模型作為代替,可能效果不佳", "保持非重复字符": "保持非重複字元", "选择文本窗口中文本框只读": "選擇文字視窗中文字方塊只讀", - "多重区域模式": "多重區域模式" + "多重区域模式": "多重區域模式", + "记忆选定区域": "記憶選定區域", + "关闭": "關閉" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/en.json b/LunaTranslator/files/lang/en.json index 39f242d4..3da0b7b0 100644 --- a/LunaTranslator/files/lang/en.json +++ b/LunaTranslator/files/lang/en.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Using Japanese models as a substitute may not be effective", "保持非重复字符": "Keep non repeating characters", "选择文本窗口中文本框只读": "Select read-only text boxes in the text window", - "多重区域模式": "Multi region mode" + "多重区域模式": "Multi region mode", + "记忆选定区域": "Memory selection area", + "关闭": "close" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/es.json b/LunaTranslator/files/lang/es.json index 0a6a03cf..f97f9307 100644 --- a/LunaTranslator/files/lang/es.json +++ b/LunaTranslator/files/lang/es.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Se está utilizando el modelo japonés como sustituto y puede que no funcione bien", "保持非重复字符": "Mantener caracteres no repetitivos", "选择文本窗口中文本框只读": "Seleccione la ventana de texto en la que el cuadro de texto es de solo lectura", - "多重区域模式": "Modelo multiregional" + "多重区域模式": "Modelo multiregional", + "记忆选定区域": "Área seleccionada de memoria", + "关闭": "Cierre" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/fr.json b/LunaTranslator/files/lang/fr.json index 66af5a06..b0961211 100644 --- a/LunaTranslator/files/lang/fr.json +++ b/LunaTranslator/files/lang/fr.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Le modèle japonais est utilisé à la place, peut ne pas fonctionner aussi bien", "保持非重复字符": "Garder les caractères non répétitifs", "选择文本窗口中文本框只读": "Sélectionner une zone de texte en lecture seule dans la fenêtre de texte", - "多重区域模式": "Mode Multi - zones" + "多重区域模式": "Mode Multi - zones", + "记忆选定区域": "Mémoriser la zone sélectionnée", + "关闭": "Fermé" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/it.json b/LunaTranslator/files/lang/it.json index 799e9d8a..3f9ae7d7 100644 --- a/LunaTranslator/files/lang/it.json +++ b/LunaTranslator/files/lang/it.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Utilizzare modelli giapponesi come sostituto potrebbe non essere efficace", "保持非重复字符": "Mantieni caratteri non ripetuti", "选择文本窗口中文本框只读": "Selezionare le caselle di testo di sola lettura nella finestra di testo", - "多重区域模式": "Modalità multi regione" + "多重区域模式": "Modalità multi regione", + "记忆选定区域": "Area di selezione della memoria", + "关闭": "chiudi" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ja.json b/LunaTranslator/files/lang/ja.json index 202e7106..f4473450 100644 --- a/LunaTranslator/files/lang/ja.json +++ b/LunaTranslator/files/lang/ja.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "代替として日本語モデルを使用していますが、効果が低いかもしれません", "保持非重复字符": "非反復文字の維持", "选择文本窗口中文本框只读": "テキストウィンドウのテキストボックス読み取り専用を選択", - "多重区域模式": "複数領域モード" + "多重区域模式": "複数領域モード", + "记忆选定区域": "メモリ選択領域", + "关闭": "閉じる" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ko.json b/LunaTranslator/files/lang/ko.json index fc0cf7b3..119116ef 100644 --- a/LunaTranslator/files/lang/ko.json +++ b/LunaTranslator/files/lang/ko.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "일본어 모델을 대체하는 중입니다. 효과가 없을 수 있습니다", "保持非重复字符": "비반복 문자 유지", "选择文本窗口中文本框只读": "텍스트 선택 창의 텍스트 상자 읽기 전용", - "多重区域模式": "다중 영역 모드" + "多重区域模式": "다중 영역 모드", + "记忆选定区域": "선택한 영역 기억하기", + "关闭": "닫기" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/pl.json b/LunaTranslator/files/lang/pl.json index b54e4245..b712049c 100644 --- a/LunaTranslator/files/lang/pl.json +++ b/LunaTranslator/files/lang/pl.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Używanie japońskich modeli jako substytutów może nie być skuteczne", "保持非重复字符": "Zachowaj niepowtarzające się znaki", "选择文本窗口中文本框只读": "Zaznaczanie pola tekstowe tylko do odczytu w oknie tekstowym", - "多重区域模式": "Tryb wielu regionów" + "多重区域模式": "Tryb wielu regionów", + "记忆选定区域": "Obszar wyboru pamięci", + "关闭": "zamknij" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/ru.json b/LunaTranslator/files/lang/ru.json index db687927..7f3fc627 100644 --- a/LunaTranslator/files/lang/ru.json +++ b/LunaTranslator/files/lang/ru.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "В качестве замены используется японская модель, которая может быть неэффективной", "保持非重复字符": "Сохранить символ без повторения", "选择文本窗口中文本框只读": "Выберите текстовое окно только для чтения", - "多重区域模式": "Многорегиональная модель" + "多重区域模式": "Многорегиональная модель", + "记忆选定区域": "Запоминание выбранной области", + "关闭": "Закрыть" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/th.json b/LunaTranslator/files/lang/th.json index b9a44415..02c38734 100644 --- a/LunaTranslator/files/lang/th.json +++ b/LunaTranslator/files/lang/th.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "ใช้แบบจำลองภาษาญี่ปุ่นแทนอาจไม่ได้ผล", "保持非重复字符": "เก็บอักขระที่ไม่ซ้ำกัน", "选择文本窗口中文本框只读": "เลือกกล่องข้อความในหน้าต่างข้อความ อ่านอย่างเดียว", - "多重区域模式": "โหมดหลายโซน" + "多重区域模式": "โหมดหลายโซน", + "记忆选定区域": "หน่วยความจำพื้นที่ที่เลือก", + "关闭": "ปิด" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/tr.json b/LunaTranslator/files/lang/tr.json index e25a8738..93b4c4a6 100644 --- a/LunaTranslator/files/lang/tr.json +++ b/LunaTranslator/files/lang/tr.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Japon modellerini yerine koymak etkili olmayabilir.", "保持非重复字符": "Tekrarlamayan karakterleri tut", "选择文本窗口中文本框只读": "Metin penceresindeki sadece okuyulmuş metin kutularını seçin", - "多重区域模式": "Çok bölge modu" + "多重区域模式": "Çok bölge modu", + "记忆选定区域": "Hafıza seçim alanı", + "关闭": "kapat" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/uk.json b/LunaTranslator/files/lang/uk.json index 4dca2f3b..8354bde2 100644 --- a/LunaTranslator/files/lang/uk.json +++ b/LunaTranslator/files/lang/uk.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Використання японських моделей як заміни може бути неефективним", "保持非重复字符": "Зберегти символи, які не повторюються", "选择文本窗口中文本框只读": "Виберіть текстові поля для лише читання у текстовому вікні", - "多重区域模式": "Режим декількох регіонів" + "多重区域模式": "Режим декількох регіонів", + "记忆选定区域": "Область вибору пам' яті", + "关闭": "закрити" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/vi.json b/LunaTranslator/files/lang/vi.json index dca3889e..dfbc6e03 100644 --- a/LunaTranslator/files/lang/vi.json +++ b/LunaTranslator/files/lang/vi.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "Các mô hình tiếng Nhật đang được sử dụng thay thế và có thể không hiệu quả", "保持非重复字符": "Giữ các ký tự không trùng lặp", "选择文本窗口中文本框只读": "Chọn hộp văn bản chỉ đọc trong cửa sổ văn bản", - "多重区域模式": "Chế độ đa vùng" + "多重区域模式": "Chế độ đa vùng", + "记忆选定区域": "Ghi nhớ vùng chọn", + "关闭": "Đóng cửa" } \ No newline at end of file diff --git a/LunaTranslator/files/lang/zh.json b/LunaTranslator/files/lang/zh.json index 91d949c2..9afd76c0 100644 --- a/LunaTranslator/files/lang/zh.json +++ b/LunaTranslator/files/lang/zh.json @@ -708,5 +708,7 @@ "正在使用日语模型作为代替,可能效果不佳": "", "保持非重复字符": "", "选择文本窗口中文本框只读": "", - "多重区域模式": "" + "多重区域模式": "", + "记忆选定区域": "", + "关闭": "" } \ No newline at end of file