This commit is contained in:
Alex Yatskov 2017-07-01 12:41:27 -07:00
parent 7c410ee64a
commit d529cc12f4

View File

@ -385,10 +385,18 @@ class AnkiBridge:
return aqt.mw return aqt.mw
def reviewer(self):
return self.window().reviewer
def collection(self): def collection(self):
return self.window().col return self.window().col
def scheduler(self):
return self.collection().sched
def media(self): def media(self):
collection = self.collection() collection = self.collection()
if collection is not None: if collection is not None:
@ -401,14 +409,20 @@ class AnkiBridge:
return collection.models.allNames() return collection.models.allNames()
def modelNameFromId(self, mid):
collection = self.collection()
if collection is not None:
model = collection.models.get(mid)
if model is not None:
return model['name']
def modelFieldNames(self, modelName): def modelFieldNames(self, modelName):
collection = self.collection() collection = self.collection()
if collection is None: if collection is not None:
return model = collection.models.byName(modelName)
if model is not None:
model = collection.models.byName(modelName) return [field['name'] for field in model['flds']]
if model is not None:
return [field['name'] for field in model['flds']]
def deckNames(self): def deckNames(self):
@ -417,6 +431,14 @@ class AnkiBridge:
return collection.decks.allNames() return collection.decks.allNames()
def deckNameFromId(self, did):
collection = self.collection()
if collection is not None:
deck = collection.decks.get(did)
if deck is not None:
return deck['name']
def guiBrowse(self, query): def guiBrowse(self, query):
browser = aqt.dialogs.open('Browser', self.window()) browser = aqt.dialogs.open('Browser', self.window())
browser.activateWindow() browser.activateWindow()
@ -434,59 +456,65 @@ class AnkiBridge:
addCards.activateWindow() addCards.activateWindow()
def guiGetNextCard(self): def guiReviewActive(self):
window = self.window() return self.reviewer().card is not None
if window.reviewer.card is None or window.state != 'review':
window.moveToState('review')
def guiReview(self):
window = self.window()
window.moveToState('review')
return self.guiReviewActive()
def guiCurrentCard(self):
if self.guiReviewActive():
return False
reviewer = self.reviewer()
card = reviewer.card
card = window.reviewer.card
if card is not None: if card is not None:
return { return {
'id': card.id, 'cardId': card.id,
'question': card._getQA()['q'], 'question': card._getQA()['q'],
'answer': card._getQA()['a'], 'answer': card._getQA()['a'],
'answerButtons': self.window().reviewer._answerButtonList(), 'buttons': map(lambda b: b[0], self.window().reviewer._answerButtonList()),
'modelName': card.note(reload)._model['name'], 'modelName': card.model()['name'],
'fieldOrder': card.ord, 'deckName': self.deckNameFromId(card.did)
'fields': card.note(reload).fields,
'fieldMap': card.note(reload)._fmap
} }
def guiShowQuestion(self): def guiShowQuestion(self):
window = self.window() if self.guiReviewActive():
if window.reviewer.card is None or window.state != 'review': self.reviewer()._showQuestion()
window.moveToState('review') return True
else:
window.reviewer._showQuestion() return False
def guiShowAnswer(self): def guiShowAnswer(self):
window = self.window() if self.guiReviewActive():
if window.reviewer.mw.state == 'review':
self.window().reviewer._showAnswer() self.window().reviewer._showAnswer()
return True
else:
return False
def guiAnswerCard(self, cardId, ease): def guiAnswerCard(self, cardId, ease):
window = self.window() if not self.guiReviewActive():
if window.reviewer.mw.state != 'review':
return False return False
elif window.reviewer.state != 'answer':
return False
elif window.reviewer.card.id != cardId:
return False
elif window.col.sched.answerButtons(window.reviewer.card) < ease:
return False
else:
self.window().reviewer._answerCard(ease)
return True
reviewer = self.reviewer()
card = reviewer.card
if card.id != cardId:
return False
if reviewer.state != 'answer':
return False
if ease <= 0 or ease > self.scheduler().answerButtons(card):
return False
def guiCheckState(self): reviewer._answerCard(ease)
return { return True
'windowState': self.window().state,
'reviewerState': self.window().reviewer.state
}
# #
@ -518,6 +546,7 @@ class AnkiConnect:
def handler(self, request): def handler(self, request):
action = 'api_' + request.get('action', '') action = 'api_' + request.get('action', '')
if hasattr(self, action): if hasattr(self, action):
return getattr(self, action)(**(request.get('params') or {}))
try: try:
return getattr(self, action)(**(request.get('params') or {})) return getattr(self, action)(**(request.get('params') or {}))
except TypeError: except TypeError:
@ -596,8 +625,12 @@ class AnkiConnect:
return self.anki.guiAddCards() return self.anki.guiAddCards()
def api_guiGetNextCard(self): def api_guiReview(self):
return self.anki.guiGetNextCard() return self.anki.guiReview()
def api_guiCurrentCard(self):
return self.anki.guiCurrentCard()
def api_guiAnswerCard(self, id, ease): def api_guiAnswerCard(self, id, ease):
@ -612,9 +645,6 @@ class AnkiConnect:
return self.anki.guiShowAnswer() return self.anki.guiShowAnswer()
def api_checkState(self):
return self.anki.guiCheckState()
# #
# Entry # Entry
# #