Skip to content

Commit

Permalink
Move OCR configuration to NVDA's settings dialog to take advantage of…
Browse files Browse the repository at this point in the history
… config profiles
  • Loading branch information
lukaszgo1 committed Mar 19, 2020
1 parent 0fb2df0 commit d4b6d1b
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions addon/globalPlugins/ocr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d4b6d1b

Please sign in to comment.