Merge pull request #944 from Arthur-Milchior/help

NF: HelpPage in an enum
This commit is contained in:
Damien Elmes 2021-01-26 11:33:39 +10:00 committed by GitHub
commit e324f06348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 95 additions and 31 deletions

View File

@ -16,6 +16,7 @@ from aqt.qt import *
from aqt.sound import av_player from aqt.sound import av_player
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
addCloseShortcut, addCloseShortcut,
askUser, askUser,
disable_help_button, disable_help_button,
@ -64,7 +65,7 @@ class AddCards(QDialog):
self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea) self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea)
def helpRequested(self): def helpRequested(self):
openHelp("editing?id=adding-cards-and-notes") openHelp(HelpPage.ADDING_CARD_AND_NOTE)
def setupButtons(self) -> None: def setupButtons(self) -> None:
bb = self.form.buttonBox bb = self.form.buttonBox
@ -172,7 +173,7 @@ class AddCards(QDialog):
problem = tr(TR.ADDING_THE_FIRST_FIELD_IS_EMPTY) problem = tr(TR.ADDING_THE_FIRST_FIELD_IS_EMPTY)
problem = gui_hooks.add_cards_will_add_note(problem, note) problem = gui_hooks.add_cards_will_add_note(problem, note)
if problem is not None: if problem is not None:
showWarning(problem, help="editing?id=adding-cards-and-notes") showWarning(problem, help=HelpPage.ADDING_CARD_AND_NOTE)
return None return None
if note.model()["type"] == MODEL_CLOZE: if note.model()["type"] == MODEL_CLOZE:
if not note.cloze_numbers_in_fields(): if not note.cloze_numbers_in_fields():

View File

@ -8,7 +8,7 @@ import time
from concurrent.futures import Future from concurrent.futures import Future
from dataclasses import dataclass from dataclasses import dataclass
from operator import itemgetter from operator import itemgetter
from typing import List, Sequence, Tuple, cast from typing import List, Optional, Sequence, Tuple, cast
import aqt import aqt
import aqt.forms import aqt.forms
@ -39,6 +39,7 @@ from aqt.sidebar import SidebarTreeView
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
MenuList, MenuList,
SubMenu, SubMenu,
askUser, askUser,
@ -1196,7 +1197,7 @@ where id in %s"""
return sf return sf
def onHelp(self): def onHelp(self):
openHelp("browsing") openHelp(HelpPage.BROWSING)
# Misc menu options # Misc menu options
###################################################################### ######################################################################
@ -1296,7 +1297,7 @@ where id in %s"""
current=current, current=current,
accept=tr(TR.BROWSING_MOVE_CARDS), accept=tr(TR.BROWSING_MOVE_CARDS),
title=tr(TR.BROWSING_CHANGE_DECK), title=tr(TR.BROWSING_CHANGE_DECK),
help="browsing", help=HelpPage.BROWSING,
parent=self, parent=self,
) )
if not ret.name: if not ret.name:
@ -1648,7 +1649,7 @@ where id in %s"""
self.mw.taskman.run_in_background(do_search, on_done) self.mw.taskman.run_in_background(do_search, on_done)
def onFindReplaceHelp(self): def onFindReplaceHelp(self):
openHelp("browsing?id=find-and-replace") openHelp(HelpPage.BROWSING_FIND_AND_REPLACE)
# Edit: finding dupes # Edit: finding dupes
###################################################################### ######################################################################
@ -2002,7 +2003,7 @@ class ChangeModel(QDialog):
QDialog.accept(self) QDialog.accept(self)
def onHelp(self): def onHelp(self):
openHelp("browsing?id=other-menu-items") openHelp(HelpPage.BROWSING_OTHER_MENU_ITEMS)
# Card Info Dialog # Card Info Dialog

View File

@ -20,6 +20,7 @@ from aqt.sound import av_player, play_clicked_audio
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
downArrow, downArrow,
@ -828,4 +829,4 @@ class CardLayout(QDialog):
self.mw = None self.mw = None
def onHelp(self): def onHelp(self):
openHelp("templates/intro") openHelp(HelpPage.TEMPLATES)

View File

@ -5,7 +5,7 @@ from typing import Any
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import TR, shortcut, tr from aqt.utils import TR, HelpPage, shortcut, tr
class DeckChooser(QHBoxLayout): class DeckChooser(QHBoxLayout):
@ -86,7 +86,7 @@ class DeckChooser(QHBoxLayout):
current=current, current=current,
accept=tr(TR.ACTIONS_CHOOSE), accept=tr(TR.ACTIONS_CHOOSE),
title=tr(TR.QT_MISC_CHOOSE_DECK), title=tr(TR.QT_MISC_CHOOSE_DECK),
help="editing", help=HelpPage.EDITING,
cancel=False, cancel=False,
parent=self.widget, parent=self.widget,
geomKey="selectDeck", geomKey="selectDeck",

View File

@ -13,6 +13,7 @@ from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
getOnlyText, getOnlyText,
@ -40,7 +41,9 @@ class DeckConf(QDialog):
self.setupCombos() self.setupCombos()
self.setupConfs() self.setupConfs()
self.setWindowModality(Qt.WindowModal) 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.confOpts.clicked, self.confOpts)
qconnect( qconnect(
self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults).clicked, self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults).clicked,

View File

@ -9,6 +9,7 @@ from anki.rsbackend import InvalidInput
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
openHelp, openHelp,
@ -36,7 +37,9 @@ class DeckConf(QDialog):
self.mw.checkpoint(tr(TR.ACTIONS_OPTIONS)) self.mw.checkpoint(tr(TR.ACTIONS_OPTIONS))
disable_help_button(self) disable_help_button(self)
self.setWindowModality(Qt.WindowModal) 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( self.setWindowTitle(
without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=self.deck["name"])) without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=self.deck["name"]))
) )

View File

@ -32,6 +32,7 @@ from aqt.sound import av_player
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
disable_help_button, disable_help_button,
getFile, getFile,
openHelp, openHelp,
@ -572,7 +573,9 @@ class Editor:
form.setupUi(d) form.setupUi(d)
restoreGeom(d, "htmlEditor") restoreGeom(d, "htmlEditor")
disable_help_button(d) 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]) form.textEdit.setPlainText(self.note.fields[field])
d.show() d.show()
form.textEdit.moveCursor(QTextCursor.End) form.textEdit.moveCursor(QTextCursor.End)

View File

@ -11,6 +11,7 @@ from aqt.qt import *
from aqt.schema_change_tracker import ChangeTracker from aqt.schema_change_tracker import ChangeTracker
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
getOnlyText, getOnlyText,
@ -245,4 +246,4 @@ class FieldDialog(QDialog):
self.mw.taskman.with_progress(save, on_done, self) self.mw.taskman.with_progress(save, on_done, self)
def onHelp(self): def onHelp(self):
openHelp("editing?id=customizing-fields") openHelp(HelpPage.CUSTOMIZING_FIELDS)

View File

@ -19,6 +19,7 @@ from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
getFile, getFile,
@ -122,7 +123,7 @@ class ImportDialog(QDialog):
getOnlyText( getOnlyText(
tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE), tr(TR.IMPORTING_BY_DEFAULT_ANKI_WILL_DETECT_THE),
self, self,
help="importing", help=HelpPage.IMPORTING,
) )
or "\t" or "\t"
) )
@ -280,7 +281,7 @@ class ImportDialog(QDialog):
QDialog.reject(self) QDialog.reject(self)
def helpRequested(self): def helpRequested(self):
openHelp("importing") openHelp(HelpPage.IMPORTING)
def importModeChanged(self, newImportMode): def importModeChanged(self, newImportMode):
if newImportMode == 0: if newImportMode == 0:

View File

@ -48,6 +48,7 @@ from aqt.taskman import TaskManager
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
checkInvalidFilename, checkInvalidFilename,
disable_help_button, disable_help_button,
@ -1090,7 +1091,7 @@ title="%s" %s>%s</button>""" % (
openLink(aqt.appDonate) openLink(aqt.appDonate)
def onDocumentation(self): def onDocumentation(self):
openHelp("") openHelp(HelpPage.INDEX)
# Importing & exporting # Importing & exporting
########################################################################## ##########################################################################

View File

@ -5,7 +5,7 @@ from typing import Optional
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import TR, shortcut, tr from aqt.utils import TR, HelpPage, shortcut, tr
class ModelChooser(QHBoxLayout): class ModelChooser(QHBoxLayout):
@ -82,7 +82,7 @@ class ModelChooser(QHBoxLayout):
names=nameFunc, names=nameFunc,
accept=tr(TR.ACTIONS_CHOOSE), accept=tr(TR.ACTIONS_CHOOSE),
title=tr(TR.QT_MISC_CHOOSE_NOTE_TYPE), title=tr(TR.QT_MISC_CHOOSE_NOTE_TYPE),
help="getting-started?id=note-types", help=HelpPage.NOTE_TYPE,
current=current, current=current,
parent=self.widget, parent=self.widget,
buttons=[edit], buttons=[edit],

View File

@ -15,6 +15,7 @@ from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
getText, getText,
@ -48,7 +49,7 @@ class Models(QDialog):
self.form.setupUi(self) self.form.setupUi(self)
qconnect( qconnect(
self.form.buttonBox.helpRequested, 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.models: List[pb.NoteTypeNameIDUseCount] = []
self.setupModels() self.setupModels()
@ -181,7 +182,7 @@ class Models(QDialog):
d.setWindowTitle( d.setWindowTitle(
without_unicode_isolation(tr(TR.ACTIONS_OPTIONS_FOR, val=nt["name"])) 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") restoreGeom(d, "modelopts")
gui_hooks.models_advanced_will_show(d) gui_hooks.models_advanced_will_show(d)
d.exec_() d.exec_()
@ -266,4 +267,4 @@ class AddModel(QDialog):
QDialog.accept(self) QDialog.accept(self)
def onHelp(self) -> None: def onHelp(self) -> None:
openHelp("editing?id=adding-a-note-type") openHelp(HelpPage.ADDING_A_NOTE_TYPE)

View File

@ -8,6 +8,7 @@ from aqt.profiles import RecordingDriver, VideoDriver
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
askUser, askUser,
disable_help_button, disable_help_button,
openHelp, openHelp,
@ -42,7 +43,9 @@ class Preferences(QDialog):
disable_help_button(self) disable_help_button(self)
self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False) self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False)
self.form.buttonBox.button(QDialogButtonBox.Close).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.silentlyClose = True
self.prefs = self.mw.col.backend.get_preferences() self.prefs = self.mw.col.backend.get_preferences()
self.setupLang() self.setupLang()

View File

@ -1,11 +1,14 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import aqt import aqt
from aqt import gui_hooks from aqt import gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import ( from aqt.utils import (
TR, TR,
HelpPage,
HelpPageArgument,
disable_help_button, disable_help_button,
getOnlyText, getOnlyText,
openHelp, openHelp,
@ -24,7 +27,7 @@ class StudyDeck(QDialog):
names=None, names=None,
accept=None, accept=None,
title=None, title=None,
help="studying?id=keyboard-shortcuts", help: HelpPageArgument = HelpPage.KEYBOARD_SHORTCUTS,
current=None, current=None,
cancel=True, cancel=True,
parent=None, parent=None,

View File

@ -7,6 +7,7 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
from enum import Enum
from typing import TYPE_CHECKING, Any, List, Optional, Union, cast from typing import TYPE_CHECKING, Any, List, Optional, Union, cast
from markdown import markdown from markdown import markdown
@ -46,10 +47,40 @@ def tr(key: TRValue, **kwargs: Union[str, int, float]) -> str:
return anki.lang.current_i18n.translate(key, **kwargs) 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 link = aqt.appHelpSite
if section: if section:
link += section if isinstance(section, HelpPage):
link += section.value
else:
link += section
openLink(link) openLink(link)
@ -180,7 +211,14 @@ def showText(
return diag, box 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." "Show a yes/no question. Return true if yes."
if not parent: if not parent:
parent = aqt.mw.app.activeWindow() parent = aqt.mw.app.activeWindow()
@ -204,7 +242,9 @@ def askUser(text, parent=None, help="", defaultno=False, msgfunc=None, title="An
class ButtonedDialog(QMessageBox): 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) QMessageBox.__init__(self, parent)
self._buttons = [] self._buttons = []
self.setWindowTitle(title) self.setWindowTitle(title)
@ -231,7 +271,9 @@ class ButtonedDialog(QMessageBox):
self.setDefaultButton(self._buttons[idx]) 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: if not parent:
parent = aqt.mw parent = aqt.mw
diag = ButtonedDialog(text, buttons, parent, help, title=title) diag = ButtonedDialog(text, buttons, parent, help, title=title)
@ -243,7 +285,7 @@ class GetTextDialog(QDialog):
self, self,
parent, parent,
question, question,
help=None, help: HelpPageArgument = None,
edit=None, edit=None,
default="", default="",
title="Anki", title="Anki",
@ -289,7 +331,7 @@ class GetTextDialog(QDialog):
def getText( def getText(
prompt, prompt,
parent=None, parent=None,
help=None, help: HelpPageArgument = None,
edit=None, edit=None,
default="", default="",
title="Anki", title="Anki",