move saved search code into sidebar.py, and remove from Filter button

This commit is contained in:
Damien Elmes 2021-01-29 23:05:30 +10:00
parent 132bb5ff36
commit 79239c89b7
2 changed files with 56 additions and 90 deletions

View File

@ -44,7 +44,6 @@ from aqt.utils import (
SubMenu,
askUser,
disable_help_button,
getOnlyText,
getTag,
openHelp,
qtMenuShortcutWorkaround,
@ -970,9 +969,6 @@ QTableView {{ gridline-color: {grid} }}
toggle_sidebar.setCheckable(True)
toggle_sidebar.setChecked(self.sidebarDockWidget.isVisible())
ml.addChild(toggle_sidebar)
ml.addSeparator()
ml.addChild(self._savedSearches())
ml.popupOver(self.form.filter)
@ -1060,81 +1056,6 @@ QTableView {{ gridline-color: {grid} }}
)
return subm
# Favourites
######################################################################
def _savedSearches(self):
ml = MenuList()
# make sure exists
if "savedFilters" not in self.col.conf:
self.col.set_config("savedFilters", {})
ml.addSeparator()
if self._currentFilterIsSaved():
ml.addItem(tr(TR.BROWSING_REMOVE_CURRENT_FILTER), self._onRemoveFilter)
else:
ml.addItem(tr(TR.BROWSING_SAVE_CURRENT_FILTER), self._onSaveFilter)
return ml
def _onSaveFilter(self) -> None:
try:
filt = self.col.backend.normalize_search(
self.form.searchEdit.lineEdit().text()
)
except InvalidInput as e:
show_invalid_search_error(e)
else:
name = getOnlyText(tr(TR.BROWSING_PLEASE_GIVE_YOUR_FILTER_A_NAME))
if not name:
return
conf = self.col.get_config("savedFilters")
conf[name] = filt
self.col.set_config("savedFilters", conf)
self.sidebar.refresh()
def _onRemoveFilter(self) -> None:
self.removeFilter(self._currentFilterIsSaved())
def removeFilter(self, name: str) -> None:
if not askUser(tr(TR.BROWSING_REMOVE_FROM_YOUR_SAVED_SEARCHES, val=name)):
return
conf = self.col.get_config("savedFilters")
del conf[name]
self.col.set_config("savedFilters", conf)
self.sidebar.refresh()
def renameFilter(self, old: str) -> None:
conf = self.col.get_config("savedFilters")
try:
filt = conf[old]
except KeyError:
return
new = getOnlyText(tr(TR.ACTIONS_NEW_NAME), default=old)
if new == old or not new:
return
conf[new] = filt
del conf[old]
self.col.set_config("savedFilters", conf)
self.sidebar.refresh()
# returns name if found
def _currentFilterIsSaved(self) -> Optional[str]:
filt = self.form.searchEdit.lineEdit().text()
try:
filt = self.col.backend.normalize_search(filt)
except InvalidInput:
pass
for k, v in self.col.get_config("savedFilters").items():
try:
v = self.col.backend.normalize_search(v)
except InvalidInput:
pass
if filt == v:
return k
return None
# Info
######################################################################

View File

@ -11,13 +11,27 @@ from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Sequence, Tupl
import aqt
from anki.collection import ConfigBoolKey
from anki.errors import DeckRenameError
from anki.rsbackend import DeckTreeNode, FilterToSearchIn, NamedFilter, TagTreeNode
from anki.rsbackend import (
DeckTreeNode,
FilterToSearchIn,
InvalidInput,
NamedFilter,
TagTreeNode,
)
from aqt import gui_hooks
from aqt.main import ResetReason
from aqt.models import Models
from aqt.qt import *
from aqt.theme import theme_manager
from aqt.utils import TR, getOnlyText, showInfo, showWarning, tr
from aqt.utils import (
TR,
askUser,
getOnlyText,
show_invalid_search_error,
showInfo,
showWarning,
tr,
)
if TYPE_CHECKING:
from anki.collection import ConfigBoolKeyValue, TRValue
@ -231,8 +245,8 @@ class SidebarTreeView(QTreeView):
(tr(TR.ACTIONS_DELETE), self.remove_tag),
),
SidebarItemType.SAVED_SEARCH: (
(tr(TR.ACTIONS_RENAME), self.rename_filter),
(tr(TR.ACTIONS_DELETE), self.remove_filter),
(tr(TR.ACTIONS_RENAME), self.rename_saved_search),
(tr(TR.ACTIONS_DELETE), self.remove_saved_search),
),
SidebarItemType.NOTETYPE: ((tr(TR.ACTIONS_MANAGE), self.manage_notetype),),
SidebarItemType.SAVED_SEARCH_ROOT: (
@ -700,16 +714,47 @@ class SidebarTreeView(QTreeView):
self.browser.model.beginReset()
self.mw.taskman.run_in_background(do_delete, on_done)
def remove_filter(self, item: "aqt.browser.SidebarItem") -> None:
self.browser.removeFilter(item.name)
def remove_saved_search(self, item: "aqt.browser.SidebarItem") -> None:
name = item.name
if not askUser(tr(TR.BROWSING_REMOVE_FROM_YOUR_SAVED_SEARCHES, val=name)):
return
conf = self.col.get_config("savedFilters")
del conf[name]
self.col.set_config("savedFilters", conf)
self.refresh()
def rename_filter(self, item: "aqt.browser.SidebarItem") -> None:
self.browser.renameFilter(item.name)
def rename_saved_search(self, item: "aqt.browser.SidebarItem") -> None:
old = item.name
conf = self.col.get_config("savedFilters")
try:
filt = conf[old]
except KeyError:
return
new = getOnlyText(tr(TR.ACTIONS_NEW_NAME), default=old)
if new == old or not new:
return
conf[new] = filt
del conf[old]
self.col.set_config("savedFilters", conf)
self.refresh()
def save_current_search(self, _item=None) -> None:
try:
filt = self.col.backend.normalize_search(
self.browser.form.searchEdit.lineEdit().text()
)
except InvalidInput as e:
show_invalid_search_error(e)
else:
name = getOnlyText(tr(TR.BROWSING_PLEASE_GIVE_YOUR_FILTER_A_NAME))
if not name:
return
conf = self.col.get_config("savedFilters")
conf[name] = filt
self.col.set_config("savedFilters", conf)
self.refresh()
def manage_notetype(self, item: "aqt.browser.SidebarItem") -> None:
Models(
self.mw, parent=self.browser, fromMain=True, selected_notetype_id=item.id
)
def save_current_search(self, _item=None) -> None:
self.browser._onSaveFilter()