-
Notifications
You must be signed in to change notification settings - Fork 1
/
Leech_Dialog.py
208 lines (182 loc) · 8.06 KB
/
Leech_Dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Leech Dialog
version 0.1.0
Copyright (c) 2016 Soren Bjornstad.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This add-on borrows some code directly from Anki's source and the add-on
writing guide; comments are listed at those places.
"""
from __future__ import division
from aqt import mw, dialogs
from aqt.utils import showInfo, showWarning, askUser, tooltip
from anki.hooks import addHook
from anki.lang import _, ngettext
from anki.stats import CardStats
from anki.utils import fmtTimeSpan
from PyQt4.QtGui import QDialog
import leechdialog.leechdialog as ldForm
class LeechDialog(QDialog):
def __init__(self, mw, card):
self.mw = mw
self.card = card
QDialog.__init__(self)
self.form = ldForm.Ui_Dialog()
self.form.setupUi(self)
self.form.ignoreButton.clicked.connect(self.reject)
self.form.suspendButton.clicked.connect(self.onSuspend)
self.form.deleteButton.clicked.connect(self.onDelete)
self.form.editButton.clicked.connect(self.onEdit)
self.form.addNotesButton.clicked.connect(self.onAdd)
self.form.changeDeckButton.clicked.connect(self.onChangeDeck)
self.form.closeEditorLabel.setHidden(True)
self._showCardValues()
# save func for use if the dialog is disabled, see _toggleDlgEnabled()
self.workingReject = self.reject
def _toggleDlgEnabled(self):
f = self.form
doHide = not f.suspendButton.isHidden()
for i in (f.suspendButton, f.deleteButton, f.editButton,
f.ignoreButton, f.changeDeckButton, f.addNotesButton):
i.setHidden(doHide)
if doHide:
def closeEditorAlert():
# can't use a lambda because we need to return None
showInfo("Please close the editor first.")
return None
self.reject = closeEditorAlert
self.form.closeEditorLabel.setHidden(False)
else:
self.reject = self.workingReject
self.form.closeEditorLabel.setHidden(True)
def _showCardValues(self):
lapses = self.card.lapses
# since we just failed the card, current ivl is no use
interval = fmtTimeSpan(self.card.lastIvl * 86400)
ease = self.card.factor // 10
statManager = CardStats(self.mw.col, self.card)
secondsSpent = self.mw.col.db.first(
"select sum(time)/1000 from revlog where cid = :id",
id=self.card.id)[0]
totalTime = statManager.time(secondsSpent)
# watch on updates: calling a private method of sched for config info
leechThreshold = self.mw.col.sched._lapseConf(self.card)['leechFails']
if leechThreshold == lapses:
timesLeeched = "Leech for the first time."
else:
beyondOne = (lapses - leechThreshold) // (leechThreshold // 2)
timesLeeched = "Leeched %i times." % (beyondOne + 1)
txt = ("{leechtimes}<br>"
"<b>Lapses</b>: {lapses}<br>"
"<b>Last interval</b>: {lastivl}<br>"
"<b>Ease</b>: {ease}%<br>"
"<b>Total time</b>: {ttime}")
self.form.label.setText(txt.format(leechtimes=timesLeeched,
lapses=lapses, lastivl=interval, ease=ease, ttime=totalTime))
def onSuspend(self):
# This code is taken from the original leech handler,
# anki/sched.py:_checkLeech.
# We can't just use mw.col.sched.suspendCards() because when the
# reviewer saves the card for us, this change will be lost.
# https://anki.tenderapp.com/discussions/add-ons/7745-cant-get-cards-to-suspend
self.mw.checkpoint(_("Suspend"))
if self.card.odue:
self.card.due = self.card.odue
if self.card.odid:
self.card.did = self.card.odid
self.card.odue = self.card.odid = 0
self.card.queue = -1
tooltip(_("Card suspended."))
self.accept()
def onDelete(self):
note = self.card.note()
numCards = len(note.cards())
if numCards > 1:
r = askUser("Really delete this note? It has %i cards." % numCards,
defaultno=True, title="Delete note?")
if not r:
return
self.mw.checkpoint(_("Delete"))
self.mw.col.remNotes([self.card.note().id])
tooltip(ngettext(
"Note and its %d card deleted.",
"Note and its %d cards deleted.",
numCards) % numCards)
self.accept()
def onEdit(self):
"""
This function needs to set the leech dialog to non-modal so the user
can interact with the edit window. This actually only allows the user
to interact with the editor -- it's not possible to click the "resume
editing" button in the main window for example.
I don't understand why, but it works fine to set the dialog to
non-modal permanently here, but it doesn't work if the dialog is set to
non-modal in the constructor!
In order to make sure the user can't do funny things by pressing a
button in the leech dialog while the editor is still open, we hide all
the buttons and block reject. Then we temporarily wrap the close-window
method in aqt to toggle the buttons back on (the normal version is
restored as soon as it is called once on the edit-current dialog).
"""
dlg = dialogs.open("EditCurrent", mw)
oldCloseDlg = dialogs.close
def newCloseDlg(name):
if name == "EditCurrent":
dialogs.close = oldCloseDlg # restore old functionality
self._toggleDlgEnabled()
oldCloseDlg(name)
dialogs.close = newCloseDlg
self.setModal(False)
self._toggleDlgEnabled()
def onAdd(self):
dialogs.open("AddCards", mw)
self.setModal(False)
def onChangeDeck(self):
# based on aqt/browser.py:setDeck(), and add-on writing guide example
self.mw.checkpoint(_("Suspend"))
from aqt.studydeck import StudyDeck
cids = [self.card.id]
did = self.mw.col.db.scalar(
"select did from cards where id = ?", cids[0])
current = self.mw.col.decks.get(did)['name']
ret = StudyDeck(
self.mw, current=current, accept=_("Move Cards"),
title = _("Change Deck"), help="browse", parent=self)
if not ret.name:
return
did = self.mw.col.decks.id(ret.name)
deck = self.mw.col.decks.get(did)
if deck['dyn']:
showWarning(_("Cards can't be manually moved into a filtered deck."))
return
self.mw.checkpoint(_("Change deck"))
self.card.did = did
# if the card was in a cram deck, we have to put back the original due
# time and original deck
self.card.odid = 0
if self.card.odue:
self.card.due = self.card.odue
self.card.odue = 0
self.accept()
def leechHook(card):
ld = LeechDialog(mw, card)
ld.exec_()
# Since this add-on doesn't work on mobile, or anywhere it's not installed,
# it's useful to keep separate track of which notes are leeches that have
# been marked but not handled and which are leeches that have been marked
# and dealt with in the dialog. Therefore, anytime this add-on handles a
# card, it will be tagged 'leech_dialog' rather than 'leech' (and any
# 'leech' tag present from before will be removed).
n = card.note()
n.delTag("leech")
n.addTag("leech_dialog")
n.flush()
addHook("leech", leechHook)