From e0a2d90a68c73b98ddecc72cb166eb6e12a1df9e Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Mon, 25 Jan 2021 14:45:47 +0100 Subject: [PATCH] NF: HelpPage in an enum Hopefully, this can help with updating on next manual update and maybe even linking to manual translation --- qt/aqt/addcards.py | 5 ++-- qt/aqt/browser.py | 11 +++++---- qt/aqt/clayout.py | 3 ++- qt/aqt/deckchooser.py | 4 +-- qt/aqt/deckconf.py | 5 +++- qt/aqt/dyndeckconf.py | 5 +++- qt/aqt/editor.py | 5 +++- qt/aqt/fields.py | 3 ++- qt/aqt/importing.py | 5 ++-- qt/aqt/main.py | 3 ++- qt/aqt/modelchooser.py | 4 +-- qt/aqt/models.py | 7 +++--- qt/aqt/preferences.py | 5 +++- qt/aqt/studydeck.py | 5 +++- qt/aqt/utils.py | 56 ++++++++++++++++++++++++++++++++++++------ 15 files changed, 95 insertions(+), 31 deletions(-) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index 080788936..067290342 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -16,6 +16,7 @@ from aqt.qt import * from aqt.sound import av_player from aqt.utils import ( TR, + HelpPage, addCloseShortcut, askUser, disable_help_button, @@ -64,7 +65,7 @@ class AddCards(QDialog): self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea) def helpRequested(self): - openHelp("editing?id=adding-cards-and-notes") + openHelp(HelpPage.ADDING_CARD_AND_NOTE) def setupButtons(self) -> None: bb = self.form.buttonBox @@ -172,7 +173,7 @@ class AddCards(QDialog): problem = tr(TR.ADDING_THE_FIRST_FIELD_IS_EMPTY) problem = gui_hooks.add_cards_will_add_note(problem, note) if problem is not None: - showWarning(problem, help="editing?id=adding-cards-and-notes") + showWarning(problem, help=HelpPage.ADDING_CARD_AND_NOTE) return None if note.model()["type"] == MODEL_CLOZE: if not note.cloze_numbers_in_fields(): diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 81a50ae4e..5e5c1282a 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -8,7 +8,7 @@ import time from concurrent.futures import Future from dataclasses import dataclass from operator import itemgetter -from typing import List, Sequence, Tuple, cast +from typing import List, Optional, Sequence, Tuple, cast from markdown import markdown @@ -41,6 +41,7 @@ from aqt.sidebar import SidebarTreeView from aqt.theme import theme_manager from aqt.utils import ( TR, + HelpPage, MenuList, SubMenu, askUser, @@ -1205,7 +1206,7 @@ where id in %s""" return sf def onHelp(self): - openHelp("browsing") + openHelp(HelpPage.BROWSING) # Misc menu options ###################################################################### @@ -1305,7 +1306,7 @@ where id in %s""" current=current, accept=tr(TR.BROWSING_MOVE_CARDS), title=tr(TR.BROWSING_CHANGE_DECK), - help="browsing", + help=HelpPage.BROWSING, parent=self, ) if not ret.name: @@ -1657,7 +1658,7 @@ where id in %s""" self.mw.taskman.run_in_background(do_search, on_done) def onFindReplaceHelp(self): - openHelp("browsing?id=find-and-replace") + openHelp(HelpPage.BROWSING_FIND_AND_REPLACE) # Edit: finding dupes ###################################################################### @@ -2011,7 +2012,7 @@ class ChangeModel(QDialog): QDialog.accept(self) def onHelp(self): - openHelp("browsing?id=other-menu-items") + openHelp(HelpPage.BROWSING_OTHER_MENU_ITEMS) # Card Info Dialog diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index c1e04ea40..1b82e62c3 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -20,6 +20,7 @@ from aqt.sound import av_player, play_clicked_audio from aqt.theme import theme_manager from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, downArrow, @@ -828,4 +829,4 @@ class CardLayout(QDialog): self.mw = None def onHelp(self): - openHelp("templates/intro") + openHelp(HelpPage.TEMPLATES) diff --git a/qt/aqt/deckchooser.py b/qt/aqt/deckchooser.py index 24e85be70..85842ebe1 100644 --- a/qt/aqt/deckchooser.py +++ b/qt/aqt/deckchooser.py @@ -5,7 +5,7 @@ from typing import Any from aqt import AnkiQt, gui_hooks from aqt.qt import * -from aqt.utils import TR, shortcut, tr +from aqt.utils import TR, HelpPage, shortcut, tr class DeckChooser(QHBoxLayout): @@ -86,7 +86,7 @@ class DeckChooser(QHBoxLayout): current=current, accept=tr(TR.ACTIONS_CHOOSE), title=tr(TR.QT_MISC_CHOOSE_DECK), - help="editing", + help=HelpPage.EDITING, cancel=False, parent=self.widget, geomKey="selectDeck", diff --git a/qt/aqt/deckconf.py b/qt/aqt/deckconf.py index 41d5cc077..f5446fa85 100644 --- a/qt/aqt/deckconf.py +++ b/qt/aqt/deckconf.py @@ -13,6 +13,7 @@ from aqt import gui_hooks from aqt.qt import * from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, getOnlyText, @@ -40,7 +41,9 @@ class DeckConf(QDialog): self.setupCombos() self.setupConfs() self.setWindowModality(Qt.WindowModal) - qconnect(self.form.buttonBox.helpRequested, lambda: openHelp("deck-options")) + qconnect( + self.form.buttonBox.helpRequested, lambda: openHelp(HelpPage.DECK_OPTIONS) + ) qconnect(self.form.confOpts.clicked, self.confOpts) qconnect( self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults).clicked, diff --git a/qt/aqt/dyndeckconf.py b/qt/aqt/dyndeckconf.py index 2d18de317..40d2fba64 100644 --- a/qt/aqt/dyndeckconf.py +++ b/qt/aqt/dyndeckconf.py @@ -8,6 +8,7 @@ from anki.lang import without_unicode_isolation from aqt.qt import * from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, openHelp, @@ -34,7 +35,9 @@ class DeckConf(QDialog): self.mw.checkpoint(tr(TR.ACTIONS_OPTIONS)) disable_help_button(self) self.setWindowModality(Qt.WindowModal) - qconnect(self.form.buttonBox.helpRequested, lambda: openHelp("filtered-decks")) + qconnect( + self.form.buttonBox.helpRequested, lambda: openHelp(HelpPage.FILTERED_DECK) + ) self.setWindowTitle( without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=self.deck["name"])) ) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index babd4f24a..5e276f1fe 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -32,6 +32,7 @@ from aqt.sound import av_player from aqt.theme import theme_manager from aqt.utils import ( TR, + HelpPage, disable_help_button, getFile, openHelp, @@ -572,7 +573,9 @@ class Editor: form.setupUi(d) restoreGeom(d, "htmlEditor") disable_help_button(d) - qconnect(form.buttonBox.helpRequested, lambda: openHelp("editing?id=features")) + qconnect( + form.buttonBox.helpRequested, lambda: openHelp(HelpPage.EDITING_FEATURES) + ) form.textEdit.setPlainText(self.note.fields[field]) d.show() form.textEdit.moveCursor(QTextCursor.End) diff --git a/qt/aqt/fields.py b/qt/aqt/fields.py index 549d7e926..9c676ffe7 100644 --- a/qt/aqt/fields.py +++ b/qt/aqt/fields.py @@ -11,6 +11,7 @@ from aqt.qt import * from aqt.schema_change_tracker import ChangeTracker from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, getOnlyText, @@ -245,4 +246,4 @@ class FieldDialog(QDialog): self.mw.taskman.with_progress(save, on_done, self) def onHelp(self): - openHelp("editing?id=customizing-fields") + openHelp(HelpPage.CUSTOMIZING_FIELDS) diff --git a/qt/aqt/importing.py b/qt/aqt/importing.py index 1af505d34..ed69fc2b9 100644 --- a/qt/aqt/importing.py +++ b/qt/aqt/importing.py @@ -19,6 +19,7 @@ from aqt import AnkiQt, gui_hooks from aqt.qt import * from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, getFile, @@ -122,7 +123,7 @@ class ImportDialog(QDialog): getOnlyText( tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE), self, - help="importing", + help=HelpPage.IMPORTING, ) or "\t" ) @@ -280,7 +281,7 @@ class ImportDialog(QDialog): QDialog.reject(self) def helpRequested(self): - openHelp("importing") + openHelp(HelpPage.IMPORTING) def importModeChanged(self, newImportMode): if newImportMode == 0: diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 0f4aa2dbc..8812a4cde 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -48,6 +48,7 @@ from aqt.taskman import TaskManager from aqt.theme import theme_manager from aqt.utils import ( TR, + HelpPage, askUser, checkInvalidFilename, disable_help_button, @@ -1090,7 +1091,7 @@ title="%s" %s>%s""" % ( openLink(aqt.appDonate) def onDocumentation(self): - openHelp("") + openHelp(HelpPage.INDEX) # Importing & exporting ########################################################################## diff --git a/qt/aqt/modelchooser.py b/qt/aqt/modelchooser.py index a4ab97eca..754d7b31a 100644 --- a/qt/aqt/modelchooser.py +++ b/qt/aqt/modelchooser.py @@ -5,7 +5,7 @@ from typing import Optional from aqt import AnkiQt, gui_hooks from aqt.qt import * -from aqt.utils import TR, shortcut, tr +from aqt.utils import TR, HelpPage, shortcut, tr class ModelChooser(QHBoxLayout): @@ -82,7 +82,7 @@ class ModelChooser(QHBoxLayout): names=nameFunc, accept=tr(TR.ACTIONS_CHOOSE), title=tr(TR.QT_MISC_CHOOSE_NOTE_TYPE), - help="getting-started?id=note-types", + help=HelpPage.NOTE_TYPE, current=current, parent=self.widget, buttons=[edit], diff --git a/qt/aqt/models.py b/qt/aqt/models.py index f4f49d8c0..1d238a496 100644 --- a/qt/aqt/models.py +++ b/qt/aqt/models.py @@ -15,6 +15,7 @@ from aqt import AnkiQt, gui_hooks from aqt.qt import * from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, getText, @@ -48,7 +49,7 @@ class Models(QDialog): self.form.setupUi(self) qconnect( self.form.buttonBox.helpRequested, - lambda: openHelp("editing?id=adding-a-note-type"), + lambda: openHelp(HelpPage.ADDING_A_NOTE_TYPE), ) self.models: List[pb.NoteTypeNameIDUseCount] = [] self.setupModels() @@ -181,7 +182,7 @@ class Models(QDialog): d.setWindowTitle( without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=nt["name"])) ) - qconnect(frm.buttonBox.helpRequested, lambda: openHelp("math?id=latex")) + qconnect(frm.buttonBox.helpRequested, lambda: openHelp(HelpPage.LATEX)) restoreGeom(d, "modelopts") gui_hooks.models_advanced_will_show(d) d.exec_() @@ -266,4 +267,4 @@ class AddModel(QDialog): QDialog.accept(self) def onHelp(self) -> None: - openHelp("editing?id=adding-a-note-type") + openHelp(HelpPage.ADDING_A_NOTE_TYPE) diff --git a/qt/aqt/preferences.py b/qt/aqt/preferences.py index 680c9b743..8ed2ac384 100644 --- a/qt/aqt/preferences.py +++ b/qt/aqt/preferences.py @@ -8,6 +8,7 @@ from aqt.profiles import RecordingDriver, VideoDriver from aqt.qt import * from aqt.utils import ( TR, + HelpPage, askUser, disable_help_button, openHelp, @@ -42,7 +43,9 @@ class Preferences(QDialog): disable_help_button(self) self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False) self.form.buttonBox.button(QDialogButtonBox.Close).setAutoDefault(False) - qconnect(self.form.buttonBox.helpRequested, lambda: openHelp("preferences")) + qconnect( + self.form.buttonBox.helpRequested, lambda: openHelp(HelpPage.PREFERENCES) + ) self.silentlyClose = True self.prefs = self.mw.col.backend.get_preferences() self.setupLang() diff --git a/qt/aqt/studydeck.py b/qt/aqt/studydeck.py index 3cf6d1e4c..106a38635 100644 --- a/qt/aqt/studydeck.py +++ b/qt/aqt/studydeck.py @@ -1,11 +1,14 @@ # Copyright: Ankitects Pty Ltd and contributors # -*- coding: utf-8 -*- # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + import aqt from aqt import gui_hooks from aqt.qt import * from aqt.utils import ( TR, + HelpPage, + HelpPageArgument, disable_help_button, getOnlyText, openHelp, @@ -24,7 +27,7 @@ class StudyDeck(QDialog): names=None, accept=None, title=None, - help="studying?id=keyboard-shortcuts", + help: HelpPageArgument = HelpPage.KEYBOARD_SHORTCUTS, current=None, cancel=True, parent=None, diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index 0030dc462..db0a85422 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -7,6 +7,7 @@ import os import re import subprocess import sys +from enum import Enum from typing import TYPE_CHECKING, Any, List, Optional, Union, cast import anki @@ -44,10 +45,40 @@ def tr(key: TRValue, **kwargs: Union[str, int, float]) -> str: return anki.lang.current_i18n.translate(key, **kwargs) -def openHelp(section): +class HelpPage(Enum): + NOTE_TYPE = "getting-started?id=note-types" + BROWSING = "browsing" + BROWSING_FIND_AND_REPLACE = "browsing?id=find-and-replace" + BROWSING_OTHER_MENU_ITEMS = "browsing?id=other-menu-items" + KEYBOARD_SHORTCUTS = "studying?id=keyboard-shortcuts" + EDITING = "editing" + ADDING_CARD_AND_NOTE = "editing?id=adding-cards-and-notes" + ADDING_A_NOTE_TYPE = "editing?id=adding-a-note-type" + LATEX = "math?id=latex" + PREFERENCES = "preferences" + INDEX = "" + TEMPLATES = "templates/intro" + FILTERED_DECK = "filtered-decks" + IMPORTING = "importing" + CUSTOMIZING_FIELDS = "editing?id=customizing-fields" + DECK_OPTIONS = "deck-options" + EDITING_FEATURES = "editing?id=features" + + +HelpPageArgument = Optional[Union[HelpPage, str]] +"""This type represents what can be used as argument expecting a specific help page. Anki code should use HelpPage as +argument. However, add-on may use string, and we want to accept this. + +""" + + +def openHelp(section: HelpPageArgument): link = aqt.appHelpSite if section: - link += section + if isinstance(section, HelpPage): + link += section.value + else: + link += section openLink(link) @@ -170,7 +201,14 @@ def showText( return diag, box -def askUser(text, parent=None, help="", defaultno=False, msgfunc=None, title="Anki"): +def askUser( + text, + parent=None, + help: HelpPageArgument = None, + defaultno=False, + msgfunc=None, + title="Anki", +): "Show a yes/no question. Return true if yes." if not parent: parent = aqt.mw.app.activeWindow() @@ -194,7 +232,9 @@ def askUser(text, parent=None, help="", defaultno=False, msgfunc=None, title="An class ButtonedDialog(QMessageBox): - def __init__(self, text, buttons, parent=None, help="", title="Anki"): + def __init__( + self, text, buttons, parent=None, help: HelpPageArgument = None, title="Anki" + ): QMessageBox.__init__(self, parent) self._buttons = [] self.setWindowTitle(title) @@ -221,7 +261,9 @@ class ButtonedDialog(QMessageBox): self.setDefaultButton(self._buttons[idx]) -def askUserDialog(text, buttons, parent=None, help="", title="Anki"): +def askUserDialog( + text, buttons, parent=None, help: HelpPageArgument = None, title="Anki" +): if not parent: parent = aqt.mw diag = ButtonedDialog(text, buttons, parent, help, title=title) @@ -233,7 +275,7 @@ class GetTextDialog(QDialog): self, parent, question, - help=None, + help: HelpPageArgument = None, edit=None, default="", title="Anki", @@ -279,7 +321,7 @@ class GetTextDialog(QDialog): def getText( prompt, parent=None, - help=None, + help: HelpPageArgument = None, edit=None, default="", title="Anki",