add the ability to open deck stats in the browser

This commit is contained in:
Damien Elmes 2011-03-24 13:26:24 +09:00
parent 1f98ef588f
commit 1b20f948f3
2 changed files with 48 additions and 13 deletions

View file

@ -144,7 +144,8 @@ class AnkiQt(QMainWindow):
body {
/*background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#bbb));*/
background: #eee;
margin: 2em; }
margin: 2em;
}
a:hover { background-color: #aaa; }
.but {
-webkit-box-shadow: 2px 2px 6px rgba(0,0,0,0.6);
@ -1193,7 +1194,7 @@ learnt today")
self.cardStats.show()
def onDeckStats(self):
aqt.stats.DeckStats(self)
aqt.stats.deckStats(self)
# Graphs
##########################################################################

View file

@ -4,7 +4,9 @@
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os, tempfile
from aqt.webview import AnkiWebView
from aqt.utils import saveGeom, restoreGeom
from anki.hooks import addHook
# Card stats
@ -60,26 +62,58 @@ class CardStats(object):
# Deck stats
######################################################################
class DeckStats(QDialog):
class PrintableReport(QDialog):
def __init__(self, mw):
def __init__(self, mw, type, title, func, css):
self.mw = mw
QDialog.__init__(self, mw)
self.setWindowTitle(_("Deck Statistics"))
restoreGeom(self, type)
self.type = type
self.setWindowTitle(title)
self.setModal(True)
self.mw.progress.start()
self.web = AnkiWebView(self)
stats = self.mw.deck.deckStats()
stats = func()
l = QVBoxLayout(self)
l.setContentsMargins(0,0,0,0)
l.addWidget(self.web)
self.setLayout(l)
self.web.stdHtml(stats, css=self.mw.sharedCSS+"""
body { margin: 2em; }
h1 { font-size: 18px; border-bottom: 1px solid #000; margin-top: 1em;
clear: both; }
.info {float:right; padding: 10px; max-width: 300px; border-radius: 5px;
background: #ddd; font-size: 14px; }
""")
self.report = func()
self.css = css
self.web.stdHtml(self.report, css=css)
box = QDialogButtonBox(QDialogButtonBox.Close)
b = box.addButton(_("Open In Browser"), QDialogButtonBox.AcceptRole)
box.button(QDialogButtonBox.Close).setDefault(True)
l.addWidget(box)
self.connect(box, SIGNAL("accepted()"), self.browser)
self.connect(box, SIGNAL("rejected()"), self, SLOT("reject()"))
self.mw.progress.finish()
self.exec_()
def reject(self):
saveGeom(self, self.type)
QDialog.reject(self)
def browser(self):
# dump to a temporary file
tmpdir = tempfile.mkdtemp(prefix="anki")
path = os.path.join(tmpdir, "report.html")
open(path, "w").write("""
<html><head><style>%s</style></head><body>%s</body></html>""" % (
self.css, self.report))
QDesktopServices.openUrl(QUrl("file://" + path))
def deckStats(mw):
css=mw.sharedCSS+"""
body { margin: 2em; font-family: arial; }
h1 { font-size: 18px; border-bottom: 1px solid #000; margin-top: 1em;
clear: both; margin-bottom: 0.5em; }
.info {float:right; padding: 10px; max-width: 300px; border-radius: 5px;
background: #ddd; font-size: 14px; }
"""
return PrintableReport(
mw,
"deckstats",
_("Deck Statistics"),
mw.deck.deckStats,
css)