# -*- 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 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 # 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": self.shownLearnHelp = False self.shownReviewHelp = False self.shownFinalHelp = False return elif self.state == "noDeck": self.clearWindow() self.drawNoDeckMessage() 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() self.maybeHelp() if self.main.deck.cardCount(): if not self.main.lastCard or ( self.main.config['suppressLastCardContent'] and self.main.config['suppressLastCardInterval']): 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.drawDeckEmptyMessage() 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 drawQuestion(self, nosound=False): "Show the question." q = self.main.currentCard.htmlQuestion q = renderLatex(self.main.deck, q) self.write(stripSounds(q)) if self.state != self.oldState and not nosound: playFromText(q) def drawAnswer(self): "Show the answer." a = self.main.currentCard.htmlAnswer a = renderLatex(self.main.deck, a) self.write(stripSounds(a)) if self.state != self.oldState: playFromText(a) # 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 not self.main.config['suppressLastCardContent']: 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 not self.main.config['suppressLastCardInterval']: 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) # Help ########################################################################## def maybeHelp(self): return stats = self.main.deck.sched.getStats() if not stats['pending']: self.main.help.hide() elif (self.main.currentCard and self.main.currentCard.nextTime > time.time()): if not self.shownFinalHelp: self.shownFinalHelp = True self.main.help.showHideableKey("finalReview") elif stats['learnMode']: if not self.shownLearnHelp: if stats['pending'] != 0: self.shownLearnHelp = True self.main.help.showHideableKey("learn") else: if not self.shownReviewHelp: self.shownReviewHelp = True self.main.help.showHideableKey("review") # Welcome/empty/finished deck messages ########################################################################## def drawNoDeckMessage(self): self.write(_("""

Welcome to Anki!

Create a new deck

Open a sample deck

Open an existing deck

""")) def drawDeckEmptyMessage(self): "Tell the user the deck is empty." self.write(_("""

Empty deck

The current deck has no cards in it. Please select 'Add card' from the Edit menu.""")) def drawDeckFinishedMessage(self): "Tell the user the deck is finished." self.write("
" + self.main.deck.deckFinishedMsg() + "
")