Esempio n. 1
0
class SearchResultsPage(sc.SizedPanel):
    def __init__(self, parent, search_results, list_label):
        super().__init__(parent, -1)
        column_spec = (
            ColumnDefn(_("Snippet"), "left", 255, operator.attrgetter("snippet")),
            ColumnDefn(
                _("Title"), "center", 255, operator.attrgetter("document_title")
            ),
            ColumnDefn(_("Page"), "right", 120, lambda ins: ins.page_index + 1),
        )
        wx.StaticText(self, -1, list_label)
        self.result_list = ImmutableObjectListView(self, -1, columns=column_spec)
        self.result_list.set_objects(search_results, set_focus=False)
        self.result_list.Bind(
            wx.EVT_LIST_ITEM_ACTIVATED, self.onItemActivated, self.result_list
        )

    def onItemActivated(self, event):
        selected_result = self.result_list.get_selected()
        page = selected_result.page_index
        position = Page.get_text_start_position(
            selected_result.page_id, selected_result.snippet
        )
        uri = selected_result.document.uri.create_copy(
            openner_args=dict(page=page, position=position)
        )
        # Translators: spoken message
        speech.announce("Openning document...")
        sounds.navigation.play()
        EBookReader.open_document_in_a_new_instance(uri)
Esempio n. 2
0
class TesseractLanguagePanel(sc.SizedPanel):
    def __init__(self, *args, is_offline, **kwargs):
        super().__init__(*args, **kwargs)
        self.is_offline = is_offline
        self.online_languages = tesseract_download.get_downloadable_languages()

        # Translators: label of a list control containing bookmarks
        wx.StaticText(self, -1, _("Tesseract Languages"))
        listPanel = sc.SizedPanel(self)
        listPanel.SetSizerType("horizontal")
        listPanel.SetSizerProps(expand=True, align="center")
        self.tesseractLanguageList = ImmutableObjectListView(
            listPanel,
            wx.ID_ANY,
            style=wx.LC_REPORT | wx.SUNKEN_BORDER,
            size=(500, -1))
        self.btnPanel = btnPanel = sc.SizedPanel(self, -1)
        btnPanel.SetSizerType("horizontal")
        btnPanel.SetSizerProps(expand=True)
        if not self.is_offline:
            # Translators: text of a button to add a language to Tesseract OCR Engine (best quality model)
            self.addBestButton = wx.Button(btnPanel, wx.ID_ANY,
                                           _("Download &Best Model"))
            # Translators: text of a button to add a language to Tesseract OCR Engine (fastest model)
            self.addFastButton = wx.Button(btnPanel, wx.ID_ANY,
                                           _("Download &Fast Model"))
            self.Bind(wx.EVT_BUTTON, self.onAdd, self.addFastButton)
            self.Bind(wx.EVT_BUTTON, self.onAdd, self.addBestButton)
        else:
            # Translators: text of a button to remove a language from Tesseract OCR Engine
            self.removeButton = wx.Button(btnPanel, wx.ID_REMOVE, _("&Remove"))
            self.Bind(wx.EVT_BUTTON, self.onRemove, id=wx.ID_REMOVE)
        self.tesseractLanguageList.Bind(wx.EVT_SET_FOCUS, self.onListFocus,
                                        self.tesseractLanguageList)

    def populate_list(self, set_focus=True):
        installed_languages = [(True, info) for info in sorted(
            TesseractOcrEngine.get_recognition_languages(),
            key=lambda l: l.english_name,
        )]
        if self.is_offline:
            languages = installed_languages
        else:
            languages = []
            added_locale_infos = set(l[1] for l in installed_languages)
            for lang in self.online_languages:
                loc_info = LocaleInfo.from_three_letter_code(lang)
                if loc_info in added_locale_infos:
                    continue
                else:
                    languages.append((False, loc_info))
                    added_locale_infos.add(loc_info)
        column_defn = [
            ColumnDefn(
                # Translators: the title of a column in the Tesseract language list
                _("Language"),
                "left",
                450,
                lambda lang: lang[1].description,
            ),
            ColumnDefn(
                # Translators: the title of a column in the Tesseract language list
                _("Installed"),
                "center",
                100,
                lambda lang: _("Yes") if lang[0] else _("No"),
            ),
        ]
        self.tesseractLanguageList.set_columns(column_defn)
        if set_focus:
            self.tesseractLanguageList.set_objects(languages, focus_item=0)
        else:
            self.tesseractLanguageList.set_objects(languages, set_focus=False)
        # Maintain the state of the list
        if not any(languages):
            if not self.is_offline:
                self.addBestButton.Enable(False)
                self.addFastButton.Enable(False)
            else:
                self.removeButton.Enable(False)
            self.btnPanel.Enable(False)

    def onAdd(self, event):
        if (selected := self.tesseractLanguageList.get_selected()) is None:
            return
        lang = selected[1]
        variant = "best" if event.GetEventObject(
        ) == self.addBestButton else "fast"
        lang_code = lang.given_locale_name
        if lang_code not in self.online_languages:
            log.debug(f"Could not find download info for language {lang_code}")
            return
        target_file = tesseract_download.get_language_path(lang_code)
        if target_file.exists():
            msg = wx.MessageBox(
                # Translators: content of a messagebox
                _("A version of the selected language model already exists.\n"
                  "Are you sure you want to replace it."),
                # Translators: title of a message box
                _("Confirm"),
                style=wx.YES_NO | wx.ICON_WARNING,
                parent=self,
            )
            if msg == wx.NO:
                return
        try:
            target_file.unlink(missing_ok=True)
        except:
            return
        progress_dlg = RobustProgressDialog(
            wx.GetApp().mainFrame,
            # Translators: title of a progress dialog
            _("Downloading Language"),
            # Translators: content of a progress dialog
            _("Getting download information..."),
            maxvalue=100,
            can_hide=True,
            can_abort=True,
        )
        threaded_worker.submit(
            tesseract_download.download_language,
            lang_code,
            variant,
            target_file,
            progress_dlg,
        ).add_done_callback(lambda future: wx.CallAfter(
            self._after_download_language, progress_dlg, future))