From b344ee5402427e007119bd8647976d72db56aae9 Mon Sep 17 00:00:00 2001 From: Echoosx <1399478309@qq.com> Date: Wed, 31 Jan 2024 01:27:51 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=89=8D=E7=AB=AF=E8=B0=83=E6=95=B4=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BF=BB=E8=AF=91=E5=B9=B6=E6=8B=BC=E6=8E=A5?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=20-=20=E5=AE=8C=E5=96=84=E5=8F=B3=E9=94=AE=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E7=9B=B8=E4=BC=BC=E6=80=A7=E5=8A=9F=E8=83=BD=20-=20OCR?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E5=BF=BD=E7=95=A5=E5=89=8D=E5=90=8E=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Config.py | 53 ++++--- GUI.py | 315 ++++++++++++++++++++++++++------------- OCR_style.py | 85 +++++------ OCR_style.ui | 162 +++++++++++--------- config_style.py | 135 ++++++++++++----- config_style.ui | 277 ++++++++++++++++++++++++++++++---- customerDefineDict.py | 17 ++- customerDefineQtClass.py | 14 +- 8 files changed, 743 insertions(+), 315 deletions(-) diff --git a/Config.py b/Config.py index a5196f8..6350609 100644 --- a/Config.py +++ b/Config.py @@ -1,5 +1,7 @@ +import os +import sys + from yaml import CLoader as CLoader, CDumper as CDumper, load as yaml_load, dump as yaml_dump -import sys, os NOPROXIES = { "http": "", @@ -11,38 +13,43 @@ configPath = f'{os.path.dirname(sys.executable)}/TranslatorConfig.yaml' if isPacked else f'{os.path.dirname(os.path.abspath(__file__))}/TranslatorConfig.yaml' + def writeConfig(data: dict) -> None: with open(configPath, mode='w', encoding='utf-8') as f: yaml_dump(data, f, CDumper) + def readConfig() -> dict: - with open(configPath,encoding='utf-8') as f: - return yaml_load(f,CLoader) + with open(configPath, encoding='utf-8') as f: + return yaml_load(f, CLoader) + def initConfig() -> None: emptyData = {'YOUDAO_KEY': '', - 'YOUDAO_SECRET': '', - 'CAIYUN_TOKEN': '', - 'BAIDU_APPID': '', - 'BAIDU_SECRETKEY': '', - 'TENCENT_SECERTID': '', - 'TENCENT_SECERTKEY': '', - 'XIAONIU_KEY': '', - 'ALIYUN_KEY': '', - 'ALIYUN_SECRET': '', - 'SELECTED_TRANSLATORS': [], - 'Hotkey_OCR': 'Ctrl + Space', - 'OCR_APPID': 'c788b7aa', - 'OCR_SECRET': 'ODFmODQwZWJmZDhlNTIzOTljNGI3OTcy', - 'OCR_KEY': '7ec53833f14724cffb810c14e72eef0d', - 'YOUDAO_FREE_RIDER': False, - 'CAIYUN_FREE_RIDER': False, - 'BAIDU_FREE_RIDER': False, - 'TENCENT_FREE_RIDER': False, - 'OPACITY': 95} + 'YOUDAO_SECRET': '', + 'CAIYUN_TOKEN': '', + 'BAIDU_APPID': '', + 'BAIDU_SECRETKEY': '', + 'TENCENT_SECERTID': '', + 'TENCENT_SECERTKEY': '', + 'XIAONIU_KEY': '', + 'ALIYUN_KEY': '', + 'ALIYUN_SECRET': '', + 'SELECTED_TRANSLATORS': [], + 'Hotkey_OCR': 'Ctrl + Space', + 'Hotkey_CONCAT': 'Ctrl + Shift + Space', + 'OCR_APPID': 'c788b7aa', + 'OCR_SECRET': 'ODFmODQwZWJmZDhlNTIzOTljNGI3OTcy', + 'OCR_KEY': '7ec53833f14724cffb810c14e72eef0d', + 'YOUDAO_FREE_RIDER': False, + 'CAIYUN_FREE_RIDER': False, + 'BAIDU_FREE_RIDER': False, + 'TENCENT_FREE_RIDER': False, + 'OPACITY': 95} writeConfig(emptyData) -if(not os.path.exists(configPath)): + +if not os.path.exists(configPath): isInit = False print('Config file not found, creating a new one...') initConfig() diff --git a/GUI.py b/GUI.py index 4476d12..7c4c67c 100644 --- a/GUI.py +++ b/GUI.py @@ -15,14 +15,16 @@ from Config import readConfig, writeConfig, isInit import keyboard from customerDefineDict import ZhNameDict, JPNameDict, hirakanaList, katakanaList + + # from var_dump import var_dump def keymap_replace( - string: str, - mappings: dict, - lower_keys=False, - lower_values=False, - lower_string=False, + string: str, + mappings: dict, + lower_keys=False, + lower_values=False, + lower_string=False, ) -> str: """Replace parts of a string based on a dictionary. @@ -45,44 +47,49 @@ def keymap_replace( ) return replaced_string + def getSimilarKana(kana: str) -> list: - return [similarKana for similarKana in katakanaList+hirakanaList if kana in similarKana] + return [similarKana for similarKana in katakanaList + hirakanaList if kana in similarKana] + def nameReplace(string: str, reverse=False): _dict = ZhNameDict if reverse else JPNameDict return keymap_replace(string, _dict) + class getSecretWidget_class(QtWidgets.QWidget, Ui_getSecretWidget): def __init__(self, parent) -> None: super().__init__() self.setupUi(self) - self.parent = parent # type: ignore - + self.parent = parent # type: ignore + def setupUi(self, Config): super().setupUi(Config) - Config.setWindowOpacity(0.9) + # Config.setWindowOpacity(0.9) + Config.setWindowOpacity(1) Config.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint) - + def fillListWithSecret(self): self.secretList = getOCRSecret() self.ListWidget_Secrets.addItems((each[1] for each in self.secretList)) - + def checkSecret(self): _tempList = self.secretList[self.ListWidget_Secrets.currentRow()] self.ListWidget_Secrets.item(self.ListWidget_Secrets.currentRow()).setBackground(Qt.GlobalColor.gray) - if(checkSecretAvailable(_tempList[0], _tempList[1], _tempList[2])): + if (checkSecretAvailable(_tempList[0], _tempList[1], _tempList[2])): self.ListWidget_Secrets.item(self.ListWidget_Secrets.currentRow()).setBackground(Qt.GlobalColor.green) self.PushButton_Confirm.setEnabled(True) return self.ListWidget_Secrets.item(self.ListWidget_Secrets.currentRow()).setBackground(Qt.GlobalColor.red) self.PushButton_Confirm.setEnabled(False) - + def checkCurrectIsAvailable(self): - if(self.ListWidget_Secrets.item(self.ListWidget_Secrets.currentRow()).background().color() == Qt.GlobalColor.green): + if (self.ListWidget_Secrets.item( + self.ListWidget_Secrets.currentRow()).background().color() == Qt.GlobalColor.green): self.PushButton_Confirm.setEnabled(True) return self.PushButton_Confirm.setEnabled(False) - + def save(self): _APPID, _SECRET, _KEY = self.secretList[self.ListWidget_Secrets.currentRow()] self.parent.LineEdit_OCRAPPID.setText(_APPID) @@ -91,14 +98,21 @@ def save(self): print('OCR密钥已设置!') self.close() + class configWidget_class(QtWidgets.QWidget, Ui_Config): def __init__(self, parent) -> None: super().__init__() self.setupUi(self) self.ListWidget_SelectedSource.setDragDropMode(QtWidgets.QAbstractItemView.DragDropMode.InternalMove) - self.parent = parent # type: ignore + self.parent = parent # type: ignore self.Hotkey_OCR = parent.Hotkey_OCR - self.OCRKeyEdit.hide(); self.cancelHotKeyButton.hide(); self.confirmHotKeyButton.hide() + self.Hotkey_CONCAT = parent.Hotkey_CONCAT + self.OCRKeyEdit.hide() + self.ConcatKeyEdit.hide() + self.cancelHotKeyButton.hide() + self.confirmHotKeyButton.hide() + self.cancelConcatHotKeyButton.hide() + self.confirmConcatHotKeyButton.hide() self.LineEditMapping = { 'YOUDAO_KEY': self.LineEdit_YoudaoKEY, 'YOUDAO_SECRET': self.LineEdit_YoudaoSECRET, @@ -115,10 +129,10 @@ def __init__(self, parent) -> None: 'OCR_KEY': self.LineEdit_OCRKEY, } self.FreeRiderMapping = { - 'YOUDAO_FREE_RIDER': self.CheckBox_Youdao, - 'CAIYUN_FREE_RIDER': self.CheckBox_CaiYun, - 'BAIDU_FREE_RIDER': self.CheckBox_Baidu, - 'TENCENT_FREE_RIDER': self.CheckBox_Tencent, + 'YOUDAO_FREE_RIDER': self.CheckBox_Youdao, + 'CAIYUN_FREE_RIDER': self.CheckBox_CaiYun, + 'BAIDU_FREE_RIDER': self.CheckBox_Baidu, + 'TENCENT_FREE_RIDER': self.CheckBox_Tencent, } self.getSecretWidget = getSecretWidget_class(self) @@ -126,9 +140,10 @@ def closeEvent(self, event): self.parent.Status = True self.parent.setEnabled(True) if ((data := readConfig()) != self.getConfig()): - reply = QtWidgets.QMessageBox.warning (self, '设置尚未保存', - "是否保存再关闭窗口?", QtWidgets.QMessageBox.StandardButton.Save | - QtWidgets.QMessageBox.StandardButton.Discard, QtWidgets.QMessageBox.StandardButton.Discard) + reply = QtWidgets.QMessageBox.warning(self, '设置尚未保存', + "是否保存再关闭窗口?", QtWidgets.QMessageBox.StandardButton.Save | + QtWidgets.QMessageBox.StandardButton.Discard, + QtWidgets.QMessageBox.StandardButton.Discard) if reply == QtWidgets.QMessageBox.StandardButton.Save: self.saveConfig() else: @@ -136,13 +151,14 @@ def closeEvent(self, event): def setupUi(self, Config): super().setupUi(Config) - Config.setWindowOpacity(1) + Config.setWindowOpacity(0.9) Config.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint) - + def replaceWithCurrentConfig(self): self.ListWidget_SelectableSource.clear() self.ListWidget_SelectedSource.clear() self.Label_ShortcutKeyText.setText(self.Hotkey_OCR) + self.Label_ConcatShortcutKeyText.setText(self.Hotkey_CONCAT) configDict = readConfig() self.spinBox_Opacity.setValue(configDict['OPACITY']) for each in self.LineEditMapping: @@ -155,28 +171,32 @@ def replaceWithCurrentConfig(self): self.ListWidget_SelectedSource.addItem(each) def addTranslator(self): - if(self.ListWidget_SelectableSource.currentItem()): + if (self.ListWidget_SelectableSource.currentItem()): self.ListWidget_SelectedSource.addItem(self.ListWidget_SelectableSource.currentItem().text()) self.ListWidget_SelectableSource.takeItem(self.ListWidget_SelectableSource.currentRow()) self.checkSelectedTranslatorCount() def removeTranslator(self): - if(self.ListWidget_SelectedSource.currentItem()): + if (self.ListWidget_SelectedSource.currentItem()): self.ListWidget_SelectableSource.addItem(self.ListWidget_SelectedSource.currentItem().text()) self.ListWidget_SelectedSource.takeItem(self.ListWidget_SelectedSource.currentRow()) self.checkSelectedTranslatorCount() def upTranslator(self): - if(self.ListWidget_SelectedSource.currentItem()): + if (self.ListWidget_SelectedSource.currentItem()): _currentRow = self.ListWidget_SelectedSource.currentRow() - self.ListWidget_SelectedSource.insertItem(self.ListWidget_SelectedSource.currentRow()-1, self.ListWidget_SelectedSource.takeItem(self.ListWidget_SelectedSource.currentRow())) - self.ListWidget_SelectedSource.setCurrentRow(_currentRow-1) + self.ListWidget_SelectedSource.insertItem(self.ListWidget_SelectedSource.currentRow() - 1, + self.ListWidget_SelectedSource.takeItem( + self.ListWidget_SelectedSource.currentRow())) + self.ListWidget_SelectedSource.setCurrentRow(_currentRow - 1) def downTranslator(self): - if(self.ListWidget_SelectedSource.currentItem()): + if (self.ListWidget_SelectedSource.currentItem()): _currentRow = self.ListWidget_SelectedSource.currentRow() - self.ListWidget_SelectedSource.insertItem(self.ListWidget_SelectedSource.currentRow()+1, self.ListWidget_SelectedSource.takeItem(self.ListWidget_SelectedSource.currentRow())) - self.ListWidget_SelectedSource.setCurrentRow(_currentRow+1) + self.ListWidget_SelectedSource.insertItem(self.ListWidget_SelectedSource.currentRow() + 1, + self.ListWidget_SelectedSource.takeItem( + self.ListWidget_SelectedSource.currentRow())) + self.ListWidget_SelectedSource.setCurrentRow(_currentRow + 1) def getCurrentSelectedTranslator(self): return [self.ListWidget_SelectedSource.item(i).text() for i in range(self.ListWidget_SelectedSource.count())] @@ -191,32 +211,74 @@ def checkSelectedTranslatorCount(self): def getIntoHotKeyChangeMode(self): print('请摁下快捷键!') - self.OCRKeyEdit.show(); self.confirmHotKeyButton.show(); self.cancelHotKeyButton.show(); self.changeHotKeyButton.hide(); self.Label_ShortcutKeyText.hide() - + self.OCRKeyEdit.show() + self.confirmHotKeyButton.show() + self.cancelHotKeyButton.show() + self.changeHotKeyButton.hide() + self.Label_ShortcutKeyText.hide() + + def getIntoConcatHotKeyChangeMode(self): + print('请摁下快捷键!') + self.ConcatKeyEdit.show() + self.confirmConcatHotKeyButton.show() + self.cancelConcatHotKeyButton.show() + self.changeConcatHotKeyButton.hide() + self.Label_ConcatShortcutKeyText.hide() + def confirmHotkey(self): - if(not self.OCRKeyEdit._string): + if (not self.OCRKeyEdit._string): self.cancelHotKey() return - self.Hotkey_OCR = self.OCRKeyEdit._string.replace('+',' + ') + self.Hotkey_OCR = self.OCRKeyEdit._string.replace('+', ' + ') self.Label_ShortcutKeyText.setText(self.OCRKeyEdit._string) print(f'热键将更改为: {self.Hotkey_OCR}') - self.OCRKeyEdit.hide(); self.confirmHotKeyButton.hide(); self.cancelHotKeyButton.hide(); self.changeHotKeyButton.show(); self.Label_ShortcutKeyText.show() - + self.OCRKeyEdit.hide() + self.confirmHotKeyButton.hide() + self.cancelHotKeyButton.hide() + self.changeHotKeyButton.show() + self.Label_ShortcutKeyText.show() + + def confirmConcatHotkey(self): + if (not self.ConcatKeyEdit._string): + self.cancelConcatHotKey() + return + self.Hotkey_CONCAT = self.ConcatKeyEdit._string.replace('+', ' + ') + self.Label_ConcatShortcutKeyText.setText(self.ConcatKeyEdit._string) + print(f'热键将更改为: {self.Hotkey_CONCAT}') + self.ConcatKeyEdit.hide() + self.confirmConcatHotKeyButton.hide() + self.cancelConcatHotKeyButton.hide() + self.changeConcatHotKeyButton.show() + self.Label_ConcatShortcutKeyText.show() + def cancelHotKey(self): - self.OCRKeyEdit.hide(); self.confirmHotKeyButton.hide(); self.cancelHotKeyButton.hide(); self.changeHotKeyButton.show(); self.Label_ShortcutKeyText.show() + self.OCRKeyEdit.hide() + self.confirmHotKeyButton.hide() + self.cancelHotKeyButton.hide() + self.changeHotKeyButton.show() + self.Label_ShortcutKeyText.show() print('已取消更改热键') - + + def cancelConcatHotKey(self): + self.ConcatKeyEdit.hide() + self.confirmConcatHotKeyButton.hide() + self.cancelConcatHotKeyButton.hide() + self.changeConcatHotKeyButton.show() + self.Label_ConcatShortcutKeyText.show() + print('已取消更改热键') + def showGetSecretWidget(self): self.getSecretWidget.show() - - def getConfig(self) -> dict|None: - if(not self.getCurrentSelectedTranslator()): - QtWidgets.QMessageBox.critical(self,"配置有误","至少选择一个翻译源!") + + def getConfig(self) -> dict | None: + if (not self.getCurrentSelectedTranslator()): + QtWidgets.QMessageBox.critical(self, "配置有误", "至少选择一个翻译源!") return None data = {each: self.LineEditMapping[each].text() for each in self.LineEditMapping} - data.update({each: self.FreeRiderMapping[each].isChecked() for each in self.FreeRiderMapping}) # type: ignore - data['SELECTED_TRANSLATORS'] = self.getCurrentSelectedTranslator() # type: ignore + data.update({each: self.FreeRiderMapping[each].isChecked() for each in self.FreeRiderMapping}) # type: ignore + data['SELECTED_TRANSLATORS'] = self.getCurrentSelectedTranslator() # type: ignore data['Hotkey_OCR'] = self.Hotkey_OCR + data['Hotkey_CONCAT'] = self.Hotkey_CONCAT data['OPACITY'] = self.horizontalSlider_Opacity.value() return data @@ -225,16 +287,18 @@ def saveConfig(self): return writeConfig(data) self.parent.changeHotkey(self.Hotkey_OCR) + self.parent.changeConcatHotkey(self.Hotkey_CONCAT) self.parent.updateTranslatorList(self.getCurrentSelectedTranslator()) - reloadOCRConfig(); reloadTranslatorConfig() + reloadOCRConfig(); + reloadTranslatorConfig() self.close() - + def changeHorizontalSliderOpacity(self, value): self.horizontalSlider_Opacity.setValue(value) self.parent.changeOpacity(value) - + def changeSpinBoxOpacity(self, _value): - self.spinBox_Opacity.setValue(self.horizontalSlider_Opacity.value()) + self.spinBox_Opacity.setValue(self.horizontalSlider_Opacity.value()) class dictWindow_class(QtWidgets.QMainWindow, Ui_dict_Window): @@ -252,53 +316,60 @@ def setupUi(self, Config): Config.setWindowOpacity(0.9) Config.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint) - def updateAutoSearch(self,_bool): + def updateAutoSearch(self, _bool): self.autoSearch = _bool - def showEvent(self,event): - super(dictWindow_class,self).showEvent(event) + def showEvent(self, event): + super(dictWindow_class, self).showEvent(event) def searchWord(self): self.wordsList.clear() source = self.inputLineEdit.text() - if(source): + if (source): self._wordList = searchWord(source) tempItemList = tuple(each[0] for each in self._wordList) self.wordsList.addItems(tempItemList) self.wordsList.setCurrentItem(self.wordsList.item(0)) - def setQueryWord(self,word): + def setQueryWord(self, word): self.inputLineEdit.setText(word) def editingFinished(self): - if(self.autoSearch): + if (self.autoSearch): self.searchWord() def returnPressed(self): - if(not self.autoSearch): + if (not self.autoSearch): self.searchWord() - def fromtHtml(self,sourceList) -> str: + def fromtHtml(self, sourceList) -> str: try: html = f'
{sourceList[0][0]}
{sourceList[0][2]} {sourceList[0][3]}
'+ sourceList[1][1][eachId][0] +':
' + \ + sourceList[1][1][eachId][ + 0] + ':
'+ eachExampleList[1] +'
' + html += '' + \ + eachExampleList[1] + '
' html += '确认更改快捷键
")) + self.cancelConcatHotKeyButton.setText(_translate("Config", "取消")) + self.confirmConcatHotKeyButton.setToolTip(_translate("Config", "确认更改快捷键
")) + self.confirmConcatHotKeyButton.setText(_translate("Config", "确认")) + self.changeConcatHotKeyButton.setToolTip(_translate("Config", "确认更改快捷键
")) + self.changeConcatHotKeyButton.setText(_translate("Config", "更改")) from customerDefineQtClass import oneKeyQKeySequenceEdit diff --git a/config_style.ui b/config_style.ui index 1047e77..e30cc17 100644 --- a/config_style.ui +++ b/config_style.ui @@ -68,7 +68,7 @@