# -*- coding: utf-8 -*- # Copyright: Damien Elmes # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html from PyQt4.QtGui import * from PyQt4.QtCore import * import anki, anki.utils from anki.sound import playFromText, stripSounds from anki.latex import renderLatex, stripLatex from anki.utils import stripHTML import types, time, re, os from ankiqt import ui # Views - define the way a user is prompted for questions, etc ########################################################################## class View(object): "Handle the main window update as we transition through various states." def __init__(self, parent, body, frame=None): self.main = parent self.body = body self.frame = frame self.main.connect(self.body, SIGNAL("loadFinished(bool)"), self.onLoadFinished) # State control ########################################################################## def setState(self, state): "Change to STATE, and update the display." self.oldState = getattr(self, 'state', None) self.state = state if self.state == "initial": return elif self.state == "noDeck": self.clearWindow() self.drawWelcomeMessage() self.flush() return self.redisplay() def redisplay(self): "Idempotently display the current state (prompt for question, etc)" if self.state == "noDeck": return self.clearWindow() self.setBackgroundColour() if not self.main.deck.isEmpty(): if not self.main.lastCard or ( not self.main.config['showLastCardContent'] and not self.main.config['showLastCardInterval']): self.buffer += "
" else: self.drawTopSection() if self.state == "showQuestion": self.drawQuestion() elif self.state == "showAnswer": if not self.main.currentCard.cardModel.questionInAnswer: self.drawQuestion(nosound=True) self.drawAnswer() elif self.state == "deckEmpty": self.drawWelcomeMessage() elif self.state == "deckFinished": self.drawDeckFinishedMessage() self.flush() def addStyles(self): # card styles s = "" return s def clearWindow(self): self.body.setHtml("") self.buffer = "" # Font properties & output ########################################################################## def flush(self): "Write the current HTML buffer to the screen." self.body.setHtml(self.addStyles() + '
' + self.buffer + "
") def write(self, text): if type(text) != types.UnicodeType: text = unicode(text, "utf-8") self.buffer += text def setBackgroundColour(self): p = QPalette() p.setColor(QPalette.Base, QColor(self.main.config['backgroundColour'])) self.body.setPalette(p) if self.frame: p.setColor(QPalette.Background, QColor(self.main.config['backgroundColour'])) self.frame.setPalette(p) # Question and answer ########################################################################## def munge(self, txt): txt = renderLatex(self.main.deck, txt) txt = stripSounds(txt) txt = re.sub( 'img src="(.*?)"', 'img src="file://%s/\\1"' % os.getcwd(), txt) return txt def drawQuestion(self, nosound=False): "Show the question." q = self.main.currentCard.htmlQuestion() self.write(self.munge(q)) if self.state != self.oldState and not nosound: playFromText(q) def drawAnswer(self): "Show the answer." a = self.main.currentCard.htmlAnswer() self.write('' + self.munge(a)) if self.state != self.oldState: playFromText(a) def onLoadFinished(self): if self.state == "showAnswer": mf = self.body.page().mainFrame() mf.evaluateJavaScript("location.hash = 'answer'") # Top section ########################################################################## def drawTopSection(self): "Show previous card, next scheduled time, and stats." self.buffer += "
" self.drawLastCard() self.buffer += "
" def drawLastCard(self): "Show the last card if not the current one, and next time." if self.main.lastCard: if self.main.config['showLastCardContent']: if (self.state == "deckFinished" or self.main.currentCard.id != self.main.lastCard.id): q = self.main.lastCard.question.replace("
", " ") q = stripHTML(q) if len(q) > 50: q = q[:50] + "..." a = self.main.lastCard.answer.replace("
", " ") a = stripHTML(a) if len(a) > 50: a = a[:50] + "..." s = "%s
%s" % (q, a) s = stripLatex(s) self.write('%s
' % s) if self.main.config['showLastCardInterval']: if self.main.lastQuality > 1: msg = _("Well done! This card will appear again in " "%(next)s.") % \ {"next":self.main.lastScheduledTime} else: msg = _("This card will appear again in less than " "%(next)s.") % \ {"next":self.main.lastScheduledTime} self.write(msg) self.write("
") # Welcome/empty/finished deck messages ########################################################################## def drawWelcomeMessage(self): self.main.mainWin.welcomeText.setText(_("""

Welcome to Anki!

Add material

Start adding your own material.

Open Local Deck

Open Online Deck

Open Sample Deck

Get More Decks

""")) def drawDeckFinishedMessage(self): "Tell the user the deck is finished." self.write("

" + self.main.deck.deckFinishedMsg() + "
")