From d4b6d1ba83d6682518c8fbee4f31423e5e4e6516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Golonka?= Date: Fri, 20 Mar 2020 00:56:44 +0100 Subject: [PATCH] Move OCR configuration to NVDA's settings dialog to take advantage of config profiles --- addon/globalPlugins/ocr/__init__.py | 59 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/addon/globalPlugins/ocr/__init__.py b/addon/globalPlugins/ocr/__init__.py index c36bf23..abacaae 100644 --- a/addon/globalPlugins/ocr/__init__.py +++ b/addon/globalPlugins/ocr/__init__.py @@ -133,39 +133,42 @@ def _getPointFromOffset(self, offset): return locationHelper.Point(int(self._parser.leftCoordOffset), int(self._parser.topCoordOffset)) return locationHelper.Point(int(word.left), int(word.top)) -class GlobalPlugin(globalPluginHandler.GlobalPlugin): - def __init__(self): - super(globalPluginHandler.GlobalPlugin, self).__init__() - self.ocrSettingsItem = gui.mainFrame.sysTrayIcon.preferencesMenu.Append(wx.ID_ANY, - # Translators: The name of the OCR settings item - # in the NVDA Preferences menu. - _("OCR settings...")) - gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onOCRSettings, self.ocrSettingsItem) +class OCRSettingsPanel(gui.SettingsPanel): - def terminate(self): - try: - gui.mainFrame.sysTrayIcon.preferencesMenu.RemoveItem(self.ocrSettingsItem) - except wx.PyDeadObjectError: - pass + # Translators: Title of the OCR settings dialog in the NVDA settings. + title = _("OCR settings") - def onOCRSettings(self, event): - # Pop a dialog with available OCR languages to set - langs = sorted(getAvailableTesseractLanguages()) + def makeSettings(self, settingsSizer): + sHelper = gui.guiHelper.BoxSizerHelper(self, sizer = settingsSizer) + # Translators: Label of a combobox used to choose a recognition language + recogLanguageLabel = _("Recognition &language") + self.availableLangs = {languageHandler.getLanguageDescription(tesseractLangsToLocales[lang]) or tesseractLangsToLocales[lang] : lang for lang in getAvailableTesseractLanguages()} + self.recogLanguageCB = sHelper.addLabeledControl( + recogLanguageLabel, + wx.Choice, + choices = list(self.availableLangs.keys()), + style = wx.CB_SORT + ) + tessLangsToDescs = {v : k for k, v in self.availableLangs.items()} curlang = config.conf["ocr"]["language"] try: - select = langs.index(curlang) + select = tessLangsToDescs[curlang] except ValueError: - select = langs.index('eng') - choices = [languageHandler.getLanguageDescription(tesseractLangsToLocales[lang]) or tesseractLangsToLocales[lang] for lang in langs] - log.debug("Available OCR languages: %s", ", ".join(choices)) - dialog = wx.SingleChoiceDialog(gui.mainFrame, _("Select OCR Language"), _("OCR Settings"), choices=choices) - dialog.SetSelection(select) - gui.mainFrame.prePopup() - ret = dialog.ShowModal() - gui.mainFrame.postPopup() - if ret == wx.ID_OK: - lang = langs[dialog.GetSelection()] - config.conf["ocr"]["language"] = lang + select = tessLangsToDescs['eng'] + select = self.recogLanguageCB.FindString(select) + self.recogLanguageCB.SetSelection(select) + + def onSave (self): + lang = self.availableLangs[self.recogLanguageCB.GetStringSelection()] + config.conf["ocr"]["language"] = lang + +class GlobalPlugin(globalPluginHandler.GlobalPlugin): + def __init__(self): + super(globalPluginHandler.GlobalPlugin, self).__init__() + gui.NVDASettingsDialog.categoryClasses.append(OCRSettingsPanel) + + def terminate(self): + gui.NVDASettingsDialog.categoryClasses.remove(OCRSettingsPanel) def script_ocrNavigatorObject(self, gesture): nav = api.getNavigatorObject()