forked from ankitects/anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
86 lines (76 loc) · 2.99 KB
/
stats.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
# Copyright: Damien Elmes <[email protected]>
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http:https://www.gnu.org/licenses/agpl.html
from aqt.qt import *
import os, time
from aqt.utils import saveGeom, restoreGeom, maybeHideClose, showInfo, addCloseShortcut
import aqt
# Deck Stats
######################################################################
class DeckStats(QDialog):
def __init__(self, mw):
QDialog.__init__(self, mw, Qt.Window)
self.mw = mw
self.name = "deckStats"
self.period = 0
self.form = aqt.forms.stats.Ui_Dialog()
self.oldPos = None
self.wholeCollection = False
self.setMinimumWidth(700)
f = self.form
f.setupUi(self)
restoreGeom(self, self.name)
b = f.buttonBox.addButton(_("Save Image"),
QDialogButtonBox.ActionRole)
b.connect(b, SIGNAL("clicked()"), self.browser)
b.setAutoDefault(False)
c = self.connect
s = SIGNAL("clicked()")
c(f.groups, s, lambda: self.changeScope("deck"))
f.groups.setShortcut("g")
c(f.all, s, lambda: self.changeScope("collection"))
c(f.month, s, lambda: self.changePeriod(0))
c(f.year, s, lambda: self.changePeriod(1))
c(f.life, s, lambda: self.changePeriod(2))
c(f.web, SIGNAL("loadFinished(bool)"), self.loadFin)
maybeHideClose(self.form.buttonBox)
addCloseShortcut(self)
self.refresh()
self.exec_()
def reject(self):
saveGeom(self, self.name)
QDialog.reject(self)
def browser(self):
name = time.strftime("-%Y-%m-%d@%H-%M-%S.png",
time.localtime(time.time()))
name = "anki-"+_("stats")+name
desktopPath = QDesktopServices.storageLocation(QDesktopServices.DesktopLocation)
if not os.path.exists(desktopPath):
os.mkdir(desktopPath)
path = os.path.join(desktopPath, name)
p = self.form.web.page()
oldsize = p.viewportSize()
p.setViewportSize(p.mainFrame().contentsSize())
image = QImage(p.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
p.mainFrame().render(painter)
painter.end()
image.save(path, "png")
p.setViewportSize(oldsize)
showInfo(_("An image was saved to your desktop."))
def changePeriod(self, n):
self.period = n
self.refresh()
def changeScope(self, type):
self.wholeCollection = type == "collection"
self.refresh()
def loadFin(self, b):
self.form.web.page().mainFrame().setScrollPosition(self.oldPos)
def refresh(self):
self.mw.progress.start(immediate=True)
self.oldPos = self.form.web.page().mainFrame().scrollPosition()
stats = self.mw.col.stats()
stats.wholeCollection = self.wholeCollection
self.report = stats.report(type=self.period)
self.form.web.setHtml(self.report)
self.mw.progress.finish()