anki/qt/aqt/stats.py

105 lines
3.1 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2019-12-20 10:19:03 +01:00
import time
import aqt
2019-03-04 02:58:34 +01:00
from anki.lang import _
2019-12-20 10:19:03 +01:00
from aqt.qt import *
from aqt.theme import theme_manager
2019-12-23 01:34:10 +01:00
from aqt.utils import (
addCloseShortcut,
getSaveFile,
maybeHideClose,
restoreGeom,
saveGeom,
tooltip,
)
# Deck Stats
######################################################################
2019-12-23 01:34:10 +01:00
class DeckStats(QDialog):
def __init__(self, mw):
QDialog.__init__(self, mw, Qt.Window)
mw.setupDialogGC(self)
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
if theme_manager.night_mode and not theme_manager.macos_dark_mode():
self.setStyleSheet("QGroupBox { padding-top: 0; }")
f.setupUi(self)
restoreGeom(self, self.name)
2019-12-23 01:34:10 +01:00
b = f.buttonBox.addButton(_("Save PDF"), QDialogButtonBox.ActionRole)
2017-01-13 08:36:26 +01:00
b.clicked.connect(self.saveImage)
b.setAutoDefault(False)
f.groups.clicked.connect(lambda: self.changeScope("deck"))
f.groups.setShortcut("g")
f.all.clicked.connect(lambda: self.changeScope("collection"))
f.month.clicked.connect(lambda: self.changePeriod(0))
f.year.clicked.connect(lambda: self.changePeriod(1))
f.life.clicked.connect(lambda: self.changePeriod(2))
maybeHideClose(self.form.buttonBox)
addCloseShortcut(self)
self.show()
self.refresh()
self.activateWindow()
def reject(self):
self.form.web = None
saveGeom(self, self.name)
aqt.dialogs.markClosed("DeckStats")
QDialog.reject(self)
def closeWithCallback(self, callback):
self.reject()
callback()
2017-01-13 08:36:26 +01:00
def _imagePath(self):
2019-12-23 01:34:10 +01:00
name = time.strftime("-%Y-%m-%d@%H-%M-%S.pdf", time.localtime(time.time()))
name = "anki-" + _("stats") + name
file = getSaveFile(
self,
title=_("Save PDF"),
dir_description="stats",
key="stats",
ext=".pdf",
fname=name,
)
2017-09-10 07:50:34 +02:00
return file
2017-01-13 08:36:26 +01:00
def saveImage(self):
path = self._imagePath()
2017-09-10 07:50:34 +02:00
if not path:
return
2017-01-13 08:36:26 +01:00
self.form.web.page().printToPdf(path)
2017-09-10 07:50:34 +02:00
tooltip(_("Saved."))
def changePeriod(self, n):
self.period = n
self.refresh()
def changeScope(self, type):
self.wholeCollection = type == "collection"
self.refresh()
def refresh(self):
self.mw.progress.start(immediate=True, parent=self)
stats = self.mw.col.stats()
stats.wholeCollection = self.wholeCollection
self.report = stats.report(type=self.period)
self.form.web.title = "deck stats"
2019-12-23 01:34:10 +01:00
self.form.web.stdHtml(
"<html><body>" + self.report + "</body></html>",
js=["jquery.js", "plot.js"],
context=self,
2019-12-23 01:34:10 +01:00
)
self.mw.progress.finish()