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
|
2021-10-03 10:59:42 +02:00
|
|
|
|
2022-01-20 05:25:22 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import List, Optional, Tuple
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
import aqt
|
2022-02-13 04:40:47 +01:00
|
|
|
import aqt.customstudy
|
|
|
|
import aqt.forms
|
2021-06-02 03:01:30 +02:00
|
|
|
from anki.lang import with_collapsed_whitespace
|
2021-02-01 13:08:56 +01:00
|
|
|
from aqt.main import AnkiQt
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.qt import *
|
2021-06-02 03:01:30 +02:00
|
|
|
from aqt.utils import disable_help_button, restoreGeom, saveGeom, showWarning, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class TagLimit(QDialog):
|
2022-01-20 05:25:22 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_tags(
|
|
|
|
mw: AnkiQt, parent: aqt.customstudy.CustomStudy
|
|
|
|
) -> Tuple[List[str], List[str]]:
|
|
|
|
"""Get two lists of tags to include/exclude."""
|
|
|
|
return TagLimit(mw, parent).tags
|
|
|
|
|
|
|
|
def __init__(self, mw: AnkiQt, parent: aqt.customstudy.CustomStudy) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, parent, Qt.WindowType.Window)
|
2022-01-20 05:25:22 +01:00
|
|
|
self.tags: Tuple[List[str], List[str]] = ([], [])
|
2021-10-03 10:59:42 +02:00
|
|
|
self.tags_list: list[str] = []
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
2022-01-20 05:25:22 +01:00
|
|
|
self.parent_: Optional[aqt.customstudy.CustomStudy] = parent
|
2021-03-17 05:51:59 +01:00
|
|
|
self.deck = self.parent_.deck
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog = aqt.forms.taglimit.Ui_Dialog()
|
|
|
|
self.dialog.setupUi(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2019-12-23 01:34:10 +01:00
|
|
|
s = QShortcut(
|
2021-10-05 05:53:01 +02:00
|
|
|
QKeySequence("ctrl+d"),
|
|
|
|
self.dialog.activeList,
|
|
|
|
context=Qt.ShortcutContext.WidgetShortcut,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(s.activated, self.dialog.activeList.clearSelection)
|
2019-12-23 01:34:10 +01:00
|
|
|
s = QShortcut(
|
2021-10-05 05:53:01 +02:00
|
|
|
QKeySequence("ctrl+d"),
|
|
|
|
self.dialog.inactiveList,
|
|
|
|
context=Qt.ShortcutContext.WidgetShortcut,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(s.activated, self.dialog.inactiveList.clearSelection)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.rebuildTagList()
|
|
|
|
restoreGeom(self, "tagLimit")
|
2021-10-05 02:01:45 +02:00
|
|
|
self.exec()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def rebuildTagList(self) -> None:
|
2021-10-25 06:50:13 +02:00
|
|
|
usertags = self.mw.col.tags.by_deck(self.deck["id"], True)
|
2012-12-21 08:51:59 +01:00
|
|
|
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()
|
2017-08-11 12:59:15 +02:00
|
|
|
groupedTags.append(usertags)
|
2021-02-01 13:08:56 +01:00
|
|
|
self.tags_list = []
|
2017-08-11 12:59:15 +02:00
|
|
|
for tags in groupedTags:
|
2012-12-21 08:51:59 +01:00
|
|
|
for t in tags:
|
2021-02-01 13:08:56 +01:00
|
|
|
self.tags_list.append(t)
|
2017-08-11 12:59:15 +02:00
|
|
|
item = QListWidgetItem(t.replace("_", " "))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.activeList.addItem(item)
|
|
|
|
if t in yesHash:
|
2021-10-05 05:53:01 +02:00
|
|
|
mode = QItemSelectionModel.SelectionFlag.Select
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.activeCheck.setChecked(True)
|
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
mode = QItemSelectionModel.SelectionFlag.Deselect
|
2012-12-21 08:51:59 +01:00
|
|
|
idx = self.dialog.activeList.indexFromItem(item)
|
|
|
|
self.dialog.activeList.selectionModel().select(idx, mode)
|
|
|
|
# inactive
|
2017-08-11 12:59:15 +02:00
|
|
|
item = QListWidgetItem(t.replace("_", " "))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.dialog.inactiveList.addItem(item)
|
|
|
|
if t in noHash:
|
2021-10-05 05:53:01 +02:00
|
|
|
mode = QItemSelectionModel.SelectionFlag.Select
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
mode = QItemSelectionModel.SelectionFlag.Deselect
|
2012-12-21 08:51:59 +01:00
|
|
|
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:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def accept(self) -> None:
|
2022-01-20 05:25:22 +01:00
|
|
|
include_tags = exclude_tags = []
|
2012-12-21 08:51:59 +01:00
|
|
|
# gather yes/no tags
|
|
|
|
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):
|
2022-01-20 05:25:22 +01:00
|
|
|
include_tags.append(self.tags_list[c])
|
2012-12-21 08:51:59 +01:00
|
|
|
# inactive
|
|
|
|
item = self.dialog.inactiveList.item(c)
|
|
|
|
idx = self.dialog.inactiveList.indexFromItem(item)
|
|
|
|
if self.dialog.inactiveList.selectionModel().isSelected(idx):
|
2022-01-20 05:25:22 +01:00
|
|
|
exclude_tags.append(self.tags_list[c])
|
|
|
|
|
|
|
|
if (len(include_tags) + len(exclude_tags)) > 100:
|
2021-06-02 03:01:30 +02:00
|
|
|
showWarning(with_collapsed_whitespace(tr.errors_100_tags_max()))
|
|
|
|
return
|
2022-01-20 05:25:22 +01:00
|
|
|
|
2021-06-02 03:01:30 +02:00
|
|
|
self.hide()
|
2022-01-20 05:25:22 +01:00
|
|
|
self.tags = (include_tags, exclude_tags)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# save in the deck for future invocations
|
2022-01-20 05:25:22 +01:00
|
|
|
self.deck["activeTags"] = include_tags
|
|
|
|
self.deck["inactiveTags"] = exclude_tags
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.col.decks.save(self.deck)
|
2022-01-20 05:25:22 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(self, "tagLimit")
|
|
|
|
QDialog.accept(self)
|