remove help, drop aqt.utils from frequently used functions

This commit is contained in:
Damien Elmes 2011-03-24 11:00:45 +09:00
parent abe67f6faa
commit 3c8763602c
3 changed files with 40 additions and 117 deletions

View file

@ -1,87 +0,0 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import aqt.forms
from anki.hooks import runHook
# Hideable help area widget
##########################################################################
class HelpArea(object):
def __init__(self, helpFrame, config, mainWindow=None, focus=None):
self.helpFrame = helpFrame
self.widget = helpFrame.findChild(QTextBrowser)
self.mainWindow = mainWindow
if mainWindow:
self.focus=mainWindow
else:
self.focus=focus
self.config = config
self.handlers = []
self.widget.connect(self.widget, SIGNAL("anchorClicked(QUrl)"),
self.anchorClicked)
if sys.platform.startswith("darwin"):
self.widget.setFixedWidth(300)
self.hide()
def show(self):
"Show the help area."
self.helpFrame.show()
self.widget.show()
def hide(self):
self.helpFrame.hide()
self.widget.hide()
if self.mainWindow:
runHook("helpChanged")
def showText(self, text, py={}):
if "hide" in self.handlers:
if self.handlers['hide'] != py.get('hide'):
self.handlers["hide"]()
self.show()
self.buffer = text
self.addHider()
self.handlers = py
self.flush()
if self.mainWindow:
runHook("helpChanged")
def flush(self):
if sys.platform.startswith("darwin"):
font = "helvetica"
else:
font = "arial"
# qt seems to ignore font-size on elements like h1
style = "" #("<style>#content { font-family: %s; " +
#"font-size: 12px; }</style>\n") % font
self.widget.setHtml(style + '<div id="content">' +
self.buffer + '</div>')
def addHider(self):
self.buffer += _("<p><a href=hide:>Hide this</a>")
def anchorClicked(self, url):
# prevent the link being handled
self.widget.setSource(QUrl(""))
addr = unicode(url.toString())
if addr.startswith("hide:"):
if len(addr) > 5:
# hide for good
self.config[addr] = True
self.hide()
if "hide" in self.handlers:
self.handlers["hide"]()
elif addr.startswith("py:"):
key = addr[3:]
if key in self.handlers:
self.handlers[key]()
else:
# open in browser
QDesktopServices.openUrl(QUrl(url))
if self.focus:
self.focus.setFocus()

View file

@ -17,10 +17,11 @@ from anki.utils import addTags, parseTags, canonifyTags, stripHTML, checksum
from anki.hooks import runHook, addHook, removeHook from anki.hooks import runHook, addHook, removeHook
import anki.consts import anki.consts
import aqt, aqt.utils, aqt.view, aqt.help, aqt.status, aqt.facteditor, \ import aqt, aqt.dockable, aqt.facteditor, aqt.progress, aqt.webview
aqt.progress, aqt.webview
from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \ from aqt.utils import saveGeom, restoreGeom, showInfo, showWarning, \
saveState, restoreState saveState, restoreState, getOnlyText, askUser, GetTextDialog, \
askUserDialog, applyStyles, getText, showText
config = aqt.config config = aqt.config
class AnkiQt(QMainWindow): class AnkiQt(QMainWindow):
@ -464,9 +465,9 @@ counts are %d %d %d
return 0 return 0
# FIXME: this needs updating # FIXME: this needs updating
if hasattr(e, 'data') and e.data.get('type') == 'inuse': if hasattr(e, 'data') and e.data.get('type') == 'inuse':
aqt.utils.showWarning(_("Deck is already open.")) showWarning(_("Deck is already open."))
else: else:
aqt.utils.showCritical(_("""\ showCritical(_("""\
File is corrupt or not an Anki database. Click help for more info.\n File is corrupt or not an Anki database. Click help for more info.\n
Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors") Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
self.moveToState("deckBrowser") self.moveToState("deckBrowser")
@ -579,7 +580,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
if not prompt: if not prompt:
prompt = _("Please give your deck a name:") prompt = _("Please give your deck a name:")
while 1: while 1:
name = aqt.utils.getOnlyText( name = getOnlyText(
prompt, default=name, title=_("New Deck")) prompt, default=name, title=_("New Deck"))
if not name: if not name:
self.moveToState("deckBrowser") self.moveToState("deckBrowser")
@ -598,7 +599,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
break break
path = os.path.join(self.documentDir, name) path = os.path.join(self.documentDir, name)
if os.path.exists(path): if os.path.exists(path):
if aqt.utils.askUser(_("That deck already exists. Overwrite?"), if askUser(_("That deck already exists. Overwrite?"),
defaultno=True): defaultno=True):
os.unlink(path) os.unlink(path)
else: else:
@ -685,7 +686,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
ret = self.loadDeck(file) ret = self.loadDeck(file)
if not ret: if not ret:
if ret is None: if ret is None:
aqt.utils.showWarning(_("Unable to load file.")) showWarning(_("Unable to load file."))
self.deck = None self.deck = None
return False return False
else: else:
@ -781,7 +782,7 @@ your deck."""))
return self.onSave() return self.onSave()
if os.path.exists(file): if os.path.exists(file):
# check for existence after extension # check for existence after extension
if not aqt.utils.askUser( if not askUser(
"This file exists. Are you sure you want to overwrite it?"): "This file exists. Are you sure you want to overwrite it?"):
return return
self.closeAllDeckWindows() self.closeAllDeckWindows()
@ -820,7 +821,7 @@ your deck."""))
try: try:
self.config.save() self.config.save()
except (IOError, OSError), e: except (IOError, OSError), e:
aqt.utils.showWarning(_("Anki was unable to save your " showWarning(_("Anki was unable to save your "
"configuration file:\n%s" % e)) "configuration file:\n%s" % e))
def closeEvent(self, event): def closeEvent(self, event):
@ -1179,15 +1180,17 @@ learnt today")
addHook("showQuestion", self.onCardStats) addHook("showQuestion", self.onCardStats)
addHook("deckFinished", self.onCardStats) addHook("deckFinished", self.onCardStats)
txt = "" txt = ""
if self.currentCard: if self.reviewer.card:
txt += _("<h1>Current card</h1>") txt += _("<h1>Current card</h1>")
txt += anki.stats.CardStats(self.deck, self.currentCard).report() txt += self.deck.cardStats(self.reviewer.card)
if self.lastCard and self.lastCard != self.currentCard: lc = self.reviewer.lastCard()
if lc:
txt += _("<h1>Last card</h1>") txt += _("<h1>Last card</h1>")
txt += anki.stats.CardStats(self.deck, self.lastCard).report() txt += self.deck.cardStats(lc)
if not txt: if not txt:
txt = _("No current card or last card.") txt = _("No current card or last card.")
self.help.showText(txt, py={'hide': self.removeCardStatsHook}) print txt
#self.help.showText(txt, py={'hide': self.removeCardStatsHook})
def removeCardStatsHook(self): def removeCardStatsHook(self):
"Remove the update hook if the help menu was changed." "Remove the update hook if the help menu was changed."
@ -1330,7 +1333,7 @@ Please give your deck a name:"""))
def onCram(self, cardIds=[]): def onCram(self, cardIds=[]):
te = aqt.tagedit.TagEdit(self) te = aqt.tagedit.TagEdit(self)
te.setDeck(self.deck, "all") te.setDeck(self.deck, "all")
diag = aqt.utils.GetTextDialog( diag = GetTextDialog(
self, _("Tags to cram:"), help="CramMode", edit=te) self, _("Tags to cram:"), help="CramMode", edit=te)
l = diag.layout() l = diag.layout()
g = QGroupBox(_("Review Mode")) g = QGroupBox(_("Review Mode"))
@ -1590,7 +1593,7 @@ will sync automatically from then on."""))
return ok return ok
def onConflict(self, deckName): def onConflict(self, deckName):
diag = aqt.utils.askUserDialog(_("""\ diag = askUserDialog(_("""\
<b>%s</b> has been changed on both <b>%s</b> has been changed on both
the local and remote side. What do the local and remote side. What do
you want to do?""" % deckName), you want to do?""" % deckName),
@ -1607,7 +1610,7 @@ you want to do?""" % deckName),
self.syncThread.conflictResolution = "cancel" self.syncThread.conflictResolution = "cancel"
def onClobber(self, deckName): def onClobber(self, deckName):
diag = aqt.utils.askUserDialog(_("""\ diag = askUserDialog(_("""\
You are about to upload <b>%s</b> You are about to upload <b>%s</b>
to AnkiOnline. This will overwrite to AnkiOnline. This will overwrite
the online copy of this deck. the online copy of this deck.
@ -1676,7 +1679,7 @@ Are you sure?""" % deckName),
# name chosen # name chosen
p = os.path.join(self.documentDir, name + ".anki") p = os.path.join(self.documentDir, name + ".anki")
if os.path.exists(p): if os.path.exists(p):
d = aqt.utils.askUserDialog(_("""\ d = askUserDialog(_("""\
This deck already exists on your computer. Overwrite the local copy?"""), This deck already exists on your computer. Overwrite the local copy?"""),
["Overwrite", "Cancel"]) ["Overwrite", "Cancel"])
d.setDefault(1) d.setDefault(1)
@ -1702,7 +1705,7 @@ This deck already exists on your computer. Overwrite the local copy?"""),
self.form.welcomeText.append("<font size=+2>" + text + "</font>") self.form.welcomeText.append("<font size=+2>" + text + "</font>")
def syncClockOff(self, diff): def syncClockOff(self, diff):
aqt.utils.showWarning( showWarning(
_("The time or date on your computer is not correct.\n") + _("The time or date on your computer is not correct.\n") +
ngettext("It is off by %d second.\n\n", ngettext("It is off by %d second.\n\n",
"It is off by %d seconds.\n\n", diff) % diff + "It is off by %d seconds.\n\n", diff) % diff +
@ -1712,7 +1715,7 @@ This deck already exists on your computer. Overwrite the local copy?"""),
self.onSyncFinished() self.onSyncFinished()
def showSyncWarning(self, text): def showSyncWarning(self, text):
aqt.utils.showWarning(text, self) showWarning(text, self)
self.setStatus("") self.setStatus("")
def badUserPass(self): def badUserPass(self):
@ -1741,7 +1744,7 @@ This deck already exists on your computer. Overwrite the local copy?"""),
self.syncProgressDialog.setLabelText("Downloading %s..." % fname) self.syncProgressDialog.setLabelText("Downloading %s..." % fname)
def bulkSyncFailed(self): def bulkSyncFailed(self):
aqt.utils.showWarning(_( showWarning(_(
"Failed to upload media. Please run 'check media db'."), self) "Failed to upload media. Please run 'check media db'."), self)
def fullSyncStarted(self, max): def fullSyncStarted(self, max):
@ -1935,7 +1938,7 @@ This deck already exists on your computer. Overwrite the local copy?"""),
ret = _("late") ret = _("late")
else: else:
ret = _("early") ret = _("early")
aqt.utils.showWarning( showWarning(
_("The time or date on your computer is not correct.\n") + _("The time or date on your computer is not correct.\n") +
ngettext("It is %(sec)d second %(type)s.\n", ngettext("It is %(sec)d second %(type)s.\n",
"It is %(sec)d seconds %(type)s.\n", abs(diff)) "It is %(sec)d seconds %(type)s.\n", abs(diff))
@ -1975,7 +1978,7 @@ This deck already exists on your computer. Overwrite the local copy?"""),
try: try:
runHook('init') runHook('init')
except: except:
aqt.utils.showWarning( showWarning(
_("Broken plugin:\n\n%s") % _("Broken plugin:\n\n%s") %
unicode(traceback.format_exc(), "utf-8", "replace")) unicode(traceback.format_exc(), "utf-8", "replace"))
@ -2113,7 +2116,7 @@ to work with this version of Anki."""))
########################################################################## ##########################################################################
def setupStyle(self): def setupStyle(self):
aqt.utils.applyStyles(self) applyStyles(self)
# Sounds # Sounds
########################################################################## ##########################################################################
@ -2230,7 +2233,7 @@ command+click on a Mac), choose DropBox>Copy Public Link, and paste the \
link into Anki.""")) link into Anki."""))
# open folder and text prompt # open folder and text prompt
self.onOpenPluginFolder(deck.mediaPrefix) self.onOpenPluginFolder(deck.mediaPrefix)
txt = aqt.utils.getText(_("Paste path here:"), parent=self) txt = getText(_("Paste path here:"), parent=self)
if txt[0]: if txt[0]:
fail = False fail = False
if not txt[0].lower().startswith("http"): if not txt[0].lower().startswith("http"):
@ -2254,14 +2257,14 @@ is next loaded."""))
def onCheckDB(self): def onCheckDB(self):
"True if no problems" "True if no problems"
if not aqt.utils.askUser(_("""\ if not askUser(_("""\
This operation will find and fix some common problems.<br><br> This operation will find and fix some common problems.<br><br>
On the next sync, all cards will be sent to the server. \ On the next sync, all cards will be sent to the server. \
Any changes on the server since your last sync will be lost.<br><br> Any changes on the server since your last sync will be lost.<br><br>
<b>This operation is not undoable.</b> Proceed?""")): <b>This operation is not undoable.</b> Proceed?""")):
return return
ret = self.deck.fixIntegrity() ret = self.deck.fixIntegrity()
aqt.utils.showText(ret) showText(ret)
self.reset() self.reset()
return ret return ret
@ -2310,7 +2313,7 @@ doubt."""))
report += "\n" + "\n".join(unused) report += "\n" + "\n".join(unused)
if not report: if not report:
report = _("No unused or missing files found.") report = _("No unused or missing files found.")
aqt.utils.showText(report, parent=self, type="text") showText(report, parent=self, type="text")
def onDownloadMissingMedia(self): def onDownloadMissingMedia(self):
res = downloadMissing(self.deck) res = downloadMissing(self.deck)
@ -2329,7 +2332,7 @@ doubt."""))
showInfo(msg) showInfo(msg)
def onLocalizeMedia(self): def onLocalizeMedia(self):
if not aqt.utils.askUser(_("""\ if not askUser(_("""\
This will look for remote images and sounds on your cards, download them to \ This will look for remote images and sounds on your cards, download them to \
your media folder, and convert the links to local ones. \ your media folder, and convert the links to local ones. \
It can take a long time. Proceed?""")): It can take a long time. Proceed?""")):

View file

@ -20,6 +20,7 @@ class Reviewer(object):
self.web = mw.web self.web = mw.web
self.card = None self.card = None
self.cardQueue = [] self.cardQueue = []
self._answeredIds = []
self.state = None self.state = None
self._setupStatus() self._setupStatus()
@ -28,6 +29,11 @@ class Reviewer(object):
self.web.setLinkHandler(self._linkHandler) self.web.setLinkHandler(self._linkHandler)
self._getCard() self._getCard()
def lastCard(self):
if self._answeredIds:
if not self.card or self._answeredIds[-1] != self.card.id:
return self.mw.deck.getCard(self._answeredIds[-1])
# Fetching a card # Fetching a card
########################################################################## ##########################################################################
@ -186,6 +192,7 @@ $(".ansbut").focus();
def _answerCard(self, ease): def _answerCard(self, ease):
"Reschedule card and show next." "Reschedule card and show next."
self.mw.deck.sched.answerCard(self.card, ease) self.mw.deck.sched.answerCard(self.card, ease)
self._answeredIds.append(self.card.id)
print "fixme: save" print "fixme: save"
self._getCard() self._getCard()