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
|
|
|
|
|
|
|
from typing import Optional
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
import aqt
|
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.customstudy import CustomStudy
|
|
|
|
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):
|
2021-02-01 13:08:56 +01:00
|
|
|
def __init__(self, mw: AnkiQt, parent: CustomStudy) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, parent, Qt.WindowType.Window)
|
2021-02-01 13:08:56 +01:00
|
|
|
self.tags: 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
|
2021-03-17 05:51:59 +01:00
|
|
|
self.parent_: Optional[CustomStudy] = parent
|
|
|
|
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:
|
2019-12-23 01:34:10 +01:00
|
|
|
usertags = self.mw.col.tags.byDeck(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
|
|
|
self.tags = ""
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
2021-02-01 13:08:56 +01:00
|
|
|
def accept(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# 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):
|
2021-02-01 13:08:56 +01:00
|
|
|
yes.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):
|
2021-02-01 13:08:56 +01:00
|
|
|
no.append(self.tags_list[c])
|
2021-06-02 03:01:30 +02:00
|
|
|
if (len(yes) + len(no)) > 100:
|
|
|
|
showWarning(with_collapsed_whitespace(tr.errors_100_tags_max()))
|
|
|
|
return
|
|
|
|
self.hide()
|
2012-12-21 08:51:59 +01:00
|
|
|
# save in the deck for future invocations
|
2019-12-23 01:34:10 +01:00
|
|
|
self.deck["activeTags"] = yes
|
|
|
|
self.deck["inactiveTags"] = no
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.col.decks.save(self.deck)
|
|
|
|
# build query string
|
|
|
|
self.tags = ""
|
|
|
|
if yes:
|
|
|
|
arr = []
|
|
|
|
for req in yes:
|
2021-02-11 01:09:06 +01:00
|
|
|
arr.append(f'tag:"{req}"')
|
|
|
|
self.tags += f"({' or '.join(arr)})"
|
2012-12-21 08:51:59 +01:00
|
|
|
if no:
|
|
|
|
arr = []
|
|
|
|
for req in no:
|
2021-02-11 01:09:06 +01:00
|
|
|
arr.append(f'-tag:"{req}"')
|
|
|
|
self.tags += f" {' '.join(arr)}"
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(self, "tagLimit")
|
|
|
|
QDialog.accept(self)
|