-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlc_bitzer.py
1443 lines (1264 loc) · 49.2 KB
/
rlc_bitzer.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#-*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# This file is a plugin for anki flashcard application http://ichi2.net/anki/
# ---------------------------------------------------------------------------
# File: rlc_bitzer.py
# Description:
#
# Author: Richard Colley (richard.colley@rcolley.com)
# License: GPL v3
# ---------------------------------------------------------------------------
plugin_version = "v0.27"
# Changelog:
#
# ---- 0.27 -- 2009-05-09 -- Richard Colley ----
# Quick hack to remove code broken by anki changes
# ---- 0.26 -- 2008-09-08 -- Richard Colley ----
# Activate external html tip loading
# Multiple html tip urls (depends on modifier pressed during mouse-over)
# ---- 0.25 -- 2008-09-08 -- Richard Colley ----
# Typo fixed
# ---- 0.24 -- 2008-09-07 -- Richard Colley ----
# Disable new card distribution scheduler if anki 0.9.7.8
# ---- 0.23 -- 2008-09-07 -- Richard Colley ----
# Fixed bug in html lookup code
# ---- 0.22 -- 2008-09-02 -- Richard Colley ----
# Fixed bug in debugging code
# ---- 0.21 -- 2008-08-27 -- Richard Colley ----
# Port to Anki 0.9.7.x
# - removed: ease time display, edit window start state, tray icon
# ---- 0.20 -- 2008-05-31 -- Richard Colley ----
# Load kanji tip html in background (and only if changed)
# First look at key editor
# ---- 0.19 -- 2008-05-27 -- Richard Colley ----
# Properly fixed Windows minimize to tray
# Fixed kanji tip string expansion.
# ---- 0.18 -- 2008-05-26 -- Richard Colley ----
# Fixed mouse-over character selection
# Env var 'bitzer_debug' turns on debug output
# Fixed bug on Windows that tray icon wasn't immediately removed on close
# Possibly fixed bug on Windows where minimize to tray leaves item in taskbar.
# ---- 0.17 -- 2008-05-01 -- Richard Colley ----
# Mouse-over character info.
# Preferences tool tips.
# Minimise to tray icon.
# ---- 0.16 -- 2008-04-29 -- Richard Colley ----
# Try to detect if the main Anki install has implemented some of the
# the features of this plugin. If so, don't interfere.
# ---- 0.15 -- 2008-04-25 -- Richard Colley ----
# Merged with latest Anki scheduling code.
# New option to not display interval time with answer buttons
# ---- 0.14 -- 2008-04-24 -- Richard Colley ----
# Added tray icon, and plugin version string to prefs
# ---- 0.13 -- 2008-04-19 -- Richard Colley ----
# Finished first pass of personal trainer.
# ---- 0.12 -- 2008-04-19 -- Richard Colley ----
# Added new scheduling policy ... speedround.
# This just keeps showing cards until the users stops.
# Implications for future scheduling are unknown, but it looks ok.
# No warranties!! :)
# ---- 0.11 -- 2008-04-03 -- Richard Colley ----
# Added personal trainer ... unfinished
# ---- 0.10 -- 2008-04-02 -- Richard Colley ----
# Refactored extension classes
# ---- 0.09 -- 2008-03-31 -- Richard Colley ----
# Changed all to new-style classes
# ---- 0.08 -- 2008-03-31 -- Richard Colley ----
# added combo box to choose new card scheduling policy
# ---- 0.07 -- 2008-03-30 -- Richard Colley ----
# added button on scribble pad to clear
# fixed changelog date mistakes
# ---- 0.06 -- 2008-03-30 -- Richard Colley ----
# finalised scribble pad
# ---- 0.05 -- 2008-03-29 -- Richard Colley ----
# added new card distribution policy
# ---- 0.04 -- 2008-03-27 -- Richard Colley ----
# another refactor ... but still not happy
# update check notification can be relegated to status bar
# change selection behaviour when edit card window is displayed
# ---- 0.03 -- 2008-03-27 -- Richard Colley ----
# another refactor ... but still more to do
# for instance, can only add 1 tab to preferences ... ideally allow multiple
# ---- 0.02 -- 2008-03-27 -- Richard Colley ----
# bit of a restructure ... could still be better
# ---- 0.01 -- 2008-03-26 -- Richard Colley ----
# initial release
# ---------------------------------------------------------------------------
from PyQt4 import QtGui, QtCore
from ankiqt import mw
from ankiqt.ui.main import AnkiQt
from ankiqt.ui.cardlist import EditDeck, DeckModel
from ankiqt.ui.preferences import Preferences
from ankiqt.ui.utils import askUser
############
class AnkiFunctionality(object):
def __init__(self):
pass
def isSuppressUpdateImplemented( self ):
try:
if mw.newVerInStatusBar:
pass
return True
except:
return False
isSuppressUpdateImplemented=classmethod(isSuppressUpdateImplemented)
def isNewCardSpreadImplemented( self ):
try:
import anki
if 'NEW_CARDS_DISTRIBUTE' in dir(anki.deck):
return True
else:
return False
except:
return False
isNewCardSpreadImplemented=classmethod(isNewCardSpreadImplemented)
############
# based on anki hook stuff
class Hooks(object):
def __init__( self ):
self.setupHooks()
def setupHooks(self):
self.hooks = {}
def addHook(self, hookName, func):
if not self.hooks.get(hookName, None):
self.hooks[hookName] = []
if func not in self.hooks[hookName]:
self.hooks[hookName].append(func)
def removeHook(self, hookName, func):
hook = self.hooks.get(hookName, [])
if func in hook:
hook.remove(func)
def runHook(self, hookName, *args):
hook = self.hooks.get(hookName, None)
if hook:
for func in hook:
func(*args)
############ Debug stuff
import os
import traceback
class RlcDebug(object):
disabled = not os.environ.has_key('bitzer_debug')
def debug( self, *args ):
if self.disabled:
return
for a in args:
print a,
print
debug=classmethod(debug)
def breakpoint( self ):
if self.disabled:
return
breakpoint=classmethod(breakpoint)
def whereAmI( self, str = None ):
if self.disabled:
return
if str:
print str
try:
raise "dummy"
except:
traceback.print_stack()
whereAmI=classmethod(whereAmI)
#####################
# Config stuff
def getConfig( dict, itemName, defaultValue=None ):
if dict.has_key( itemName ):
return dict[itemName]
else:
return defaultValue
def getConfigInt( dict, itemName, defaultValue=None ):
if dict.has_key( itemName ):
return int(dict[itemName])
else:
return int(defaultValue)
def setConfig( dict, itemName, value ):
dict[itemName] = value
#####################
class ExtendAnkiPrefs(Hooks):
def __init__( self, name ):
Hooks.__init__( self )
self.name = name
self.tabItems = {}
self.origSetupAdvanced = Preferences.setupAdvanced
self.origPrefsAccept = Preferences.accept
self.origPrefsReject = Preferences.reject
Preferences.setupAdvanced = lambda prefs: self.interceptSetupAdvanced( prefs )
Preferences.accept = lambda prefs: self.interceptPrefsAccept( prefs )
Preferences.reject = lambda prefs: self.interceptPrefsReject( prefs )
def hookSetup( self, func ):
self.addHook( 'setup', func )
def hookAccept( self, func ):
self.addHook( 'accept', func )
def hookReject( self, func ):
self.addHook( 'reject', func )
def interceptSetupAdvanced( self, prefs ):
self.prefs = prefs
self.prefsAddTab( self.name + " (" + plugin_version + ")" )
self.origSetupAdvanced( prefs )
self.runHook( 'setup', self )
def interceptPrefsAccept( self, prefs ):
self.runHook( 'accept', self )
self.origPrefsAccept( prefs )
def interceptPrefsReject( self, prefs ):
self.runHook( 'reject', self )
self.origPrefsReject( prefs )
def prefsGetConfig( self, itemName, defaultValue=None ):
if self.prefs.config.has_key( itemName ):
return self.prefs.config[itemName]
else:
return defaultValue
def prefsSetConfig( self, itemName, value ):
self.prefs.config[itemName] = value
def prefsGetItem( self, itemName ):
return self.tabItems[itemName]
def prefsCommitCheckBox( self, itemName ):
checkBox = self.prefsGetItem( itemName )
self.prefsSetConfig( itemName, checkBox.isChecked() )
def prefsCommitIntegerBox( self, itemName ):
item = self.prefsGetItem( itemName )
self.prefsSetConfig( itemName, item.text() )
def prefsCommitStringBox( self, itemName ):
item = self.prefsGetItem( itemName )
self.prefsSetConfig( itemName, item.text() )
def prefsCommitSlider( self, itemName ):
slider = self.prefsGetItem( itemName )
self.prefsSetConfig( itemName, slider.value() )
def prefsCommitComboBox( self, itemName ):
combo = self.prefsGetItem( itemName )
self.prefsSetConfig( itemName, combo.currentIndex() )
# UI stuff
def prefsAddTab( self, tabTitle ):
self.tabTitle = tabTitle
self.rlcPrefsTab = QtGui.QWidget()
self.rlcPrefsTab.setObjectName( "rlcPrefsTab" )
vboxLayout = QtGui.QVBoxLayout(self.rlcPrefsTab)
vboxLayout.setObjectName("rlcPrefsTabVboxLayout")
self.layout = QtGui.QGridLayout()
self.layout.setObjectName("rlcPrefsGridLayout")
vboxLayout.addLayout(self.layout)
spacerItem1 = QtGui.QSpacerItem(20,40,QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
vboxLayout.addItem(spacerItem1)
self.prefs.dialog.tabWidget.addTab(self.rlcPrefsTab,self.tabTitle)
return self.layout
def prefsTabAddLabel( self, labelText, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1 ):
label = QtGui.QLabel()
label.setText( labelText )
if toolTip:
print type(toolTip)
label.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(label,row,col,rowSpan,colSpan)
return label
def prefsTabAddCheckBox( self, label, itemName, defaultValue, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1 ):
checkBox = QtGui.QCheckBox()
checkBox.setObjectName( itemName )
checkBox.setText( label )
checkBox.setChecked( self.prefsGetConfig( itemName, defaultValue ) )
self.tabItems[itemName] = checkBox
if toolTip:
checkBox.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(checkBox,row,col,rowSpan,colSpan)
return checkBox
def prefsTabAddIntegerBox( self, label, itemName, defaultValue, min, max, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1 ):
textLine = QtGui.QLineEdit()
textLine.setObjectName( itemName )
validator = QtGui.QIntValidator(None)
if min != None:
validator.setBottom( min )
if max != None:
validator.setTop( max )
textLine.setValidator( validator )
textLine.setText( QtCore.QString( str( self.prefsGetConfig(itemName, defaultValue) ) ) )
self.tabItems[itemName] = textLine
if toolTip:
textLine.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(textLine,row,col,rowSpan,colSpan)
return textLine
def prefsTabAddStringBox( self, label, itemName, defaultValue, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1 ):
textLine = QtGui.QLineEdit()
textLine.setObjectName( itemName )
textLine.setText( QtCore.QString( self.prefsGetConfig(itemName, defaultValue) ) )
self.tabItems[itemName] = textLine
if toolTip:
textLine.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(textLine,row,col,rowSpan,colSpan)
return textLine
def prefsTabAddSlider( self, label, itemName, defaultValue, min, max, step, legend, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1 ):
slider = QtGui.QSlider()
slider.setOrientation( Qt.Horizontal )
slider.setTickPosition(QtGui.QSlider.TicksBelow)
slider.setMinimum( min )
slider.setMaximum( max )
slider.setTickInterval( step )
slider.setSingleStep(step)
slider.setPageStep(step)
slider.setSliderPosition( self.prefsGetConfig( itemName, defaultValue ) / step )
self.tabItems[itemName] = slider
if legend:
comp = QtGui.QWidget()
gl = QtGui.QGridLayout(comp)
gl.addWidget(slider,0,col,1,-1)
legendLen = len( legend )
for i in range(legendLen):
midpoint = legendLen / 2
if i < midpoint:
align=Qt.AlignLeft
elif i == midpoint:
align=Qt.AlignHCenter
else:
align=Qt.AlignRight
label = QtGui.QLabel()
label.setText( legend[i] )
label.setAlignment( align )
gl.addWidget(label,1,col+i,1,1)
else:
comp=slider
if toolTip:
slider.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(comp,row,col,rowSpan,colSpan)
return comp
def prefsTabAddComboBox( self, label, itemName, defaultValue, optionList, toolTip=None, row=None, col=0, rowSpan=1, colSpan=-1, sizeAdjust=QtGui.QComboBox.AdjustToContentsOnFirstShow ):
combo = QtGui.QComboBox()
combo.addItems( optionList )
combo.setCurrentIndex( self.prefsGetConfig( itemName, defaultValue ) )
self.tabItems[itemName] = combo
if toolTip:
combo.setToolTip( toolTip )
if row == None:
row = self.layout.rowCount()
self.layout.addWidget(combo,row,col,rowSpan,colSpan)
return combo
class ExtendAnkiMain(object):
# set to 1 to allow update checks to be completely disabled by user
permitUpdateDisable = 0
PREFS_FOCUS_ON_ANSWER = 'rlc.bitzer.answer.focusOnButton'
DEFAULT_FOCUS_ON_ANSWER = True
TIP_FOCUS_ON_ANSWER = """<p>If enabled, when choosing an answer, [space] will select
the default answer.
<p>Otherwise, there will be no default."""
PREFS_CHECK_FOR_UPDATE = 'rlc.bitzer.update.check'
DEFAULT_CHECK_FOR_UPDATE = True
PREFS_QUIETEN_UPDATE = 'rlc.bitzer.update.quieten'
DEFAULT_QUIETEN_UPDATE = False
TIP_QUIETEN_UPDATE = """<p>If enabled, on startup Anki will display a dialog if there's
a new version available.
<p>If not enabled, a message will be shown in the status bar
instead"""
def __init__( self, extPrefs ):
self.origShowEaseButtons = AnkiQt.showEaseButtons
AnkiQt.showEaseButtons = lambda main: self.interceptShowEaseButtons( main )
if not AnkiFunctionality.isSuppressUpdateImplemented():
self.origSetupAutoUpdate = AnkiQt.setupAutoUpdate
AnkiQt.setupAutoUpdate = lambda main: self.interceptSetupAutoUpdate( main )
self.origNewVerAvail = AnkiQt.newVerAvail
AnkiQt.newVerAvail = lambda main, version: self.interceptNewVerAvail( main, version )
# add to preferences tab
extPrefs.hookSetup( self.addToPrefsTab )
extPrefs.hookAccept( self.acceptPrefs )
def addToPrefsTab( self, extPrefs ):
extPrefs.prefsTabAddLabel( _("<h1>Main Window Settings</h1>") )
extPrefs.prefsTabAddCheckBox( _("Focus on answer"),
self.PREFS_FOCUS_ON_ANSWER,
self.DEFAULT_FOCUS_ON_ANSWER,
self.TIP_FOCUS_ON_ANSWER
)
if not AnkiFunctionality.isSuppressUpdateImplemented():
if self.permitUpdateDisable:
extPrefs.prefsTabAddCheckBox( _("Check for Anki updates"),
self.PREFS_CHECK_FOR_UPDATE,
self.DEFAULT_CHECK_FOR_UPDATE,
)
extPrefs.prefsTabAddCheckBox( _("Suppress update dialog"),
self.PREFS_QUIETEN_UPDATE,
self.DEFAULT_QUIETEN_UPDATE,
self.TIP_QUIETEN_UPDATE,
)
def acceptPrefs( self, extPrefs ):
extPrefs.prefsCommitCheckBox( self.PREFS_FOCUS_ON_ANSWER )
if not AnkiFunctionality.isSuppressUpdateImplemented():
if self.permitUpdateDisable:
extPrefs.prefsCommitCheckBox( self.PREFS_CHECK_FOR_UPDATE )
extPrefs.prefsCommitCheckBox( self.PREFS_QUIETEN_UPDATE )
def interceptShowEaseButtons( self, mw ):
self.origShowEaseButtons( mw )
if not getConfig(mw.config, self.PREFS_FOCUS_ON_ANSWER, self.DEFAULT_FOCUS_ON_ANSWER):
# now remove focus from answer button
mw.setFocus()
def interceptSetupAutoUpdate( self, mw ):
if not getConfig(mw.config, self.PREFS_CHECK_FOR_UPDATE, self.DEFAULT_CHECK_FOR_UPDATE):
RlcDebug.debug( "Checking for update!" )
self.origSetupAutoUpdate( mw )
def interceptNewVerAvail( self, mw, version ):
if getConfig(mw.config, self.PREFS_QUIETEN_UPDATE, self.DEFAULT_QUIETEN_UPDATE):
mw.statusView.statusbar.showMessage( _("Anki Update Available: ") + version, 5000 )
else:
self.origNewVerAvail( mw, version )
######################
import time
from heapq import heappush, heappop
from anki.deck import Deck
import anki
class CardSchedulingPolicy(object):
def __init__( self ):
pass
def getCard( self, deck, orm=True ):
raise "Must implement this! And return a card"
class AnkiDefaultSchedulingPolicy( CardSchedulingPolicy ):
def __init__( self, default ):
CardSchedulingPolicy.__init__( self )
self.default = default
def getCard( self, deck, orm=True ):
card = self.default( deck, orm )
if card: RlcDebug.debug( "Default returning card: ", card.question )
return card
class DistributeNewCardsSchedulingPolicy( CardSchedulingPolicy ):
def __init__( self, distribution ):
CardSchedulingPolicy.__init__( self )
self.distribution = distribution
self.totalCardsScheduled = 0
self.newCardsScheduled = 0
def getDistribution( self, distribution ):
return self.distribution
def setDistribution( self, distribution ):
self.totalCardsScheduled = 0
self.newCardsScheduled = 0
self.distribution = distribution
def _timeForForcedNew( self, deck ):
RlcDebug.debug( "timeForForcedNew(): self.distribution=", self.distribution )
RlcDebug.debug( "timeForForcedNew(): self.newCardsScheduled=", self.newCardsScheduled )
RlcDebug.debug( "timeForForcedNew(): self.totalCardsScheduled=", self.totalCardsScheduled )
# avoid division issues
if self.distribution == 0:
rv = False
elif self.distribution == 100:
rv = True
elif self.totalCardsScheduled == 0:
rv = False
else:
runningDistribution = (100 * self.newCardsScheduled) / self.totalCardsScheduled
RlcDebug.debug( "timeForForcedNew(): runningDistribution=", runningDistribution )
rv = runningDistribution < self.distribution
RlcDebug.debug( "timeForForcedNew(): returning ", rv )
return rv
def getCard( self, deck, orm=True ):
"Return the next due card, or None"
ids = self.getCardIds( deck )
if ids:
return deck.cardFromId(ids[0], orm)
def getCardIds(self, deck, limit=1):
"""Return up to LIMIT number of pending card IDs.
Caller is responsible for checking cards are not spaced if
limit is above 1."""
ids = []
# check if we should schedule a new card now
if self._timeForForcedNew(deck):
RlcDebug.debug( "should show a new card" )
# new card
if deck.newCardOrder == 0:
ids += deck.s.column0(
"select id from acqCardsRandom limit 1")
else:
ids += deck.s.column0(
"select id from acqCardsOrdered limit 1")
if ids: RlcDebug.debug( "popped: ", ids[0] )
self.newCardsScheduled += len(ids)
rem = limit - len(ids)
if rem > 0:
# failed card due?
ids += deck.s.column0("select id from failedCardsNow limit %d" % limit)
rem = limit - len(ids)
if rem > 0:
# failed card queue too big?
if deck.failedCount >= deck.failedCardMax:
ids += deck.s.column0(
"select id from failedCardsSoon limit %d" % rem)
rem = limit - len(ids)
if rem > 0:
# card due for review?
ids += deck.s.column0("select id from revCards limit %d" % rem)
rem = limit - len(ids)
if rem > 0:
# new card
if deck.newCardOrder == 0:
newids = deck.s.column0(
"select id from acqCardsRandom limit %d" % rem)
else:
newids = deck.s.column0(
"select id from acqCardsOrdered limit %d" % rem)
ids += newids
self.newCardsScheduled += len(newids)
if not ids:
if deck.collapseTime:
# final review
ids += deck.s.column0(
"select id from failedCardsSoon limit %d" % rem)
if ids:
RlcDebug.debug( "Got card(s): ", ids )
self.totalCardsScheduled=self.totalCardsScheduled+len(ids)
return ids
#class SpeedRoundSchedulingPolicy( CardSchedulingPolicy ):
# def __init__( self ):
# CardSchedulingPolicy.__init__( self )
#
# def getCard( self, deck, orm=True ):
# "Return the next due card, or None"
#
# ids = self.getCardIds( deck )
# if ids:
# return deck.cardFromId(ids[0], orm)
#
# def getCardIds(self, deck, limit=1):
# """Return up to LIMIT number of pending card IDs.
#Caller is responsible for checking cards are not spaced if
#limit is above 1."""
# ids = []
# # check if we should schedule a new card now
# if self._timeForForcedNew(deck):
# RlcDebug.debug( "should show a new card" )
# # new card
# if deck.newCardOrder == 0:
# ids += deck.s.column0(
# "select id from acqCardsRandom limit 1")
# else:
# ids += deck.s.column0(
# "select id from acqCardsOrdered limit 1")
# if ids: RlcDebug.debug( "popped: ", ids[0] )
# self.newCardsScheduled += len(ids)
#
# rem = limit - len(ids)
# if rem > 0:
# # failed card due?
# ids += deck.s.column0("select id from failedCardsNow limit %d" % limit)
# rem = limit - len(ids)
# if rem > 0:
# # failed card queue too big?
# if deck.failedCount >= deck.failedCardMax:
# ids += deck.s.column0(
# "select id from failedCardsSoon limit %d" % rem)
# rem = limit - len(ids)
# if rem > 0:
# # card due for review?
# ids += deck.s.column0("select id from revCards limit %d" % rem)
# rem = limit - len(ids)
# if rem > 0:
# # new card
# if deck.newCardOrder == 0:
# newids += deck.s.column0(
# "select id from acqCardsRandom limit %d" % rem)
# else:
# newids += deck.s.column0(
# "select id from acqCardsOrdered limit %d" % rem)
# ids += newids
# self.newCardsScheduled += len(newids)
# if not ids:
# if deck.collapseTime:
# # final review
# ids += deck.s.column0(
# "select id from failedCardsSoon limit %d" % rem)
#
# if ids:
# RlcDebug.debug( "Got card(s): ", ids )
# self.totalCardsScheduled=self.totalCardsScheduled+len(ids)
# return ids
# def getCard( self, deck, orm=True ):
# "Return the next card, due or not"
#
# item = None
#
# while deck.futureQueue:
# newItem = heappop(deck.futureQueue)
# deck.addExpiredItem(newItem)
#
# # failed card due?
# if deck.failedQueue:
# item = heappop(deck.failedQueue)
# # card due for revision
# elif deck.revQueue:
# item = heappop(deck.revQueue)
# # card due for acquisition
# elif deck.acqQueue:
# item = heappop(deck.acqQueue)
#
# if not item:
# return
#
# card = deck.s.query(anki.cards.Card).get(item.id)
# if card:
# card.genFuzz()
# card.startTimer()
# return card
class ExtendAnkiScheduling(object):
# types of new card scheduling policy
NC_POLICY_ORIGINAL = 0
NC_POLICY_DISTRIBUTE = 1
NC_POLICY_SPEEDROUND = 2
PREFS_NEW_CARD_POLICY = 'rlc.bitzer.cards.new.policy'
DEFAULT_NEW_CARD_POLICY = NC_POLICY_ORIGINAL
TIP_NEW_CARD_POLICY = """<p>Select between different card scheduling policies.
These policies affect the order and timing of cards.
<ul>
<li>Anki default policy</li>"""
if not AnkiFunctionality.isNewCardSpreadImplemented():
TIP_NEW_CARD_POLICY += """
<li>New card distribution policy - this lets you mix in
new cards while answering old ones. You will be able to
choose how frequently new cards will be shown.</li>
"""
# <li>Ignore timing policy - review all cards immediately.
# Cards will be shown in the scheduled order, but the timing
# will be ignored. Possibly useful for a quick review.</li>
TIP_NEW_CARD_POLICY += """</ul>"""
PREFS_NEW_CARD_DISTRIBUTION = 'rlc.bitzer.cards.new.distribution'
DEFAULT_NEW_CARD_DISTRIBUTION = 0
def __init__( self, extPrefs, config ):
self.oldDeckGetCard = Deck.getCard
Deck.getCard = lambda deck, orm=True : self.interceptDeckGetCard( deck, orm )
self.defaultPolicy = AnkiDefaultSchedulingPolicy( self.oldDeckGetCard )
self.setSchedulingPolicyFromConfig( config )
extPrefs.hookSetup( self.addToPrefsTab )
extPrefs.hookAccept( self.acceptPrefs )
def addToPrefsTab( self, extPrefs ):
extPrefs.prefsTabAddLabel( _( """<h1>Card Scheduling</h1>""" ) )
combo = extPrefs.prefsTabAddComboBox( _("Choose Policy"),
self.PREFS_NEW_CARD_POLICY,
self.DEFAULT_NEW_CARD_POLICY,
#[ 'Anki Default', 'New cards mixed in', 'Ignore card timing' ], #with Speed-round
( [ 'Anki Default', 'New cards mixed in' ], [ 'Anki Default' ] )[AnkiFunctionality.isNewCardSpreadImplemented()],
self.TIP_NEW_CARD_POLICY
)
extPrefs.prefs.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), lambda i: self.policySelected(i))
self.slider = extPrefs.prefsTabAddSlider( _("Frequency of new cards during review"),
self.PREFS_NEW_CARD_DISTRIBUTION,
self.DEFAULT_NEW_CARD_DISTRIBUTION,
0, 4, 1,
[ _("Old first"), _("1 in 4 new"), _("1 in 2 new"), _("3 in 4 new"), _("New first") ] )
self.policySelected( getConfig( extPrefs.prefs.config, self.PREFS_NEW_CARD_POLICY, self.DEFAULT_NEW_CARD_POLICY ) )
def policySelected( self, index ):
if ( not AnkiFunctionality.isNewCardSpreadImplemented() and
index == self.NC_POLICY_DISTRIBUTE ):
self.slider.show()
else:
self.slider.hide()
def acceptPrefs( self, extPrefs ):
extPrefs.prefsCommitComboBox( self.PREFS_NEW_CARD_POLICY )
if not AnkiFunctionality.isNewCardSpreadImplemented():
extPrefs.prefsCommitSlider( self.PREFS_NEW_CARD_DISTRIBUTION )
self.setSchedulingPolicyFromConfig( extPrefs.prefs.config )
def setSchedulingPolicy( self, schedulingPolicy ):
self.schedulingPolicy = schedulingPolicy
def interceptDeckGetCard( self, deck, orm=True ):
RlcDebug.debug( "deck get card" )
return self.schedulingPolicy.getCard( deck, orm )
def setSchedulingPolicyFromConfig( self, config ):
policy = getConfig( config, self.PREFS_NEW_CARD_POLICY, self.DEFAULT_NEW_CARD_POLICY )
if ( not AnkiFunctionality.isNewCardSpreadImplemented() and
policy == self.NC_POLICY_DISTRIBUTE ):
self.schedulingPolicy = DistributeNewCardsSchedulingPolicy(
getConfig( config, self.PREFS_NEW_CARD_DISTRIBUTION,
self.DEFAULT_NEW_CARD_DISTRIBUTION) * 25 )
# elif policy == self.NC_POLICY_SPEEDROUND:
# self.schedulingPolicy = SpeedRoundSchedulingPolicy()
else:
self.schedulingPolicy = self.defaultPolicy
self.setSchedulingPolicy( self.schedulingPolicy )
class ExtendAnkiHelp(object):
PREFS_ENABLE_SCRIBBLE = 'rlc.bitzer.help.scribble'
DEFAULT_ENABLE_SCRIBBLE = False
TIP_ENABLE_SCRIBBLE = """<p>Choose whether or not to enable the scribble pad. This
area can be used for practising writing kanji or kana.
<p>Draw with the left mouse-button held down. Right mouse-button
will clear the area."""
def __init__( self, extPrefs, mw ):
self.mw = mw
self.scribbleActive = False
self.scribble = None
extPrefs.hookSetup( self.addToPrefsTab )
extPrefs.hookAccept( self.acceptPrefs )
def addToPrefsTab( self, extPrefs ):
extPrefs.prefsTabAddCheckBox( _("Enable scribble pad"),
self.PREFS_ENABLE_SCRIBBLE,
self.DEFAULT_ENABLE_SCRIBBLE,
self.TIP_ENABLE_SCRIBBLE,
)
def acceptPrefs( self, extPrefs ):
extPrefs.prefsCommitCheckBox( self.PREFS_ENABLE_SCRIBBLE )
self.setScribble( extPrefs.prefsGetConfig( self.PREFS_ENABLE_SCRIBBLE ) )
def createScribble( self ):
mwui = self.mw.mainWin
self.scribble = QtGui.QWidget()
layout = QtGui.QVBoxLayout(self.scribble)
layout.setSpacing(3)
layout.setMargin(3)
pad = Painting( mwui.innerHelpFrame, 300, 300 )
pad.setEnabled( True )
pad.setMinimumSize(QtCore.QSize(200,0))
pad.setMaximumSize(QtCore.QSize(300,300))
button = QtGui.QPushButton(_("Clear Drawing"))
button.setDefault(True)
#button.setFixedHeight(self.mw.easeButtonHeight)
self.mw.connect(button, QtCore.SIGNAL("clicked()"),
lambda: pad.clearScreen())
layout.addWidget(button)
layout.addWidget(pad,1)
def setScribble( self, display=False ):
mwui = self.mw.mainWin
RlcDebug.debug( "setScribble(): display = ", display )
if not display and self.scribbleActive:
RlcDebug.debug( "setScribble(): hiding" )
self.scribbleActive = False
mwui.hboxlayout2.removeWidget( self.helpSplitter )
mwui.hboxlayout2.addWidget( mwui.help )
self.scribble.hide()
self.mw.help.hide()
elif display and not self.scribbleActive:
RlcDebug.debug( "setScribble(): showing" )
self.scribbleActive = True
if not self.scribble:
self.createScribble()
mwui.hboxlayout2.removeWidget( mwui.help )
self.helpSplitter = QtGui.QSplitter( mwui.innerHelpFrame )
self.helpSplitter.setOrientation( Qt.Vertical )
mwui.hboxlayout2.addWidget( self.helpSplitter )
self.helpSplitter.addWidget( mwui.help )
self.helpSplitter.addWidget( self.scribble )
self.helpSplitter.setSizes( [100,150] )
self.scribble.show()
self.mw.help.showText( """
<h1>Scribble</h1>
This is the scribble pane.
<p>
The top portion is Anki's help text (where this text appears).
<p>
The bottom part is a drawing area. Hold the left mouse button, and drag to draw. Right mouse button clears the drawing.
<p>
Betwen the top & bottom sections is a grab handle so you can re-size the two parts.
<p>
You can disable the scribble functionality from Preferences->RLC Bitzer Settings
""")
##############################
from PyQt4.QtGui import QDialog, QPainter, QWidget, QBrush, QImage, QPen, qRgb, QItemSelectionModel
from PyQt4.QtCore import Qt, QPoint
class Painting(QWidget):
def __init__(self, parent, width=300, height=200 ):
QWidget.__init__(self, parent)
self.image = QImage( width, height , QImage.Format_Mono )
self.image.setColor( 0, qRgb(0,0,0) )
self.image.setColor( 1, qRgb(255,255,255) )
self.scribble = 0
self.clearScreen()
def paintEvent(self, ev):
p = QPainter()
p.begin(self)
p.drawImage( QPoint(0,0), self.image )
p.end()
def mouseMoveEvent(self, ev):
if self.scribble:
self.drawLineTo( QPoint( ev.pos() ) )
def mousePressEvent(self, ev):
if bool(ev.buttons() & QtCore.Qt.RightButton):
self.clearScreen()
else:
self.scribble = 1
self.currentPos=QPoint(ev.pos())
def mouseReleaseEvent(self, ev):
self.scribble = 0
def clearScreen( self ):
p = QPainter( self.image )
p.fillRect(self.image.rect(), QBrush(Qt.white))
self.update()
def drawLineTo( self, newPos ):
p = QPainter( self.image )
pen = QPen(QtCore.Qt.black, 4, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap)
p.setPen(pen)
p.drawLine( self.currentPos, newPos )
self.currentPos = newPos
self.update()
###############################################
class AnkiPersonalTrainer(object):
"""
Motivate the user to the point of exhaustion :)
"""
PREFS_TRAINER_ENABLE = 'rlc.bitzer.trainer.enable'
DEFAULT_TRAINER_ENABLE = False
TIP_TRAINER_ENABLE = """<p>The Anki Personal Trainer can be used to limit your Anki
study sessions. You can set time limits or card number limits
on a session. Pace yourself, and get a sense of accomplishment.
<p>When the session ends, Anki will ask you if you want to
extend ... push that little bit further! :)"""
PREFS_TRAINER_CARD_LIMIT = 'rlc.bitzer.trainer.limit.card'
DEFAULT_TRAINER_CARD_LIMIT = True
TIP_TRAINER_CARD_LIMIT = """<p>Enable if you want to set a limit on the number of cards
you study in a session."""
PREFS_TRAINER_CARD_LIMIT_VALUE = 'rlc.bitzer.trainer.limit.card.value'
DEFAULT_TRAINER_CARD_LIMIT_VALUE = 100
PREFS_TRAINER_TIME_LIMIT = 'rlc.bitzer.trainer.limit.time'
DEFAULT_TRAINER_TIME_LIMIT = False
TIP_TRAINER_TIME_LIMIT = """<p>Enable if you want to set a limit on the time you study
in a session. Time is in minutes."""
PREFS_TRAINER_TIME_LIMIT_VALUE = 'rlc.bitzer.trainer.limit.time.value'
DEFAULT_TRAINER_TIME_LIMIT_VALUE = 60 # minutes
def __init__( self, extPrefs, mw ):
self.mw = mw
self.enable = 0
self.firstTime = True
self.cardsThisSession = 0
self.sessionStart = time.time()
self.setLimitsFromConfig( mw.config )
extPrefs.hookSetup( self.addToPrefsTab )
extPrefs.hookAccept( self.acceptPrefs )
self.origMoveToState = AnkiQt.moveToState
AnkiQt.moveToState = lambda main, state: self.interceptMoveToState( main, state )
def addToPrefsTab( self, extPrefs ):
extPrefs.prefsTabAddLabel( _("<h1>Personal Trainer</h1>") )
self.ptCheck = extPrefs.prefsTabAddCheckBox( _("Enable personal trainer"),
self.PREFS_TRAINER_ENABLE,
self.DEFAULT_TRAINER_ENABLE,
self.TIP_TRAINER_ENABLE,
)
row = extPrefs.layout.rowCount()
extPrefs.prefs.connect(self.ptCheck, QtCore.SIGNAL("stateChanged(int)"), lambda i: self.ptCheckChanged(i))
self.ptCardCheck = extPrefs.prefsTabAddCheckBox( _("Card limit"),
self.PREFS_TRAINER_CARD_LIMIT,
self.DEFAULT_TRAINER_CARD_LIMIT,
self.TIP_TRAINER_CARD_LIMIT,
row, 0, 1, 1 )
extPrefs.prefs.connect(self.ptCardCheck, QtCore.SIGNAL("stateChanged(int)"), lambda i: self.ptCardCheckChanged(i))
self.ptCardValue = extPrefs.prefsTabAddIntegerBox( None,
self.PREFS_TRAINER_CARD_LIMIT_VALUE,
self.DEFAULT_TRAINER_CARD_LIMIT_VALUE,
1, 1000,
None,
row, 1, 1, 1 )
self.ptTimeCheck = extPrefs.prefsTabAddCheckBox( _("Time limit"),
self.PREFS_TRAINER_TIME_LIMIT,
self.DEFAULT_TRAINER_TIME_LIMIT,
self.TIP_TRAINER_TIME_LIMIT,
row, 2, 1, 1 )
extPrefs.prefs.connect(self.ptTimeCheck, QtCore.SIGNAL("stateChanged(int)"), lambda i: self.ptTimeCheckChanged(i))
self.ptTimeValue = extPrefs.prefsTabAddIntegerBox( None,
self.PREFS_TRAINER_TIME_LIMIT_VALUE,
self.DEFAULT_TRAINER_TIME_LIMIT_VALUE,
10, 600,
None,
row, 3, 1, 1 )
self.ptCheckChanged( self.ptCheck.isChecked() )
def acceptPrefs( self, extPrefs ):
extPrefs.prefsCommitCheckBox( self.PREFS_TRAINER_ENABLE )
extPrefs.prefsCommitCheckBox( self.PREFS_TRAINER_CARD_LIMIT )
extPrefs.prefsCommitIntegerBox( self.PREFS_TRAINER_CARD_LIMIT_VALUE )
extPrefs.prefsCommitCheckBox( self.PREFS_TRAINER_TIME_LIMIT )
extPrefs.prefsCommitIntegerBox( self.PREFS_TRAINER_TIME_LIMIT_VALUE )
self.setLimitsFromConfig( extPrefs.prefs.config )
def setLimitsFromConfig( self, config ):
self.enable = getConfig( config, self.PREFS_TRAINER_ENABLE, self.DEFAULT_TRAINER_ENABLE )
self.card_limit = getConfig( config, self.PREFS_TRAINER_CARD_LIMIT, self.DEFAULT_TRAINER_CARD_LIMIT )
self.card_limit_value = getConfigInt( config, self.PREFS_TRAINER_CARD_LIMIT_VALUE, self.DEFAULT_TRAINER_CARD_LIMIT_VALUE )
self.time_limit = getConfig( config, self.PREFS_TRAINER_TIME_LIMIT, self.DEFAULT_TRAINER_TIME_LIMIT )
self.time_limit_value = getConfigInt( config, self.PREFS_TRAINER_TIME_LIMIT_VALUE, self.DEFAULT_TRAINER_TIME_LIMIT_VALUE )
def ptCheckChanged( self, state ):
self.ptCardCheck.setEnabled(state)
self.ptTimeCheck.setEnabled(state)
if state: