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
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
import aqt
|
|
|
|
from aqt.qt import *
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.utils import restoreGeom, saveGeom
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class TagLimit(QDialog):
|
|
|
|
def __init__(self, mw, parent):
|
|
|
|
QDialog.__init__(self, parent, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.parent = parent
|
|
|
|
self.deck = self.parent.deck
|
|
|
|
self.dialog = aqt.forms.taglimit.Ui_Dialog()
|
|
|
|
self.dialog.setupUi(self)
|
2019-12-23 01:34:10 +01:00
|
|
|
s = QShortcut(
|
|
|
|
QKeySequence("ctrl+d"), self.dialog.activeList, context=Qt.WidgetShortcut
|
|
|
|
)
|
2017-10-16 07:31:29 +02:00
|
|
|
s.activated.connect(self.dialog.activeList.clearSelection)
|
2019-12-23 01:34:10 +01:00
|
|
|
s = QShortcut(
|
|
|
|
QKeySequence("ctrl+d"), self.dialog.inactiveList, context=Qt.WidgetShortcut
|
|
|
|
)
|
2017-10-16 07:31:29 +02:00
|
|
|
s.activated.connect(self.dialog.inactiveList.clearSelection)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.rebuildTagList()
|
|
|
|
restoreGeom(self, "tagLimit")
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
def rebuildTagList(self):
|
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)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.tags = []
|
2017-08-11 12:59:15 +02:00
|
|
|
for tags in groupedTags:
|
2012-12-21 08:51:59 +01:00
|
|
|
for t in tags:
|
|
|
|
self.tags.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:
|
|
|
|
mode = QItemSelectionModel.Select
|
|
|
|
self.dialog.activeCheck.setChecked(True)
|
|
|
|
else:
|
|
|
|
mode = QItemSelectionModel.Deselect
|
|
|
|
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:
|
|
|
|
mode = QItemSelectionModel.Select
|
|
|
|
else:
|
|
|
|
mode = QItemSelectionModel.Deselect
|
|
|
|
idx = self.dialog.inactiveList.indexFromItem(item)
|
|
|
|
self.dialog.inactiveList.selectionModel().select(idx, mode)
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
self.tags = ""
|
|
|
|
QDialog.reject(self)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
self.hide()
|
|
|
|
n = 0
|
|
|
|
# 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[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[c])
|
|
|
|
# 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:
|
|
|
|
arr.append("tag:'%s'" % req)
|
|
|
|
self.tags += "(" + " or ".join(arr) + ")"
|
|
|
|
if no:
|
|
|
|
arr = []
|
|
|
|
for req in no:
|
|
|
|
arr.append("-tag:'%s'" % req)
|
|
|
|
self.tags += " " + " ".join(arr)
|
|
|
|
saveGeom(self, "tagLimit")
|
|
|
|
QDialog.accept(self)
|