2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
import sre_constants
|
|
|
|
import cgi
|
|
|
|
import time
|
|
|
|
import re
|
2012-12-22 01:17:10 +01:00
|
|
|
from operator import itemgetter
|
2013-10-03 17:07:11 +02:00
|
|
|
from anki.lang import ngettext
|
2013-05-03 10:52:46 +02:00
|
|
|
|
|
|
|
from aqt.qt import *
|
|
|
|
import anki
|
|
|
|
import aqt.forms
|
2017-03-14 07:48:40 +01:00
|
|
|
from anki.utils import fmtTimeSpan, ids2str, stripHTMLMedia, htmlToTextLine, isWin, intTime, isMac
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter, \
|
|
|
|
saveHeader, restoreHeader, saveState, restoreState, applyStyles, getTag, \
|
2016-07-07 15:39:48 +02:00
|
|
|
showInfo, askUser, tooltip, openHelp, showWarning, shortcut, mungeQA
|
2012-12-21 08:51:59 +01:00
|
|
|
from anki.hooks import runHook, addHook, remHook
|
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
from aqt.toolbar import Toolbar
|
|
|
|
from anki.consts import *
|
2013-05-03 10:52:46 +02:00
|
|
|
from anki.sound import playFromText, clearAudioQueue
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
COLOUR_SUSPENDED = "#FFFFB2"
|
|
|
|
COLOUR_MARKED = "#D9B2E9"
|
|
|
|
|
|
|
|
# fixme: need to refresh after undo
|
|
|
|
|
|
|
|
# Data model
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
class DataModel(QAbstractTableModel):
|
|
|
|
|
|
|
|
def __init__(self, browser):
|
|
|
|
QAbstractTableModel.__init__(self)
|
|
|
|
self.browser = browser
|
|
|
|
self.col = browser.col
|
|
|
|
self.sortKey = None
|
|
|
|
self.activeCols = self.col.conf.get(
|
|
|
|
"activeCols", ["noteFld", "template", "cardDue", "deck"])
|
|
|
|
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:
|
2012-12-22 02:10:52 +01:00
|
|
|
if self.activeCols[index.column()] not in (
|
|
|
|
"question", "answer", "noteFld"):
|
|
|
|
return
|
|
|
|
f = QFont()
|
|
|
|
row = index.row()
|
|
|
|
c = self.getCard(index)
|
|
|
|
t = c.template()
|
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
|
|
|
|
elif role == Qt.TextAlignmentRole:
|
2012-12-21 08:51:59 +01:00
|
|
|
align = Qt.AlignVCenter
|
|
|
|
if self.activeCols[index.column()] not in ("question", "answer",
|
|
|
|
"template", "deck", "noteFld", "note"):
|
|
|
|
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
|
2014-01-13 11:07:34 +01:00
|
|
|
# handle case where extension has set an invalid column type
|
|
|
|
if not txt:
|
|
|
|
txt = self.browser.columns[0][1]
|
2012-12-21 08:51:59 +01:00
|
|
|
return txt
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def flags(self, index):
|
|
|
|
return Qt.ItemFlag(Qt.ItemIsEnabled |
|
|
|
|
Qt.ItemIsSelectable)
|
|
|
|
|
|
|
|
# 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
|
2012-12-21 08:51:59 +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])
|
|
|
|
if getattr(self.browser, 'card', None):
|
|
|
|
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())
|
2016-06-07 08:32:19 +02:00
|
|
|
# we save and then restore the horizontal scroll position because
|
|
|
|
# scrollTo() also scrolls horizontally which is confusing
|
|
|
|
h = tv.horizontalScrollBar().value()
|
2012-12-21 08:51:59 +01:00
|
|
|
tv.scrollTo(idx, tv.PositionAtCenter)
|
2016-06-07 08:32:19 +02:00
|
|
|
tv.horizontalScrollBar().setValue(h)
|
2012-12-21 08:51:59 +01:00
|
|
|
if count < 500:
|
|
|
|
# discard large selections; they're too slow
|
|
|
|
sm.select(items, QItemSelectionModel.SelectCurrent |
|
|
|
|
QItemSelectionModel.Rows)
|
|
|
|
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":
|
|
|
|
t = c.template()['name']
|
|
|
|
if c.model()['type'] == MODEL_CLOZE:
|
|
|
|
t += " %d" % (c.ord+1)
|
|
|
|
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":
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(c.note().id/1000))
|
|
|
|
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":
|
|
|
|
return c.model()['name']
|
|
|
|
elif type == "cardIvl":
|
|
|
|
if c.type == 0:
|
|
|
|
return _("(new)")
|
|
|
|
elif c.type == 1:
|
|
|
|
return _("(learning)")
|
|
|
|
return fmtTimeSpan(c.ivl*86400)
|
|
|
|
elif type == "cardEase":
|
|
|
|
if c.type == 0:
|
|
|
|
return _("(new)")
|
|
|
|
return "%d%%" % (c.factor/10)
|
|
|
|
elif type == "deck":
|
|
|
|
if c.odid:
|
|
|
|
# in a cram deck
|
|
|
|
return "%s (%s)" % (
|
|
|
|
self.browser.mw.col.decks.name(c.did),
|
|
|
|
self.browser.mw.col.decks.name(c.odid))
|
|
|
|
# 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):
|
|
|
|
if c.template().get('bafmt'):
|
|
|
|
# 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):
|
|
|
|
return a[len(q):].strip()
|
|
|
|
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)
|
|
|
|
elif c.queue in (2,3) or (c.type == 2 and c.queue < 0):
|
|
|
|
date = time.time() + ((c.due - self.col.sched.today)*86400)
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
return time.strftime("%Y-%m-%d", time.localtime(date))
|
|
|
|
|
|
|
|
# Line painter
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class StatusDelegate(QItemDelegate):
|
|
|
|
|
|
|
|
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
|
2012-12-21 08:51:59 +01:00
|
|
|
col = None
|
|
|
|
if c.note().hasTag("Marked"):
|
|
|
|
col = COLOUR_MARKED
|
|
|
|
elif c.queue == -1:
|
|
|
|
col = COLOUR_SUSPENDED
|
|
|
|
if col:
|
|
|
|
brush = QBrush(QColor(col))
|
|
|
|
painter.save()
|
|
|
|
painter.fillRect(option.rect, brush)
|
|
|
|
painter.restore()
|
|
|
|
return QItemDelegate.paint(self, painter, option, index)
|
|
|
|
|
|
|
|
# Browser window
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# fixme: respond to reset+edit hooks
|
|
|
|
|
|
|
|
class Browser(QMainWindow):
|
|
|
|
|
|
|
|
def __init__(self, mw):
|
|
|
|
QMainWindow.__init__(self, None, Qt.Window)
|
|
|
|
applyStyles(self)
|
|
|
|
self.mw = mw
|
|
|
|
self.col = self.mw.col
|
2017-04-12 10:40:37 +02:00
|
|
|
self.forceClose = False
|
2012-12-21 08:51:59 +01:00
|
|
|
self.lastFilter = ""
|
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)
|
|
|
|
restoreGeom(self, "editor", 0)
|
|
|
|
restoreState(self, "editor")
|
|
|
|
restoreSplitter(self.form.splitter_2, "editor2")
|
|
|
|
restoreSplitter(self.form.splitter, "editor3")
|
|
|
|
self.form.splitter_2.setChildrenCollapsible(False)
|
|
|
|
self.form.splitter.setChildrenCollapsible(False)
|
|
|
|
self.card = None
|
|
|
|
self.setupToolbar()
|
|
|
|
self.setupColumns()
|
|
|
|
self.setupTable()
|
|
|
|
self.setupMenus()
|
|
|
|
self.setupTree()
|
|
|
|
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()
|
|
|
|
|
|
|
|
def setupToolbar(self):
|
|
|
|
self.toolbarWeb = AnkiWebView()
|
2016-07-07 09:29:53 +02:00
|
|
|
self.toolbarWeb.title = "browser toolbar"
|
2012-12-21 08:51:59 +01:00
|
|
|
self.toolbar = BrowserToolbar(self.mw, self.toolbarWeb, self)
|
|
|
|
self.form.verticalLayout_3.insertWidget(0, self.toolbarWeb)
|
|
|
|
self.toolbar.draw()
|
|
|
|
|
|
|
|
def setupMenus(self):
|
|
|
|
# actions
|
2016-05-31 10:51:40 +02:00
|
|
|
f = self.form
|
2013-05-31 07:16:37 +02:00
|
|
|
if not isMac:
|
|
|
|
f.actionClose.setVisible(False)
|
2016-05-31 10:51:40 +02:00
|
|
|
f.actionReposition.triggered.connect(self.reposition)
|
|
|
|
f.actionReschedule.triggered.connect(self.reschedule)
|
|
|
|
f.actionChangeModel.triggered.connect(self.onChangeModel)
|
2012-12-21 08:51:59 +01:00
|
|
|
# edit
|
2016-05-31 10:51:40 +02:00
|
|
|
f.actionUndo.triggered.connect(self.mw.onUndo)
|
|
|
|
f.previewButton.clicked.connect(self.onTogglePreview)
|
2013-05-22 03:04:34 +02:00
|
|
|
f.previewButton.setToolTip(_("Preview Selected Card (%s)") %
|
|
|
|
shortcut(_("Ctrl+Shift+P")))
|
2016-05-31 10:51:40 +02:00
|
|
|
f.actionInvertSelection.triggered.connect(self.invertSelection)
|
|
|
|
f.actionSelectNotes.triggered.connect(self.selectNotes)
|
|
|
|
f.actionFindReplace.triggered.connect(self.onFindReplace)
|
|
|
|
f.actionFindDuplicates.triggered.connect(self.onFindDupes)
|
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)
|
|
|
|
f.actionTags.triggered.connect(self.onTags)
|
|
|
|
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)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.pgUpCut.activated.connect(self.onFirstCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pgDownCut = QShortcut(QKeySequence("Shift+End"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.pgDownCut.activated.connect(self.onLastCard)
|
2016-02-26 17:32:04 +01:00
|
|
|
# add note
|
|
|
|
self.addCut = QShortcut(QKeySequence("Ctrl+E"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.addCut.activated.connect(self.mw.onAddCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
# card info
|
|
|
|
self.infoCut = QShortcut(QKeySequence("Ctrl+Shift+I"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.infoCut.activated.connect(self.showCardInfo)
|
2012-12-21 08:51:59 +01:00
|
|
|
# set deck
|
|
|
|
self.changeDeckCut = QShortcut(QKeySequence("Ctrl+D"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.changeDeckCut.activated.connect(self.setDeck)
|
2012-12-21 08:51:59 +01:00
|
|
|
# add/remove tags
|
|
|
|
self.tagCut1 = QShortcut(QKeySequence("Ctrl+Shift+T"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.tagCut1.activated.connect(self.addTags)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.tagCut2 = QShortcut(QKeySequence("Ctrl+Alt+T"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.tagCut2.activated.connect(self.deleteTags)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.tagCut3 = QShortcut(QKeySequence("Ctrl+K"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.tagCut3.activated.connect(self.onMark)
|
2016-02-21 12:10:02 +01:00
|
|
|
# suspending
|
|
|
|
self.susCut1 = QShortcut(QKeySequence("Ctrl+J"), self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.susCut1.activated.connect(self.onSuspend)
|
2012-12-21 08:51:59 +01:00
|
|
|
# deletion
|
|
|
|
self.delCut1 = QShortcut(QKeySequence("Delete"), self)
|
|
|
|
self.delCut1.setAutoRepeat(False)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.delCut1.activated.connect(self.deleteNotes)
|
2012-12-21 08:51:59 +01:00
|
|
|
# add-on hook
|
|
|
|
runHook('browser.setupMenus', self)
|
|
|
|
self.mw.maybeHideAccelerators(self)
|
|
|
|
|
|
|
|
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():
|
|
|
|
for t in m['tmpls']:
|
|
|
|
bsize = t.get("bsize", 0)
|
|
|
|
if bsize > curmax:
|
|
|
|
curmax = bsize
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.tableView.verticalHeader().setDefaultSectionSize(
|
2013-05-23 07:12:04 +02:00
|
|
|
curmax + 6)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def closeEvent(self, evt):
|
2016-07-14 12:23:44 +02:00
|
|
|
if not self._closeEventHasCleanedUp:
|
2017-04-12 10:40:37 +02:00
|
|
|
if self.editor.note and not self.forceClose:
|
2016-07-14 12:23:44 +02:00
|
|
|
# ignore event for now to allow us to save
|
|
|
|
self.editor.saveNow(self._closeEventAfterSave)
|
|
|
|
evt.ignore()
|
|
|
|
else:
|
|
|
|
self._closeEventCleanup()
|
|
|
|
evt.accept()
|
|
|
|
self.mw.gcWindow(self)
|
|
|
|
else:
|
|
|
|
evt.accept()
|
|
|
|
self.mw.gcWindow(self)
|
|
|
|
|
|
|
|
def _closeEventAfterSave(self):
|
|
|
|
self._closeEventCleanup()
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def _closeEventCleanup(self):
|
|
|
|
self.editor.setNote(None)
|
2012-12-21 08:51:59 +01:00
|
|
|
saveSplitter(self.form.splitter_2, "editor2")
|
|
|
|
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")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.col.conf['activeCols'] = self.model.activeCols
|
|
|
|
self.col.setMod()
|
|
|
|
self.teardownHooks()
|
|
|
|
self.mw.maybeReset()
|
2016-07-14 12:23:44 +02:00
|
|
|
aqt.dialogs.close("Browser")
|
|
|
|
self._closeEventHasCleanedUp = True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2013-04-11 12:23:32 +02:00
|
|
|
def canClose(self):
|
|
|
|
return True
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
"Show answer on RET or register answer."
|
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
self.close()
|
|
|
|
elif self.mw.app.focusWidget() == self.form.tree:
|
|
|
|
if evt.key() in (Qt.Key_Return, Qt.Key_Enter):
|
|
|
|
item = self.form.tree.currentItem()
|
|
|
|
self.onTreeClick(item, 0)
|
|
|
|
|
|
|
|
def setupColumns(self):
|
|
|
|
self.columns = [
|
2013-02-22 03:13:59 +01:00
|
|
|
('question', _("Question")),
|
|
|
|
('answer', _("Answer")),
|
2012-12-21 08:51:59 +01:00
|
|
|
('template', _("Card")),
|
|
|
|
('deck', _("Deck")),
|
|
|
|
('noteFld', _("Sort Field")),
|
|
|
|
('noteCrt', _("Created")),
|
|
|
|
('noteMod', _("Edited")),
|
|
|
|
('cardMod', _("Changed")),
|
|
|
|
('cardDue', _("Due")),
|
|
|
|
('cardIvl', _("Interval")),
|
|
|
|
('cardEase', _("Ease")),
|
|
|
|
('cardReps', _("Reviews")),
|
|
|
|
('cardLapses', _("Lapses")),
|
2013-09-03 20:20:20 +02:00
|
|
|
('noteTags', _("Tags")),
|
2012-12-21 08:51:59 +01:00
|
|
|
('note', _("Note")),
|
|
|
|
]
|
|
|
|
self.columns.sort(key=itemgetter(1))
|
|
|
|
|
|
|
|
# Searching
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setupSearch(self):
|
2014-05-22 13:53:35 +02:00
|
|
|
self.form.searchEdit.setLineEdit(FavouritesLineEdit(self.mw, 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)
|
|
|
|
self.form.searchEdit.addItems(self.mw.pm.profile['searchHistory'])
|
2016-07-14 12:23:44 +02:00
|
|
|
self._searchPrompt = _("<type here to search; hit enter to show current deck>")
|
|
|
|
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 ")
|
|
|
|
|
|
|
|
# update history
|
2016-05-12 06:45:35 +02:00
|
|
|
txt = str(self.form.searchEdit.lineEdit().text()).strip()
|
2012-12-21 08:51:59 +01:00
|
|
|
sh = self.mw.pm.profile['searchHistory']
|
|
|
|
if txt in sh:
|
|
|
|
sh.remove(txt)
|
|
|
|
sh.insert(0, txt)
|
|
|
|
sh = sh[:30]
|
|
|
|
self.form.searchEdit.clear()
|
|
|
|
self.form.searchEdit.addItems(sh)
|
|
|
|
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.
|
|
|
|
def search(self):
|
|
|
|
if "is:current" in self._lastSearchTxt:
|
|
|
|
# show current card if there is one
|
|
|
|
c = self.mw.reviewer.card
|
|
|
|
nid = c and c.nid or 0
|
|
|
|
self.model.search("nid:%d"%nid)
|
|
|
|
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)
|
|
|
|
self.setWindowTitle(ngettext("Browser (%(cur)d card shown; %(sel)s)",
|
|
|
|
"Browser (%(cur)d cards shown; %(sel)s)",
|
|
|
|
cur) % {
|
|
|
|
"cur": cur,
|
|
|
|
"sel": ngettext("%d selected", "%d selected", selected) % selected
|
|
|
|
})
|
|
|
|
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)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupEditor(self):
|
|
|
|
self.editor = aqt.editor.Editor(
|
|
|
|
self.mw, self.form.fieldsArea, self)
|
|
|
|
self.editor.stealFocus = False
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
def _onRowChanged(self, current, previous):
|
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)
|
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:
|
|
|
|
self.card = self.model.getCard(
|
|
|
|
self.form.tableView.selectionModel().currentIndex())
|
|
|
|
self.editor.setNote(self.card.note(reload=True))
|
|
|
|
self.editor.card = self.card
|
2013-05-10 08:02:58 +02:00
|
|
|
self.singleCard = True
|
2013-05-03 10:52:46 +02:00
|
|
|
self._renderPreview(True)
|
2016-07-07 09:29:53 +02:00
|
|
|
self.toolbar.update()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def refreshCurrentCard(self, note):
|
|
|
|
self.model.refreshNote(note)
|
2013-05-03 10:52:46 +02:00
|
|
|
self._renderPreview(False)
|
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):
|
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":
|
|
|
|
showInfo(_("""\
|
2013-03-03 00:19:16 +01:00
|
|
|
This column can't be sorted on, but you can search for individual card types, \
|
2016-07-14 12:23:44 +02:00
|
|
|
such as 'card:1'."""))
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "deck":
|
|
|
|
showInfo(_("""\
|
2013-03-03 00:19:16 +01:00
|
|
|
This column can't be sorted on, but you can search for specific decks \
|
2012-12-21 08:51:59 +01:00
|
|
|
by clicking on one on the left."""))
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
# default to descending for non-text fields
|
|
|
|
if type == "noteFld":
|
|
|
|
ord = not ord
|
|
|
|
self.col.conf['sortBackwards'] = ord
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
if self.col.conf['sortBackwards'] != ord:
|
|
|
|
self.col.conf['sortBackwards'] = ord
|
|
|
|
self.model.reverse()
|
|
|
|
self.setSortIndicator()
|
|
|
|
|
|
|
|
def setSortIndicator(self):
|
|
|
|
hh = self.form.tableView.horizontalHeader()
|
|
|
|
type = self.col.conf['sortType']
|
|
|
|
if type not in self.model.activeCols:
|
|
|
|
hh.setSortIndicatorShown(False)
|
|
|
|
return
|
|
|
|
idx = self.model.activeCols.index(type)
|
|
|
|
if self.col.conf['sortBackwards']:
|
|
|
|
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:
|
|
|
|
return showInfo(_("You must have at least one column."))
|
|
|
|
self.model.activeCols.remove(type)
|
2013-10-04 01:43:46 +02:00
|
|
|
adding=False
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
self.model.activeCols.append(type)
|
2013-10-04 01:43:46 +02: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)
|
|
|
|
hh.setSectionResizeMode(hh.logicalIndex(len(self.model.activeCols)-1),
|
2014-03-14 07:41:00 +01:00
|
|
|
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()
|
|
|
|
|
|
|
|
# Filter tree
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class CallbackItem(QTreeWidgetItem):
|
2017-06-14 03:43:47 +02:00
|
|
|
def __init__(self, root, name, onclick, oncollapse=None, expanded=False):
|
2013-02-25 22:03:55 +01:00
|
|
|
QTreeWidgetItem.__init__(self, root, [name])
|
2017-06-14 03:43:47 +02:00
|
|
|
self.setExpanded(expanded)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.onclick = onclick
|
2014-02-15 03:32:14 +01:00
|
|
|
self.oncollapse = oncollapse
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def setupTree(self):
|
2016-05-31 10:51:40 +02:00
|
|
|
self.form.tree.itemClicked.connect(self.onTreeClick)
|
2012-12-21 08:51:59 +01:00
|
|
|
p = QPalette()
|
|
|
|
p.setColor(QPalette.Base, QColor("#d6dde0"))
|
|
|
|
self.form.tree.setPalette(p)
|
|
|
|
self.buildTree()
|
2016-05-31 10:51:40 +02:00
|
|
|
self.form.tree.itemExpanded.connect(lambda item: self.onTreeCollapse(item))
|
|
|
|
self.form.tree.itemCollapsed.connect(lambda item: self.onTreeCollapse(item))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def buildTree(self):
|
|
|
|
self.form.tree.clear()
|
2013-02-25 22:03:55 +01:00
|
|
|
root = self.form.tree
|
2012-12-21 08:51:59 +01:00
|
|
|
self._systemTagTree(root)
|
2014-04-08 20:35:00 +02:00
|
|
|
self._favTree(root)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._decksTree(root)
|
|
|
|
self._modelTree(root)
|
|
|
|
self._userTagTree(root)
|
|
|
|
self.form.tree.setIndentation(15)
|
|
|
|
|
|
|
|
def onTreeClick(self, item, col):
|
|
|
|
if getattr(item, 'onclick', None):
|
|
|
|
item.onclick()
|
|
|
|
|
2014-02-15 03:32:14 +01:00
|
|
|
def onTreeCollapse(self, item):
|
|
|
|
if getattr(item, 'oncollapse', None):
|
|
|
|
item.oncollapse()
|
|
|
|
|
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
|
|
|
|
if " " in txt or "(" in txt or ")" in txt:
|
2013-02-20 07:15:27 +01:00
|
|
|
txt = '"%s"' % txt
|
2012-12-21 08:51:59 +01:00
|
|
|
items.append(txt)
|
|
|
|
txt = ""
|
|
|
|
txt = " ".join(items)
|
|
|
|
if self.mw.app.keyboardModifiers() & Qt.AltModifier:
|
|
|
|
txt = "-"+txt
|
|
|
|
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:
|
2014-06-25 20:46:21 +02: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
|
|
|
|
|
|
|
def _systemTagTree(self, root):
|
|
|
|
tags = (
|
|
|
|
(_("Whole Collection"), "ankibw", ""),
|
|
|
|
(_("Current Deck"), "deck16", "deck:current"),
|
|
|
|
(_("Added Today"), "view-pim-calendar.png", "added:1"),
|
|
|
|
(_("Studied Today"), "view-pim-calendar.png", "rated:1"),
|
|
|
|
(_("Again Today"), "view-pim-calendar.png", "rated:1:1"),
|
|
|
|
(_("New"), "plus16.png", "is:new"),
|
|
|
|
(_("Learning"), "stock_new_template_red.png", "is:learn"),
|
|
|
|
(_("Review"), "clock16.png", "is:review"),
|
|
|
|
(_("Due"), "clock16.png", "is:due"),
|
|
|
|
(_("Marked"), "star16.png", "tag:marked"),
|
|
|
|
(_("Suspended"), "media-playback-pause.png", "is:suspended"),
|
|
|
|
(_("Leech"), "emblem-important.png", "tag:leech"))
|
|
|
|
for name, icon, cmd in tags:
|
|
|
|
item = self.CallbackItem(
|
2013-02-25 22:03:55 +01:00
|
|
|
root, name, lambda c=cmd: self.setFilter(c))
|
2012-12-21 08:51:59 +01:00
|
|
|
item.setIcon(0, QIcon(":/icons/" + icon))
|
|
|
|
return root
|
|
|
|
|
2014-04-08 20:35:00 +02:00
|
|
|
def _favTree(self, root):
|
|
|
|
saved = self.col.conf.get('savedFilters', [])
|
|
|
|
if not saved:
|
|
|
|
# Don't add favourites to tree if none saved
|
|
|
|
return
|
|
|
|
root = self.CallbackItem(root, _("My Searches"), None)
|
|
|
|
root.setExpanded(True)
|
|
|
|
root.setIcon(0, QIcon(":/icons/emblem-favorite-dark.png"))
|
2014-11-11 00:13:29 +01:00
|
|
|
for name, filt in sorted(saved.items()):
|
2014-04-08 20:35:00 +02:00
|
|
|
item = self.CallbackItem(root, name, lambda s=filt: self.setFilter(s))
|
|
|
|
item.setIcon(0, QIcon(":/icons/emblem-favorite-dark.png"))
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def _userTagTree(self, root):
|
|
|
|
for t in sorted(self.col.tags.all()):
|
2012-12-26 00:40:02 +01:00
|
|
|
if t.lower() == "marked" or t.lower() == "leech":
|
2012-12-21 08:51:59 +01:00
|
|
|
continue
|
|
|
|
item = self.CallbackItem(
|
2013-02-25 22:03:55 +01:00
|
|
|
root, t, lambda t=t: self.setFilter("tag", t))
|
2012-12-21 08:51:59 +01:00
|
|
|
item.setIcon(0, QIcon(":/icons/anki-tag.png"))
|
|
|
|
|
|
|
|
def _decksTree(self, root):
|
|
|
|
grps = self.col.sched.deckDueTree()
|
|
|
|
def fillGroups(root, grps, head=""):
|
|
|
|
for g in grps:
|
|
|
|
item = self.CallbackItem(
|
2014-02-15 03:32:14 +01:00
|
|
|
root, g[0],
|
|
|
|
lambda g=g: self.setFilter("deck", head+g[0]),
|
2017-06-14 03:43:47 +02:00
|
|
|
lambda g=g: self.mw.col.decks.collapseBrowser(g[1]),
|
|
|
|
not self.mw.col.decks.get(g[1]).get('browserCollapsed', False))
|
2012-12-21 08:51:59 +01:00
|
|
|
item.setIcon(0, QIcon(":/icons/deck16.png"))
|
|
|
|
newhead = head + g[0]+"::"
|
|
|
|
fillGroups(item, g[5], newhead)
|
|
|
|
fillGroups(root, grps)
|
|
|
|
|
|
|
|
def _modelTree(self, root):
|
|
|
|
for m in sorted(self.col.models.all(), key=itemgetter("name")):
|
|
|
|
mitem = self.CallbackItem(
|
2013-02-25 22:03:55 +01:00
|
|
|
root, m['name'], lambda m=m: self.setFilter("mid", str(m['id'])))
|
2012-12-21 08:51:59 +01:00
|
|
|
mitem.setIcon(0, QIcon(":/icons/product_design.png"))
|
|
|
|
# for t in m['tmpls']:
|
|
|
|
# titem = self.CallbackItem(
|
|
|
|
# t['name'], lambda m=m, t=t: self.setFilter(
|
|
|
|
# "model", m['name'], "card", t['name']))
|
|
|
|
# titem.setIcon(0, QIcon(":/icons/stock_new_template.png"))
|
|
|
|
# mitem.addChild(titem)
|
|
|
|
|
|
|
|
# Info
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def showCardInfo(self):
|
|
|
|
if not self.card:
|
|
|
|
return
|
|
|
|
info, cs = self._cardInfoData()
|
|
|
|
reps = self._revlogData(cs)
|
|
|
|
d = QDialog(self)
|
|
|
|
l = QVBoxLayout()
|
2016-05-31 10:51:40 +02: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
|
|
|
saveGeom(d, "revlog")
|
|
|
|
|
|
|
|
def _cardInfoData(self):
|
|
|
|
from anki.stats import CardStats
|
|
|
|
cs = CardStats(self.col, self.card)
|
|
|
|
rep = cs.report()
|
|
|
|
m = self.card.model()
|
|
|
|
rep = """
|
|
|
|
<div style='width: 400px; margin: 0 auto 0;
|
|
|
|
border: 1px solid #000; padding: 3px; '>%s</div>""" % rep
|
|
|
|
return rep, cs
|
|
|
|
|
|
|
|
def _revlogData(self, cs):
|
|
|
|
entries = self.mw.col.db.all(
|
|
|
|
"select id/1000.0, ease, ivl, factor, time/1000.0, type "
|
|
|
|
"from revlog where cid = ?", self.card.id)
|
|
|
|
if not entries:
|
|
|
|
return ""
|
|
|
|
s = "<table width=100%%><tr><th align=left>%s</th>" % _("Date")
|
|
|
|
s += ("<th align=right>%s</th>" * 5) % (
|
|
|
|
_("Type"), _("Rating"), _("Interval"), _("Ease"), _("Time"))
|
|
|
|
cnt = 0
|
|
|
|
for (date, ease, ivl, factor, taken, type) in reversed(entries):
|
|
|
|
cnt += 1
|
|
|
|
s += "<tr><td>%s</td>" % time.strftime(_("<b>%Y-%m-%d</b> @ %H:%M"),
|
|
|
|
time.localtime(date))
|
|
|
|
tstr = [_("Learn"), _("Review"), _("Relearn"), _("Filtered"),
|
|
|
|
_("Resched")][type]
|
|
|
|
import anki.stats as st
|
|
|
|
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:
|
|
|
|
ivl = fmtTimeSpan(ivl*86400, short=True)
|
|
|
|
else:
|
|
|
|
ivl = cs.time(-ivl)
|
|
|
|
s += ("<td align=right>%s</td>" * 5) % (
|
|
|
|
tstr,
|
|
|
|
ease, ivl,
|
|
|
|
"%d%%" % (factor/10) if factor else "",
|
|
|
|
cs.time(taken)) + "</tr>"
|
|
|
|
s += "</table>"
|
|
|
|
if cnt < self.card.reps:
|
|
|
|
s += _("""\
|
|
|
|
Note: Some of the history is missing. For more information, \
|
|
|
|
please see the browser documentation.""")
|
|
|
|
return s
|
|
|
|
|
|
|
|
# Menu helpers
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def selectedCards(self):
|
|
|
|
return [self.model.cards[idx.row()] for idx in
|
|
|
|
self.form.tableView.selectionModel().selectedRows()]
|
|
|
|
|
|
|
|
def selectedNotes(self):
|
|
|
|
return self.col.db.list("""
|
|
|
|
select distinct nid from cards
|
|
|
|
where id in %s""" % ids2str(
|
|
|
|
[self.model.cards[idx.row()] for idx in
|
|
|
|
self.form.tableView.selectionModel().selectedRows()]))
|
|
|
|
|
|
|
|
def selectedNotesAsCards(self):
|
|
|
|
return self.col.db.list(
|
|
|
|
"select id from cards where nid in (%s)" %
|
|
|
|
",".join([str(s) for s in self.selectedNotes()]))
|
|
|
|
|
|
|
|
def oneModelNotes(self):
|
|
|
|
sf = self.selectedNotes()
|
|
|
|
if not sf:
|
|
|
|
return
|
|
|
|
mods = self.col.db.scalar("""
|
|
|
|
select count(distinct mid) from notes
|
|
|
|
where id in %s""" % ids2str(sf))
|
|
|
|
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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
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"
|
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)
|
2013-05-03 10:52:46 +02:00
|
|
|
vbox = QVBoxLayout()
|
2016-05-31 10:51:40 +02: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
|
|
|
|
2014-06-21 22:35:45 +02:00
|
|
|
self._previewReplay = bbox.addButton(_("Replay Audio"), QDialogButtonBox.ActionRole)
|
|
|
|
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
|
|
|
|
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-03-24 17:09:16 +01:00
|
|
|
if self._previewState == "answer":
|
|
|
|
self._previewState = "question"
|
2013-05-03 10:52:46 +02:00
|
|
|
self._renderPreview()
|
|
|
|
else:
|
|
|
|
self.onPreviousCard()
|
|
|
|
self._updatePreviewButtons()
|
|
|
|
|
|
|
|
def _onPreviewNext(self):
|
|
|
|
if self._previewState == "question":
|
|
|
|
self._previewState = "answer"
|
|
|
|
self._renderPreview()
|
|
|
|
else:
|
|
|
|
self.onNextCard()
|
|
|
|
self._updatePreviewButtons()
|
|
|
|
|
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()
|
|
|
|
canBack = (current > 0 or (current == 0 and self._previewState == "answer" ))
|
2013-05-10 08:02:58 +02:00
|
|
|
self._previewPrev.setEnabled(not not (self.singleCard and canBack))
|
2013-05-03 10:52:46 +02: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
|
|
|
|
|
|
|
|
def _renderPreview(self, cardChanged=False):
|
|
|
|
if not self._previewWindow:
|
|
|
|
return
|
|
|
|
c = self.card
|
|
|
|
if not c:
|
|
|
|
txt = _("(please select 1 card)")
|
|
|
|
self._previewWeb.stdHtml(txt)
|
|
|
|
self._updatePreviewButtons()
|
|
|
|
return
|
|
|
|
self._updatePreviewButtons()
|
|
|
|
if cardChanged:
|
|
|
|
self._previewState = "question"
|
|
|
|
# need to force reload even if answer
|
|
|
|
txt = c.q(reload=True)
|
|
|
|
if self._previewState == "answer":
|
|
|
|
txt = c.a()
|
|
|
|
txt = re.sub("\[\[type:[^]]+\]\]", "", txt)
|
|
|
|
ti = lambda x: x
|
2016-07-07 15:39:48 +02:00
|
|
|
base = self.mw.baseHTML()
|
2013-05-03 10:52:46 +02:00
|
|
|
self._previewWeb.stdHtml(
|
2013-06-10 08:28:34 +02:00
|
|
|
ti(mungeQA(self.col, txt)), self.mw.reviewer._styles(),
|
2013-05-03 10:52:46 +02:00
|
|
|
bodyClass="card card%d" % (c.ord+1), head=base,
|
|
|
|
js=anki.js.browserSel)
|
|
|
|
clearAudioQueue()
|
|
|
|
if self.mw.reviewer.autoplay(c):
|
|
|
|
playFromText(txt)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Card deletion
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def deleteNotes(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._deleteNotes)
|
|
|
|
|
|
|
|
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()
|
|
|
|
selectedRows = [i.row() for i in
|
|
|
|
self.form.tableView.selectionModel().selectedRows()]
|
|
|
|
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()
|
2014-08-26 09:25:13 +02: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
|
2013-02-20 07:25:59 +01:00
|
|
|
cids = self.selectedCards()
|
|
|
|
if not cids:
|
|
|
|
return
|
|
|
|
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(
|
2013-02-20 07:25:59 +01:00
|
|
|
self.mw, current=current, accept=_("Move Cards"),
|
2012-12-21 08:51:59 +01:00
|
|
|
title=_("Change Deck"), help="browse", parent=self)
|
|
|
|
if not ret.name:
|
|
|
|
return
|
|
|
|
did = self.col.decks.id(ret.name)
|
|
|
|
deck = self.col.decks.get(did)
|
|
|
|
if deck['dyn']:
|
|
|
|
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
|
|
|
|
self.col.db.execute("""
|
|
|
|
update cards set usn=?, mod=?, did=? where id in """ + scids,
|
|
|
|
usn, mod, did)
|
|
|
|
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")
|
|
|
|
self.addTags(tags, label, _("Enter tags to delete:"),
|
|
|
|
func=self.col.tags.bulkRem)
|
|
|
|
|
|
|
|
# Suspending and marking
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def isMarked(self):
|
|
|
|
return not not (self.card and self.card.note().hasTag("Marked"))
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
"select id from cards where type = 0 and id in " + ids2str(cids))
|
|
|
|
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(
|
2014-01-14 07:06:22 +01:00
|
|
|
"select min(due), max(due) from cards where type=0 and odid=0")
|
|
|
|
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(
|
|
|
|
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)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.col.sched.reschedCards(
|
2013-05-17 09:27:58 +02:00
|
|
|
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
|
|
|
|
self._lastSearchTxt = "nid:"+",".join([str(x) for x in nids])
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Edit: undo
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setupHooks(self):
|
|
|
|
addHook("undoState", self.onUndoState)
|
|
|
|
addHook("reset", self.onReset)
|
|
|
|
addHook("editTimer", self.refreshCurrentCard)
|
|
|
|
addHook("editFocusLost", self.refreshCurrentCardFilter)
|
|
|
|
for t in "newTag", "newModel", "newDeck":
|
|
|
|
addHook(t, self.buildTree)
|
|
|
|
|
|
|
|
def teardownHooks(self):
|
|
|
|
remHook("reset", self.onReset)
|
|
|
|
remHook("editTimer", self.refreshCurrentCard)
|
2013-04-09 23:58:04 +02:00
|
|
|
remHook("editFocusLost", self.refreshCurrentCardFilter)
|
2012-12-21 08:51:59 +01:00
|
|
|
remHook("undoState", self.onUndoState)
|
|
|
|
for t in "newTag", "newModel", "newDeck":
|
|
|
|
remHook(t, self.buildTree)
|
|
|
|
|
|
|
|
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
|
|
|
|
fields = sorted(anki.find.fieldNames(self.col, downcase=False))
|
|
|
|
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:
|
|
|
|
field = fields[frm.field.currentIndex()-1]
|
|
|
|
self.mw.checkpoint(_("Find and Replace"))
|
|
|
|
self.mw.progress.start()
|
|
|
|
self.model.beginReset()
|
|
|
|
try:
|
|
|
|
changed = self.col.findReplace(sf,
|
2016-05-12 06:45:35 +02:00
|
|
|
str(frm.find.text()),
|
|
|
|
str(frm.replace.text()),
|
2012-12-21 08:51:59 +01:00
|
|
|
frm.re.isChecked(),
|
|
|
|
field,
|
|
|
|
frm.ignoreCase.isChecked())
|
|
|
|
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()
|
|
|
|
showInfo(ngettext(
|
|
|
|
"%(a)d of %(b)d note updated",
|
|
|
|
"%(a)d of %(b)d notes updated", len(sf)) % {
|
|
|
|
'a': changed,
|
|
|
|
'b': len(sf),
|
|
|
|
})
|
|
|
|
|
|
|
|
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")
|
|
|
|
fields = sorted(anki.find.fieldNames(self.col, downcase=False))
|
|
|
|
frm.fields.addItems(fields)
|
2013-10-03 17:07:11 +02:00
|
|
|
self._dupesButton = None
|
2012-12-21 08:51:59 +01:00
|
|
|
# links
|
2016-06-06 07:50:03 +02:00
|
|
|
frm.webView.onBridgeCmd = self.dupeLinkClicked
|
2012-12-21 08:51:59 +01:00
|
|
|
def onFin(code):
|
|
|
|
saveGeom(d, "findDupes")
|
2016-05-31 10:51:40 +02:00
|
|
|
d.finished.connect(onFin)
|
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)
|
2012-12-21 08:51:59 +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(
|
|
|
|
_("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:
|
2016-06-06 07:50:03 +02:00
|
|
|
t += '''<li><a href=# onclick="pycmd('%s')">%s</a>: %s</a>''' % (
|
2012-12-21 08:51:59 +01:00
|
|
|
"nid:" + ",".join(str(id) for id in nids),
|
|
|
|
ngettext("%d note", "%d notes", len(nids)) % len(nids),
|
|
|
|
cgi.escape(val))
|
|
|
|
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())
|
|
|
|
tv.selectionModel().clear()
|
|
|
|
tv.setCurrentIndex(idx)
|
|
|
|
|
|
|
|
def onPreviousCard(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onPreviousCard)
|
|
|
|
|
|
|
|
def _onPreviousCard(self):
|
2017-03-01 20:25:59 +01:00
|
|
|
tagfocus = self.editor.tags.hasFocus()
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.editor.currentField
|
|
|
|
self._moveCur(QAbstractItemView.MoveUp)
|
2017-03-01 20:25:59 +01:00
|
|
|
if tagfocus:
|
|
|
|
self.editor.tags.setFocus()
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
self.editor.web.setFocus()
|
|
|
|
self.editor.web.eval("focusField(%d)" % f)
|
|
|
|
|
|
|
|
def onNextCard(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onNextCard)
|
|
|
|
|
|
|
|
def _onNextCard(self):
|
2017-03-01 20:25:59 +01:00
|
|
|
tagfocus = self.editor.tags.hasFocus()
|
2012-12-21 08:51:59 +01:00
|
|
|
f = self.editor.currentField
|
|
|
|
self._moveCur(QAbstractItemView.MoveDown)
|
2017-03-01 20:25:59 +01:00
|
|
|
if tagfocus:
|
|
|
|
self.editor.tags.setFocus()
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
self.editor.web.setFocus()
|
|
|
|
self.editor.web.eval("focusField(%d)" % f)
|
|
|
|
|
|
|
|
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)
|
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent|
|
|
|
|
QItemSelectionModel.Rows)
|
|
|
|
|
|
|
|
def onLastCard(self):
|
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
idx = sm.currentIndex()
|
|
|
|
self._moveCur(
|
|
|
|
None, self.model.index(len(self.model.cards) - 1, 0))
|
|
|
|
if not self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
|
|
|
return
|
|
|
|
idx2 = sm.currentIndex()
|
|
|
|
item = QItemSelection(idx, idx2)
|
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent|
|
|
|
|
QItemSelectionModel.Rows)
|
|
|
|
|
|
|
|
def onFind(self):
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
self.form.searchEdit.lineEdit().selectAll()
|
|
|
|
|
|
|
|
def onNote(self):
|
|
|
|
self.editor.focus()
|
2013-01-29 01:49:04 +01:00
|
|
|
self.editor.web.setFocus()
|
|
|
|
self.editor.web.eval("focusField(0);")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onTags(self):
|
|
|
|
self.form.tree.setFocus()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Change model dialog
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class ChangeModel(QDialog):
|
|
|
|
|
|
|
|
def __init__(self, browser, nids):
|
|
|
|
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")
|
|
|
|
addHook("reset", self.onReset)
|
|
|
|
addHook("currentModelChanged", self.onReset)
|
|
|
|
self.exec_()
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# maps
|
|
|
|
self.flayout = QHBoxLayout()
|
2016-05-31 10:51:40 +02: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()
|
2016-05-31 10:51:40 +02: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
|
|
|
|
self.oldModel = self.browser.col.models.get(
|
|
|
|
self.browser.col.db.scalar(
|
|
|
|
"select mid from notes where id = ?", self.nids[0]))
|
|
|
|
self.form.oldModelLabel.setText(self.oldModel['name'])
|
|
|
|
self.modelChooser = aqt.modelchooser.ModelChooser(
|
|
|
|
self.browser.mw, self.form.modelChooserWidget, label=False)
|
|
|
|
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 = []
|
|
|
|
targets = [x['name'] for x in dst] + [_("Nothing")]
|
|
|
|
indices = {}
|
|
|
|
for i, x in enumerate(src):
|
|
|
|
l.addWidget(QLabel(_("Change %s to:") % x['name']), i, 0)
|
|
|
|
cb = QComboBox()
|
|
|
|
cb.addItems(targets)
|
|
|
|
idx = min(i, len(targets)-1)
|
|
|
|
cb.setCurrentIndex(idx)
|
|
|
|
indices[cb] = idx
|
2016-05-31 10:51:40 +02:00
|
|
|
cb.currentIndexChanged.connect(
|
|
|
|
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:
|
|
|
|
old = self.oldModel['tmpls']
|
|
|
|
combos = self.tcombos
|
|
|
|
new = self.targetModel['tmpls']
|
|
|
|
map = {}
|
|
|
|
for i, f in enumerate(old):
|
|
|
|
idx = combos[i].currentIndex()
|
|
|
|
if idx == len(new):
|
|
|
|
# ignore
|
|
|
|
map[f['ord']] = None
|
|
|
|
else:
|
|
|
|
f2 = new[idx]
|
|
|
|
map[f['ord']] = f2['ord']
|
|
|
|
return map
|
|
|
|
|
|
|
|
def getFieldMap(self):
|
|
|
|
return self.getTemplateMap(
|
|
|
|
old=self.oldModel['flds'],
|
|
|
|
combos=self.fcombos,
|
|
|
|
new=self.targetModel['flds'])
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
remHook("reset", self.onReset)
|
|
|
|
remHook("currentModelChanged", self.onReset)
|
|
|
|
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):
|
2012-12-21 08:51:59 +01:00
|
|
|
if not askUser(_("""\
|
|
|
|
Any cards mapped to nothing will be deleted. \
|
|
|
|
If a note has no remaining cards, it will be lost. \
|
|
|
|
Are you sure you want to continue?""")):
|
|
|
|
return
|
|
|
|
QDialog.accept(self)
|
|
|
|
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()
|
|
|
|
|
|
|
|
def onHelp(self):
|
|
|
|
openHelp("browsermisc")
|
|
|
|
|
|
|
|
# Toolbar
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class BrowserToolbar(Toolbar):
|
|
|
|
|
|
|
|
def __init__(self, mw, web, browser):
|
|
|
|
self.browser = browser
|
|
|
|
Toolbar.__init__(self, mw, web)
|
|
|
|
|
|
|
|
def draw(self):
|
2016-07-07 09:29:53 +02:00
|
|
|
self._loaded = False
|
|
|
|
self.web.onBridgeCmd = self._linkHandler
|
|
|
|
self.web.onLoadFinished = self.onLoaded
|
|
|
|
self.web.stdHtml(self.html(), self.css())
|
|
|
|
|
|
|
|
def onLoaded(self):
|
|
|
|
super().onLoaded()
|
|
|
|
self._loaded = True
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
if not self._loaded:
|
|
|
|
return
|
|
|
|
for link, enabled in (
|
|
|
|
("mark", self.browser.isMarked()),
|
|
|
|
("pause", self.browser.isSuspended())):
|
|
|
|
if enabled:
|
|
|
|
self.web.eval("$('#%s').addClass('buttonOn')" % link)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2016-07-07 09:29:53 +02:00
|
|
|
self.web.eval("$('#%s').removeClass('buttonOn')" % link)
|
|
|
|
|
|
|
|
def html(self):
|
|
|
|
def borderImg(link, icon, title, tooltip=None):
|
|
|
|
fmt = '''\
|
|
|
|
<a class=hitem title="%s" href=# onclick="pycmd('%s')"><img id=%s valign=bottom src="qrc:/icons/%s.png"> %s</a>'''
|
|
|
|
return fmt % (tooltip or title, link, link, icon, title)
|
2012-12-21 08:51:59 +01:00
|
|
|
right = "<div>"
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("add", "add16", _("Add"),
|
2016-02-26 17:32:04 +01:00
|
|
|
shortcut(_("Add Note (Ctrl+E)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("info", "info", _("Info"),
|
2012-12-21 08:51:59 +01:00
|
|
|
shortcut(_("Card Info (Ctrl+Shift+I)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("mark", "star16", _("Mark"),
|
2012-12-21 08:51:59 +01:00
|
|
|
shortcut(_("Mark Note (Ctrl+K)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("pause", "pause16", _("Suspend"),
|
2016-02-22 00:31:00 +01:00
|
|
|
shortcut(_("Suspend Card (Ctrl+J)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("setDeck", "deck16", _("Change Deck"),
|
2012-12-21 08:51:59 +01:00
|
|
|
shortcut(_("Move To Deck (Ctrl+D)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("addtag", "addtag16", _("Add Tags"),
|
2012-12-21 08:51:59 +01:00
|
|
|
shortcut(_("Bulk Add Tags (Ctrl+Shift+T)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("deletetag", "deletetag16", _("Remove Tags"), shortcut(_(
|
2012-12-21 08:51:59 +01:00
|
|
|
"Bulk Remove Tags (Ctrl+Alt+T)")))
|
2016-07-07 09:29:53 +02:00
|
|
|
right += borderImg("delete", "delete16", _("Delete"))
|
2012-12-21 08:51:59 +01:00
|
|
|
right += "</div>"
|
2016-07-07 09:29:53 +02:00
|
|
|
return self._body % ("", right, "")
|
|
|
|
|
|
|
|
def css(self):
|
|
|
|
return self._css + """
|
2012-12-21 08:51:59 +01:00
|
|
|
#header { font-weight: normal; }
|
|
|
|
a { margin-right: 1em; }
|
2016-07-07 09:29:53 +02:00
|
|
|
.hitem { overflow: hidden; white-space: nowrap; }
|
|
|
|
.hitem img { padding: 1px; }
|
|
|
|
.buttonOn { border: 1px solid #aaa; padding: 0px !important; }
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Link handling
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _linkHandler(self, l):
|
|
|
|
if l == "anki":
|
|
|
|
self.showMenu()
|
|
|
|
elif l == "add":
|
|
|
|
self.browser.mw.onAddCard()
|
|
|
|
elif l == "delete":
|
|
|
|
self.browser.deleteNotes()
|
|
|
|
elif l == "setDeck":
|
|
|
|
self.browser.setDeck()
|
|
|
|
# icons
|
|
|
|
elif l == "info":
|
|
|
|
self.browser.showCardInfo()
|
|
|
|
elif l == "mark":
|
|
|
|
self.browser.onMark()
|
|
|
|
elif l == "pause":
|
|
|
|
self.browser.onSuspend()
|
|
|
|
elif l == "addtag":
|
|
|
|
self.browser.addTags()
|
|
|
|
elif l == "deletetag":
|
|
|
|
self.browser.deleteTags()
|
2014-04-08 20:35:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Favourites button
|
|
|
|
######################################################################
|
|
|
|
class FavouritesLineEdit(QLineEdit):
|
|
|
|
buttonClicked = pyqtSignal(bool)
|
|
|
|
|
|
|
|
def __init__(self, mw, browser, parent=None):
|
2017-02-21 22:08:41 +01:00
|
|
|
super().__init__(parent)
|
2014-04-08 20:35:00 +02:00
|
|
|
self.mw = mw
|
|
|
|
self.browser = browser
|
|
|
|
# add conf if missing
|
2016-05-12 06:45:35 +02:00
|
|
|
if 'savedFilters' not in self.mw.col.conf:
|
2014-04-08 20:35:00 +02:00
|
|
|
self.mw.col.conf['savedFilters'] = {}
|
|
|
|
self.button = QToolButton(self)
|
|
|
|
self.button.setStyleSheet('border: 0px;')
|
|
|
|
self.button.setCursor(Qt.ArrowCursor)
|
|
|
|
self.button.clicked.connect(self.buttonClicked.emit)
|
|
|
|
self.setIcon(':/icons/emblem-favorite-off.png')
|
|
|
|
# flag to raise save or delete dialog on button click
|
|
|
|
self.doSave = True
|
|
|
|
# name of current saved filter (if query matches)
|
|
|
|
self.name = None
|
|
|
|
self.buttonClicked.connect(self.onClicked)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.textChanged.connect(self.updateButton)
|
|
|
|
|
2014-04-08 20:35:00 +02:00
|
|
|
def resizeEvent(self, event):
|
|
|
|
buttonSize = self.button.sizeHint()
|
|
|
|
frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth)
|
|
|
|
self.button.move(self.rect().right() - frameWidth - buttonSize.width(),
|
|
|
|
(self.rect().bottom() - buttonSize.height() + 1) / 2)
|
2014-06-27 17:38:34 +02:00
|
|
|
self.setTextMargins(0, 0, buttonSize.width() * 1.5, 0)
|
2017-02-21 22:08:41 +01:00
|
|
|
super().resizeEvent(event)
|
2014-04-08 20:35:00 +02:00
|
|
|
|
|
|
|
def setIcon(self, path):
|
|
|
|
self.button.setIcon(QIcon(path))
|
|
|
|
|
|
|
|
def setText(self, txt):
|
2017-02-21 22:08:41 +01:00
|
|
|
super().setText(txt)
|
2014-04-08 20:35:00 +02:00
|
|
|
self.updateButton()
|
|
|
|
|
|
|
|
def updateButton(self, reset=True):
|
|
|
|
# If search text is a saved query, switch to the delete button.
|
|
|
|
# Otherwise show save button.
|
2016-05-12 06:45:35 +02:00
|
|
|
txt = str(self.text()).strip()
|
|
|
|
for key, value in list(self.mw.col.conf['savedFilters'].items()):
|
2014-04-08 20:35:00 +02:00
|
|
|
if txt == value:
|
|
|
|
self.doSave = False
|
|
|
|
self.name = key
|
|
|
|
self.setIcon(QIcon(":/icons/emblem-favorite.png"))
|
|
|
|
return
|
|
|
|
self.doSave = True
|
|
|
|
self.setIcon(QIcon(":/icons/emblem-favorite-off.png"))
|
|
|
|
|
|
|
|
def onClicked(self):
|
|
|
|
if self.doSave:
|
|
|
|
self.saveClicked()
|
|
|
|
else:
|
|
|
|
self.deleteClicked()
|
|
|
|
|
|
|
|
def saveClicked(self):
|
2016-05-12 06:45:35 +02:00
|
|
|
txt = str(self.text()).strip()
|
2014-04-08 20:35:00 +02:00
|
|
|
dlg = QInputDialog(self)
|
|
|
|
dlg.setInputMode(QInputDialog.TextInput)
|
|
|
|
dlg.setLabelText(_("The current search terms will be added as a new "
|
|
|
|
"item in the sidebar.\n"
|
|
|
|
"Search name:"))
|
|
|
|
dlg.setWindowTitle(_("Save search"))
|
|
|
|
ok = dlg.exec_()
|
|
|
|
name = dlg.textValue()
|
|
|
|
if ok:
|
|
|
|
self.mw.col.conf['savedFilters'][name] = txt
|
2014-10-05 11:37:09 +02:00
|
|
|
self.mw.col.setMod()
|
2014-04-08 20:35:00 +02:00
|
|
|
|
|
|
|
self.updateButton()
|
2015-06-30 13:41:16 +02:00
|
|
|
self.browser.buildTree()
|
2014-04-08 20:35:00 +02:00
|
|
|
|
|
|
|
def deleteClicked(self):
|
|
|
|
msg = _('Remove "%s" from your saved searches?') % self.name
|
|
|
|
ok = QMessageBox.question(self, _('Remove search'),
|
|
|
|
msg, QMessageBox.Yes, QMessageBox.No)
|
|
|
|
|
|
|
|
if ok == QMessageBox.Yes:
|
|
|
|
self.mw.col.conf['savedFilters'].pop(self.name, None)
|
2014-10-05 11:37:09 +02:00
|
|
|
self.mw.col.setMod()
|
2014-04-08 20:35:00 +02:00
|
|
|
self.updateButton()
|
2015-06-30 13:41:16 +02:00
|
|
|
self.browser.buildTree()
|