Implement search replacing via Ctrl+Shift combo

Also switch to using the new backend functions for concatenating and
negating searches.
This commit is contained in:
RumovZ 2021-01-06 18:32:29 +01:00
parent 6d6cd87b8f
commit 07499dd890
2 changed files with 15 additions and 13 deletions

View File

@ -20,7 +20,7 @@ from anki.consts import *
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
from anki.models import NoteType from anki.models import NoteType
from anki.notes import Note from anki.notes import Note
from anki.rsbackend import TR, DeckTreeNode, InvalidInput from anki.rsbackend import TR, DeckTreeNode, InvalidInput, pb
from anki.stats import CardStats from anki.stats import CardStats
from anki.utils import htmlToTextLine, ids2str, isMac, isWin from anki.utils import htmlToTextLine, ids2str, isMac, isWin
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, gui_hooks
@ -1219,17 +1219,19 @@ QTableView {{ gridline-color: {grid} }}
items.append(txt) items.append(txt)
txt = "" txt = ""
txt = " AND ".join(items) txt = " AND ".join(items)
# is there something to replace or append with? if self.mw.app.keyboardModifiers() & Qt.AltModifier:
if txt: txt = self.col.backend.negate_search(txt)
if self.mw.app.keyboardModifiers() & Qt.AltModifier: cur = str(self.form.searchEdit.lineEdit().text())
txt = "-" + txt if cur != self._searchPrompt:
# is there something to replace or append to? mods = self.mw.app.keyboardModifiers()
cur = str(self.form.searchEdit.lineEdit().text()) if mods & Qt.ControlModifier and mods & Qt.ShiftModifier:
if cur and cur != self._searchPrompt: txt = self.col.backend.replace_search_term(search=cur, replacement=txt)
if self.mw.app.keyboardModifiers() & Qt.ControlModifier: elif mods & Qt.ControlModifier:
txt = cur + " AND " + txt and_sep = pb.ConcatenateSearchesIn.Separator.AND
elif self.mw.app.keyboardModifiers() & Qt.ShiftModifier: txt = self.col.backend.concatenate_searches(sep=and_sep, searches=[cur, txt])
txt = cur + " OR " + txt elif mods & Qt.ShiftModifier:
or_sep = pb.ConcatenateSearchesIn.Separator.OR
txt = self.col.backend.concatenate_searches(sep=or_sep, searches=[cur, txt])
self.form.searchEdit.lineEdit().setText(txt) self.form.searchEdit.lineEdit().setText(txt)
self.onSearchActivated() self.onSearchActivated()

View File

@ -435,7 +435,7 @@ impl BackendService for Backend {
fn negate_search(&self, input: pb::String) -> Result<pb::String> { fn negate_search(&self, input: pb::String) -> Result<pb::String> {
Ok(negate_search(&input.val)?.into()) Ok(negate_search(&input.val)?.into())
} }
fn concatenate_searches(&self, input: pb::ConcatenateSearchesIn) -> Result<pb::String> { fn concatenate_searches(&self, input: pb::ConcatenateSearchesIn) -> Result<pb::String> {
Ok(concatenate_searches(input.sep, &input.searches)?.into()) Ok(concatenate_searches(input.sep, &input.searches)?.into())
} }