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
|
|
|
|
|
2022-03-10 07:06:55 +01:00
|
|
|
from typing import Sequence
|
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
|
2022-03-10 07:06:55 +01:00
|
|
|
from anki.scheduler.base import CustomStudyDefaults
|
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-03-10 07:06:55 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
parent: QWidget,
|
|
|
|
tags: Sequence[CustomStudyDefaults.Tag],
|
|
|
|
on_success: Callable[[list[str], list[str]], None],
|
|
|
|
) -> None:
|
|
|
|
"Ask user to select tags. on_success() will be called with selected included and excluded tags."
|
2021-10-05 05:53:01 +02:00
|
|
|
QDialog.__init__(self, parent, Qt.WindowType.Window)
|
2022-03-10 07:06:55 +01:00
|
|
|
self.tags = tags
|
|
|
|
self.form = aqt.forms.taglimit.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
self.on_success = on_success
|
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"),
|
2022-03-10 07:06:55 +01:00
|
|
|
self.form.activeList,
|
2021-10-05 05:53:01 +02:00
|
|
|
context=Qt.ShortcutContext.WidgetShortcut,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2022-03-10 07:06:55 +01:00
|
|
|
qconnect(s.activated, self.form.activeList.clearSelection)
|
2019-12-23 01:34:10 +01:00
|
|
|
s = QShortcut(
|
2021-10-05 05:53:01 +02:00
|
|
|
QKeySequence("ctrl+d"),
|
2022-03-10 07:06:55 +01:00
|
|
|
self.form.inactiveList,
|
2021-10-05 05:53:01 +02:00
|
|
|
context=Qt.ShortcutContext.WidgetShortcut,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2022-03-10 07:06:55 +01:00
|
|
|
qconnect(s.activated, self.form.inactiveList.clearSelection)
|
|
|
|
self.build_tag_lists()
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, "tagLimit")
|
2022-03-10 07:06:55 +01:00
|
|
|
self.open()
|
|
|
|
|
|
|
|
def build_tag_lists(self) -> None:
|
|
|
|
def add_tag(tag: str, select: bool, list: QListWidget) -> None:
|
|
|
|
item = QListWidgetItem(tag.replace("_", " "))
|
|
|
|
list.addItem(item)
|
|
|
|
if select:
|
|
|
|
idx = list.indexFromItem(item)
|
|
|
|
list.selectionModel().select(
|
|
|
|
idx, QItemSelectionModel.SelectionFlag.Select
|
|
|
|
)
|
|
|
|
|
|
|
|
had_included_tag = False
|
|
|
|
|
|
|
|
for tag in self.tags:
|
|
|
|
if tag.include:
|
|
|
|
had_included_tag = True
|
|
|
|
add_tag(tag.name, tag.include, self.form.activeList)
|
|
|
|
add_tag(tag.name, tag.exclude, self.form.inactiveList)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2022-03-10 07:06:55 +01:00
|
|
|
if had_included_tag:
|
|
|
|
self.form.activeCheck.setChecked(True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
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-03-10 07:06:55 +01:00
|
|
|
include_tags = []
|
|
|
|
exclude_tags = []
|
|
|
|
want_active = self.form.activeCheck.isChecked()
|
|
|
|
for c, tag in enumerate(self.tags):
|
2012-12-21 08:51:59 +01:00
|
|
|
# active
|
2022-03-10 07:06:55 +01:00
|
|
|
if want_active:
|
|
|
|
item = self.form.activeList.item(c)
|
|
|
|
idx = self.form.activeList.indexFromItem(item)
|
|
|
|
if self.form.activeList.selectionModel().isSelected(idx):
|
|
|
|
include_tags.append(tag.name)
|
2012-12-21 08:51:59 +01:00
|
|
|
# inactive
|
2022-03-10 07:06:55 +01:00
|
|
|
item = self.form.inactiveList.item(c)
|
|
|
|
idx = self.form.inactiveList.indexFromItem(item)
|
|
|
|
if self.form.inactiveList.selectionModel().isSelected(idx):
|
|
|
|
exclude_tags.append(tag.name)
|
2022-01-20 05:25:22 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(self, "tagLimit")
|
|
|
|
QDialog.accept(self)
|
2022-03-10 07:06:55 +01:00
|
|
|
|
|
|
|
self.on_success(include_tags, exclude_tags)
|