2012-12-21 08:51:59 +01:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-05 04:59:03 +01:00
|
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
2017-12-11 07:20:00 +01:00
|
|
|
|
import html
|
2019-12-20 10:19:03 +01:00
|
|
|
|
import json
|
2013-05-03 10:52:46 +02:00
|
|
|
|
import re
|
2019-12-20 10:19:03 +01:00
|
|
|
|
import sre_constants
|
|
|
|
|
import time
|
2018-11-12 03:43:54 +01:00
|
|
|
|
import unicodedata
|
2019-12-20 10:19:03 +01:00
|
|
|
|
from operator import itemgetter
|
2020-01-15 22:41:23 +01:00
|
|
|
|
from typing import Callable, List, Optional, Union
|
2019-12-19 12:11:12 +01:00
|
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
|
import anki
|
|
|
|
|
import aqt.forms
|
2020-01-15 04:49:26 +01:00
|
|
|
|
from anki import hooks
|
2020-01-15 22:41:23 +01:00
|
|
|
|
from anki.cards import Card
|
2019-12-20 08:55:19 +01:00
|
|
|
|
from anki.collection import _Collection
|
2019-12-20 10:19:03 +01:00
|
|
|
|
from anki.consts import *
|
|
|
|
|
from anki.lang import _, ngettext
|
2020-01-17 01:17:33 +01:00
|
|
|
|
from anki.models import NoteType
|
2020-01-15 22:53:12 +01:00
|
|
|
|
from anki.notes import Note
|
2020-01-23 06:08:10 +01:00
|
|
|
|
from anki.utils import fmtTimeSpan, htmlToTextLine, ids2str, intTime, isMac, isWin
|
2020-01-14 23:53:57 +01:00
|
|
|
|
from aqt import AnkiQt, gui_hooks
|
2020-01-15 22:41:23 +01:00
|
|
|
|
from aqt.editor import Editor
|
2013-05-03 10:52:46 +02:00
|
|
|
|
from aqt.qt import *
|
2020-01-25 07:01:16 +01:00
|
|
|
|
from aqt.sound import av_player, play_clicked_audio
|
2020-01-23 06:08:10 +01:00
|
|
|
|
from aqt.theme import theme_manager
|
2019-12-23 01:34:10 +01:00
|
|
|
|
from aqt.utils import (
|
|
|
|
|
MenuList,
|
|
|
|
|
SubMenu,
|
|
|
|
|
askUser,
|
|
|
|
|
getOnlyText,
|
|
|
|
|
getTag,
|
|
|
|
|
openHelp,
|
|
|
|
|
qtMenuShortcutWorkaround,
|
|
|
|
|
restoreGeom,
|
|
|
|
|
restoreHeader,
|
|
|
|
|
restoreSplitter,
|
|
|
|
|
restoreState,
|
|
|
|
|
saveGeom,
|
|
|
|
|
saveHeader,
|
|
|
|
|
saveSplitter,
|
|
|
|
|
saveState,
|
|
|
|
|
shortcut,
|
|
|
|
|
showInfo,
|
|
|
|
|
showWarning,
|
|
|
|
|
tooltip,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
|
|
|
|
|
# Data model
|
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
class DataModel(QAbstractTableModel):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def __init__(self, browser):
|
|
|
|
|
QAbstractTableModel.__init__(self)
|
|
|
|
|
self.browser = browser
|
|
|
|
|
self.col = browser.col
|
|
|
|
|
self.sortKey = None
|
|
|
|
|
self.activeCols = self.col.conf.get(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"activeCols", ["noteFld", "template", "cardDue", "deck"]
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.cards = []
|
|
|
|
|
self.cardObjs = {}
|
|
|
|
|
|
|
|
|
|
def getCard(self, index):
|
|
|
|
|
id = self.cards[index.row()]
|
|
|
|
|
if not id in self.cardObjs:
|
2013-10-30 14:42:32 +01:00
|
|
|
|
self.cardObjs[id] = self.col.getCard(id)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return self.cardObjs[id]
|
|
|
|
|
|
|
|
|
|
def refreshNote(self, note):
|
|
|
|
|
refresh = False
|
|
|
|
|
for c in note.cards():
|
|
|
|
|
if c.id in self.cardObjs:
|
|
|
|
|
del self.cardObjs[c.id]
|
|
|
|
|
refresh = True
|
|
|
|
|
if refresh:
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self.layoutChanged.emit()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Model interface
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2017-02-19 05:30:35 +01:00
|
|
|
|
def rowCount(self, parent):
|
2017-02-23 07:21:00 +01:00
|
|
|
|
if parent and parent.isValid():
|
2017-02-19 05:30:35 +01:00
|
|
|
|
return 0
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return len(self.cards)
|
|
|
|
|
|
2017-02-19 05:30:35 +01:00
|
|
|
|
def columnCount(self, parent):
|
2017-02-23 07:21:00 +01:00
|
|
|
|
if parent and parent.isValid():
|
2017-02-19 05:30:35 +01:00
|
|
|
|
return 0
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return len(self.activeCols)
|
|
|
|
|
|
|
|
|
|
def data(self, index, role):
|
|
|
|
|
if not index.isValid():
|
|
|
|
|
return
|
|
|
|
|
if role == Qt.FontRole:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if self.activeCols[index.column()] not in ("question", "answer", "noteFld"):
|
2012-12-22 02:10:52 +01:00
|
|
|
|
return
|
|
|
|
|
row = index.row()
|
|
|
|
|
c = self.getCard(index)
|
|
|
|
|
t = c.template()
|
2017-07-29 08:00:02 +02:00
|
|
|
|
if not t.get("bfont"):
|
|
|
|
|
return
|
|
|
|
|
f = QFont()
|
2016-06-23 04:06:16 +02:00
|
|
|
|
f.setFamily(t.get("bfont", "arial"))
|
|
|
|
|
f.setPixelSize(t.get("bsize", 12))
|
2012-12-22 02:10:52 +01:00
|
|
|
|
return f
|
2017-07-29 08:00:02 +02:00
|
|
|
|
|
2012-12-22 02:10:52 +01:00
|
|
|
|
elif role == Qt.TextAlignmentRole:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
align = Qt.AlignVCenter
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if self.activeCols[index.column()] not in (
|
|
|
|
|
"question",
|
|
|
|
|
"answer",
|
|
|
|
|
"template",
|
|
|
|
|
"deck",
|
|
|
|
|
"noteFld",
|
|
|
|
|
"note",
|
|
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
align |= Qt.AlignHCenter
|
|
|
|
|
return align
|
|
|
|
|
elif role == Qt.DisplayRole or role == Qt.EditRole:
|
|
|
|
|
return self.columnData(index)
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def headerData(self, section, orientation, role):
|
|
|
|
|
if orientation == Qt.Vertical:
|
|
|
|
|
return
|
|
|
|
|
elif role == Qt.DisplayRole and section < len(self.activeCols):
|
|
|
|
|
type = self.columnType(section)
|
2014-01-13 11:07:34 +01:00
|
|
|
|
txt = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
for stype, name in self.browser.columns:
|
|
|
|
|
if type == stype:
|
|
|
|
|
txt = name
|
|
|
|
|
break
|
2019-12-12 00:53:42 +01:00
|
|
|
|
# give the user a hint an invalid column was added by an add-on
|
2014-01-13 11:07:34 +01:00
|
|
|
|
if not txt:
|
2019-12-12 00:53:42 +01:00
|
|
|
|
txt = _("Add-on")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return txt
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def flags(self, index):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return Qt.ItemFlag(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Filtering
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
|
def search(self, txt):
|
|
|
|
|
self.beginReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
t = time.time()
|
|
|
|
|
# the db progress handler may cause a refresh, so we need to zero out
|
|
|
|
|
# old data first
|
|
|
|
|
self.cards = []
|
2017-02-02 12:00:58 +01:00
|
|
|
|
invalid = False
|
|
|
|
|
try:
|
|
|
|
|
self.cards = self.col.findCards(txt, order=True)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
if str(e) == "invalidSearch":
|
|
|
|
|
self.cards = []
|
|
|
|
|
invalid = True
|
|
|
|
|
else:
|
|
|
|
|
raise
|
2019-12-23 01:34:10 +01:00
|
|
|
|
# print "fetch cards in %dms" % ((time.time() - t)*1000)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.endReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2017-02-02 12:00:58 +01:00
|
|
|
|
if invalid:
|
|
|
|
|
showWarning(_("Invalid search - please check for typing mistakes."))
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def reset(self):
|
|
|
|
|
self.beginReset()
|
|
|
|
|
self.endReset()
|
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
|
# caller must have called editor.saveNow() before calling this or .reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def beginReset(self):
|
|
|
|
|
self.browser.editor.setNote(None, hide=False)
|
|
|
|
|
self.browser.mw.progress.start()
|
|
|
|
|
self.saveSelection()
|
|
|
|
|
self.beginResetModel()
|
|
|
|
|
self.cardObjs = {}
|
|
|
|
|
|
|
|
|
|
def endReset(self):
|
|
|
|
|
t = time.time()
|
|
|
|
|
self.endResetModel()
|
|
|
|
|
self.restoreSelection()
|
|
|
|
|
self.browser.mw.progress.finish()
|
|
|
|
|
|
|
|
|
|
def reverse(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.browser.editor.saveNow(self._reverse)
|
|
|
|
|
|
|
|
|
|
def _reverse(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.beginReset()
|
|
|
|
|
self.cards.reverse()
|
|
|
|
|
self.endReset()
|
|
|
|
|
|
|
|
|
|
def saveSelection(self):
|
|
|
|
|
cards = self.browser.selectedCards()
|
|
|
|
|
self.selectedCards = dict([(id, True) for id in cards])
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if getattr(self.browser, "card", None):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.focusedCard = self.browser.card.id
|
|
|
|
|
else:
|
|
|
|
|
self.focusedCard = None
|
|
|
|
|
|
|
|
|
|
def restoreSelection(self):
|
|
|
|
|
if not self.cards:
|
|
|
|
|
return
|
|
|
|
|
sm = self.browser.form.tableView.selectionModel()
|
|
|
|
|
sm.clear()
|
|
|
|
|
# restore selection
|
|
|
|
|
items = QItemSelection()
|
|
|
|
|
count = 0
|
|
|
|
|
firstIdx = None
|
|
|
|
|
focusedIdx = None
|
|
|
|
|
for row, id in enumerate(self.cards):
|
|
|
|
|
# if the id matches the focused card, note the index
|
|
|
|
|
if self.focusedCard == id:
|
|
|
|
|
focusedIdx = self.index(row, 0)
|
|
|
|
|
items.select(focusedIdx, focusedIdx)
|
|
|
|
|
self.focusedCard = None
|
|
|
|
|
# if the card was previously selected, select again
|
|
|
|
|
if id in self.selectedCards:
|
|
|
|
|
count += 1
|
|
|
|
|
idx = self.index(row, 0)
|
|
|
|
|
items.select(idx, idx)
|
|
|
|
|
# note down the first card of the selection, in case we don't
|
|
|
|
|
# have a focused card
|
|
|
|
|
if not firstIdx:
|
|
|
|
|
firstIdx = idx
|
|
|
|
|
# focus previously focused or first in selection
|
|
|
|
|
idx = focusedIdx or firstIdx
|
|
|
|
|
tv = self.browser.form.tableView
|
|
|
|
|
if idx:
|
|
|
|
|
tv.selectRow(idx.row())
|
2018-11-12 03:26:23 +01:00
|
|
|
|
# scroll if the selection count has changed
|
|
|
|
|
if count != len(self.selectedCards):
|
|
|
|
|
# we save and then restore the horizontal scroll position because
|
|
|
|
|
# scrollTo() also scrolls horizontally which is confusing
|
|
|
|
|
h = tv.horizontalScrollBar().value()
|
|
|
|
|
tv.scrollTo(idx, tv.PositionAtCenter)
|
|
|
|
|
tv.horizontalScrollBar().setValue(h)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if count < 500:
|
|
|
|
|
# discard large selections; they're too slow
|
2019-12-23 01:34:10 +01:00
|
|
|
|
sm.select(
|
|
|
|
|
items, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
|
|
|
|
tv.selectRow(0)
|
|
|
|
|
|
|
|
|
|
# Column data
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def columnType(self, column):
|
|
|
|
|
return self.activeCols[column]
|
|
|
|
|
|
|
|
|
|
def columnData(self, index):
|
|
|
|
|
row = index.row()
|
|
|
|
|
col = index.column()
|
|
|
|
|
type = self.columnType(col)
|
|
|
|
|
c = self.getCard(index)
|
|
|
|
|
if type == "question":
|
|
|
|
|
return self.question(c)
|
|
|
|
|
elif type == "answer":
|
|
|
|
|
return self.answer(c)
|
|
|
|
|
elif type == "noteFld":
|
|
|
|
|
f = c.note()
|
2017-03-14 07:48:40 +01:00
|
|
|
|
return htmlToTextLine(f.fields[self.col.models.sortIdx(f.model())])
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "template":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
t = c.template()["name"]
|
|
|
|
|
if c.model()["type"] == MODEL_CLOZE:
|
|
|
|
|
t += " %d" % (c.ord + 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return t
|
|
|
|
|
elif type == "cardDue":
|
|
|
|
|
# catch invalid dates
|
|
|
|
|
try:
|
|
|
|
|
t = self.nextDue(c, index)
|
|
|
|
|
except:
|
|
|
|
|
t = ""
|
|
|
|
|
if c.queue < 0:
|
|
|
|
|
t = "(" + t + ")"
|
|
|
|
|
return t
|
|
|
|
|
elif type == "noteCrt":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(c.note().id / 1000))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "noteMod":
|
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(c.note().mod))
|
|
|
|
|
elif type == "cardMod":
|
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(c.mod))
|
|
|
|
|
elif type == "cardReps":
|
|
|
|
|
return str(c.reps)
|
|
|
|
|
elif type == "cardLapses":
|
|
|
|
|
return str(c.lapses)
|
2013-09-03 20:20:20 +02:00
|
|
|
|
elif type == "noteTags":
|
2013-10-08 00:42:06 +02:00
|
|
|
|
return " ".join(c.note().tags)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "note":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return c.model()["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "cardIvl":
|
|
|
|
|
if c.type == 0:
|
|
|
|
|
return _("(new)")
|
|
|
|
|
elif c.type == 1:
|
|
|
|
|
return _("(learning)")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return fmtTimeSpan(c.ivl * 86400)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "cardEase":
|
|
|
|
|
if c.type == 0:
|
|
|
|
|
return _("(new)")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return "%d%%" % (c.factor / 10)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "deck":
|
|
|
|
|
if c.odid:
|
|
|
|
|
# in a cram deck
|
|
|
|
|
return "%s (%s)" % (
|
|
|
|
|
self.browser.mw.col.decks.name(c.did),
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.browser.mw.col.decks.name(c.odid),
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# normal deck
|
|
|
|
|
return self.browser.mw.col.decks.name(c.did)
|
|
|
|
|
|
|
|
|
|
def question(self, c):
|
2017-03-14 07:48:40 +01:00
|
|
|
|
return htmlToTextLine(c.q(browser=True))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def answer(self, c):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if c.template().get("bafmt"):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# they have provided a template, use it verbatim
|
|
|
|
|
c.q(browser=True)
|
2017-03-14 07:48:40 +01:00
|
|
|
|
return htmlToTextLine(c.a())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# need to strip question from answer
|
|
|
|
|
q = self.question(c)
|
2017-03-14 07:48:40 +01:00
|
|
|
|
a = htmlToTextLine(c.a())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if a.startswith(q):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return a[len(q) :].strip()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
|
def nextDue(self, c, index):
|
|
|
|
|
if c.odid:
|
|
|
|
|
return _("(filtered)")
|
|
|
|
|
elif c.queue == 1:
|
|
|
|
|
date = c.due
|
|
|
|
|
elif c.queue == 0 or c.type == 0:
|
|
|
|
|
return str(c.due)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
elif c.queue in (2, 3) or (c.type == 2 and c.queue < 0):
|
|
|
|
|
date = time.time() + ((c.due - self.col.sched.today) * 86400)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
|
|
|
|
return ""
|
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(date))
|
|
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
|
def isRTL(self, index):
|
|
|
|
|
col = index.column()
|
|
|
|
|
type = self.columnType(col)
|
|
|
|
|
if type != "noteFld":
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
row = index.row()
|
|
|
|
|
c = self.getCard(index)
|
|
|
|
|
nt = c.note().model()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return nt["flds"][self.col.models.sortIdx(nt)]["rtl"]
|
|
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Line painter
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
class StatusDelegate(QItemDelegate):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def __init__(self, browser, model):
|
|
|
|
|
QItemDelegate.__init__(self, browser)
|
2013-01-28 22:45:29 +01:00
|
|
|
|
self.browser = browser
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model = model
|
|
|
|
|
|
|
|
|
|
def paint(self, painter, option, index):
|
2013-01-28 22:45:29 +01:00
|
|
|
|
self.browser.mw.progress.blockUpdates = True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
try:
|
|
|
|
|
c = self.model.getCard(index)
|
|
|
|
|
except:
|
|
|
|
|
# in the the middle of a reset; return nothing so this row is not
|
|
|
|
|
# rendered until we have a chance to reset the model
|
|
|
|
|
return
|
2013-01-28 22:45:29 +01:00
|
|
|
|
finally:
|
|
|
|
|
self.browser.mw.progress.blockUpdates = True
|
2017-08-12 08:08:10 +02:00
|
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
|
if self.model.isRTL(index):
|
|
|
|
|
option.direction = Qt.RightToLeft
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
col = None
|
2018-09-05 08:55:26 +02:00
|
|
|
|
if c.userFlag() > 0:
|
2020-01-23 06:08:10 +01:00
|
|
|
|
col = theme_manager.qcolor(f"flag{c.userFlag()}-bg")
|
2017-08-16 12:30:29 +02:00
|
|
|
|
elif c.note().hasTag("Marked"):
|
2020-01-23 06:08:10 +01:00
|
|
|
|
col = theme_manager.qcolor("marked-bg")
|
2018-09-05 08:55:26 +02:00
|
|
|
|
elif c.queue == -1:
|
2020-01-23 06:08:10 +01:00
|
|
|
|
col = theme_manager.qcolor("suspended-bg")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if col:
|
2020-01-23 06:08:10 +01:00
|
|
|
|
brush = QBrush(col)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
painter.save()
|
|
|
|
|
painter.fillRect(option.rect, brush)
|
|
|
|
|
painter.restore()
|
2017-08-12 08:08:10 +02:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return QItemDelegate.paint(self, painter, option, index)
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
# Sidebar
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
class SidebarItem:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
name: str,
|
|
|
|
|
icon: str,
|
|
|
|
|
onClick: Callable[[], None] = None,
|
|
|
|
|
onExpanded: Callable[[bool], None] = None,
|
|
|
|
|
expanded: bool = False,
|
|
|
|
|
) -> None:
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.name = name
|
|
|
|
|
self.icon = icon
|
2019-12-20 00:30:35 +01:00
|
|
|
|
self.onClick = onClick
|
|
|
|
|
self.onExpanded = onExpanded
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.expanded = expanded
|
|
|
|
|
self.children: List["SidebarItem"] = []
|
|
|
|
|
self.parentItem: Optional[SidebarItem] = None
|
2019-12-21 07:44:38 +01:00
|
|
|
|
self.tooltip: Optional[str] = None
|
2019-12-19 12:11:12 +01:00
|
|
|
|
|
|
|
|
|
def addChild(self, cb: "SidebarItem") -> None:
|
|
|
|
|
self.children.append(cb)
|
|
|
|
|
cb.parentItem = self
|
|
|
|
|
|
|
|
|
|
def rowForChild(self, child: "SidebarItem") -> Optional[int]:
|
|
|
|
|
try:
|
|
|
|
|
return self.children.index(child)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return None
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
class SidebarModel(QAbstractItemModel):
|
|
|
|
|
def __init__(self, root: SidebarItem) -> None:
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.root = root
|
|
|
|
|
|
2019-12-20 00:27:14 +01:00
|
|
|
|
# Qt API
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def rowCount(self, parent: QModelIndex = QModelIndex()) -> int:
|
|
|
|
|
if not parent.isValid():
|
|
|
|
|
return len(self.root.children)
|
|
|
|
|
else:
|
|
|
|
|
item: SidebarItem = parent.internalPointer()
|
|
|
|
|
return len(item.children)
|
|
|
|
|
|
|
|
|
|
def columnCount(self, parent: QModelIndex = QModelIndex()) -> int:
|
|
|
|
|
return 1
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
def index(
|
|
|
|
|
self, row: int, column: int, parent: QModelIndex = QModelIndex()
|
|
|
|
|
) -> QModelIndex:
|
2019-12-19 12:11:12 +01:00
|
|
|
|
if not self.hasIndex(row, column, parent):
|
|
|
|
|
return QModelIndex()
|
|
|
|
|
|
|
|
|
|
parentItem: SidebarItem
|
|
|
|
|
if not parent.isValid():
|
|
|
|
|
parentItem = self.root
|
|
|
|
|
else:
|
|
|
|
|
parentItem = parent.internalPointer()
|
|
|
|
|
|
|
|
|
|
item = parentItem.children[row]
|
|
|
|
|
return self.createIndex(row, column, item)
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
def parent(self, child: QModelIndex) -> QModelIndex: # type: ignore
|
2019-12-19 12:11:12 +01:00
|
|
|
|
if not child.isValid():
|
|
|
|
|
return QModelIndex()
|
|
|
|
|
|
|
|
|
|
childItem: SidebarItem = child.internalPointer()
|
|
|
|
|
parentItem = childItem.parentItem
|
|
|
|
|
|
|
|
|
|
if parentItem is None or parentItem == self.root:
|
|
|
|
|
return QModelIndex()
|
|
|
|
|
|
|
|
|
|
row = parentItem.rowForChild(childItem)
|
|
|
|
|
if row is None:
|
|
|
|
|
return QModelIndex()
|
|
|
|
|
|
|
|
|
|
return self.createIndex(row, 0, parentItem)
|
|
|
|
|
|
|
|
|
|
def data(self, index: QModelIndex, role: int = Qt.DisplayRole) -> QVariant:
|
|
|
|
|
if not index.isValid():
|
|
|
|
|
return QVariant()
|
|
|
|
|
|
2019-12-21 07:15:38 +01:00
|
|
|
|
if role not in (Qt.DisplayRole, Qt.DecorationRole, Qt.ToolTipRole):
|
2019-12-19 12:11:12 +01:00
|
|
|
|
return QVariant()
|
|
|
|
|
|
|
|
|
|
item: SidebarItem = index.internalPointer()
|
|
|
|
|
|
|
|
|
|
if role == Qt.DisplayRole:
|
|
|
|
|
return QVariant(item.name)
|
2019-12-21 07:15:38 +01:00
|
|
|
|
elif role == Qt.ToolTipRole:
|
|
|
|
|
return QVariant(item.tooltip)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
else:
|
2020-01-23 06:08:10 +01:00
|
|
|
|
return QVariant(theme_manager.icon_from_resources(item.icon))
|
2019-12-19 12:11:12 +01:00
|
|
|
|
|
2019-12-20 00:27:14 +01:00
|
|
|
|
# Helpers
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2020-01-23 07:36:50 +01:00
|
|
|
|
def iconFromRef(self, iconRef: str) -> QIcon:
|
|
|
|
|
print("iconFromRef() deprecated")
|
|
|
|
|
return theme_manager.icon_from_resources(iconRef)
|
|
|
|
|
|
2019-12-20 00:27:14 +01:00
|
|
|
|
def expandWhereNeccessary(self, tree: QTreeView) -> None:
|
|
|
|
|
for row, child in enumerate(self.root.children):
|
|
|
|
|
if child.expanded:
|
|
|
|
|
idx = self.index(row, 0, QModelIndex())
|
|
|
|
|
self._expandWhereNeccessary(idx, tree)
|
|
|
|
|
|
|
|
|
|
def _expandWhereNeccessary(self, parent: QModelIndex, tree: QTreeView) -> None:
|
2019-12-20 01:21:20 +01:00
|
|
|
|
parentItem: SidebarItem
|
2019-12-20 00:27:14 +01:00
|
|
|
|
if not parent.isValid():
|
|
|
|
|
parentItem = self.root
|
|
|
|
|
else:
|
2019-12-20 01:21:20 +01:00
|
|
|
|
parentItem = parent.internalPointer()
|
2019-12-20 00:27:14 +01:00
|
|
|
|
|
|
|
|
|
# nothing to do?
|
|
|
|
|
if not parentItem.expanded:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# expand children
|
|
|
|
|
for row, child in enumerate(parentItem.children):
|
|
|
|
|
if not child.expanded:
|
|
|
|
|
continue
|
|
|
|
|
childIdx = self.index(row, 0, parent)
|
|
|
|
|
self._expandWhereNeccessary(childIdx, tree)
|
|
|
|
|
|
|
|
|
|
# then ourselves
|
|
|
|
|
tree.setExpanded(parent, True)
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Browser window
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
# fixme: respond to reset+edit hooks
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
class Browser(QMainWindow):
|
2019-12-20 08:55:19 +01:00
|
|
|
|
model: DataModel
|
|
|
|
|
mw: AnkiQt
|
|
|
|
|
col: _Collection
|
2020-01-15 22:41:23 +01:00
|
|
|
|
editor: Optional[Editor]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
QMainWindow.__init__(self, None, Qt.Window)
|
|
|
|
|
self.mw = mw
|
|
|
|
|
self.col = self.mw.col
|
|
|
|
|
self.lastFilter = ""
|
2017-08-05 07:15:19 +02:00
|
|
|
|
self.focusTo = None
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._previewWindow = None
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self._closeEventHasCleanedUp = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.form = aqt.forms.browser.Ui_Dialog()
|
|
|
|
|
self.form.setupUi(self)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
self.setupSidebar()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
restoreGeom(self, "editor", 0)
|
|
|
|
|
restoreState(self, "editor")
|
|
|
|
|
restoreSplitter(self.form.splitter, "editor3")
|
|
|
|
|
self.form.splitter.setChildrenCollapsible(False)
|
2020-01-15 22:41:23 +01:00
|
|
|
|
self.card: Optional[Card] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.setupColumns()
|
|
|
|
|
self.setupTable()
|
|
|
|
|
self.setupMenus()
|
|
|
|
|
self.setupHeaders()
|
|
|
|
|
self.setupHooks()
|
|
|
|
|
self.setupEditor()
|
|
|
|
|
self.updateFont()
|
|
|
|
|
self.onUndoState(self.mw.form.actionUndo.isEnabled())
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.setupSearch()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.show()
|
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def setupMenus(self) -> None:
|
2019-03-04 07:01:10 +01:00
|
|
|
|
# pylint: disable=unnecessary-lambda
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# actions
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f = self.form
|
|
|
|
|
f.previewButton.clicked.connect(self.onTogglePreview)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
f.previewButton.setToolTip(
|
|
|
|
|
_("Preview Selected Card (%s)") % shortcut(_("Ctrl+Shift+P"))
|
|
|
|
|
)
|
2020-01-16 03:31:02 +01:00
|
|
|
|
f.previewButton.setShortcut("Ctrl+Shift+P")
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
f.filter.clicked.connect(self.onFilterButton)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
# edit
|
|
|
|
|
f.actionUndo.triggered.connect(self.mw.onUndo)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f.actionInvertSelection.triggered.connect(self.invertSelection)
|
|
|
|
|
f.actionSelectNotes.triggered.connect(self.selectNotes)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
if not isMac:
|
|
|
|
|
f.actionClose.setVisible(False)
|
|
|
|
|
# notes
|
|
|
|
|
f.actionAdd.triggered.connect(self.mw.onAddCard)
|
|
|
|
|
f.actionAdd_Tags.triggered.connect(lambda: self.addTags())
|
|
|
|
|
f.actionRemove_Tags.triggered.connect(lambda: self.deleteTags())
|
2017-08-15 10:48:22 +02:00
|
|
|
|
f.actionClear_Unused_Tags.triggered.connect(self.clearUnusedTags)
|
2017-08-16 12:30:29 +02:00
|
|
|
|
f.actionToggle_Mark.triggered.connect(lambda: self.onMark())
|
2017-08-11 06:40:51 +02:00
|
|
|
|
f.actionChangeModel.triggered.connect(self.onChangeModel)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f.actionFindDuplicates.triggered.connect(self.onFindDupes)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
f.actionFindReplace.triggered.connect(self.onFindReplace)
|
2017-08-15 10:49:41 +02:00
|
|
|
|
f.actionManage_Note_Types.triggered.connect(self.mw.onNoteTypes)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
f.actionDelete.triggered.connect(self.deleteNotes)
|
|
|
|
|
# cards
|
|
|
|
|
f.actionChange_Deck.triggered.connect(self.setDeck)
|
|
|
|
|
f.action_Info.triggered.connect(self.showCardInfo)
|
|
|
|
|
f.actionReposition.triggered.connect(self.reposition)
|
|
|
|
|
f.actionReschedule.triggered.connect(self.reschedule)
|
|
|
|
|
f.actionToggle_Suspend.triggered.connect(self.onSuspend)
|
2017-08-12 08:08:10 +02:00
|
|
|
|
f.actionRed_Flag.triggered.connect(lambda: self.onSetFlag(1))
|
2018-11-01 05:58:41 +01:00
|
|
|
|
f.actionOrange_Flag.triggered.connect(lambda: self.onSetFlag(2))
|
2017-08-12 08:08:10 +02:00
|
|
|
|
f.actionGreen_Flag.triggered.connect(lambda: self.onSetFlag(3))
|
|
|
|
|
f.actionBlue_Flag.triggered.connect(lambda: self.onSetFlag(4))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# jumps
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f.actionPreviousCard.triggered.connect(self.onPreviousCard)
|
|
|
|
|
f.actionNextCard.triggered.connect(self.onNextCard)
|
|
|
|
|
f.actionFirstCard.triggered.connect(self.onFirstCard)
|
|
|
|
|
f.actionLastCard.triggered.connect(self.onLastCard)
|
|
|
|
|
f.actionFind.triggered.connect(self.onFind)
|
|
|
|
|
f.actionNote.triggered.connect(self.onNote)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
f.actionTags.triggered.connect(self.onFilterButton)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
f.actionSidebar.triggered.connect(self.focusSidebar)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f.actionCardList.triggered.connect(self.onCardList)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# help
|
2016-05-31 10:51:40 +02:00
|
|
|
|
f.actionGuide.triggered.connect(self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# keyboard shortcut for shift+home/end
|
|
|
|
|
self.pgUpCut = QShortcut(QKeySequence("Shift+Home"), self)
|
2020-01-15 22:41:23 +01:00
|
|
|
|
qconnect(self.pgUpCut.activated, self.onFirstCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.pgDownCut = QShortcut(QKeySequence("Shift+End"), self)
|
2020-01-15 22:41:23 +01:00
|
|
|
|
qconnect(self.pgDownCut.activated, self.onLastCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# add-on hook
|
2020-01-15 08:18:11 +01:00
|
|
|
|
gui_hooks.browser_menus_did_init(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.maybeHideAccelerators(self)
|
|
|
|
|
|
2017-12-14 05:49:51 +01:00
|
|
|
|
# context menu
|
|
|
|
|
self.form.tableView.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
|
|
|
self.form.tableView.customContextMenuRequested.connect(self.onContextMenu)
|
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def onContextMenu(self, _point) -> None:
|
2017-12-14 05:49:51 +01:00
|
|
|
|
m = QMenu()
|
|
|
|
|
for act in self.form.menu_Cards.actions():
|
|
|
|
|
m.addAction(act)
|
|
|
|
|
m.addSeparator()
|
|
|
|
|
for act in self.form.menu_Notes.actions():
|
|
|
|
|
m.addAction(act)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
gui_hooks.browser_will_show_context_menu(self, m)
|
2019-02-05 05:37:07 +01:00
|
|
|
|
qtMenuShortcutWorkaround(m)
|
2017-12-14 05:49:51 +01:00
|
|
|
|
m.exec_(QCursor.pos())
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def updateFont(self):
|
2013-05-23 07:12:04 +02:00
|
|
|
|
# we can't choose different line heights efficiently, so we need
|
|
|
|
|
# to pick a line height big enough for any card template
|
|
|
|
|
curmax = 16
|
|
|
|
|
for m in self.col.models.all():
|
2019-12-23 01:34:10 +01:00
|
|
|
|
for t in m["tmpls"]:
|
2013-05-23 07:12:04 +02:00
|
|
|
|
bsize = t.get("bsize", 0)
|
|
|
|
|
if bsize > curmax:
|
|
|
|
|
curmax = bsize
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.form.tableView.verticalHeader().setDefaultSectionSize(curmax + 6)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def closeEvent(self, evt):
|
2017-08-16 04:45:33 +02:00
|
|
|
|
if self._closeEventHasCleanedUp:
|
2016-07-14 12:23:44 +02:00
|
|
|
|
evt.accept()
|
2017-08-16 04:45:33 +02:00
|
|
|
|
return
|
|
|
|
|
self.editor.saveNow(self._closeWindow)
|
|
|
|
|
evt.ignore()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
|
def _closeWindow(self):
|
2017-08-08 08:28:53 +02:00
|
|
|
|
self._cancelPreviewTimer()
|
2017-08-16 04:45:33 +02:00
|
|
|
|
self.editor.cleanup()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
saveSplitter(self.form.splitter, "editor3")
|
|
|
|
|
saveGeom(self, "editor")
|
|
|
|
|
saveState(self, "editor")
|
2014-03-14 07:29:48 +01:00
|
|
|
|
saveHeader(self.form.tableView.horizontalHeader(), "editor")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.conf["activeCols"] = self.model.activeCols
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.col.setMod()
|
|
|
|
|
self.teardownHooks()
|
|
|
|
|
self.mw.maybeReset()
|
2017-08-16 04:45:33 +02:00
|
|
|
|
aqt.dialogs.markClosed("Browser")
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self._closeEventHasCleanedUp = True
|
2017-08-16 04:45:33 +02:00
|
|
|
|
self.mw.gcWindow(self)
|
|
|
|
|
self.close()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
|
def closeWithCallback(self, onsuccess):
|
|
|
|
|
def callback():
|
|
|
|
|
self._closeWindow()
|
|
|
|
|
onsuccess()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
|
self.editor.saveNow(callback)
|
2013-04-11 12:23:32 +02:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
|
self.close()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
else:
|
|
|
|
|
super().keyPressEvent(evt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def setupColumns(self):
|
|
|
|
|
self.columns = [
|
2019-12-23 01:34:10 +01:00
|
|
|
|
("question", _("Question")),
|
|
|
|
|
("answer", _("Answer")),
|
|
|
|
|
("template", _("Card")),
|
|
|
|
|
("deck", _("Deck")),
|
|
|
|
|
("noteFld", _("Sort Field")),
|
|
|
|
|
("noteCrt", _("Created")),
|
|
|
|
|
("noteMod", _("Edited")),
|
|
|
|
|
("cardMod", _("Changed")),
|
|
|
|
|
("cardDue", _("Due")),
|
|
|
|
|
("cardIvl", _("Interval")),
|
|
|
|
|
("cardEase", _("Ease")),
|
|
|
|
|
("cardReps", _("Reviews")),
|
|
|
|
|
("cardLapses", _("Lapses")),
|
|
|
|
|
("noteTags", _("Tags")),
|
|
|
|
|
("note", _("Note")),
|
2012-12-21 08:51:59 +01:00
|
|
|
|
]
|
|
|
|
|
self.columns.sort(key=itemgetter(1))
|
|
|
|
|
|
|
|
|
|
# Searching
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def setupSearch(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.form.searchButton.clicked.connect(self.onSearchActivated)
|
|
|
|
|
self.form.searchEdit.lineEdit().returnPressed.connect(self.onSearchActivated)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.form.searchEdit.setCompleter(None)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self._searchPrompt = _("<type here to search; hit enter to show current deck>")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.form.searchEdit.addItems(
|
|
|
|
|
[self._searchPrompt] + self.mw.pm.profile["searchHistory"]
|
|
|
|
|
)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self._lastSearchTxt = "is:current"
|
|
|
|
|
self.search()
|
|
|
|
|
# then replace text for easily showing the deck
|
|
|
|
|
self.form.searchEdit.lineEdit().setText(self._searchPrompt)
|
|
|
|
|
self.form.searchEdit.lineEdit().selectAll()
|
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
|
|
|
|
|
|
# search triggered by user
|
|
|
|
|
def onSearchActivated(self):
|
|
|
|
|
self.editor.saveNow(self._onSearchActivated)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
|
def _onSearchActivated(self):
|
|
|
|
|
# convert guide text before we save history
|
|
|
|
|
if self.form.searchEdit.lineEdit().text() == self._searchPrompt:
|
|
|
|
|
self.form.searchEdit.lineEdit().setText("deck:current ")
|
|
|
|
|
|
2018-11-12 03:43:54 +01:00
|
|
|
|
# grab search text and normalize
|
|
|
|
|
txt = self.form.searchEdit.lineEdit().text()
|
|
|
|
|
txt = unicodedata.normalize("NFC", txt)
|
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
|
# update history
|
2019-12-23 01:34:10 +01:00
|
|
|
|
sh = self.mw.pm.profile["searchHistory"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if txt in sh:
|
|
|
|
|
sh.remove(txt)
|
|
|
|
|
sh.insert(0, txt)
|
|
|
|
|
sh = sh[:30]
|
|
|
|
|
self.form.searchEdit.clear()
|
|
|
|
|
self.form.searchEdit.addItems(sh)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.mw.pm.profile["searchHistory"] = sh
|
2016-07-14 12:23:44 +02:00
|
|
|
|
|
|
|
|
|
# keep track of search string so that we reuse identical search when
|
|
|
|
|
# refreshing, rather than whatever is currently in the search field
|
|
|
|
|
self._lastSearchTxt = txt
|
|
|
|
|
self.search()
|
|
|
|
|
|
|
|
|
|
# search triggered programmatically. caller must have saved note first.
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def search(self) -> None:
|
2016-07-14 12:23:44 +02:00
|
|
|
|
if "is:current" in self._lastSearchTxt:
|
|
|
|
|
# show current card if there is one
|
|
|
|
|
c = self.mw.reviewer.card
|
2018-07-23 04:50:10 +02:00
|
|
|
|
self.card = self.mw.reviewer.card
|
2016-07-14 12:23:44 +02:00
|
|
|
|
nid = c and c.nid or 0
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.model.search("nid:%d" % nid)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
else:
|
|
|
|
|
self.model.search(self._lastSearchTxt)
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not self.model.cards:
|
|
|
|
|
# no row change will fire
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self._onRowChanged(None, None)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def updateTitle(self):
|
|
|
|
|
selected = len(self.form.tableView.selectionModel().selectedRows())
|
|
|
|
|
cur = len(self.model.cards)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.setWindowTitle(
|
|
|
|
|
ngettext(
|
|
|
|
|
"Browse (%(cur)d card shown; %(sel)s)",
|
|
|
|
|
"Browse (%(cur)d cards shown; %(sel)s)",
|
|
|
|
|
cur,
|
|
|
|
|
)
|
|
|
|
|
% {
|
|
|
|
|
"cur": cur,
|
|
|
|
|
"sel": ngettext("%d selected", "%d selected", selected) % selected,
|
|
|
|
|
}
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return selected
|
|
|
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
|
self.editor.setNote(None)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Table view & editor
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def setupTable(self):
|
|
|
|
|
self.model = DataModel(self)
|
|
|
|
|
self.form.tableView.setSortingEnabled(True)
|
|
|
|
|
self.form.tableView.setModel(self.model)
|
|
|
|
|
self.form.tableView.selectionModel()
|
|
|
|
|
self.form.tableView.setItemDelegate(StatusDelegate(self, self.model))
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self.form.tableView.selectionModel().selectionChanged.connect(self.onRowChanged)
|
2020-01-23 06:08:10 +01:00
|
|
|
|
if not theme_manager.night_mode:
|
|
|
|
|
self.form.tableView.setStyleSheet(
|
|
|
|
|
"QTableView{ selection-background-color: rgba(150, 150, 150, 50); "
|
|
|
|
|
"selection-color: black; }"
|
|
|
|
|
)
|
2018-09-24 09:29:19 +02:00
|
|
|
|
self.singleCard = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def setupEditor(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onRowChanged(self, current, previous):
|
|
|
|
|
"Update current note and hide/show editor."
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._onRowChanged(current, previous))
|
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def _onRowChanged(self, current, previous) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
update = self.updateTitle()
|
|
|
|
|
show = self.model.cards and update == 1
|
2013-04-15 06:46:07 +02:00
|
|
|
|
self.form.splitter.widget(1).setVisible(not not show)
|
2017-08-31 12:38:12 +02:00
|
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
|
if idx.isValid():
|
|
|
|
|
self.card = self.model.getCard(idx)
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not show:
|
|
|
|
|
self.editor.setNote(None)
|
2013-05-10 08:02:58 +02:00
|
|
|
|
self.singleCard = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
2017-08-05 07:15:19 +02:00
|
|
|
|
self.editor.setNote(self.card.note(reload=True), focusTo=self.focusTo)
|
|
|
|
|
self.focusTo = None
|
2020-01-15 22:53:12 +01:00
|
|
|
|
self.editor.card = self.card # type: ignore
|
2013-05-10 08:02:58 +02:00
|
|
|
|
self.singleCard = True
|
2018-11-12 03:10:50 +01:00
|
|
|
|
self._updateFlagsMenu()
|
2020-01-15 08:45:35 +01:00
|
|
|
|
gui_hooks.browser_did_change_row(self)
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._renderPreview(True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
|
def refreshCurrentCard(self, note: Note) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model.refreshNote(note)
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._renderPreview(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2018-11-27 08:58:38 +01:00
|
|
|
|
def onLoadNote(self, editor):
|
|
|
|
|
self.refreshCurrentCard(editor.note)
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def refreshCurrentCardFilter(self, flag, note, fidx):
|
|
|
|
|
self.refreshCurrentCard(note)
|
|
|
|
|
return flag
|
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
def currentRow(self):
|
|
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
|
return idx.row()
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Headers & sorting
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def setupHeaders(self):
|
|
|
|
|
vh = self.form.tableView.verticalHeader()
|
|
|
|
|
hh = self.form.tableView.horizontalHeader()
|
|
|
|
|
if not isWin:
|
|
|
|
|
vh.hide()
|
|
|
|
|
hh.show()
|
2014-03-14 07:29:48 +01:00
|
|
|
|
restoreHeader(hh, "editor")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
hh.setHighlightSections(False)
|
|
|
|
|
hh.setMinimumSectionSize(50)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
hh.setSectionsMovable(True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.setColumnSizes()
|
|
|
|
|
hh.setContextMenuPolicy(Qt.CustomContextMenu)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
hh.customContextMenuRequested.connect(self.onHeaderContext)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.setSortIndicator()
|
2016-05-31 10:51:40 +02:00
|
|
|
|
hh.sortIndicatorChanged.connect(self.onSortChanged)
|
|
|
|
|
hh.sectionMoved.connect(self.onColumnMoved)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onSortChanged(self, idx, ord):
|
2019-10-28 04:25:12 +01:00
|
|
|
|
ord = bool(ord)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._onSortChanged(idx, ord))
|
|
|
|
|
|
|
|
|
|
def _onSortChanged(self, idx, ord):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
type = self.model.activeCols[idx]
|
2013-09-03 20:20:20 +02:00
|
|
|
|
noSort = ("question", "answer", "template", "deck", "note", "noteTags")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if type in noSort:
|
|
|
|
|
if type == "template":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
showInfo(
|
|
|
|
|
_(
|
|
|
|
|
"""\
|
2013-03-03 00:19:16 +01:00
|
|
|
|
This column can't be sorted on, but you can search for individual card types, \
|
2019-12-23 01:34:10 +01:00
|
|
|
|
such as 'card:1'."""
|
|
|
|
|
)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
elif type == "deck":
|
2019-12-23 01:34:10 +01:00
|
|
|
|
showInfo(
|
|
|
|
|
_(
|
|
|
|
|
"""\
|
2013-03-03 00:19:16 +01:00
|
|
|
|
This column can't be sorted on, but you can search for specific decks \
|
2019-12-23 01:34:10 +01:00
|
|
|
|
by clicking on one on the left."""
|
|
|
|
|
)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
showInfo(
|
|
|
|
|
_(
|
|
|
|
|
"Sorting on this column is not supported. Please "
|
|
|
|
|
"choose another."
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
type = self.col.conf["sortType"]
|
|
|
|
|
if self.col.conf["sortType"] != type:
|
|
|
|
|
self.col.conf["sortType"] = type
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# default to descending for non-text fields
|
|
|
|
|
if type == "noteFld":
|
|
|
|
|
ord = not ord
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.conf["sortBackwards"] = ord
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if self.col.conf["sortBackwards"] != ord:
|
|
|
|
|
self.col.conf["sortBackwards"] = ord
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model.reverse()
|
|
|
|
|
self.setSortIndicator()
|
|
|
|
|
|
|
|
|
|
def setSortIndicator(self):
|
|
|
|
|
hh = self.form.tableView.horizontalHeader()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
type = self.col.conf["sortType"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if type not in self.model.activeCols:
|
|
|
|
|
hh.setSortIndicatorShown(False)
|
|
|
|
|
return
|
|
|
|
|
idx = self.model.activeCols.index(type)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if self.col.conf["sortBackwards"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
ord = Qt.DescendingOrder
|
|
|
|
|
else:
|
|
|
|
|
ord = Qt.AscendingOrder
|
|
|
|
|
hh.blockSignals(True)
|
|
|
|
|
hh.setSortIndicator(idx, ord)
|
|
|
|
|
hh.blockSignals(False)
|
|
|
|
|
hh.setSortIndicatorShown(True)
|
|
|
|
|
|
|
|
|
|
def onHeaderContext(self, pos):
|
|
|
|
|
gpos = self.form.tableView.mapToGlobal(pos)
|
|
|
|
|
m = QMenu()
|
|
|
|
|
for type, name in self.columns:
|
|
|
|
|
a = m.addAction(name)
|
|
|
|
|
a.setCheckable(True)
|
|
|
|
|
a.setChecked(type in self.model.activeCols)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
a.toggled.connect(lambda b, t=type: self.toggleField(t))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
m.exec_(gpos)
|
|
|
|
|
|
|
|
|
|
def toggleField(self, type):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._toggleField(type))
|
|
|
|
|
|
|
|
|
|
def _toggleField(self, type):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model.beginReset()
|
|
|
|
|
if type in self.model.activeCols:
|
|
|
|
|
if len(self.model.activeCols) < 2:
|
2018-07-25 11:52:21 +02:00
|
|
|
|
self.model.endReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return showInfo(_("You must have at least one column."))
|
|
|
|
|
self.model.activeCols.remove(type)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
adding = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
|
|
|
|
self.model.activeCols.append(type)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
adding = True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# sorted field may have been hidden
|
|
|
|
|
self.setSortIndicator()
|
|
|
|
|
self.setColumnSizes()
|
|
|
|
|
self.model.endReset()
|
2013-10-04 01:43:46 +02:00
|
|
|
|
# if we added a column, scroll to it
|
|
|
|
|
if adding:
|
|
|
|
|
row = self.currentRow()
|
|
|
|
|
idx = self.model.index(row, len(self.model.activeCols) - 1)
|
|
|
|
|
self.form.tableView.scrollTo(idx)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def setColumnSizes(self):
|
|
|
|
|
hh = self.form.tableView.horizontalHeader()
|
2016-05-31 10:51:40 +02:00
|
|
|
|
hh.setSectionResizeMode(QHeaderView.Interactive)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
hh.setSectionResizeMode(
|
|
|
|
|
hh.logicalIndex(len(self.model.activeCols) - 1), QHeaderView.Stretch
|
|
|
|
|
)
|
2013-11-25 20:57:39 +01:00
|
|
|
|
# this must be set post-resize or it doesn't work
|
|
|
|
|
hh.setCascadingSectionResizes(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onColumnMoved(self, a, b, c):
|
|
|
|
|
self.setColumnSizes()
|
|
|
|
|
|
2017-08-13 11:11:40 +02:00
|
|
|
|
# Sidebar
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
class SidebarTreeView(QTreeView):
|
2019-12-20 00:30:35 +01:00
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.expanded.connect(self.onExpansion)
|
|
|
|
|
self.collapsed.connect(self.onCollapse)
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def onClickCurrent(self) -> None:
|
|
|
|
|
idx = self.currentIndex()
|
|
|
|
|
if idx.isValid():
|
|
|
|
|
item: SidebarItem = idx.internalPointer()
|
2019-12-20 00:30:35 +01:00
|
|
|
|
if item.onClick:
|
|
|
|
|
item.onClick()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
|
2019-12-19 23:52:03 +01:00
|
|
|
|
super().mouseReleaseEvent(event)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.onClickCurrent()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def keyPressEvent(self, event: QKeyEvent) -> None:
|
|
|
|
|
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
|
|
|
|
|
self.onClickCurrent()
|
|
|
|
|
else:
|
|
|
|
|
super().keyPressEvent(event)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-20 00:30:35 +01:00
|
|
|
|
def onExpansion(self, idx: QModelIndex) -> None:
|
|
|
|
|
self._onExpansionChange(idx, True)
|
|
|
|
|
|
|
|
|
|
def onCollapse(self, idx: QModelIndex) -> None:
|
|
|
|
|
self._onExpansionChange(idx, False)
|
|
|
|
|
|
|
|
|
|
def _onExpansionChange(self, idx: QModelIndex, expanded: bool) -> None:
|
|
|
|
|
item: SidebarItem = idx.internalPointer()
|
|
|
|
|
if item.expanded != expanded:
|
|
|
|
|
item.expanded = expanded
|
|
|
|
|
if item.onExpanded:
|
|
|
|
|
item.onExpanded(expanded)
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def setupSidebar(self) -> None:
|
2017-08-13 11:11:40 +02:00
|
|
|
|
dw = self.sidebarDockWidget = QDockWidget(_("Sidebar"), self)
|
|
|
|
|
dw.setFeatures(QDockWidget.DockWidgetClosable)
|
|
|
|
|
dw.setObjectName("Sidebar")
|
|
|
|
|
dw.setAllowedAreas(Qt.LeftDockWidgetArea)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.sidebarTree = self.SidebarTreeView()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
self.sidebarTree.mw = self.mw
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.sidebarTree.setUniformRowHeights(True)
|
|
|
|
|
self.sidebarTree.setHeaderHidden(True)
|
|
|
|
|
self.sidebarTree.setIndentation(15)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.sidebarTree.expanded.connect(self.onSidebarItemExpanded) # type: ignore
|
2017-08-13 11:11:40 +02:00
|
|
|
|
dw.setWidget(self.sidebarTree)
|
2019-12-27 05:23:12 +01:00
|
|
|
|
# match window background color
|
|
|
|
|
bgcolor = QPalette().window().color().name()
|
|
|
|
|
self.sidebarTree.setStyleSheet("QTreeView { background: '%s'; }" % bgcolor)
|
2017-08-15 06:54:13 +02:00
|
|
|
|
self.sidebarDockWidget.setFloating(False)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.sidebarDockWidget.visibilityChanged.connect(self.onSidebarVisChanged) # type: ignore
|
2017-08-15 07:19:22 +02:00
|
|
|
|
self.sidebarDockWidget.setTitleBarWidget(QWidget())
|
2017-08-14 08:57:43 +02:00
|
|
|
|
self.addDockWidget(Qt.LeftDockWidgetArea, dw)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-20 00:27:14 +01:00
|
|
|
|
def onSidebarItemExpanded(self, idx: QModelIndex) -> None:
|
|
|
|
|
item: SidebarItem = idx.internalPointer()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
# item.on
|
2019-12-20 00:27:14 +01:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def onSidebarVisChanged(self, _visible: bool) -> None:
|
|
|
|
|
self.maybeRefreshSidebar()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def focusSidebar(self) -> None:
|
2017-08-13 11:11:40 +02:00
|
|
|
|
self.sidebarDockWidget.setVisible(True)
|
|
|
|
|
self.sidebarTree.setFocus()
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def maybeRefreshSidebar(self) -> None:
|
2017-08-13 11:11:40 +02:00
|
|
|
|
if self.sidebarDockWidget.isVisible():
|
2019-12-19 12:11:12 +01:00
|
|
|
|
# add slight delay to allow browser window to appear first
|
|
|
|
|
def deferredDisplay():
|
|
|
|
|
root = self.buildTree()
|
|
|
|
|
model = SidebarModel(root)
|
|
|
|
|
self.sidebarTree.setModel(model)
|
2019-12-20 00:27:14 +01:00
|
|
|
|
model.expandWhereNeccessary(self.sidebarTree)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
self.mw.progress.timer(10, deferredDisplay, False)
|
|
|
|
|
|
|
|
|
|
def buildTree(self) -> SidebarItem:
|
|
|
|
|
root = SidebarItem("", "")
|
2017-08-28 13:43:44 +02:00
|
|
|
|
self._stdTree(root)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
self._favTree(root)
|
|
|
|
|
self._decksTree(root)
|
|
|
|
|
self._modelTree(root)
|
|
|
|
|
self._userTagTree(root)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
return root
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def _stdTree(self, root) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
for name, filt, icon in [
|
|
|
|
|
[_("Whole Collection"), "", "collection"],
|
|
|
|
|
[_("Current Deck"), "deck:current", "deck"],
|
|
|
|
|
]:
|
2019-12-19 12:11:12 +01:00
|
|
|
|
item = SidebarItem(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
name, ":/icons/{}.svg".format(icon), self._filterFunc(filt)
|
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
root.addChild(item)
|
2017-08-28 13:43:44 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def _favTree(self, root) -> None:
|
|
|
|
|
assert self.col
|
2019-12-23 01:34:10 +01:00
|
|
|
|
saved = self.col.conf.get("savedFilters", {})
|
2017-08-13 11:11:40 +02:00
|
|
|
|
for name, filt in sorted(saved.items()):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
item = SidebarItem(
|
|
|
|
|
name,
|
|
|
|
|
":/icons/heart.svg",
|
|
|
|
|
lambda s=filt: self.setFilter(s), # type: ignore
|
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
root.addChild(item)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def _userTagTree(self, root) -> None:
|
|
|
|
|
assert self.col
|
2017-08-13 11:11:40 +02:00
|
|
|
|
for t in sorted(self.col.tags.all(), key=lambda t: t.lower()):
|
2019-12-19 12:11:12 +01:00
|
|
|
|
item = SidebarItem(
|
2019-12-23 02:31:42 +01:00
|
|
|
|
t, ":/icons/tag.svg", lambda t=t: self.setFilter("tag", t) # type: ignore
|
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
root.addChild(item)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def _decksTree(self, root) -> None:
|
|
|
|
|
assert self.col
|
2017-08-13 11:11:40 +02:00
|
|
|
|
grps = self.col.sched.deckDueTree()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2017-08-13 11:11:40 +02:00
|
|
|
|
def fillGroups(root, grps, head=""):
|
|
|
|
|
for g in grps:
|
2019-12-19 12:11:12 +01:00
|
|
|
|
item = SidebarItem(
|
|
|
|
|
g[0],
|
|
|
|
|
":/icons/deck.svg",
|
2019-12-23 01:34:10 +01:00
|
|
|
|
lambda g=g: self.setFilter("deck", head + g[0]),
|
2019-12-20 00:30:35 +01:00
|
|
|
|
lambda expanded, g=g: self.mw.col.decks.collapseBrowser(g[1]),
|
2019-12-23 01:34:10 +01:00
|
|
|
|
not self.mw.col.decks.get(g[1]).get("browserCollapsed", False),
|
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
root.addChild(item)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
newhead = head + g[0] + "::"
|
2017-08-13 11:11:40 +02:00
|
|
|
|
fillGroups(item, g[5], newhead)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2017-08-13 11:11:40 +02:00
|
|
|
|
fillGroups(root, grps)
|
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
|
def _modelTree(self, root) -> None:
|
|
|
|
|
assert self.col
|
2017-08-13 11:11:40 +02:00
|
|
|
|
for m in sorted(self.col.models.all(), key=itemgetter("name")):
|
2019-12-19 12:11:12 +01:00
|
|
|
|
item = SidebarItem(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
m["name"],
|
|
|
|
|
":/icons/notetype.svg",
|
2019-12-23 02:31:42 +01:00
|
|
|
|
lambda m=m: self.setFilter("note", m["name"]), # type: ignore
|
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
root.addChild(item)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Filter tree
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2017-08-11 11:06:29 +02:00
|
|
|
|
def onFilterButton(self):
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addChild(self._commonFilters())
|
|
|
|
|
ml.addSeparator()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addChild(self._todayFilters())
|
|
|
|
|
ml.addChild(self._cardStateFilters())
|
|
|
|
|
ml.addChild(self._deckFilters())
|
|
|
|
|
ml.addChild(self._noteTypeFilters())
|
|
|
|
|
ml.addChild(self._tagFilters())
|
|
|
|
|
ml.addSeparator()
|
2017-08-12 08:08:10 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addChild(self.sidebarDockWidget.toggleViewAction())
|
|
|
|
|
ml.addSeparator()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addChild(self._savedSearches())
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.popupOver(self.form.filter)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def setFilter(self, *args):
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
txt = args[0]
|
|
|
|
|
else:
|
|
|
|
|
txt = ""
|
|
|
|
|
items = []
|
|
|
|
|
for c, a in enumerate(args):
|
|
|
|
|
if c % 2 == 0:
|
|
|
|
|
txt += a + ":"
|
|
|
|
|
else:
|
|
|
|
|
txt += a
|
2017-09-02 07:42:19 +02:00
|
|
|
|
for chr in " ()":
|
|
|
|
|
if chr in txt:
|
|
|
|
|
txt = '"%s"' % txt
|
|
|
|
|
break
|
2012-12-21 08:51:59 +01:00
|
|
|
|
items.append(txt)
|
|
|
|
|
txt = ""
|
|
|
|
|
txt = " ".join(items)
|
|
|
|
|
if self.mw.app.keyboardModifiers() & Qt.AltModifier:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
txt = "-" + txt
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if self.mw.app.keyboardModifiers() & Qt.ControlModifier:
|
2016-05-12 06:45:35 +02:00
|
|
|
|
cur = str(self.form.searchEdit.lineEdit().text())
|
2016-07-14 12:23:44 +02:00
|
|
|
|
if cur and cur != self._searchPrompt:
|
2019-03-04 07:54:22 +01:00
|
|
|
|
txt = cur + " " + txt
|
2013-04-11 10:07:08 +02:00
|
|
|
|
elif self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
2016-05-12 06:45:35 +02:00
|
|
|
|
cur = str(self.form.searchEdit.lineEdit().text())
|
2013-04-11 10:07:08 +02:00
|
|
|
|
if cur:
|
|
|
|
|
txt = cur + " or " + txt
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.form.searchEdit.lineEdit().setText(txt)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.onSearchActivated()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _simpleFilters(self, items):
|
|
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
for row in items:
|
|
|
|
|
if row is None:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addSeparator()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
else:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
label, filter = row
|
|
|
|
|
ml.addItem(label, self._filterFunc(filter))
|
|
|
|
|
return ml
|
|
|
|
|
|
|
|
|
|
def _filterFunc(self, *args):
|
|
|
|
|
return lambda *, f=args: self.setFilter(*f)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _commonFilters(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return self._simpleFilters(
|
|
|
|
|
((_("Whole Collection"), ""), (_("Current Deck"), "deck:current"))
|
|
|
|
|
)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _todayFilters(self):
|
|
|
|
|
subm = SubMenu(_("Today"))
|
2019-12-23 01:34:10 +01:00
|
|
|
|
subm.addChild(
|
|
|
|
|
self._simpleFilters(
|
|
|
|
|
(
|
|
|
|
|
(_("Added Today"), "added:1"),
|
|
|
|
|
(_("Studied Today"), "rated:1"),
|
|
|
|
|
(_("Again Today"), "rated:1:1"),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-15 10:41:36 +02:00
|
|
|
|
return subm
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _cardStateFilters(self):
|
|
|
|
|
subm = SubMenu(_("Card State"))
|
2019-12-23 01:34:10 +01:00
|
|
|
|
subm.addChild(
|
|
|
|
|
self._simpleFilters(
|
|
|
|
|
(
|
|
|
|
|
(_("New"), "is:new"),
|
|
|
|
|
(_("Learning"), "is:learn"),
|
|
|
|
|
(_("Review"), "is:review"),
|
|
|
|
|
(_("Due"), "is:due"),
|
|
|
|
|
None,
|
|
|
|
|
(_("Suspended"), "is:suspended"),
|
|
|
|
|
(_("Buried"), "is:buried"),
|
|
|
|
|
None,
|
|
|
|
|
(_("Red Flag"), "flag:1"),
|
|
|
|
|
(_("Orange Flag"), "flag:2"),
|
|
|
|
|
(_("Green Flag"), "flag:3"),
|
|
|
|
|
(_("Blue Flag"), "flag:4"),
|
|
|
|
|
(_("No Flag"), "flag:0"),
|
|
|
|
|
(_("Any Flag"), "-flag:0"),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-08-15 10:41:36 +02:00
|
|
|
|
return subm
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _tagFilters(self):
|
|
|
|
|
m = SubMenu(_("Tags"))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
m.addItem(_("Clear Unused"), self.clearUnusedTags)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
m.addSeparator()
|
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
tagList = MenuList()
|
|
|
|
|
for t in sorted(self.col.tags.all(), key=lambda s: s.lower()):
|
|
|
|
|
tagList.addItem(t, self._filterFunc("tag", t))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
m.addChild(tagList.chunked())
|
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
def _deckFilters(self):
|
2017-08-11 11:06:29 +02:00
|
|
|
|
def addDecks(parent, decks):
|
|
|
|
|
for head, did, rev, lrn, new, children in decks:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
name = self.mw.col.decks.get(did)["name"]
|
2017-08-11 11:06:29 +02:00
|
|
|
|
shortname = name.split("::")[-1]
|
|
|
|
|
if children:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
subm = parent.addMenu(shortname)
|
|
|
|
|
subm.addItem(_("Filter"), self._filterFunc("deck", name))
|
|
|
|
|
subm.addSeparator()
|
|
|
|
|
addDecks(subm, children)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
else:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
parent.addItem(shortname, self._filterFunc("deck", name))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
# fixme: could rewrite to avoid calculating due # in the future
|
|
|
|
|
alldecks = self.col.sched.deckDueTree()
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml = MenuList()
|
|
|
|
|
addDecks(ml, alldecks)
|
|
|
|
|
|
|
|
|
|
root = SubMenu(_("Decks"))
|
|
|
|
|
root.addChild(ml.chunked())
|
|
|
|
|
|
|
|
|
|
return root
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _noteTypeFilters(self):
|
|
|
|
|
m = SubMenu(_("Note Types"))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
m.addItem(_("Manage..."), self.mw.onNoteTypes)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
m.addSeparator()
|
2017-08-15 10:41:36 +02:00
|
|
|
|
|
|
|
|
|
noteTypes = MenuList()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
for nt in sorted(self.col.models.all(), key=lambda nt: nt["name"].lower()):
|
2017-08-11 11:06:29 +02:00
|
|
|
|
# no sub menu if it's a single template
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if len(nt["tmpls"]) == 1:
|
|
|
|
|
noteTypes.addItem(nt["name"], self._filterFunc("note", nt["name"]))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
subm = noteTypes.addMenu(nt["name"])
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
subm.addItem(_("All Card Types"), self._filterFunc("note", nt["name"]))
|
2017-08-11 11:06:29 +02:00
|
|
|
|
subm.addSeparator()
|
2017-08-15 10:41:36 +02:00
|
|
|
|
|
|
|
|
|
# add templates
|
2019-12-23 01:34:10 +01:00
|
|
|
|
for c, tmpl in enumerate(nt["tmpls"]):
|
|
|
|
|
# T: name is a card type name. n it's order in the list of card type.
|
|
|
|
|
# T: this is shown in browser's filter, when seeing the list of card type of a note type.
|
|
|
|
|
name = _("%(n)d: %(name)s") % dict(n=c + 1, name=tmpl["name"])
|
|
|
|
|
subm.addItem(
|
|
|
|
|
name, self._filterFunc("note", nt["name"], "card", str(c + 1))
|
|
|
|
|
)
|
2017-08-15 10:41:36 +02:00
|
|
|
|
|
|
|
|
|
m.addChild(noteTypes.chunked())
|
|
|
|
|
return m
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
# Favourites
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
def _savedSearches(self):
|
|
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
# make sure exists
|
|
|
|
|
if "savedFilters" not in self.col.conf:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.conf["savedFilters"] = {}
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addSeparator()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
if self._currentFilterIsSaved():
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addItem(_("Remove Current Filter..."), self._onRemoveFilter)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
else:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addItem(_("Save Current Filter..."), self._onSaveFilter)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
saved = self.col.conf["savedFilters"]
|
2014-04-08 20:35:00 +02:00
|
|
|
|
if not saved:
|
2017-08-15 10:41:36 +02:00
|
|
|
|
return ml
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addSeparator()
|
2014-11-11 00:13:29 +01:00
|
|
|
|
for name, filt in sorted(saved.items()):
|
2017-08-15 10:41:36 +02:00
|
|
|
|
ml.addItem(name, self._filterFunc(filt))
|
|
|
|
|
|
|
|
|
|
return ml
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
def _onSaveFilter(self):
|
|
|
|
|
name = getOnlyText(_("Please give your filter a name:"))
|
|
|
|
|
if not name:
|
|
|
|
|
return
|
|
|
|
|
filt = self.form.searchEdit.lineEdit().text()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.conf["savedFilters"][name] = filt
|
2017-08-11 11:06:29 +02:00
|
|
|
|
self.col.setMod()
|
2017-08-28 13:43:44 +02:00
|
|
|
|
self.maybeRefreshSidebar()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
def _onRemoveFilter(self):
|
|
|
|
|
name = self._currentFilterIsSaved()
|
|
|
|
|
if not askUser(_("Remove %s from your saved searches?") % name):
|
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
|
del self.col.conf["savedFilters"][name]
|
2017-08-11 11:06:29 +02:00
|
|
|
|
self.col.setMod()
|
2017-08-28 13:43:44 +02:00
|
|
|
|
self.maybeRefreshSidebar()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
|
|
# returns name if found
|
|
|
|
|
def _currentFilterIsSaved(self):
|
|
|
|
|
filt = self.form.searchEdit.lineEdit().text()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
for k, v in self.col.conf["savedFilters"].items():
|
2017-08-11 11:06:29 +02:00
|
|
|
|
if filt == v:
|
|
|
|
|
return k
|
|
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Info
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def showCardInfo(self):
|
|
|
|
|
if not self.card:
|
|
|
|
|
return
|
|
|
|
|
info, cs = self._cardInfoData()
|
|
|
|
|
reps = self._revlogData(cs)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2018-06-25 03:56:27 +02:00
|
|
|
|
class CardInfoDialog(QDialog):
|
2019-04-09 08:21:43 +02:00
|
|
|
|
silentlyClose = True
|
|
|
|
|
|
2018-06-25 03:56:27 +02:00
|
|
|
|
def reject(self):
|
|
|
|
|
saveGeom(self, "revlog")
|
|
|
|
|
return QDialog.reject(self)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2018-06-25 03:56:27 +02:00
|
|
|
|
d = CardInfoDialog(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
l = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
l.setContentsMargins(0, 0, 0, 0)
|
2014-09-15 08:04:14 +02:00
|
|
|
|
w = AnkiWebView()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
l.addWidget(w)
|
|
|
|
|
w.stdHtml(info + "<p>" + reps)
|
|
|
|
|
bb = QDialogButtonBox(QDialogButtonBox.Close)
|
|
|
|
|
l.addWidget(bb)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
bb.rejected.connect(d.reject)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
d.setLayout(l)
|
|
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
|
d.resize(500, 400)
|
|
|
|
|
restoreGeom(d, "revlog")
|
2016-05-31 10:51:40 +02:00
|
|
|
|
d.show()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def _cardInfoData(self):
|
|
|
|
|
from anki.stats import CardStats
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
cs = CardStats(self.col, self.card)
|
|
|
|
|
rep = cs.report()
|
|
|
|
|
m = self.card.model()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
rep = (
|
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
<div style='width: 400px; margin: 0 auto 0;
|
2019-12-23 01:34:10 +01:00
|
|
|
|
border: 1px solid #000; padding: 3px; '>%s</div>"""
|
|
|
|
|
% rep
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return rep, cs
|
|
|
|
|
|
|
|
|
|
def _revlogData(self, cs):
|
|
|
|
|
entries = self.mw.col.db.all(
|
|
|
|
|
"select id/1000.0, ease, ivl, factor, time/1000.0, type "
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"from revlog where cid = ?",
|
|
|
|
|
self.card.id,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not entries:
|
|
|
|
|
return ""
|
|
|
|
|
s = "<table width=100%%><tr><th align=left>%s</th>" % _("Date")
|
|
|
|
|
s += ("<th align=right>%s</th>" * 5) % (
|
2019-12-23 01:34:10 +01:00
|
|
|
|
_("Type"),
|
|
|
|
|
_("Rating"),
|
|
|
|
|
_("Interval"),
|
|
|
|
|
_("Ease"),
|
|
|
|
|
_("Time"),
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
cnt = 0
|
|
|
|
|
for (date, ease, ivl, factor, taken, type) in reversed(entries):
|
|
|
|
|
cnt += 1
|
2019-12-23 01:34:10 +01:00
|
|
|
|
s += "<tr><td>%s</td>" % time.strftime(
|
|
|
|
|
_("<b>%Y-%m-%d</b> @ %H:%M"), time.localtime(date)
|
|
|
|
|
)
|
|
|
|
|
tstr = [_("Learn"), _("Review"), _("Relearn"), _("Filtered"), _("Resched")][
|
|
|
|
|
type
|
|
|
|
|
]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
import anki.stats as st
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
fmt = "<span style='color:%s'>%s</span>"
|
|
|
|
|
if type == 0:
|
|
|
|
|
tstr = fmt % (st.colLearn, tstr)
|
|
|
|
|
elif type == 1:
|
|
|
|
|
tstr = fmt % (st.colMature, tstr)
|
|
|
|
|
elif type == 2:
|
|
|
|
|
tstr = fmt % (st.colRelearn, tstr)
|
|
|
|
|
elif type == 3:
|
|
|
|
|
tstr = fmt % (st.colCram, tstr)
|
|
|
|
|
else:
|
|
|
|
|
tstr = fmt % ("#000", tstr)
|
|
|
|
|
if ease == 1:
|
|
|
|
|
ease = fmt % (st.colRelearn, ease)
|
|
|
|
|
if ivl == 0:
|
|
|
|
|
ivl = _("0d")
|
|
|
|
|
elif ivl > 0:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
ivl = fmtTimeSpan(ivl * 86400, short=True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
|
|
|
|
ivl = cs.time(-ivl)
|
|
|
|
|
s += ("<td align=right>%s</td>" * 5) % (
|
|
|
|
|
tstr,
|
2019-12-23 01:34:10 +01:00
|
|
|
|
ease,
|
|
|
|
|
ivl,
|
|
|
|
|
"%d%%" % (factor / 10) if factor else "",
|
|
|
|
|
cs.time(taken),
|
|
|
|
|
) + "</tr>"
|
2012-12-21 08:51:59 +01:00
|
|
|
|
s += "</table>"
|
|
|
|
|
if cnt < self.card.reps:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
s += _(
|
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
|
Note: Some of the history is missing. For more information, \
|
2019-12-23 01:34:10 +01:00
|
|
|
|
please see the browser documentation."""
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
# Menu helpers
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def selectedCards(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return [
|
|
|
|
|
self.model.cards[idx.row()]
|
|
|
|
|
for idx in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
|
]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def selectedNotes(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
return self.col.db.list(
|
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
select distinct nid from cards
|
2019-12-23 01:34:10 +01:00
|
|
|
|
where id in %s"""
|
|
|
|
|
% ids2str(
|
|
|
|
|
[
|
|
|
|
|
self.model.cards[idx.row()]
|
|
|
|
|
for idx in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def selectedNotesAsCards(self):
|
|
|
|
|
return self.col.db.list(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"select id from cards where nid in (%s)"
|
|
|
|
|
% ",".join([str(s) for s in self.selectedNotes()])
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def oneModelNotes(self):
|
|
|
|
|
sf = self.selectedNotes()
|
|
|
|
|
if not sf:
|
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
|
mods = self.col.db.scalar(
|
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
select count(distinct mid) from notes
|
2019-12-23 01:34:10 +01:00
|
|
|
|
where id in %s"""
|
|
|
|
|
% ids2str(sf)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if mods > 1:
|
|
|
|
|
showInfo(_("Please select cards from only one note type."))
|
|
|
|
|
return
|
|
|
|
|
return sf
|
|
|
|
|
|
|
|
|
|
def onHelp(self):
|
|
|
|
|
openHelp("browser")
|
|
|
|
|
|
|
|
|
|
# Misc menu options
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def onChangeModel(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._onChangeModel)
|
|
|
|
|
|
|
|
|
|
def _onChangeModel(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
nids = self.oneModelNotes()
|
|
|
|
|
if nids:
|
|
|
|
|
ChangeModel(self, nids)
|
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
# Preview
|
|
|
|
|
######################################################################
|
|
|
|
|
|
2017-08-08 08:28:53 +02:00
|
|
|
|
_previewTimer = None
|
2020-01-15 22:53:12 +01:00
|
|
|
|
_lastPreviewRender: Union[int, float] = 0
|
2019-02-26 02:18:32 +01:00
|
|
|
|
_lastPreviewState = None
|
2019-11-29 19:46:46 +01:00
|
|
|
|
_previewCardChanged = False
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
def onTogglePreview(self):
|
|
|
|
|
if self._previewWindow:
|
|
|
|
|
self._closePreview()
|
|
|
|
|
else:
|
|
|
|
|
self._openPreview()
|
|
|
|
|
|
|
|
|
|
def _openPreview(self):
|
2013-05-09 08:32:30 +02:00
|
|
|
|
self._previewState = "question"
|
2019-04-08 07:18:27 +02:00
|
|
|
|
self._lastPreviewState = None
|
2013-05-22 03:08:39 +02:00
|
|
|
|
self._previewWindow = QDialog(None, Qt.Window)
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._previewWindow.setWindowTitle(_("Preview"))
|
2016-05-05 00:21:04 +02:00
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self._previewWindow.finished.connect(self._onPreviewFinished)
|
2017-09-08 11:44:13 +02:00
|
|
|
|
self._previewWindow.silentlyClose = True
|
2013-05-03 10:52:46 +02:00
|
|
|
|
vbox = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
vbox.setContentsMargins(0, 0, 0, 0)
|
2014-09-15 08:04:14 +02:00
|
|
|
|
self._previewWeb = AnkiWebView()
|
2013-05-03 10:52:46 +02:00
|
|
|
|
vbox.addWidget(self._previewWeb)
|
|
|
|
|
bbox = QDialogButtonBox()
|
2016-05-05 00:21:04 +02:00
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self._previewReplay = bbox.addButton(
|
|
|
|
|
_("Replay Audio"), QDialogButtonBox.ActionRole
|
|
|
|
|
)
|
2014-06-21 22:35:45 +02:00
|
|
|
|
self._previewReplay.setAutoDefault(False)
|
|
|
|
|
self._previewReplay.setShortcut(QKeySequence("R"))
|
|
|
|
|
self._previewReplay.setToolTip(_("Shortcut key: %s" % "R"))
|
2016-05-05 00:21:04 +02:00
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._previewPrev = bbox.addButton("<", QDialogButtonBox.ActionRole)
|
|
|
|
|
self._previewPrev.setAutoDefault(False)
|
|
|
|
|
self._previewPrev.setShortcut(QKeySequence("Left"))
|
2016-05-05 00:21:04 +02:00
|
|
|
|
self._previewPrev.setToolTip(_("Shortcut key: Left arrow"))
|
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._previewNext = bbox.addButton(">", QDialogButtonBox.ActionRole)
|
2016-05-05 00:21:04 +02:00
|
|
|
|
self._previewNext.setAutoDefault(True)
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._previewNext.setShortcut(QKeySequence("Right"))
|
2016-05-05 00:26:13 +02:00
|
|
|
|
self._previewNext.setToolTip(_("Shortcut key: Right arrow or Enter"))
|
2016-05-05 00:21:04 +02:00
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self._previewPrev.clicked.connect(self._onPreviewPrev)
|
|
|
|
|
self._previewNext.clicked.connect(self._onPreviewNext)
|
|
|
|
|
self._previewReplay.clicked.connect(self._onReplayAudio)
|
2016-05-05 00:21:04 +02:00
|
|
|
|
|
2017-08-28 14:16:05 +02:00
|
|
|
|
self.previewShowBothSides = QCheckBox(_("Show Both Sides"))
|
2017-08-29 16:41:01 +02:00
|
|
|
|
self.previewShowBothSides.setShortcut(QKeySequence("B"))
|
|
|
|
|
self.previewShowBothSides.setToolTip(_("Shortcut key: %s" % "B"))
|
2017-08-28 14:16:05 +02:00
|
|
|
|
bbox.addButton(self.previewShowBothSides, QDialogButtonBox.ActionRole)
|
|
|
|
|
self._previewBothSides = self.col.conf.get("previewBothSides", False)
|
|
|
|
|
self.previewShowBothSides.setChecked(self._previewBothSides)
|
2019-05-17 07:16:13 +02:00
|
|
|
|
self.previewShowBothSides.toggled.connect(self._onPreviewShowBothSides)
|
2017-08-28 14:16:05 +02:00
|
|
|
|
|
2017-08-08 08:28:53 +02:00
|
|
|
|
self._setupPreviewWebview()
|
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
vbox.addWidget(bbox)
|
|
|
|
|
self._previewWindow.setLayout(vbox)
|
|
|
|
|
restoreGeom(self._previewWindow, "preview")
|
|
|
|
|
self._previewWindow.show()
|
|
|
|
|
self._renderPreview(True)
|
|
|
|
|
|
|
|
|
|
def _onPreviewFinished(self, ok):
|
|
|
|
|
saveGeom(self._previewWindow, "preview")
|
|
|
|
|
self.mw.progress.timer(100, self._onClosePreview, False)
|
|
|
|
|
self.form.previewButton.setChecked(False)
|
|
|
|
|
|
|
|
|
|
def _onPreviewPrev(self):
|
2017-08-28 14:16:05 +02:00
|
|
|
|
if self._previewState == "answer" and not self._previewBothSides:
|
2017-03-24 17:09:16 +01:00
|
|
|
|
self._previewState = "question"
|
2013-05-03 10:52:46 +02:00
|
|
|
|
self._renderPreview()
|
|
|
|
|
else:
|
2017-08-01 09:46:08 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._moveCur(QAbstractItemView.MoveUp))
|
2013-05-03 10:52:46 +02:00
|
|
|
|
|
|
|
|
|
def _onPreviewNext(self):
|
|
|
|
|
if self._previewState == "question":
|
|
|
|
|
self._previewState = "answer"
|
|
|
|
|
self._renderPreview()
|
|
|
|
|
else:
|
2017-08-01 09:46:08 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._moveCur(QAbstractItemView.MoveDown))
|
2013-05-03 10:52:46 +02:00
|
|
|
|
|
2014-06-21 22:35:45 +02:00
|
|
|
|
def _onReplayAudio(self):
|
|
|
|
|
self.mw.reviewer.replayAudio(self)
|
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
def _updatePreviewButtons(self):
|
|
|
|
|
if not self._previewWindow:
|
|
|
|
|
return
|
2017-03-24 17:05:33 +01:00
|
|
|
|
current = self.currentRow()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
canBack = current > 0 or (
|
|
|
|
|
current == 0
|
|
|
|
|
and self._previewState == "answer"
|
|
|
|
|
and not self._previewBothSides
|
|
|
|
|
)
|
2013-05-10 08:02:58 +02:00
|
|
|
|
self._previewPrev.setEnabled(not not (self.singleCard and canBack))
|
2019-12-23 01:34:10 +01:00
|
|
|
|
canForward = (
|
|
|
|
|
self.currentRow() < self.model.rowCount(None) - 1
|
|
|
|
|
or self._previewState == "question"
|
|
|
|
|
)
|
2013-05-10 08:02:58 +02:00
|
|
|
|
self._previewNext.setEnabled(not not (self.singleCard and canForward))
|
2013-05-03 10:52:46 +02:00
|
|
|
|
|
|
|
|
|
def _closePreview(self):
|
|
|
|
|
if self._previewWindow:
|
|
|
|
|
self._previewWindow.close()
|
|
|
|
|
self._onClosePreview()
|
|
|
|
|
|
|
|
|
|
def _onClosePreview(self):
|
|
|
|
|
self._previewWindow = self._previewPrev = self._previewNext = None
|
|
|
|
|
|
2017-08-08 08:28:53 +02:00
|
|
|
|
def _setupPreviewWebview(self):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
jsinc = [
|
|
|
|
|
"jquery.js",
|
|
|
|
|
"browsersel.js",
|
|
|
|
|
"mathjax/conf.js",
|
|
|
|
|
"mathjax/MathJax.js",
|
|
|
|
|
"reviewer.js",
|
|
|
|
|
]
|
|
|
|
|
self._previewWeb.stdHtml(
|
|
|
|
|
self.mw.reviewer.revHtml(), css=["reviewer.css"], js=jsinc
|
|
|
|
|
)
|
2020-01-25 07:01:16 +01:00
|
|
|
|
self._previewWeb.set_bridge_command(self._on_preview_bridge_cmd, "preview")
|
|
|
|
|
|
|
|
|
|
def _on_preview_bridge_cmd(self, cmd: str) -> Any:
|
|
|
|
|
if cmd.startswith("play:"):
|
|
|
|
|
play_clicked_audio(cmd, self.card)
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
def _renderPreview(self, cardChanged=False):
|
2017-08-08 08:28:53 +02:00
|
|
|
|
self._cancelPreviewTimer()
|
2019-11-29 19:46:46 +01:00
|
|
|
|
# Keep track of whether _renderPreview() has ever been called
|
|
|
|
|
# with cardChanged=True since the last successful render
|
|
|
|
|
self._previewCardChanged |= cardChanged
|
2017-08-08 08:28:53 +02:00
|
|
|
|
# avoid rendering in quick succession
|
2019-12-23 01:34:10 +01:00
|
|
|
|
elapMS = int((time.time() - self._lastPreviewRender) * 1000)
|
2019-12-05 08:05:20 +01:00
|
|
|
|
delay = 300
|
|
|
|
|
if elapMS < delay:
|
2017-08-08 08:28:53 +02:00
|
|
|
|
self._previewTimer = self.mw.progress.timer(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
delay - elapMS, self._renderScheduledPreview, False
|
|
|
|
|
)
|
2017-08-08 08:28:53 +02:00
|
|
|
|
else:
|
2019-11-29 19:46:46 +01:00
|
|
|
|
self._renderScheduledPreview()
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
|
|
|
|
def _cancelPreviewTimer(self):
|
|
|
|
|
if self._previewTimer:
|
|
|
|
|
self._previewTimer.stop()
|
|
|
|
|
self._previewTimer = None
|
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def _renderScheduledPreview(self) -> None:
|
2017-08-08 12:02:37 +02:00
|
|
|
|
self._cancelPreviewTimer()
|
2017-08-08 08:28:53 +02:00
|
|
|
|
self._lastPreviewRender = time.time()
|
2017-08-08 12:02:37 +02:00
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
|
if not self._previewWindow:
|
|
|
|
|
return
|
|
|
|
|
c = self.card
|
2017-08-10 13:39:04 +02:00
|
|
|
|
func = "_showQuestion"
|
2017-08-10 11:32:19 +02:00
|
|
|
|
if not c or not self.singleCard:
|
2013-05-03 10:52:46 +02:00
|
|
|
|
txt = _("(please select 1 card)")
|
2017-08-08 08:28:53 +02:00
|
|
|
|
bodyclass = ""
|
2019-04-15 04:20:12 +02:00
|
|
|
|
self._lastPreviewState = None
|
2017-08-08 08:28:53 +02:00
|
|
|
|
else:
|
2019-02-26 02:18:32 +01:00
|
|
|
|
if self._previewBothSides:
|
|
|
|
|
self._previewState = "answer"
|
2019-11-29 19:46:46 +01:00
|
|
|
|
elif self._previewCardChanged:
|
2019-02-26 02:18:32 +01:00
|
|
|
|
self._previewState = "question"
|
|
|
|
|
|
|
|
|
|
currentState = self._previewStateAndMod()
|
|
|
|
|
if currentState == self._lastPreviewState:
|
|
|
|
|
# nothing has changed, avoid refreshing
|
|
|
|
|
return
|
|
|
|
|
|
2017-12-04 03:45:22 +01:00
|
|
|
|
# need to force reload even if answer
|
|
|
|
|
txt = c.q(reload=True)
|
|
|
|
|
|
2017-08-08 08:28:53 +02:00
|
|
|
|
if self._previewState == "answer":
|
2017-08-10 13:39:04 +02:00
|
|
|
|
func = "_showAnswer"
|
2017-08-08 08:28:53 +02:00
|
|
|
|
txt = c.a()
|
2017-12-13 05:34:54 +01:00
|
|
|
|
txt = re.sub(r"\[\[type:[^]]+\]\]", "", txt)
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
|
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
|
|
|
|
if self.mw.reviewer.autoplay(c):
|
2020-01-25 06:46:44 +01:00
|
|
|
|
if self._previewBothSides:
|
|
|
|
|
# if we're showing both sides at once, remove any audio
|
|
|
|
|
# from the answer that's appeared on the question already
|
|
|
|
|
question_audio = c.question_av_tags()
|
|
|
|
|
only_on_answer_audio = [
|
|
|
|
|
x for x in c.answer_av_tags() if x not in question_audio
|
|
|
|
|
]
|
|
|
|
|
audio = question_audio + only_on_answer_audio
|
|
|
|
|
elif self._previewState == "question":
|
|
|
|
|
audio = c.question_av_tags()
|
|
|
|
|
else:
|
|
|
|
|
audio = c.answer_av_tags()
|
|
|
|
|
av_player.play_tags(audio)
|
2017-08-08 08:28:53 +02:00
|
|
|
|
|
2020-01-24 02:06:11 +01:00
|
|
|
|
txt = self.mw.prepare_card_text_for_display(txt)
|
2020-01-15 08:45:35 +01:00
|
|
|
|
gui_hooks.card_will_show(
|
|
|
|
|
txt, c, "preview" + self._previewState.capitalize()
|
|
|
|
|
)
|
2019-04-15 04:20:12 +02:00
|
|
|
|
self._lastPreviewState = self._previewStateAndMod()
|
2017-09-08 11:43:35 +02:00
|
|
|
|
self._updatePreviewButtons()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self._previewWeb.eval("{}({},'{}');".format(func, json.dumps(txt), bodyclass))
|
2019-11-29 19:46:46 +01:00
|
|
|
|
self._previewCardChanged = False
|
2013-05-03 10:52:46 +02:00
|
|
|
|
|
2017-08-28 14:16:05 +02:00
|
|
|
|
def _onPreviewShowBothSides(self, toggle):
|
|
|
|
|
self._previewBothSides = toggle
|
|
|
|
|
self.col.conf["previewBothSides"] = toggle
|
|
|
|
|
self.col.setMod()
|
2017-08-29 16:41:01 +02:00
|
|
|
|
if self._previewState == "answer" and not toggle:
|
|
|
|
|
self._previewState = "question"
|
|
|
|
|
self._renderPreview()
|
2017-08-28 14:16:05 +02:00
|
|
|
|
|
2019-02-26 02:18:32 +01:00
|
|
|
|
def _previewStateAndMod(self):
|
2019-03-04 22:50:54 +01:00
|
|
|
|
c = self.card
|
2019-04-08 07:18:40 +02:00
|
|
|
|
n = c.note()
|
|
|
|
|
n.load()
|
|
|
|
|
return (self._previewState, c.id, n.mod)
|
2019-02-26 02:18:32 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Card deletion
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def deleteNotes(self):
|
2018-03-01 04:31:52 +01:00
|
|
|
|
focus = self.focusWidget()
|
|
|
|
|
if focus != self.form.tableView:
|
|
|
|
|
return
|
|
|
|
|
self._deleteNotes()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
|
|
|
|
|
def _deleteNotes(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
nids = self.selectedNotes()
|
|
|
|
|
if not nids:
|
|
|
|
|
return
|
|
|
|
|
self.mw.checkpoint(_("Delete Notes"))
|
|
|
|
|
self.model.beginReset()
|
2014-08-11 21:32:59 +02:00
|
|
|
|
# figure out where to place the cursor after the deletion
|
|
|
|
|
curRow = self.form.tableView.selectionModel().currentIndex().row()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
selectedRows = [
|
|
|
|
|
i.row() for i in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
|
]
|
2014-08-11 21:32:59 +02:00
|
|
|
|
if min(selectedRows) < curRow < max(selectedRows):
|
|
|
|
|
# last selection in middle; place one below last selected item
|
|
|
|
|
move = sum(1 for i in selectedRows if i > curRow)
|
|
|
|
|
newRow = curRow - move
|
|
|
|
|
elif max(selectedRows) <= curRow:
|
|
|
|
|
# last selection at bottom; place one below bottommost selection
|
|
|
|
|
newRow = max(selectedRows) - len(nids) + 1
|
|
|
|
|
else:
|
|
|
|
|
# last selection at top; place one above topmost selection
|
|
|
|
|
newRow = min(selectedRows) - 1
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.col.remNotes(nids)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if len(self.model.cards):
|
2014-08-11 21:32:59 +02:00
|
|
|
|
newRow = min(newRow, len(self.model.cards) - 1)
|
|
|
|
|
newRow = max(newRow, 0)
|
|
|
|
|
self.model.focusedCard = self.model.cards[newRow]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model.endReset()
|
|
|
|
|
self.mw.requireReset()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
tooltip(
|
|
|
|
|
ngettext("%d note deleted.", "%d notes deleted.", len(nids)) % len(nids)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Deck change
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def setDeck(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._setDeck)
|
|
|
|
|
|
|
|
|
|
def _setDeck(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2013-02-20 07:25:59 +01:00
|
|
|
|
cids = self.selectedCards()
|
|
|
|
|
if not cids:
|
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
|
did = self.mw.col.db.scalar("select did from cards where id = ?", cids[0])
|
|
|
|
|
current = self.mw.col.decks.get(did)["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
ret = StudyDeck(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.mw,
|
|
|
|
|
current=current,
|
|
|
|
|
accept=_("Move Cards"),
|
|
|
|
|
title=_("Change Deck"),
|
|
|
|
|
help="browse",
|
|
|
|
|
parent=self,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not ret.name:
|
|
|
|
|
return
|
|
|
|
|
did = self.col.decks.id(ret.name)
|
|
|
|
|
deck = self.col.decks.get(did)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if deck["dyn"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
showWarning(_("Cards can't be manually moved into a filtered deck."))
|
|
|
|
|
return
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
self.mw.checkpoint(_("Change Deck"))
|
|
|
|
|
mod = intTime()
|
|
|
|
|
usn = self.col.usn()
|
|
|
|
|
# normal cards
|
|
|
|
|
scids = ids2str(cids)
|
|
|
|
|
# remove any cards from filtered deck first
|
|
|
|
|
self.col.sched.remFromDyn(cids)
|
|
|
|
|
# then move into new deck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.db.execute(
|
|
|
|
|
"""
|
|
|
|
|
update cards set usn=?, mod=?, did=? where id in """
|
|
|
|
|
+ scids,
|
|
|
|
|
usn,
|
|
|
|
|
mod,
|
|
|
|
|
did,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.model.endReset()
|
2012-12-22 01:44:52 +01:00
|
|
|
|
self.mw.requireReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
# Tags
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def addTags(self, tags=None, label=None, prompt=None, func=None):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(lambda: self._addTags(tags, label, prompt, func))
|
|
|
|
|
|
|
|
|
|
def _addTags(self, tags, label, prompt, func):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if prompt is None:
|
|
|
|
|
prompt = _("Enter tags to add:")
|
|
|
|
|
if tags is None:
|
|
|
|
|
(tags, r) = getTag(self, self.col, prompt)
|
|
|
|
|
else:
|
|
|
|
|
r = True
|
|
|
|
|
if not r:
|
|
|
|
|
return
|
|
|
|
|
if func is None:
|
|
|
|
|
func = self.col.tags.bulkAdd
|
|
|
|
|
if label is None:
|
|
|
|
|
label = _("Add Tags")
|
|
|
|
|
if label:
|
|
|
|
|
self.mw.checkpoint(label)
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
func(self.selectedNotes(), tags)
|
|
|
|
|
self.model.endReset()
|
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
|
|
|
|
|
def deleteTags(self, tags=None, label=None):
|
|
|
|
|
if label is None:
|
|
|
|
|
label = _("Delete Tags")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.addTags(
|
|
|
|
|
tags, label, _("Enter tags to delete:"), func=self.col.tags.bulkRem
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2017-08-11 06:40:51 +02:00
|
|
|
|
def clearUnusedTags(self):
|
|
|
|
|
self.editor.saveNow(self._clearUnusedTags)
|
|
|
|
|
|
|
|
|
|
def _clearUnusedTags(self):
|
|
|
|
|
self.col.tags.registerNotes()
|
|
|
|
|
|
2017-08-12 08:08:10 +02:00
|
|
|
|
# Suspending
|
2012-12-21 08:51:59 +01:00
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def isSuspended(self):
|
|
|
|
|
return not not (self.card and self.card.queue == -1)
|
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
|
def onSuspend(self):
|
|
|
|
|
self.editor.saveNow(self._onSuspend)
|
|
|
|
|
|
|
|
|
|
def _onSuspend(self):
|
|
|
|
|
sus = not self.isSuspended()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
c = self.selectedCards()
|
|
|
|
|
if sus:
|
|
|
|
|
self.col.sched.suspendCards(c)
|
|
|
|
|
else:
|
|
|
|
|
self.col.sched.unsuspendCards(c)
|
|
|
|
|
self.model.reset()
|
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
|
2017-08-16 12:30:29 +02:00
|
|
|
|
# Flags & Marking
|
2017-08-12 08:08:10 +02:00
|
|
|
|
######################################################################
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2017-08-12 08:08:10 +02:00
|
|
|
|
def onSetFlag(self, n):
|
2019-12-06 05:23:54 +01:00
|
|
|
|
if not self.card:
|
|
|
|
|
return
|
2018-11-12 02:11:53 +01:00
|
|
|
|
# flag needs toggling off?
|
|
|
|
|
if n == self.card.userFlag():
|
|
|
|
|
n = 0
|
2017-08-12 08:08:10 +02:00
|
|
|
|
self.col.setUserFlag(n, self.selectedCards())
|
|
|
|
|
self.model.reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2018-11-12 03:10:50 +01:00
|
|
|
|
def _updateFlagsMenu(self):
|
|
|
|
|
flag = self.card and self.card.userFlag()
|
|
|
|
|
flag = flag or 0
|
|
|
|
|
|
|
|
|
|
f = self.form
|
2019-12-23 01:34:10 +01:00
|
|
|
|
flagActions = [
|
|
|
|
|
f.actionRed_Flag,
|
|
|
|
|
f.actionOrange_Flag,
|
|
|
|
|
f.actionGreen_Flag,
|
|
|
|
|
f.actionBlue_Flag,
|
|
|
|
|
]
|
2018-11-12 03:10:50 +01:00
|
|
|
|
|
|
|
|
|
for c, act in enumerate(flagActions):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
act.setChecked(flag == c + 1)
|
2019-02-05 05:37:07 +01:00
|
|
|
|
|
|
|
|
|
qtMenuShortcutWorkaround(self.form.menuFlag)
|
2018-11-12 03:10:50 +01:00
|
|
|
|
|
2017-08-16 12:30:29 +02:00
|
|
|
|
def onMark(self, mark=None):
|
|
|
|
|
if mark is None:
|
|
|
|
|
mark = not self.isMarked()
|
|
|
|
|
if mark:
|
|
|
|
|
self.addTags(tags="marked", label=False)
|
|
|
|
|
else:
|
|
|
|
|
self.deleteTags(tags="marked", label=False)
|
|
|
|
|
|
|
|
|
|
def isMarked(self):
|
|
|
|
|
return not not (self.card and self.card.note().hasTag("Marked"))
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Repositioning
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def reposition(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._reposition)
|
|
|
|
|
|
|
|
|
|
def _reposition(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
cids = self.selectedCards()
|
|
|
|
|
cids2 = self.col.db.list(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"select id from cards where type = 0 and id in " + ids2str(cids)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not cids2:
|
|
|
|
|
return showInfo(_("Only new cards can be repositioned."))
|
|
|
|
|
d = QDialog(self)
|
|
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
|
frm = aqt.forms.reposition.Ui_Dialog()
|
|
|
|
|
frm.setupUi(d)
|
|
|
|
|
(pmin, pmax) = self.col.db.first(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"select min(due), max(due) from cards where type=0 and odid=0"
|
|
|
|
|
)
|
2014-01-14 07:06:22 +01:00
|
|
|
|
pmin = pmin or 0
|
|
|
|
|
pmax = pmax or 0
|
2012-12-21 08:51:59 +01:00
|
|
|
|
txt = _("Queue top: %d") % pmin
|
|
|
|
|
txt += "\n" + _("Queue bottom: %d") % pmax
|
|
|
|
|
frm.label.setText(txt)
|
|
|
|
|
if not d.exec_():
|
|
|
|
|
return
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
self.mw.checkpoint(_("Reposition"))
|
|
|
|
|
self.col.sched.sortCards(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
cids,
|
|
|
|
|
start=frm.start.value(),
|
|
|
|
|
step=frm.step.value(),
|
|
|
|
|
shuffle=frm.randomize.isChecked(),
|
|
|
|
|
shift=frm.shift.isChecked(),
|
|
|
|
|
)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
self.model.endReset()
|
|
|
|
|
|
|
|
|
|
# Rescheduling
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def reschedule(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._reschedule)
|
|
|
|
|
|
|
|
|
|
def _reschedule(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
d = QDialog(self)
|
|
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
|
frm = aqt.forms.reschedule.Ui_Dialog()
|
|
|
|
|
frm.setupUi(d)
|
|
|
|
|
if not d.exec_():
|
|
|
|
|
return
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
self.mw.checkpoint(_("Reschedule"))
|
|
|
|
|
if frm.asNew.isChecked():
|
|
|
|
|
self.col.sched.forgetCards(self.selectedCards())
|
|
|
|
|
else:
|
2013-05-17 09:27:58 +02:00
|
|
|
|
fmin = frm.min.value()
|
|
|
|
|
fmax = frm.max.value()
|
|
|
|
|
fmax = max(fmin, fmax)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.col.sched.reschedCards(self.selectedCards(), fmin, fmax)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
self.model.endReset()
|
|
|
|
|
|
|
|
|
|
# Edit: selection
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def selectNotes(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._selectNotes)
|
|
|
|
|
|
|
|
|
|
def _selectNotes(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
nids = self.selectedNotes()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
# bypass search history
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self._lastSearchTxt = "nid:" + ",".join([str(x) for x in nids])
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.form.searchEdit.lineEdit().setText(self._lastSearchTxt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# clear the selection so we don't waste energy preserving it
|
|
|
|
|
tv = self.form.tableView
|
|
|
|
|
tv.selectionModel().clear()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
tv.selectAll()
|
|
|
|
|
|
|
|
|
|
def invertSelection(self):
|
|
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
|
items = sm.selection()
|
|
|
|
|
self.form.tableView.selectAll()
|
|
|
|
|
sm.select(items, QItemSelectionModel.Deselect | QItemSelectionModel.Rows)
|
|
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
|
# Hooks
|
2012-12-21 08:51:59 +01:00
|
|
|
|
######################################################################
|
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def setupHooks(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
|
gui_hooks.undo_state_did_change.append(self.onUndoState)
|
|
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-01-15 08:45:35 +01:00
|
|
|
|
gui_hooks.editor_did_fire_typing_timer.append(self.refreshCurrentCard)
|
|
|
|
|
gui_hooks.editor_did_load_note.append(self.onLoadNote)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
gui_hooks.editor_did_unfocus_field.append(self.on_unfocus_field)
|
|
|
|
|
hooks.tag_added.append(self.on_item_added)
|
|
|
|
|
hooks.note_type_added.append(self.on_item_added)
|
|
|
|
|
hooks.deck_added.append(self.on_item_added)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def teardownHooks(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
|
gui_hooks.undo_state_did_change.remove(self.onUndoState)
|
|
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2020-01-15 08:45:35 +01:00
|
|
|
|
gui_hooks.editor_did_fire_typing_timer.remove(self.refreshCurrentCard)
|
|
|
|
|
gui_hooks.editor_did_load_note.remove(self.onLoadNote)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
gui_hooks.editor_did_unfocus_field.remove(self.on_unfocus_field)
|
|
|
|
|
hooks.tag_added.remove(self.on_item_added)
|
|
|
|
|
hooks.note_type_added.remove(self.on_item_added)
|
|
|
|
|
hooks.deck_added.remove(self.on_item_added)
|
|
|
|
|
|
2020-01-15 23:53:28 +01:00
|
|
|
|
def on_unfocus_field(self, changed: bool, note: Note, field_idx: int) -> None:
|
2020-01-15 22:53:12 +01:00
|
|
|
|
self.refreshCurrentCard(note)
|
|
|
|
|
|
|
|
|
|
# covers the tag, note and deck case
|
2020-01-15 23:53:28 +01:00
|
|
|
|
def on_item_added(self, item: Any) -> None:
|
2020-01-15 22:53:12 +01:00
|
|
|
|
self.maybeRefreshSidebar()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onUndoState(self, on):
|
|
|
|
|
self.form.actionUndo.setEnabled(on)
|
|
|
|
|
if on:
|
|
|
|
|
self.form.actionUndo.setText(self.mw.form.actionUndo.text())
|
|
|
|
|
|
|
|
|
|
# Edit: replacing
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def onFindReplace(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._onFindReplace)
|
|
|
|
|
|
|
|
|
|
def _onFindReplace(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
sf = self.selectedNotes()
|
|
|
|
|
if not sf:
|
|
|
|
|
return
|
|
|
|
|
import anki.find
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2018-11-27 07:35:04 +01:00
|
|
|
|
fields = anki.find.fieldNamesForNotes(self.mw.col, sf)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
d = QDialog(self)
|
|
|
|
|
frm = aqt.forms.findreplace.Ui_Dialog()
|
|
|
|
|
frm.setupUi(d)
|
|
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
|
frm.field.addItems([_("All Fields")] + fields)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
frm.buttonBox.helpRequested.connect(self.onFindReplaceHelp)
|
2014-06-18 20:47:45 +02:00
|
|
|
|
restoreGeom(d, "findreplace")
|
|
|
|
|
r = d.exec_()
|
|
|
|
|
saveGeom(d, "findreplace")
|
|
|
|
|
if not r:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return
|
|
|
|
|
if frm.field.currentIndex() == 0:
|
|
|
|
|
field = None
|
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
field = fields[frm.field.currentIndex() - 1]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.checkpoint(_("Find and Replace"))
|
|
|
|
|
self.mw.progress.start()
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
try:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
changed = self.col.findReplace(
|
|
|
|
|
sf,
|
|
|
|
|
str(frm.find.text()),
|
|
|
|
|
str(frm.replace.text()),
|
|
|
|
|
frm.re.isChecked(),
|
|
|
|
|
field,
|
|
|
|
|
frm.ignoreCase.isChecked(),
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
except sre_constants.error:
|
|
|
|
|
showInfo(_("Invalid regular expression."), parent=self)
|
|
|
|
|
return
|
|
|
|
|
else:
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
finally:
|
|
|
|
|
self.model.endReset()
|
|
|
|
|
self.mw.progress.finish()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
showInfo(
|
|
|
|
|
ngettext(
|
|
|
|
|
"%(a)d of %(b)d note updated", "%(a)d of %(b)d notes updated", len(sf)
|
|
|
|
|
)
|
|
|
|
|
% {"a": changed, "b": len(sf),},
|
|
|
|
|
parent=self,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onFindReplaceHelp(self):
|
|
|
|
|
openHelp("findreplace")
|
|
|
|
|
|
|
|
|
|
# Edit: finding dupes
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def onFindDupes(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._onFindDupes)
|
|
|
|
|
|
|
|
|
|
def _onFindDupes(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
d = QDialog(self)
|
2016-07-04 05:22:35 +02:00
|
|
|
|
self.mw.setupDialogGC(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
frm = aqt.forms.finddupes.Ui_Dialog()
|
|
|
|
|
frm.setupUi(d)
|
|
|
|
|
restoreGeom(d, "findDupes")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
fields = sorted(
|
|
|
|
|
anki.find.fieldNames(self.col, downcase=False), key=lambda x: x.lower()
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
frm.fields.addItems(fields)
|
2013-10-03 17:07:11 +02:00
|
|
|
|
self._dupesButton = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# links
|
2020-01-22 01:46:35 +01:00
|
|
|
|
frm.webView.set_bridge_command(self.dupeLinkClicked, "find_dupes")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def onFin(code):
|
|
|
|
|
saveGeom(d, "findDupes")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
|
d.finished.connect(onFin)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def onClick():
|
|
|
|
|
field = fields[frm.fields.currentIndex()]
|
2013-10-03 17:07:11 +02:00
|
|
|
|
self.duplicatesReport(frm.webView, field, frm.search.text(), frm)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
|
|
search = frm.buttonBox.addButton(_("Search"), QDialogButtonBox.ActionRole)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
search.clicked.connect(onClick)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
d.show()
|
|
|
|
|
|
2013-10-03 17:07:11 +02:00
|
|
|
|
def duplicatesReport(self, web, fname, search, frm):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.mw.progress.start()
|
|
|
|
|
res = self.mw.col.findDupes(fname, search)
|
2013-10-03 17:07:11 +02:00
|
|
|
|
if not self._dupesButton:
|
|
|
|
|
self._dupesButton = b = frm.buttonBox.addButton(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
_("Tag Duplicates"), QDialogButtonBox.ActionRole
|
|
|
|
|
)
|
2016-05-31 10:51:40 +02:00
|
|
|
|
b.clicked.connect(lambda: self._onTagDupes(res))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
t = "<html><body>"
|
|
|
|
|
groups = len(res)
|
|
|
|
|
notes = sum(len(r[1]) for r in res)
|
|
|
|
|
part1 = ngettext("%d group", "%d groups", groups) % groups
|
|
|
|
|
part2 = ngettext("%d note", "%d notes", notes) % notes
|
|
|
|
|
t += _("Found %(a)s across %(b)s.") % dict(a=part1, b=part2)
|
|
|
|
|
t += "<p><ol>"
|
|
|
|
|
for val, nids in res:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
t += (
|
|
|
|
|
"""<li><a href=# onclick="pycmd('%s');return false;">%s</a>: %s</a>"""
|
|
|
|
|
% (
|
|
|
|
|
"nid:" + ",".join(str(id) for id in nids),
|
|
|
|
|
ngettext("%d note", "%d notes", len(nids)) % len(nids),
|
|
|
|
|
html.escape(val),
|
|
|
|
|
)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
t += "</ol>"
|
|
|
|
|
t += "</body></html>"
|
|
|
|
|
web.setHtml(t)
|
|
|
|
|
self.mw.progress.finish()
|
|
|
|
|
|
2013-10-03 17:07:11 +02:00
|
|
|
|
def _onTagDupes(self, res):
|
|
|
|
|
if not res:
|
|
|
|
|
return
|
|
|
|
|
self.model.beginReset()
|
|
|
|
|
self.mw.checkpoint(_("Tag Duplicates"))
|
|
|
|
|
nids = set()
|
|
|
|
|
for s, nidlist in res:
|
|
|
|
|
nids.update(nidlist)
|
|
|
|
|
self.col.tags.bulkAdd(nids, _("duplicate"))
|
|
|
|
|
self.mw.progress.finish()
|
|
|
|
|
self.model.endReset()
|
|
|
|
|
self.mw.requireReset()
|
|
|
|
|
tooltip(_("Notes tagged."))
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def dupeLinkClicked(self, link):
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self.form.searchEdit.lineEdit().setText(link)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
# manually, because we've already saved
|
|
|
|
|
self._lastSearchTxt = link
|
|
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.onNote()
|
|
|
|
|
|
|
|
|
|
# Jumping
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
def _moveCur(self, dir=None, idx=None):
|
|
|
|
|
if not self.model.cards:
|
|
|
|
|
return
|
|
|
|
|
tv = self.form.tableView
|
|
|
|
|
if idx is None:
|
|
|
|
|
idx = tv.moveCursor(dir, self.mw.app.keyboardModifiers())
|
2017-08-05 07:15:19 +02:00
|
|
|
|
tv.selectionModel().setCurrentIndex(
|
|
|
|
|
idx,
|
2019-12-23 01:34:10 +01:00
|
|
|
|
QItemSelectionModel.Clear
|
|
|
|
|
| QItemSelectionModel.Select
|
|
|
|
|
| QItemSelectionModel.Rows,
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onPreviousCard(self):
|
2017-08-05 07:15:19 +02:00
|
|
|
|
self.focusTo = self.editor.currentField
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._onPreviousCard)
|
|
|
|
|
|
|
|
|
|
def _onPreviousCard(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self._moveCur(QAbstractItemView.MoveUp)
|
|
|
|
|
|
|
|
|
|
def onNextCard(self):
|
2017-08-05 07:15:19 +02:00
|
|
|
|
self.focusTo = self.editor.currentField
|
2016-07-14 12:23:44 +02:00
|
|
|
|
self.editor.saveNow(self._onNextCard)
|
|
|
|
|
|
|
|
|
|
def _onNextCard(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self._moveCur(QAbstractItemView.MoveDown)
|
|
|
|
|
|
|
|
|
|
def onFirstCard(self):
|
|
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
|
idx = sm.currentIndex()
|
|
|
|
|
self._moveCur(None, self.model.index(0, 0))
|
|
|
|
|
if not self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
|
|
|
|
return
|
|
|
|
|
idx2 = sm.currentIndex()
|
|
|
|
|
item = QItemSelection(idx2, idx)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onLastCard(self):
|
|
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
|
idx = sm.currentIndex()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self._moveCur(None, self.model.index(len(self.model.cards) - 1, 0))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
if not self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
|
|
|
|
return
|
|
|
|
|
idx2 = sm.currentIndex()
|
|
|
|
|
item = QItemSelection(idx, idx2)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onFind(self):
|
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
|
self.form.searchEdit.lineEdit().selectAll()
|
|
|
|
|
|
|
|
|
|
def onNote(self):
|
2013-01-29 01:49:04 +01:00
|
|
|
|
self.editor.web.setFocus()
|
2019-12-06 05:22:49 +01:00
|
|
|
|
self.editor.loadNote(focusTo=0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onCardList(self):
|
|
|
|
|
self.form.tableView.setFocus()
|
|
|
|
|
|
|
|
|
|
def focusCid(self, cid):
|
|
|
|
|
try:
|
|
|
|
|
row = self.model.cards.index(cid)
|
|
|
|
|
except:
|
|
|
|
|
return
|
|
|
|
|
self.form.tableView.selectRow(row)
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
# Change model dialog
|
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
class ChangeModel(QDialog):
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def __init__(self, browser, nids) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
QDialog.__init__(self, browser)
|
|
|
|
|
self.browser = browser
|
|
|
|
|
self.nids = nids
|
|
|
|
|
self.oldModel = browser.card.note().model()
|
|
|
|
|
self.form = aqt.forms.changemodel.Ui_Dialog()
|
|
|
|
|
self.form.setupUi(self)
|
|
|
|
|
self.setWindowModality(Qt.WindowModal)
|
|
|
|
|
self.setup()
|
|
|
|
|
restoreGeom(self, "changeModel")
|
2020-01-15 07:53:24 +01:00
|
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
gui_hooks.current_note_type_did_change.append(self.on_note_type_change)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.exec_()
|
|
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
|
def on_note_type_change(self, notetype: NoteType) -> None:
|
|
|
|
|
self.onReset()
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
def setup(self):
|
|
|
|
|
# maps
|
|
|
|
|
self.flayout = QHBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.flayout.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.fwidg = None
|
|
|
|
|
self.form.fieldMap.setLayout(self.flayout)
|
|
|
|
|
self.tlayout = QHBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.tlayout.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.twidg = None
|
|
|
|
|
self.form.templateMap.setLayout(self.tlayout)
|
|
|
|
|
if self.style().objectName() == "gtk+":
|
|
|
|
|
# gtk+ requires margins in inner layout
|
|
|
|
|
self.form.verticalLayout_2.setContentsMargins(0, 11, 0, 0)
|
|
|
|
|
self.form.verticalLayout_3.setContentsMargins(0, 11, 0, 0)
|
|
|
|
|
# model chooser
|
|
|
|
|
import aqt.modelchooser
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.oldModel = self.browser.col.models.get(
|
|
|
|
|
self.browser.col.db.scalar(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
"select mid from notes where id = ?", self.nids[0]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
self.form.oldModelLabel.setText(self.oldModel["name"])
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.modelChooser = aqt.modelchooser.ModelChooser(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
self.browser.mw, self.form.modelChooserWidget, label=False
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.modelChooser.models.setFocus()
|
2016-05-31 10:51:40 +02:00
|
|
|
|
self.form.buttonBox.helpRequested.connect(self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.modelChanged(self.browser.mw.col.models.current())
|
|
|
|
|
self.pauseUpdate = False
|
|
|
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
|
self.modelChanged(self.browser.col.models.current())
|
|
|
|
|
|
|
|
|
|
def modelChanged(self, model):
|
|
|
|
|
self.targetModel = model
|
|
|
|
|
self.rebuildTemplateMap()
|
|
|
|
|
self.rebuildFieldMap()
|
|
|
|
|
|
|
|
|
|
def rebuildTemplateMap(self, key=None, attr=None):
|
|
|
|
|
if not key:
|
|
|
|
|
key = "t"
|
|
|
|
|
attr = "tmpls"
|
|
|
|
|
map = getattr(self, key + "widg")
|
|
|
|
|
lay = getattr(self, key + "layout")
|
|
|
|
|
src = self.oldModel[attr]
|
|
|
|
|
dst = self.targetModel[attr]
|
|
|
|
|
if map:
|
|
|
|
|
lay.removeWidget(map)
|
|
|
|
|
map.deleteLater()
|
|
|
|
|
setattr(self, key + "MapWidget", None)
|
|
|
|
|
map = QWidget()
|
|
|
|
|
l = QGridLayout()
|
|
|
|
|
combos = []
|
2019-12-23 01:34:10 +01:00
|
|
|
|
targets = [x["name"] for x in dst] + [_("Nothing")]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
indices = {}
|
|
|
|
|
for i, x in enumerate(src):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
l.addWidget(QLabel(_("Change %s to:") % x["name"]), i, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
cb = QComboBox()
|
|
|
|
|
cb.addItems(targets)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
idx = min(i, len(targets) - 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
cb.setCurrentIndex(idx)
|
|
|
|
|
indices[cb] = idx
|
2016-05-31 10:51:40 +02:00
|
|
|
|
cb.currentIndexChanged.connect(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
lambda i, cb=cb, key=key: self.onComboChanged(i, cb, key)
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
combos.append(cb)
|
|
|
|
|
l.addWidget(cb, i, 1)
|
|
|
|
|
map.setLayout(l)
|
|
|
|
|
lay.addWidget(map)
|
|
|
|
|
setattr(self, key + "widg", map)
|
|
|
|
|
setattr(self, key + "layout", lay)
|
|
|
|
|
setattr(self, key + "combos", combos)
|
|
|
|
|
setattr(self, key + "indices", indices)
|
|
|
|
|
|
|
|
|
|
def rebuildFieldMap(self):
|
|
|
|
|
return self.rebuildTemplateMap(key="f", attr="flds")
|
|
|
|
|
|
|
|
|
|
def onComboChanged(self, i, cb, key):
|
|
|
|
|
indices = getattr(self, key + "indices")
|
|
|
|
|
if self.pauseUpdate:
|
|
|
|
|
indices[cb] = i
|
|
|
|
|
return
|
|
|
|
|
combos = getattr(self, key + "combos")
|
|
|
|
|
if i == cb.count() - 1:
|
|
|
|
|
# set to 'nothing'
|
|
|
|
|
return
|
|
|
|
|
# find another combo with same index
|
|
|
|
|
for c in combos:
|
|
|
|
|
if c == cb:
|
|
|
|
|
continue
|
|
|
|
|
if c.currentIndex() == i:
|
|
|
|
|
self.pauseUpdate = True
|
|
|
|
|
c.setCurrentIndex(indices[cb])
|
|
|
|
|
self.pauseUpdate = False
|
|
|
|
|
break
|
|
|
|
|
indices[cb] = i
|
|
|
|
|
|
|
|
|
|
def getTemplateMap(self, old=None, combos=None, new=None):
|
|
|
|
|
if not old:
|
2019-12-23 01:34:10 +01:00
|
|
|
|
old = self.oldModel["tmpls"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
combos = self.tcombos
|
2019-12-23 01:34:10 +01:00
|
|
|
|
new = self.targetModel["tmpls"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
map = {}
|
|
|
|
|
for i, f in enumerate(old):
|
|
|
|
|
idx = combos[i].currentIndex()
|
|
|
|
|
if idx == len(new):
|
|
|
|
|
# ignore
|
2019-12-23 01:34:10 +01:00
|
|
|
|
map[f["ord"]] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
else:
|
|
|
|
|
f2 = new[idx]
|
2019-12-23 01:34:10 +01:00
|
|
|
|
map[f["ord"]] = f2["ord"]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return map
|
|
|
|
|
|
|
|
|
|
def getFieldMap(self):
|
|
|
|
|
return self.getTemplateMap(
|
2019-12-23 01:34:10 +01:00
|
|
|
|
old=self.oldModel["flds"], combos=self.fcombos, new=self.targetModel["flds"]
|
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
|
def cleanup(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
gui_hooks.current_note_type_did_change.remove(self.on_note_type_change)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
self.modelChooser.cleanup()
|
|
|
|
|
saveGeom(self, "changeModel")
|
|
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
|
self.cleanup()
|
|
|
|
|
return QDialog.reject(self)
|
|
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
|
# check maps
|
|
|
|
|
fmap = self.getFieldMap()
|
|
|
|
|
cmap = self.getTemplateMap()
|
2016-05-12 06:45:35 +02:00
|
|
|
|
if any(True for c in list(cmap.values()) if c is None):
|
2019-12-23 01:34:10 +01:00
|
|
|
|
if not askUser(
|
|
|
|
|
_(
|
|
|
|
|
"""\
|
2012-12-21 08:51:59 +01:00
|
|
|
|
Any cards mapped to nothing will be deleted. \
|
|
|
|
|
If a note has no remaining cards, it will be lost. \
|
2019-12-23 01:34:10 +01:00
|
|
|
|
Are you sure you want to continue?"""
|
|
|
|
|
)
|
|
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
|
return
|
|
|
|
|
self.browser.mw.checkpoint(_("Change Note Type"))
|
|
|
|
|
b = self.browser
|
2016-07-14 13:49:40 +02:00
|
|
|
|
b.mw.col.modSchema(check=True)
|
|
|
|
|
b.mw.progress.start()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
b.model.beginReset()
|
|
|
|
|
mm = b.mw.col.models
|
|
|
|
|
mm.change(self.oldModel, self.nids, self.targetModel, fmap, cmap)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
b.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
b.model.endReset()
|
|
|
|
|
b.mw.progress.finish()
|
|
|
|
|
b.mw.reset()
|
|
|
|
|
self.cleanup()
|
make sure change note type clears up hooks
if an error occurred after QDialog.accept() had been called,
the hook was left lying around and caused errors when reset later
fired
File "aqt\main.py", line 1028, in onCheckDB
File "aqt\main.py", line 516, in reset
File "anki\hooks.py", line 28, in runHook
File "aqt\modelchooser.py", line 47, in onReset
File "aqt\modelchooser.py", line 82, in updateModels
<class 'RuntimeError'>: wrapped C/C++ object of type QPushButton has been deleted
2018-12-15 03:45:17 +01:00
|
|
|
|
QDialog.accept(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
|
def onHelp(self):
|
|
|
|
|
openHelp("browsermisc")
|