Rework search initialisation

- Remove _searchPrompt.
- Add placeholder prompt.
- Move search for current card from browser to caller. (Thus, support
  current card search even with opened browser.)
This commit is contained in:
RumovZ 2021-01-29 21:07:42 +01:00
parent c299e271e8
commit 671c6a7b3e
3 changed files with 33 additions and 36 deletions

View File

@ -83,6 +83,7 @@ browsing-reposition = Reposition...
browsing-reposition-new-cards = Reposition New Cards browsing-reposition-new-cards = Reposition New Cards
browsing-reschedule = Reschedule browsing-reschedule = Reschedule
browsing-save-current-filter = Save Current Filter... browsing-save-current-filter = Save Current Filter...
browsing-search-bar-hint = Type here and press Enter to search. Leave empty to show whole collection.
browsing-search-in = Search in: browsing-search-in = Search in:
browsing-search-within-formatting-slow = Search within formatting (slow) browsing-search-within-formatting-slow = Search within formatting (slow)
browsing-shift-position-of-existing-cards = Shift position of existing cards browsing-shift-position-of-existing-cards = Shift position of existing cards

View File

@ -611,15 +611,14 @@ class Browser(QMainWindow):
qconnect(self.form.searchButton.clicked, self.onSearchActivated) qconnect(self.form.searchButton.clicked, self.onSearchActivated)
qconnect(self.form.searchEdit.lineEdit().returnPressed, self.onSearchActivated) qconnect(self.form.searchEdit.lineEdit().returnPressed, self.onSearchActivated)
self.form.searchEdit.setCompleter(None) self.form.searchEdit.setCompleter(None)
self._searchPrompt = tr(TR.BROWSING_TYPE_HERE_TO_SEARCH) self.form.searchEdit.lineEdit().setPlaceholderText(
self._searchPromptFilter = self.col.build_search_string( tr(TR.BROWSING_SEARCH_BAR_HINT)
SearchTerm(current_deck=True)
) )
self.form.searchEdit.addItems( self.form.searchEdit.addItems(
[self._searchPrompt] + self.mw.pm.profile["searchHistory"] [self.col.build_search_string(SearchTerm(current_deck=True))]
+ self.mw.pm.profile["searchHistory"]
) )
self.search_for("is:current", self._searchPrompt) self._onRowChanged(None, None)
# then replace text for easily showing the deck
self.form.searchEdit.lineEdit().selectAll() self.form.searchEdit.lineEdit().selectAll()
self.form.searchEdit.setFocus() self.form.searchEdit.setFocus()
@ -629,15 +628,12 @@ class Browser(QMainWindow):
def _onSearchActivated(self): def _onSearchActivated(self):
# grab search text and normalize # grab search text and normalize
prompt = self.form.searchEdit.lineEdit().text() text = self.form.searchEdit.lineEdit().text()
self.update_history(text)
# convert guide text before we save history
txt = self._searchPromptFilter if prompt == self._searchPrompt else prompt
self.update_history(txt)
# keep track of search string so that we reuse identical search when # keep track of search string so that we reuse identical search when
# refreshing, rather than whatever is currently in the search field # refreshing, rather than whatever is currently in the search field
self.search_for(txt) self.search_for(text)
def update_history(self, search: str) -> None: def update_history(self, search: str) -> None:
sh = self.mw.pm.profile["searchHistory"] sh = self.mw.pm.profile["searchHistory"]
@ -649,25 +645,14 @@ class Browser(QMainWindow):
self.form.searchEdit.addItems(sh) self.form.searchEdit.addItems(sh)
self.mw.pm.profile["searchHistory"] = sh self.mw.pm.profile["searchHistory"] = sh
def search_for(self, search: str, prompt: Optional[str] = None) -> None: def search_for(self, search: str) -> None:
self._lastSearchTxt = search self._lastSearchTxt = search
self.form.searchEdit.lineEdit().setText(prompt or search) self.form.searchEdit.lineEdit().setText(search)
self.search() self.search()
# search triggered programmatically. caller must have saved note first. # search triggered programmatically. caller must have saved note first.
def search(self) -> None: def search(self) -> None:
if "is:current" in self._lastSearchTxt: self.model.search(self._lastSearchTxt)
# show current card if there is one
c = self.card = self.mw.reviewer.card
nid = c and c.nid or 0
if nid:
search = self.col.build_search_string(nid_search_term([nid]))
search = gui_hooks.default_search(search, c)
self.model.search(search)
self.focusCid(c.id)
else:
self.model.search(self._lastSearchTxt)
if not self.model.cards: if not self.model.cards:
# no row change will fire # no row change will fire
self._onRowChanged(None, None) self._onRowChanged(None, None)
@ -688,6 +673,17 @@ class Browser(QMainWindow):
) )
return selected return selected
def show_single_card(self, card: Optional[Card]) -> None:
nid = card and card.nid
if nid:
self.card = card
search = self.col.build_search_string(nid_search_term([nid]))
search = gui_hooks.default_search(search, card)
self.form.searchEdit.lineEdit().setText(search)
self.onSearchActivated()
self.form.tableView.clearSelection()
self.focusCid(card.id)
def onReset(self): def onReset(self):
self.sidebar.refresh() self.sidebar.refresh()
self.editor.setNote(None) self.editor.setNote(None)
@ -954,20 +950,19 @@ QTableView {{ gridline-color: {grid} }}
ml.popupOver(self.form.filter) ml.popupOver(self.form.filter)
def update_search(self, *terms: Union[str, SearchTerm]): def update_search(self, *terms: Union[str, SearchTerm]):
"Modify the current search string based on modified keys, then refresh." """Modify the current search string based on modified keys, then refresh."""
try: try:
search = self.col.build_search_string(*terms) search = self.col.build_search_string(*terms)
mods = self.mw.app.keyboardModifiers() mods = self.mw.app.keyboardModifiers()
if mods & Qt.AltModifier: if mods & Qt.AltModifier:
search = self.col.build_search_string(search, negate=True) search = self.col.build_search_string(search, negate=True)
cur = str(self.form.searchEdit.lineEdit().text()) cur = str(self.form.searchEdit.lineEdit().text())
if cur != self._searchPrompt: if mods & Qt.ControlModifier and mods & Qt.ShiftModifier:
if mods & Qt.ControlModifier and mods & Qt.ShiftModifier: search = self.col.replace_search_term(cur, search)
search = self.col.replace_search_term(cur, search) elif mods & Qt.ControlModifier:
elif mods & Qt.ControlModifier: search = self.col.build_search_string(cur, search)
search = self.col.build_search_string(cur, search) elif mods & Qt.ShiftModifier:
elif mods & Qt.ShiftModifier: search = self.col.build_search_string(cur, search, match_any=True)
search = self.col.build_search_string(cur, search, match_any=True)
except InvalidInput as e: except InvalidInput as e:
show_invalid_search_error(e) show_invalid_search_error(e)
else: else:
@ -1038,7 +1033,7 @@ QTableView {{ gridline-color: {grid} }}
if self._currentFilterIsSaved(): if self._currentFilterIsSaved():
ml.addItem(tr(TR.BROWSING_REMOVE_CURRENT_FILTER), self._onRemoveFilter) ml.addItem(tr(TR.BROWSING_REMOVE_CURRENT_FILTER), self._onRemoveFilter)
elif self._searchPrompt != self.form.searchEdit.lineEdit().text(): else:
ml.addItem(tr(TR.BROWSING_SAVE_CURRENT_FILTER), self._onSaveFilter) ml.addItem(tr(TR.BROWSING_SAVE_CURRENT_FILTER), self._onSaveFilter)
return ml return ml

View File

@ -1045,7 +1045,8 @@ title="%s" %s>%s</button>""" % (
aqt.dialogs.open("AddCards", self) aqt.dialogs.open("AddCards", self)
def onBrowse(self) -> None: def onBrowse(self) -> None:
aqt.dialogs.open("Browser", self) browser = aqt.dialogs.open("Browser", self)
browser.show_single_card(self.reviewer.card)
def onEditCurrent(self): def onEditCurrent(self):
aqt.dialogs.open("EditCurrent", self) aqt.dialogs.open("EditCurrent", self)