mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Make browser accept optional args and add reopen
That way, the caller doesn't have to hold a reference to the browser and explicitly call it again, if it wants to search for something specific. Also, if the browser was closed and opened for a single-card-search, it now won't perform a redundant current-deck-search first.
This commit is contained in:
parent
d2024d1d1e
commit
6de6e5f339
6 changed files with 55 additions and 31 deletions
|
@ -162,7 +162,7 @@ class AddCards(QDialog):
|
|||
m.exec_(self.historyButton.mapToGlobal(QPoint(0, 0)))
|
||||
|
||||
def editHistory(self, nid):
|
||||
self.mw.browser_search(SearchTerm(nid=nid))
|
||||
aqt.dialogs.open("Browser", self.mw, search=(SearchTerm(nid=nid),))
|
||||
|
||||
def addNote(self, note) -> Optional[Note]:
|
||||
note.model()["did"] = self.deckChooser.selectedId()
|
||||
|
|
|
@ -437,7 +437,12 @@ class Browser(QMainWindow):
|
|||
col: Collection
|
||||
editor: Optional[Editor]
|
||||
|
||||
def __init__(self, mw: AnkiQt) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
mw: AnkiQt,
|
||||
card: Optional[Card] = None,
|
||||
search: Optional[Tuple[Union[str, SearchTerm]]] = None,
|
||||
) -> None:
|
||||
QMainWindow.__init__(self, None, Qt.Window)
|
||||
self.mw = mw
|
||||
self.col = self.mw.col
|
||||
|
@ -461,7 +466,7 @@ class Browser(QMainWindow):
|
|||
self.setupEditor()
|
||||
self.updateFont()
|
||||
self.onUndoState(self.mw.form.actionUndo.isEnabled())
|
||||
self.setupSearch()
|
||||
self.setupSearch(card, search)
|
||||
gui_hooks.browser_will_show(self)
|
||||
self.show()
|
||||
|
||||
|
@ -600,17 +605,41 @@ class Browser(QMainWindow):
|
|||
]
|
||||
self.columns.sort(key=itemgetter(1))
|
||||
|
||||
def reopen(
|
||||
self,
|
||||
_mw: AnkiQt,
|
||||
card: Optional[Card] = None,
|
||||
search: Optional[Tuple[Union[str, SearchTerm]]] = None,
|
||||
) -> None:
|
||||
if search is not None:
|
||||
self.search_for_terms(*search)
|
||||
self.form.searchEdit.setFocus()
|
||||
elif card:
|
||||
self.show_single_card(card)
|
||||
self.form.searchEdit.setFocus()
|
||||
|
||||
# Searching
|
||||
######################################################################
|
||||
|
||||
def setupSearch(self):
|
||||
def setupSearch(
|
||||
self,
|
||||
card: Optional[Card] = None,
|
||||
search: Optional[Tuple[Union[str, SearchTerm]]] = None,
|
||||
):
|
||||
qconnect(self.form.searchEdit.lineEdit().returnPressed, self.onSearchActivated)
|
||||
self.form.searchEdit.setCompleter(None)
|
||||
self.form.searchEdit.lineEdit().setPlaceholderText(
|
||||
tr(TR.BROWSING_SEARCH_BAR_HINT)
|
||||
)
|
||||
self.form.searchEdit.addItems(self.mw.pm.profile["searchHistory"])
|
||||
self.search_for(self.col.build_search_string(SearchTerm(deck="current")), "")
|
||||
if search is not None:
|
||||
self.search_for_terms(*search)
|
||||
elif card:
|
||||
self.show_single_card(card)
|
||||
else:
|
||||
self.search_for(
|
||||
self.col.build_search_string(SearchTerm(deck="current")), ""
|
||||
)
|
||||
self.form.searchEdit.setFocus()
|
||||
|
||||
# search triggered by user
|
||||
|
@ -669,15 +698,17 @@ class Browser(QMainWindow):
|
|||
)
|
||||
return selected
|
||||
|
||||
def show_single_card(self, card: Optional[Card]):
|
||||
"""Try to search for the according note and select the given card."""
|
||||
def search_for_terms(self, *search_terms: Union[str, SearchTerm]):
|
||||
search = self.col.build_search_string(*search_terms)
|
||||
self.form.searchEdit.setEditText(search)
|
||||
self.onSearchActivated()
|
||||
|
||||
nid: Optional[int] = card and card.nid or 0
|
||||
if nid:
|
||||
def show_single_card(self, card: Card):
|
||||
if card.nid:
|
||||
|
||||
def on_show_single_card():
|
||||
self.card = card
|
||||
search = self.col.build_search_string(SearchTerm(nid=nid))
|
||||
search = self.col.build_search_string(SearchTerm(nid=card.nid))
|
||||
search = gui_hooks.default_search(search, card)
|
||||
self.search_for(search, "")
|
||||
self.focusCid(card.id)
|
||||
|
|
|
@ -538,11 +538,16 @@ class Editor:
|
|||
self.web.eval("setBackgrounds(%s);" % json.dumps(cols))
|
||||
|
||||
def showDupes(self):
|
||||
self.mw.browser_search(
|
||||
SearchTerm(
|
||||
dupe=SearchTerm.Dupe(
|
||||
notetype_id=self.note.model()["id"], first_field=self.note.fields[0]
|
||||
)
|
||||
aqt.dialogs.open(
|
||||
"Browser",
|
||||
self.mw,
|
||||
search=(
|
||||
SearchTerm(
|
||||
dupe=SearchTerm.Dupe(
|
||||
notetype_id=self.note.model()["id"],
|
||||
first_field=self.note.fields[0],
|
||||
)
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ class EmptyCardsDialog(QDialog):
|
|||
self._delete_button.clicked.connect(self._on_delete)
|
||||
|
||||
def _on_note_link_clicked(self, link):
|
||||
self.mw.browser_search(link)
|
||||
aqt.dialogs.open("Browser", self.mw, search=(link,))
|
||||
|
||||
def _on_delete(self):
|
||||
self.mw.progress.start()
|
||||
|
|
|
@ -27,7 +27,7 @@ import aqt.toolbar
|
|||
import aqt.webview
|
||||
from anki import hooks
|
||||
from anki._backend import RustBackend as _RustBackend
|
||||
from anki.collection import Collection, SearchTerm
|
||||
from anki.collection import Collection
|
||||
from anki.decks import Deck
|
||||
from anki.hooks import runHook
|
||||
from anki.sound import AVTag, SoundOrVideoTag
|
||||
|
@ -1044,8 +1044,7 @@ title="%s" %s>%s</button>""" % (
|
|||
aqt.dialogs.open("AddCards", self)
|
||||
|
||||
def onBrowse(self) -> None:
|
||||
browser = aqt.dialogs.open("Browser", self)
|
||||
browser.show_single_card(self.reviewer.card)
|
||||
aqt.dialogs.open("Browser", self, card=self.reviewer.card)
|
||||
|
||||
def onEditCurrent(self):
|
||||
aqt.dialogs.open("EditCurrent", self)
|
||||
|
@ -1598,14 +1597,3 @@ title="%s" %s>%s</button>""" % (
|
|||
|
||||
def serverURL(self) -> str:
|
||||
return "http://127.0.0.1:%d/" % self.mediaServer.getPort()
|
||||
|
||||
# Helpers for all windows
|
||||
##########################################################################
|
||||
|
||||
def browser_search(self, *terms: Union[str, SearchTerm]) -> None:
|
||||
"""Wrapper for col.build_search_string() to look up the result in the browser."""
|
||||
|
||||
search = self.col.build_search_string(*terms)
|
||||
browser = aqt.dialogs.open("Browser", self)
|
||||
browser.form.searchEdit.lineEdit().setText(search)
|
||||
browser.onSearchActivated()
|
||||
|
|
|
@ -148,7 +148,7 @@ class MediaChecker:
|
|||
|
||||
if out is not None:
|
||||
nid, err = out
|
||||
self.mw.browser_search(SearchTerm(nid=nid))
|
||||
aqt.dialogs.open("Browser", self.mw, search=(SearchTerm(nid=nid),))
|
||||
showText(err, type="html")
|
||||
else:
|
||||
tooltip(tr(TR.MEDIA_CHECK_ALL_LATEX_RENDERED))
|
||||
|
|
Loading…
Reference in a new issue