diff --git a/lookup.py b/lookup.py index ea650de..4ec7377 100644 --- a/lookup.py +++ b/lookup.py @@ -1,8 +1,9 @@ # Copyright: Ren Tatsumoto and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -from collections import OrderedDict +import io from gettext import gettext as _ +from typing import Optional from aqt import gui_hooks, mw from aqt.browser import Browser @@ -10,7 +11,8 @@ from aqt.utils import showInfo, restoreGeom, saveGeom from aqt.webview import AnkiWebView -from .ajt_common.about_menu import menu_root_entry, tweak_window +from .pitch_accents.common import AccentDict +from .ajt_common.about_menu import menu_root_entry, tweak_window, garbage_collect_on_dialog_finish from .config_view import config_view as cfg from .helpers.tokens import clean_furigana from .reading import get_pronunciations, format_pronunciations, update_html @@ -26,14 +28,18 @@ class ViewPitchAccentsDialog(QDialog): mw.addonManager.setWebExports(__name__, r"(img|web)/.*\.(js|css|html|png|svg)") + _pronunciations: Optional[AccentDict] + _webview: Optional[AnkiWebView] + def __init__(self, parent: QWidget): super().__init__(parent) self._webview = AnkiWebView(parent=self, title=ACTION_NAME) self._pronunciations = None self._setup_ui() + garbage_collect_on_dialog_finish(self) + tweak_window(self) def _setup_ui(self): - tweak_window(self) self.setWindowModality(Qt.WindowModality.ApplicationModal) self.setWindowTitle(ACTION_NAME) self.setMinimumSize(420, 240) @@ -70,34 +76,36 @@ def lookup_pronunciations(self, search: str): self._pronunciations = get_pronunciations(search) return self - def set_html_result(self): - """Format pronunciations as an HTML list.""" - ordered_dict = OrderedDict() + def _format_html_result(self): + """Create HTML body""" + assert self._pronunciations, "Populate pronunciations first." + html = io.StringIO() + html.write('
') for word, entries in self._pronunciations.items(): - ordered_dict[word] = "".join( - dict.fromkeys(f"
  • {update_html(entry.html_notation)}[{entry.pitch_number}]
  • " for entry in entries) - ) - - entries = [] - for word, html in ordered_dict.items(): - entries.append(f'
    {word}
      {html}
    ') + html.write(f'
    {word}
    ') + html.write('
    ') + html.write("
      ") + entries = dict.fromkeys(f"{update_html(entry.html_notation)}[{entry.pitch_number}]" for entry in entries) + for entry in entries: + html.write(f"
    1. {entry}
    2. ") + html.write("
    ") + html.write(f"
    ") + html.write("
    ") + return html.getvalue() + def set_html_result(self): + """Format pronunciations as an HTML list.""" self._webview.stdHtml( - body=f'
    {"".join(entries)}
    ', + body=self._format_html_result(), css=[self._css_relpath], ) return self - def reject(self) -> None: - self._webview = None - return super().reject() - - def accept(self) -> None: - self._webview = None - return super().accept() - def done(self, *args, **kwargs) -> None: + print("closing AJT lookup window...") saveGeom(self, ACTION_NAME) + self._webview = None + self._pronunciations = None return super().done(*args, **kwargs)