def getBase(col): mdir = sendToAnki("mediaDir") prefix = u"file://" base = prefix + unicode( urllib.quote(mdir.encode("utf-8")), "utf-8") + "/" return '<base href="%s">' % base
def setupDecks(self): if self.label: self.deckLabel = QLabel(_("Deck")) self.addWidget(self.deckLabel) # decks box self.deck = QPushButton() self.deck.setToolTip(shortcut(_("Target Deck (Ctrl+D)"))) s = QShortcut(QKeySequence(_("Ctrl+D")), self.widget) s.connect(s, SIGNAL("activated()"), self.onDeckChange) self.addWidget(self.deck) self.connect(self.deck, SIGNAL("clicked()"), self.onDeckChange) # starting label decks = sendToAnki("decks") deck_name = "Default" for deck in decks: if deck['id'] == 1: deck_name = deck["name"] break self.deck.setText(deck_name) # layout sizePolicy = QSizePolicy( QSizePolicy.Policy(7), QSizePolicy.Policy(0)) self.deck.setSizePolicy(sizePolicy)
def dupeOrEmpty(self): val = self.fields[0] if not val.strip(): return 1 # find any matching csums and compare if sendToAnki("isDupe", {"field": self.fields[0], "model": self.model["name"]}): return 2 return False
def __init__(self, mw, widget, label=True): QHBoxLayout.__init__(self) self.widget = widget self.mw = mw self.currentModel = sendToAnki("modelByName", {"name": "Basic"}) self.label = label self.setMargin(0) self.setSpacing(8) self.setupModels() addHook('reset', self.onReset) self.widget.setLayout(self)
def selectedId(self): # save deck name name = self.deck.text() if not name.strip(): did = 1 else: decks = sendToAnki("decks") for deck in decks: if deck['name'] == name: did = int(deck["id"]) break return did
def _addMedia(self, path, delete=False): "Add to media folder and return basename." # copy to media folder name = sendToAnki("addFile", {"path": path}) # remove original? if delete: if os.path.abspath(name) != os.path.abspath(path): try: os.unlink(path) except: pass # return a local html link ext = name.split(".")[-1].lower() if ext in pics: return '<img src="%s">' % name else: anki.sound.play(name) return '[sound:%s]' % name
def onModelChange(self): from aqt.studydeck import StudyDeck current = self.currentModel["name"] # edit button edit = QPushButton(_("Manage")) self.connect(edit, SIGNAL("clicked()"), self.onEdit) def nameFunc(): models = sendToAnki("models") return sorted([m["name"] for m in models]) ret = StudyDeck( self.mw, names=nameFunc, accept=_("Choose"), title=_("Choose Note Type"), help="_notes", current=current, parent=self.widget, buttons=[edit], cancel=False) if not ret.name: return m = sendToAnki("modelByName", {"name": ret.name}) self.currentModel = m self.modelChanged()
def addCards(self): self.editor.saveNow() # grab data note = self.editor.note # sanity check if note.dupeOrEmpty(): showCritical("Note is a dupe or empty; not adding.") return # check for cloze sanity in case of potential cloze-y notes if len(note.fields) == 2: # find the highest existing cloze highest = note.highestCloze() if highest > 0 and not note.isCloze(): # clozes used, but wrong model, so switch it to cloze self.editor.changeToModel("Cloze") note = self.editor.note elif note.isCloze() and highest == 0: # no clozes, switch to basic self.editor.changeToModel("Basic") note = self.editor.note # send data to TCP server in Anki data = { "model": note.model["name"], "deck": self.deckChooser.currentDeck(), "fields": note.fields, "tags": note.tags, } ret = sendToAnki("addNote", data) if ret == True: self.mw.reset() # stop anything playing clearAudioQueue() self.onReset(keep=True) else: showCritical("Failed to add card. Is Anki ok?")
def changeToModel(self, name): model = sendToAnki("modelByName", {"name": name}) self.currentModel = model self.modelChanged()
def nameFunc(): models = sendToAnki("models") return sorted([m["name"] for m in models])
def nameFunc(): decks = sendToAnki("decks") return sorted([d["name"] for d in decks])
def updateTags(self): if self.type == 0: l = sorted(sendToAnki("tags")) else: l = sorted([d["name"] for d in sendToAnki("decks")]) self.model.setStringList(l)