anki/qt/aqt/taglimit.py

119 lines
4.4 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/copyleft/agpl.html
from typing import Optional
import aqt
from anki.lang import with_collapsed_whitespace
from aqt.customstudy import CustomStudy
from aqt.main import AnkiQt
from aqt.qt import *
from aqt.utils import disable_help_button, restoreGeom, saveGeom, showWarning, tr
2019-12-20 10:19:03 +01:00
class TagLimit(QDialog):
def __init__(self, mw: AnkiQt, parent: CustomStudy) -> None:
QDialog.__init__(self, parent, Qt.WindowType.Window)
self.tags: str = ""
self.tags_list: list[str] = []
self.mw = mw
self.parent_: Optional[CustomStudy] = parent
self.deck = self.parent_.deck
self.dialog = aqt.forms.taglimit.Ui_Dialog()
self.dialog.setupUi(self)
disable_help_button(self)
2019-12-23 01:34:10 +01:00
s = QShortcut(
QKeySequence("ctrl+d"),
self.dialog.activeList,
context=Qt.ShortcutContext.WidgetShortcut,
2019-12-23 01:34:10 +01:00
)
qconnect(s.activated, self.dialog.activeList.clearSelection)
2019-12-23 01:34:10 +01:00
s = QShortcut(
QKeySequence("ctrl+d"),
self.dialog.inactiveList,
context=Qt.ShortcutContext.WidgetShortcut,
2019-12-23 01:34:10 +01:00
)
qconnect(s.activated, self.dialog.inactiveList.clearSelection)
self.rebuildTagList()
restoreGeom(self, "tagLimit")
self.exec()
def rebuildTagList(self) -> None:
PEP8 for rest of pylib (#1451) * PEP8 dbproxy.py * PEP8 errors.py * PEP8 httpclient.py * PEP8 lang.py * PEP8 latex.py * Add decorator to deprectate key words * Make replacement for deprecated attribute optional * Use new helper `_print_replacement_warning()` * PEP8 media.py * PEP8 rsbackend.py * PEP8 sound.py * PEP8 stdmodels.py * PEP8 storage.py * PEP8 sync.py * PEP8 tags.py * PEP8 template.py * PEP8 types.py * Fix DeprecatedNamesMixinForModule The class methods need to be overridden with instance methods, so every module has its own dicts. * Use `# pylint: disable=invalid-name` instead of id * PEP8 utils.py * Only decorate `__getattr__` with `@no_type_check` * Fix mypy issue with snakecase Importing it from `anki._vendor` raises attribute errors. * Format * Remove inheritance of DeprecatedNamesMixin There's almost no shared code now and overriding classmethods with instance methods raises mypy issues. * Fix traceback frames of deprecation warnings * remove fn/TimedLog (dae) Neither Anki nor add-ons appear to have been using it * fix some issues with stringcase use (dae) - the wheel was depending on the PyPI version instead of our vendored version - _vendor:stringcase should not have been listed in the anki py_library. We already include the sources in py_srcs, and need to refer to them directly. By listing _vendor:stringcase as well, we were making a top-level stringcase library available, which would have only worked for distributing because the wheel definition was also incorrect. - mypy errors are what caused me to mistakenly add the above - they were because the type: ignore at the top of stringcase.py was causing mypy to completely ignore the file, so it was not aware of any attributes it contained.
2021-10-25 06:50:13 +02:00
usertags = self.mw.col.tags.by_deck(self.deck["id"], True)
yes = self.deck.get("activeTags", [])
no = self.deck.get("inactiveTags", [])
yesHash = {}
noHash = {}
for y in yes:
yesHash[y] = True
for n in no:
noHash[n] = True
groupedTags = []
usertags.sort()
groupedTags.append(usertags)
self.tags_list = []
for tags in groupedTags:
for t in tags:
self.tags_list.append(t)
item = QListWidgetItem(t.replace("_", " "))
self.dialog.activeList.addItem(item)
if t in yesHash:
mode = QItemSelectionModel.SelectionFlag.Select
self.dialog.activeCheck.setChecked(True)
else:
mode = QItemSelectionModel.SelectionFlag.Deselect
idx = self.dialog.activeList.indexFromItem(item)
self.dialog.activeList.selectionModel().select(idx, mode)
# inactive
item = QListWidgetItem(t.replace("_", " "))
self.dialog.inactiveList.addItem(item)
if t in noHash:
mode = QItemSelectionModel.SelectionFlag.Select
else:
mode = QItemSelectionModel.SelectionFlag.Deselect
idx = self.dialog.inactiveList.indexFromItem(item)
self.dialog.inactiveList.selectionModel().select(idx, mode)
2021-02-01 14:28:21 +01:00
def reject(self) -> None:
self.tags = ""
QDialog.reject(self)
def accept(self) -> None:
# gather yes/no tags
yes = []
no = []
for c in range(self.dialog.activeList.count()):
# active
if self.dialog.activeCheck.isChecked():
item = self.dialog.activeList.item(c)
idx = self.dialog.activeList.indexFromItem(item)
if self.dialog.activeList.selectionModel().isSelected(idx):
yes.append(self.tags_list[c])
# inactive
item = self.dialog.inactiveList.item(c)
idx = self.dialog.inactiveList.indexFromItem(item)
if self.dialog.inactiveList.selectionModel().isSelected(idx):
no.append(self.tags_list[c])
if (len(yes) + len(no)) > 100:
showWarning(with_collapsed_whitespace(tr.errors_100_tags_max()))
return
self.hide()
# save in the deck for future invocations
2019-12-23 01:34:10 +01:00
self.deck["activeTags"] = yes
self.deck["inactiveTags"] = no
self.mw.col.decks.save(self.deck)
# build query string
self.tags = ""
if yes:
arr = []
for req in yes:
arr.append(f'tag:"{req}"')
self.tags += f"({' or '.join(arr)})"
if no:
arr = []
for req in no:
arr.append(f'-tag:"{req}"')
self.tags += f" {' '.join(arr)}"
saveGeom(self, "tagLimit")
QDialog.accept(self)