2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-06-29 06:51:15 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
import time
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2019-03-04 02:58:34 +01:00
|
|
|
from anki.lang import _
|
2020-07-16 21:48:46 +02:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-06-30 08:23:46 +02:00
|
|
|
from aqt.theme import theme_manager
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
|
|
|
addCloseShortcut,
|
|
|
|
getSaveFile,
|
|
|
|
maybeHideClose,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
|
|
|
tooltip,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
|
2020-06-30 09:08:10 +02:00
|
|
|
class NewDeckStats(QDialog):
|
|
|
|
"""New deck stats."""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-29 06:51:15 +02:00
|
|
|
def __init__(self, mw: aqt.main.AnkiQt):
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw, Qt.Window)
|
2016-07-04 05:22:35 +02:00
|
|
|
mw.setupDialogGC(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
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)
|
2020-06-29 06:51:15 +02:00
|
|
|
f.groupBox.setVisible(False)
|
|
|
|
f.groupBox_2.setVisible(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, self.name)
|
2019-12-23 01:34:10 +01:00
|
|
|
b = f.buttonBox.addButton(_("Save PDF"), QDialogButtonBox.ActionRole)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(b.clicked, self.saveImage)
|
2012-12-21 08:51:59 +01:00
|
|
|
b.setAutoDefault(False)
|
|
|
|
maybeHideClose(self.form.buttonBox)
|
2012-12-22 01:11:29 +01:00
|
|
|
addCloseShortcut(self)
|
2020-07-16 21:48:46 +02:00
|
|
|
gui_hooks.stats_dialog_will_show(self)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.show()
|
2018-10-23 09:09:16 +02:00
|
|
|
self.refresh()
|
2017-06-06 08:16:00 +02:00
|
|
|
self.activateWindow()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def reject(self):
|
2019-09-16 07:16:41 +02:00
|
|
|
self.form.web = None
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(self, self.name)
|
2020-06-30 09:08:10 +02:00
|
|
|
aqt.dialogs.markClosed("NewDeckStats")
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
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."))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def changePeriod(self, n):
|
2020-06-29 06:51:15 +02:00
|
|
|
pass
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def changeScope(self, type):
|
2020-06-29 06:51:15 +02:00
|
|
|
pass
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def refresh(self):
|
2020-06-29 06:51:15 +02:00
|
|
|
self.form.web.set_open_links_externally(False)
|
2020-06-30 08:23:46 +02:00
|
|
|
if theme_manager.night_mode:
|
|
|
|
extra = "#night"
|
|
|
|
else:
|
|
|
|
extra = ""
|
2020-06-30 09:08:10 +02:00
|
|
|
self.form.web.load(QUrl(f"{self.mw.serverURL()}_anki/graphs.html" + extra))
|
|
|
|
|
|
|
|
|
|
|
|
class DeckStats(QDialog):
|
|
|
|
"""Legacy deck stats, used by some add-ons."""
|
|
|
|
|
|
|
|
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():
|
|
|
|
# the grouping box renders incorrectly in the fusion theme. 5.9+
|
|
|
|
# 5.13 behave differently to 5.14, but it looks bad in either case,
|
|
|
|
# and adjusting the top margin makes the 'save PDF' button show in
|
|
|
|
# the wrong place, so for now we just disable the border instead
|
|
|
|
self.setStyleSheet("QGroupBox { border: 0; }")
|
|
|
|
f.setupUi(self)
|
|
|
|
restoreGeom(self, self.name)
|
|
|
|
b = f.buttonBox.addButton(_("Save PDF"), QDialogButtonBox.ActionRole)
|
|
|
|
qconnect(b.clicked, self.saveImage)
|
|
|
|
b.setAutoDefault(False)
|
|
|
|
qconnect(f.groups.clicked, lambda: self.changeScope("deck"))
|
|
|
|
f.groups.setShortcut("g")
|
|
|
|
qconnect(f.all.clicked, lambda: self.changeScope("collection"))
|
|
|
|
qconnect(f.month.clicked, lambda: self.changePeriod(0))
|
|
|
|
qconnect(f.year.clicked, lambda: self.changePeriod(1))
|
|
|
|
qconnect(f.life.clicked, lambda: self.changePeriod(2))
|
|
|
|
maybeHideClose(self.form.buttonBox)
|
|
|
|
addCloseShortcut(self)
|
2020-07-16 21:48:46 +02:00
|
|
|
gui_hooks.stats_dialog_old_will_show(self)
|
2020-06-30 09:08:10 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
def _imagePath(self):
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
return file
|
|
|
|
|
|
|
|
def saveImage(self):
|
|
|
|
path = self._imagePath()
|
|
|
|
if not path:
|
|
|
|
return
|
|
|
|
self.form.web.page().printToPdf(path)
|
|
|
|
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(parent=self)
|
|
|
|
stats = self.mw.col.stats()
|
|
|
|
stats.wholeCollection = self.wholeCollection
|
|
|
|
self.report = stats.report(type=self.period)
|
|
|
|
self.form.web.title = "deck stats"
|
|
|
|
self.form.web.stdHtml(
|
|
|
|
"<html><body>" + self.report + "</body></html>",
|
|
|
|
js=["jquery.js", "plot.js"],
|
|
|
|
context=self,
|
|
|
|
)
|
|
|
|
self.mw.progress.finish()
|